【一】uart接收数据
串口采用中断的方式接受数据,将数据保存在队列中。
//在串口中初始化接收中断
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
中断处理函数:
void USART2_IRQHandler(void)
{
if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET)
{
rev = USART_ReceiveData(USART2);
if(!is_full()){
queue_in(rev);
}
}
}
数据处理——队列:
#define N 128
int head = 0,tail = 0;
unsigned char queue[N] = {’\0’};
void queue_in(unsigned char val)
{
if((tail+1)%N == head){
printf(“queue full…\n”);
}else{
queue[tail] = val;
tail = (tail+1)%N;
}