1、 main.c
------------------------------------------------------------------------------------------------------------------------
/* Includes ------------------------------------------------------------------*/
#include "stm32f10x.h"
#include "stm32f10x_rcc.h"
#include "stm32f10x_gpio.h"
#include "stm32f10x_usart.h"
#include "stm32f10x_exti.h"
#include "system_stm32f10x.h"
#include "misc.h"
GPIO_InitTypeDef GPIO_InitStructure;
/* ÅäÖÃÏμí3ê±Öó 72M */
void SystemInit(void);//SystemInit();
u16 CCR1= 10;
u16 CCR2= 10;
u16 CCR3= 10;
u8 rx_data[20];
u8 rx_num;
u8 rx_status;
/****************************************************************
* ???:void GPIO_Config(void)
* ?? :??????PWM????I/O
* ?? :?
* ?? :?
* ?? :main()??
***************************************************************/
void GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
//′ò¿aGPIOA£¬GPIOBê±Öó
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA , ENABLE);
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB, ENABLE);
/*GPIOA Configuration: TIM3 channel 1 (GPIOA_Pin_6)and 2(GPIOA_Pin_7) as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6| GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; // ??????
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/*GPIOA Configuration: TIM3 channel 3 (GPIOB_Pin_0)and 4(GPIOB_Pin_1) as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 ;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; // ??????
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 ; // PB8£PPB9 = DIêäèë
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //ÅäÖÃ3鸡¿Õêäèë
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
//Timer3õê¼»ˉ:
/****************************************************************
* ???:void TIM3_Config(void)
* ?? :??TIM3???PWM?????
* CH1:?? T=2.5ms(f=1/2.5ms=400Hz) D=0.6?PWM?(?????,?????)
* CH2:?? T=2.5ms(f=1/2.5ms=400Hz) D=0.4?PWM?(?????,?????)
* ???:??T?TIMxCLK??????TIM_Period?TIM_Prescaler
* T=(TIM_Period+1)*(TIM_Prescaler+1)/TIMxCLK=2.5ms
* ?? TIM_Period<65535,?? TIM_Prescaler>1,? TIM_Prescaler=2
* ?? TIM_Period=59999=0xEA5F
* ???:??TIM_Period??,???????D,??CCR?TIM_OCPolarity
* CH1:??D=0.6,????;
* ??CCR1=(TIM_Period+1)* D=36000;TIM_OCPolarity=TIM_OCPolarity_High
* CH2:??D=0.4,????;
* ??CCR1=(TIM_Period+1)* (1-D)=36000;TIM_OCPolarity=TIM_OCPolarity_Low
* ???:????????
* ???:????????
* ???:??TIM3?????ARR
* ???:??TIM3
* ?? :?
* ?? :?
* ?? :main()??
***************************************************************/
void TIM3_Config(u16 CCR1,u16 CCR2,u16 CCR3)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
/*′ò¿aTIM3ê±Öó 72MHz*/
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
/* Time base configuration */
TIM_TimeBaseStructure.TIM_Period =200;
TIM_TimeBaseStructure.TIM_Prescaler = 2; //?????:???=2,??72/3=24MHz
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //????????:???
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //????????
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //???PWM??1
/* PWM1 Mode configuration: Channel1 */
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = CCR1; //?????,???????????,??????
TIM_OCInitStructure.TIM_OCPolarity =TIM_OCPolarity_High; //?????????CCR1?????
TIM_OC1Init(TIM3, &TIM_OCInitStructure); //????1
TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable);
/* PWM1 Mode configuration: Channel2 */
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = CCR1; //????2??????,??????????PWM
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //?????????CCR2?????
TIM_OC2Init(TIM3, &TIM_OCInitStructure); //????2
TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable);
/* PWM1 Mode configuration: Channel3 */
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = CCR2; //????2??????,??????????PWM
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //?????????CCR2?????
TIM_OC3Init(TIM3, &TIM_OCInitStructure); //????2
TIM_OC3PreloadConfig(TIM3, TIM_OCPreload_Enable);
/* PWM1 Mode configuration: Channel4 */
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = CCR3; //????2??????,??????????PWM
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //?????????CCR2?????
TIM_OC4Init(TIM3, &TIM_OCInitStructure); //????2
TIM_OC4PreloadConfig(TIM3, TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM3, ENABLE); //??TIM3?????ARR
/* TIM3 enable counter */
TIM_Cmd(TIM3, ENABLE); //??TIM3
}
//Ñóê±oˉêy £¨oáÃ룩
void delay_ms(u16 time)
{
u16 i=0;
while(time--)
{
i=12000; //????
while(i--) ;
}
}
/*****************************************************************
*????: UART1Init
******************************************************************/
void UART1Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
//′ò¿aUSART1ê±Öó
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);
//GPIO?????????:
/*--------------4851ü½ÅÅäÖÃ----------*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 ; // êÕ·¢×a»»¿a1Ø
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //50M????
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //????
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //485_TX
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //??????
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //485_RX
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //????
GPIO_Init(GPIOA, &GPIO_InitStructure);
//′®¿ú2ÎêyÅäÖÃ:
USART_InitStructure.USART_BaudRate = 9600;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
USART_Init(USART1, &USART_InitStructure);
USART_ITConfig(USART1,USART_IT_TXE,ENABLE); //ÔêDí′®¿ú·¢ËíÖD¶Ï
USART_ITConfig(USART1,USART_IT_RXNE,ENABLE); //ÔêDí′®¿ú½óêÕÖD¶Ï
USART_ITConfig(USART1,USART_IT_IDLE,ENABLE); //ÔêDí′®¿ú¿ÕÏDÖD¶Ï£¨ÅD¶ÏêÇ·ñêÕμ½ò»¸öêy¾Y°üμÄê1Äü£©
USART_DMACmd(USART1,USART_DMAReq_Rx,ENABLE); //DMA·½ê½½óêÕ′®¿úêy¾Y
USART_Cmd(USART1, ENABLE);
USART_ClearFlag(USART1, USART_FLAG_TC); //·¢Ëííê3é±êÖ¾
// USART_GetITStatus(USART1, USART_FLAG_TC); /*»ñè¡′®¿ú×′쬱ê־λ*/
}
/* DMA ÅäÖÃoˉêy */
void DMA_init(void)
{
DMA_InitTypeDef DMA_Initstructure;
/*??DMA??*/
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1,ENABLE);
// /* Enable the DMA1 Interrupt */
DMA_Initstructure.DMA_PeripheralBaseAddr = (u32)(&USART1->DR);;
DMA_Initstructure.DMA_MemoryBaseAddr = (u32)rx_data;
DMA_Initstructure.DMA_DIR = DMA_DIR_PeripheralSRC;
DMA_Initstructure.DMA_BufferSize = 128;
DMA_Initstructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_Initstructure.DMA_MemoryInc =DMA_MemoryInc_Enable;
DMA_Initstructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
DMA_Initstructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
DMA_Initstructure.DMA_Mode = DMA_Mode_Normal;
DMA_Initstructure.DMA_Priority = DMA_Priority_High;
DMA_Initstructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA1_Channel5,&DMA_Initstructure);
//Æô¶ˉDMA
DMA_Cmd(DMA1_Channel5,ENABLE);
//??DMA·¢ËíÖD¶Ï
//DMA_ITConfig(DMA1_Channel4,DMA_IT_TC,ENABLE);
}
/* ÖD¶ÏÅäÖÃoˉêy */
void NVIC_Configuration(void)//???????
{
NVIC_InitTypeDef NVIC_InitStructure;
#ifdef VECT_TAB_RAM
/*??????????0x20000000?*/
NVIC_SetVectorTable(NVIC_VectTab_RAM,0x00);
#else
/*????????????0x80000000?*/
NVIC_SetVectorTable(NVIC_VectTab_FLASH,0x00);
#endif
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //ÖD¶ÏÕ¼Ïèμ輶
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //ÖD¶ÏÏìó|óÅÏ輶
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
//DMAÖD¶ÏéèÖÃ
NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel5_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
/***************************************************************
* ???:main
* ?? :???
* ?? :?
* ?? :?
***************************************************************/
int main(void)
{
SystemInit();
GPIO_Config();
//TIM3_Config(CCR1,CCR2,CCR3);
UART1Init();
NVIC_Configuration();
DMA_init();
while (1)
{
delay_ms(3000);
CCR1= CCR1*2;
CCR2= CCR1*2.5;
CCR3= CCR1*3;
if (CCR1>=200 ) CCR1= CCR1-200;
if (CCR2>=200 ) CCR2= CCR2-200;
if (CCR3>=200 ) CCR3= CCR1-200;
//TIM3_Config(CCR1,CCR2,60000);
if(GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_8)) //¼ì2aPB¿úμÄμú8¸ö1ü½ÅμÄμçƽ
TIM3_Config(CCR1,CCR2,200); //Õy3£êä3ö
else
TIM3_Config(0,0,0); //êä3ö0
}
}
#ifdef USE_FULL_ASSERT
void assert_failed(uint8_t* file, uint32_t line)
{
while (1)
{
}
}
#endif
2、中断程序
------------------------------------------------------------------------------------------------------------------------
void UartASendStr (u8 *pucStr, u8 ulNum)
{
u8 i;
for(i = 0;i<ulNum;i++)
{
while(USART_GetFlagStatus(USART1,USART_FLAG_TC)==RESET);
USART_SendData(USART1,*pucStr++);
}
}
// ′®¿úÖD¶Ïoˉêy
void USART1_IRQHandler(void)
{
extern u8 rx_data[20];
extern u8 rx_num;
extern u8 rx_status;
u8 RX_dat;
u16 clear=0;
u8 data[5];
data[0]=0x55;
data[1]=0xAA;
data[2]=0x36;
data[3]=0xFF;
data[4]=0xEF;
if(USART_GetITStatus(USART1,USART_IT_RXNE)==SET)//êÕμ½1¸ö×Ö½ú
{
//RX_dat=USART_ReceiveData(USART1);
//USART_ClearFlag(USART1,USART_IT_RXNE);
rx_data[rx_num++]=USART_ReceiveData(USART1);
if (rx_num>5)
{
rx_num=0; //êy×éÖ¸Õë»Øμ½¿aí·
rx_status=1; //êÕμ½ò»Ö¡êy¾Y
GPIO_SetBits(GPIOA,GPIO_Pin_8); //ÔêDí′®¿ú·¢Ëí
delay_ms(10);
// UartASendStr(data,5);
// while(USART_GetFlagStatus(USART1,USART_FLAG_TXE) == RESET){} //???????????
delay_ms(20);
GPIO_ResetBits(GPIOA,GPIO_Pin_8); //ÔêDí′®¿ú½óêÕ
}
}
if (USART_GetITStatus(USART1,USART_IT_IDLE)==SET)//êÕμ½1Ö¡êy¾Y
{
clear= USART1->SR;
clear= USART1->DR; //Çå3yIDLE ±êÖ¾
DMA_Cmd(DMA1_Channel5,DISABLE); //??DMA
rx_num = 128 - DMA_GetCurrDataCounter(DMA1_Channel5); //??????????
rx_data[rx_num] = '\0';
DMA1_Channel5->CNDTR=128; //??????????
DMA_Cmd(DMA1_Channel5,ENABLE); //??DMA
rx_status=1; //êÕμ½ò»Ö¡êy¾Y
GPIO_SetBits(GPIOA,GPIO_Pin_8); //ÔêDí′®¿ú·¢Ëí
delay_ms(1);
// USART_SendData(USART1,RX_dat);
UartASendStr(data,5);
while(USART_GetFlagStatus(USART1,USART_FLAG_TXE) == RESET){} //???????????
delay_ms(2);
GPIO_ResetBits(GPIOA,GPIO_Pin_8); //ÔêDí′®¿ú½óêÕ
}
}
|