if(myusart.reflag>0) { Com_Handle(); myusart.recount=0; myusart.reflag=0; }main
#define _maxbuf 100 typedef struct { u8 myadd; u8 rcbuf[_maxbuf]; u16 timout; u8 recount; u8 timrun; u8 reflag; u8 Sendbuf[_maxbuf]; / u8 flag; }MYUSART; extern MYUSART myusart;MYUSART
YUSART myusart; u16 tdat=0; u16 tflag=0; void TIME4_Init(u16 arr,u16 psc) { TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; NVIC_InitTypeDef NVIC_InitStructure; RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE); //时钟使能 TIM_TimeBaseStructure.TIM_Period = arr; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值 计数到5000为500ms TIM_TimeBaseStructure.TIM_Prescaler =psc; //设置用来作为TIMx时钟频率除数的预分频值 10Khz的计数频率 TIM_TimeBaseStructure.TIM_ClockDivision = 0; //设置时钟分割:TDTS = Tck_tim TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式 TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure); //根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位 TIM_ITConfig( //使能或者失能指定的TIM中断 TIM4, //TIM2 TIM_IT_Update , ENABLE //使能 ); NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn; //TIM3中断 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //先占优先级0级 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //从优先级3级 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道被使能 NVIC_Init(&NVIC_InitStructure); //根据NVIC_InitStruct中指定的参数初始化外设NVIC寄存器 TIM_Cmd(TIM4, ENABLE); //使能TIMx外设 } void TIM4_IRQHandler(void) //TIM3中断 1MS { u8 st; if (TIM_GetITStatus(TIM4, TIM_IT_Update) != RESET) //检查指定的TIM中断发生与否:TIM 中断源 { st= TIM_GetFlagStatus(TIM4, TIM_FLAG_Update); if(st==SET) { TIM_ClearFlag(TIM4, TIM_FLAG_Update); tdat++; if(!(tdat&0x0F)) //16MS { tflag |= _bit(1); } if(myusart.timout>0) { if(--myusart.timout==0) { myusart.reflag=1; //收到一帧数据 } } } TIM_ClearITPendingBit(TIM4, TIM_IT_Update ); //清除TIMx的中断待处理位:TIM 中断源 } }time4
void USART1_IRQHandler(void) //串口1中断服务程序 { u8 sbuf; #ifdef OS_TICKS_PER_SEC //如果时钟节拍数定义了,说明要使用ucosII了. OSIntEnter(); #endif if (USART_GetFlagStatus(USART1, USART_FLAG_ORE) != RESET)//注意!不能使用if(USART_GetITStatus(USART1, USART_IT_RXNE) != sbufET)来判断 { sbuf=USART_ReceiveData(USART1); } if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) //接收中断(接收到的数据必须是0xbb 结尾) { sbuf =USART_ReceiveData(USART1);//(USART1->DR); //读取接收到的数据 if( myusart.reflag==0) { myusart.timout=10; if(myusart.recount<_maxbuf) { myusart.rcbuf[myusart.recount++]=sbuf; } } #if 0 if( myusart.reflag==1) //有数据包正在处理 { return ; } myusart.rcbuf[myusart.recount++]=sbuf; myusart.timout=0; if(myusart.recount==1) //收到主机发来的一帧数据的第一字节 { myusart.timrun=1; //启动定时 } #endif USART_ClearITPendingBit(USART1,USART_IT_RXNE); }USART1_IRQHandler
标签:NVIC,u8,TIM4,--,USART,STM32,TIM,串口,myusart From: https://www.cnblogs.com/zhouyuqing1024/p/18229215