程序实现的功能:对PA0接受的脉冲信号计数,用USART将的到的计数值发送串口助手显示void RCC_Configuration(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE); //GPIOA时钟打开
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE); //使能USART2的时钟
}
void PortInit(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //50M时钟速度
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
void USART_Configuration(void)
{
USART_InitTypeDef USART_InitStructure;
USART_InitStructure.USART_BaudRate=9600; //设置串口通信时的波特率为9600
USART_InitStructure.USART_WordLength=USART_WordLength_8b; //设置数据位长度8个位
USART_InitStructure.USART_StopBits=USART_StopBits_1; //在帧结尾传输1个停止位
USART_InitStructure.USART_Parity=USART_Parity_No; //设置校验位“无”
//设置硬件控制流失能
USART_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode=USART_Mode_Rx|USART_Mode_Tx;//设置发送和接收使能
USART_Init(USART2,&USART_InitStructure);
USART_Cmd(USART2,ENABLE);//开启USART2
}
void TIM2_Configuration(void) //只用一个外部脉冲端口
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
//配置TIMER2作为计数器
TIM_DeInit(TIM2);
TIM_TimeBaseStructure.TIM_Period = 0xFFFF;
TIM_TimeBaseStructure.TIM_Prescaler = 0x00;
TIM_TimeBaseStructure.TIM_ClockDivision = 0x0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); // Time base configuration
TIM_ETRClockMode2Config(TIM2, TIM_ExtTRGPSC_OFF, TIM_ExtTRGPolarity_NonInverted, 0);
TIM_SetCounter(TIM2, 0);
TIM_Cmd(TIM2, ENABLE);
}
u8 SendTemp[] = ":00";
u8 i,j;
u8 COUN2=0;
/*************主函数**************************/
int main(void)
{
Init_SysTick(); //SysTick时钟初始化
RCC_Configuration(); //端口时钟初始化
PortInit(); //端口初始化
USART_Configuration(); //串口初始化
TIM2_Configuration();
j = sizeof(SendTemp); //提取字节数
while(1)
{
COUN2=TIM2->CNT;
SendTemp[1]=COUN2/10;
SendTemp[2]=COUN2%10;
for(i=0;i<j;i++)
{
//j = sizeof(SendTemp); //提取字节数
USART_SendData(USART2,SendTemp[i]);//发送一位数据
while(USART_GetFlagStatus(USART2,USART_FLAG_TXE)==RESET);//判断是否发送完毕
}
delay_ms(50); //延时50ms
}
}
|