嗨我有关于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;
sta
tic 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
0