完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
嗨我有关于Usart中断的问题。我检查了所有使用RFI接口传输和接收数据的内容。我可以正确传输数据(我用USBee检查)但我的mcu不能计算接收数据。问题可能与配置选项有关,但我不确定。有人可以帮我解决这个问题吗?
如果(USART_DataInRxBuffer()) { 如果(USART_Rx()==为0x50) { GPIO_SetBits(LEDS_PORT,LED1); //关于GPIO选项的一切正确我没有在这里写 延迟((uint32_t的)60000); GPIO_ResetBits(LEDS_PORT,LED1); 延迟((uint32_t的)10000); } } & sharpdefine USART_RX_BUFFER_SIZE 128 / * 2,4,8,16,32,64,128或256字节* / & sharpdefine USART_TX_BUFFER_SIZE 128 / * 2,4,8,16,32,64,128或256字节* / & sharpdefine USART_RX_BUFFER_MASK(USART_RX_BUFFER_SIZE - 1) & sharpdefine USART_TX_BUFFER_MASK(USART_TX_BUFFER_SIZE - 1) & sharpif(USART_RX_BUFFER_SIZE& USART_RX_BUFFER_MASK) & sharperror RX缓冲区大小不是2的幂 &安培; sharpendif & sharpif(USART_TX_BUFFER_SIZE& USART_TX_BUFFER_MASK) & sharperror TX缓冲区大小不是2的幂 &安培; sharpendif // Rx缓冲区 typedef unsigned char uint8_t; static unsigned char USART_RxBuf [USART_RX_BUFFER_SIZE]; static volatile unsigned char USART_RxHead; static volatile uint8_t USART_RxTail; static volatile uint8_t USART_RxOverflow; // Tx缓冲区 / static unsigned char USART_TxBuf [USART_TX_BUFFER_SIZE]; static volatile uint8_t USART_TxHead; static volatile uint8_t USART_TxTail; void Delay(uint32_t nCount); void USART_RX_IRQHandler(void); void USART_TX_IRQHandler(void); unsigned char USART_Rx(void); uint8_t USART_DataInRxBuffer(void); enableInterrupts(); CLK_MasterPrescalerConfig(CLK_MasterPrescaler_HSIDiv1); //高速内部时钟预分频器:1 GPIO_ExternalPullUpConfig(GPIOC,GPIO_Pin_2 | GPIO_Pin_3,ENABLE); //将USART RX和USART TX设置为高电平 CLK_PeripheralClockConfig(CLK_Peripheral_USART,ENABLE); //启用USART时钟 USART_DeInit(); USART_Init((uint32_t)9600,USART_WordLength_8D,USART_StopBits_1,USART_Parity_No,USART_Mode_Rx | USART_Mode_Tx); //使能USART发送中断:USART发送数据寄存器为空时产生该中断 // USART_ITConfig(USART_IT_TXE,ENABLE); //使能USART接收中断:当USART接收数据寄存器不为空时,产生该中断 USART_ITConfig(USART_IT_RXNE,ENABLE); USART_Cmd(ENABLE) void USART_RX_IRQHandler(void) { unsigned char RxData; uint8_t tmphead; / *从接收数据寄存器读取一个字节* / RxData =(uint8_t)(USART_ReceiveData8()& 0x7F); //读取接收的数据 tmphead =(USART_RxHead + 1)& USART_RX_BUFFER_MASK; //计算缓冲区索引 USART_RxHead = tmphead; //存储新索引 if(tmphead == USART_RxTail) { / *错误!接收缓冲区溢出* / / *数据会丢失! * / USART_RxOverflow = 1; } 其他 { USART_RxBuf [tmphead] = RxData; //将接收的数据存储在缓冲区中 USART_RxOverflow = 0; } } uint8_t USART_DataInRxBuffer(void) { return(USART_RxHead!= USART_RxTail); / *如果接收缓冲区为空,则返回0(FALSE)* / } unsigned char USART_Rx(void) { uint8_t tmptail; //看看我们是否有任何数据 while(USART_RxHead == USART_RxTail); tmptail =(USART_RxTail + 1)& USART_RX_BUFFER_MASK; USART_RxTail = tmptail; 返回USART_RxBuf [tmptail]; } void Delay(uint32_t nCount) { / *减少nCount值* / while(nCount!= 0) { nCount--; } } #stm8l101xx #usart #buffer 以上来自于谷歌翻译 以下为原文 Hi i have problem about Usart interrupt . I checked everything i use RFI interface for transmit and receive data. I can transmit data correctly(i checked with USBee) but my mcu not calculate receive data. Problem can be about configuration options but i'm not sure. Can anybody help me about this subject? if(USART_DataInRxBuffer()) { if(USART_Rx()==0x50) { GPIO_SetBits(LEDS_PORT,LED1); //Everything right about GPIO Options I didn't write here Delay((uint32_t)60000); GPIO_ResetBits(LEDS_PORT,LED1); Delay((uint32_t)10000); } } &sharpdefine USART_RX_BUFFER_SIZE 128 /* 2,4,8,16,32,64,128 or 256 bytes */ &sharpdefine USART_TX_BUFFER_SIZE 128 /* 2,4,8,16,32,64,128 or 256 bytes */ &sharpdefine USART_RX_BUFFER_MASK ( USART_RX_BUFFER_SIZE - 1 ) &sharpdefine USART_TX_BUFFER_MASK ( USART_TX_BUFFER_SIZE - 1 ) &sharpif ( USART_RX_BUFFER_SIZE & USART_RX_BUFFER_MASK ) &sharperror RX buffer size is not a power of 2 &sharpendif &sharpif ( USART_TX_BUFFER_SIZE & USART_TX_BUFFER_MASK ) &sharperror TX buffer size is not a power of 2 &sharpendif // Rx Buffer typedef unsigned char uint8_t ; static unsigned char USART_RxBuf[USART_RX_BUFFER_SIZE]; static volatile unsigned char USART_RxHead; static volatile uint8_t USART_RxTail; static volatile uint8_t USART_RxOverflow; // Tx Buffer /static unsigned char USART_TxBuf[USART_TX_BUFFER_SIZE]; static volatile uint8_t USART_TxHead; static volatile uint8_t USART_TxTail; void Delay(uint32_t nCount); void USART_RX_IRQHandler(void); void USART_TX_IRQHandler(void); unsigned char USART_Rx( void ); uint8_t USART_DataInRxBuffer( void ); enableInterrupts(); CLK_MasterPrescalerConfig(CLK_MasterPrescaler_HSIDiv1);//High speed internal clock prescaler: 1 GPIO_ExternalPullUpConfig(GPIOC,GPIO_Pin_2|GPIO_Pin_3, ENABLE);//Set the USART RX and USART TX at high level CLK_PeripheralClockConfig(CLK_Peripheral_USART, ENABLE);// Enable USART clock USART_DeInit(); USART_Init((uint32_t)9600, USART_WordLength_8D, USART_StopBits_1, USART_Parity_No, USART_Mode_Rx|USART_Mode_Tx); // Enable the USART Transmit interrupt: this interrupt is generated when the USART transmit data register is empty //USART_ITConfig(USART_IT_TXE, ENABLE); // Enable the USART Receive interrupt: this interrupt is generated when the USART receive data register is not empty USART_ITConfig(USART_IT_RXNE, ENABLE); USART_Cmd(ENABLE) void USART_RX_IRQHandler(void) { unsigned char RxData; uint8_t tmphead; /* Read one byte from the receive data register */ RxData = (uint8_t) (USART_ReceiveData8() & 0x7F); //Read the received data tmphead = (USART_RxHead + 1) & USART_RX_BUFFER_MASK; //Calculate buffer index USART_RxHead = tmphead; // Store new index if (tmphead == USART_RxTail) { /* ERROR! Receive buffer overflow */ /* data will get lost !!! */ USART_RxOverflow = 1; } else { USART_RxBuf[tmphead] = RxData; // Store received data in buffer USART_RxOverflow = 0; } } uint8_t USART_DataInRxBuffer( void ) { return ( USART_RxHead != USART_RxTail); /* Return 0 (FALSE) if the receive buffer is empty */ } unsigned char USART_Rx( void ) { uint8_t tmptail; //see if we have any data while ( USART_RxHead == USART_RxTail ); tmptail = (USART_RxTail + 1) & USART_RX_BUFFER_MASK; USART_RxTail = tmptail; return USART_RxBuf[tmptail]; } void Delay(uint32_t nCount) { /* Decrement nCount value */ while (nCount != 0) { nCount--; } } #stm8l101xx #usart #buffer |
|
相关推荐
1个回答
|
|
关于你的代码,我注意到了几件事
头部和尾部没有初始化。此外,您似乎将新接收的字符添加到Q的头部并从尾部移除。当然你应该添加尾部(最新的char)并从头部(最旧的char)中删除FIFO。 以上来自于谷歌翻译 以下为原文 a couple of things I have noticed about your code head and tail are not initialised. Also you appear to be adding new received chars to the head of the Q and removing from the tail. Surely you should add to the tail ( latest char ) and remove form the head (oldest char) for FIFO. |
|
|
|
只有小组成员才能发言,加入小组>>
请教:在使用UDE STK时,单片机使用SPC560D30L1,在配置文件怎么设置或选择?里面只有SPC560D40的选项
2735 浏览 1 评论
3241 浏览 1 评论
请问是否有通过UART连接的两个微处理器之间实现双向值交换的方法?
1810 浏览 1 评论
3650 浏览 6 评论
6039 浏览 21 评论
1339浏览 4评论
201浏览 3评论
对H747I-DISCO写程序时将CN2的st-link复用为usart1,再次烧录时无法检测到stlink怎么解决?
353浏览 2评论
STM32G474RE芯片只是串口发个数据就发烫严重是怎么回事?
444浏览 2评论
STM32处理增量式编码器Z信号如何判断中断是正转的还是反向转的?
274浏览 2评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-12-24 20:21 , Processed in 1.083021 second(s), Total 46, Slave 40 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号