stm32串口中断接收数组
时间:10-02
整理:3721RD
点击:
各位大神,我最近在做扫描器读码的程序,主要为我向串口发送一段数据,启动扫描器,扫码器读取条码后,把读取一连串字符串发回串口,我要读取这一连串的字符串。我使用的中断方式读取字符串,下面是我写的程序,能进入中断,但是无法读取数据。大神们帮我解决解决这个问题吧,困扰我好久了!主程序:
int main(void)
{
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
delay_init(168);
uart_init(9600);
while(1)
{
USART_Cmd(USART3,ENABLE);
USART_SendData(USART3, 0x02);
delay_ms(10);
USART_SendData(USART3, 0x2B);
delay_ms(10);
USART_SendData(USART3, 0x0D);
delay_ms(10);
USART_SendData(USART3, 0x0A);//启动扫描器
delay_ms(10);
while(USART_GetFlagStatus(USART3,USART_FLAG_TXE)==RESET);
delay_ms(5000);
}
中断程序:
void uart_init(u32 bound){
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3,ENABLE);
GPIO_PinAFConfig(GPIOB,GPIO_PinSource10,GPIO_AF_USART3);
GPIO_PinAFConfig(GPIOB,GPIO_PinSource11,GPIO_AF_USART3);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOB,&GPIO_InitStructure);
USART_InitStructure.USART_BaudRate = bound;
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_Rx | USART_Mode_Tx;
USART_Init(USART3, &USART_InitStructure);
USART_Cmd(USART3, ENABLE);
//USART_ClearFlag(USART1, USART_FLAG_TC);
#if EN_USART3_RX
USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);
NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3;
NVIC_InitStructure.NVIC_IRQChannelSubPriority =3;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
USART_ITConfig(USART3,USART_IT_RXNE,ENABLE);
#endif
}
void USART3_IRQHandler(void)
{
u32 Res;
unsigned char*a;
unsigned char i=0;
if(USART_GetITStatus(USART3, USART_IT_RXNE)!= RESET)
{
USART_ClearITPendingBit(USART3,USART_IT_RXNE);
Res=USART_ReceiveData(USART3); //(USART1->DR);
}
a=Res;
i++;
}
int main(void)
{
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
delay_init(168);
uart_init(9600);
while(1)
{
USART_Cmd(USART3,ENABLE);
USART_SendData(USART3, 0x02);
delay_ms(10);
USART_SendData(USART3, 0x2B);
delay_ms(10);
USART_SendData(USART3, 0x0D);
delay_ms(10);
USART_SendData(USART3, 0x0A);//启动扫描器
delay_ms(10);
while(USART_GetFlagStatus(USART3,USART_FLAG_TXE)==RESET);
delay_ms(5000);
}
中断程序:
void uart_init(u32 bound){
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3,ENABLE);
GPIO_PinAFConfig(GPIOB,GPIO_PinSource10,GPIO_AF_USART3);
GPIO_PinAFConfig(GPIOB,GPIO_PinSource11,GPIO_AF_USART3);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOB,&GPIO_InitStructure);
USART_InitStructure.USART_BaudRate = bound;
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_Rx | USART_Mode_Tx;
USART_Init(USART3, &USART_InitStructure);
USART_Cmd(USART3, ENABLE);
//USART_ClearFlag(USART1, USART_FLAG_TC);
#if EN_USART3_RX
USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);
NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3;
NVIC_InitStructure.NVIC_IRQChannelSubPriority =3;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
USART_ITConfig(USART3,USART_IT_RXNE,ENABLE);
#endif
}
void USART3_IRQHandler(void)
{
u32 Res;
unsigned char*a;
unsigned char i=0;
if(USART_GetITStatus(USART3, USART_IT_RXNE)!= RESET)
{
USART_ClearITPendingBit(USART3,USART_IT_RXNE);
Res=USART_ReceiveData(USART3); //(USART1->DR);
}
a=Res;
i++;
}
你配置完串口应该加上:USART_ClearFlag(USART3,USART_IT_RXNE);清楚中断标志,然后进中断的判断是:if(USART_GetFlagStatus(USART3,USART_FLAG_RXNE)!=RESET);不是 if(USART_GetITStatus(USART3, USART_IT_RXNE)!= RESET);
你的串口中断写错了
我觉得还是用寄存器的版本比较好,如果有需要话,可以私信我
長知識,學習~~~~