AnsweredAssumed Answered

STM8L101xx Usart Rx Interrupt Problem

Question asked by tekdemir.mustafa on Jan 20, 2012
Latest reply on Feb 16, 2012 by mcwiggin.patrick
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?

exam :
 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);
                   }
       }
           /////////////////////////// DEFINES///////////////////////////////
#define USART_RX_BUFFER_SIZE 128     /* 2,4,8,16,32,64,128 or 256 bytes */
#define USART_TX_BUFFER_SIZE 128     /* 2,4,8,16,32,64,128 or 256 bytes */
#define USART_RX_BUFFER_MASK ( USART_RX_BUFFER_SIZE - 1 )
#define USART_TX_BUFFER_MASK ( USART_TX_BUFFER_SIZE - 1 )

#if ( USART_RX_BUFFER_SIZE & USART_RX_BUFFER_MASK )
   #error RX buffer size is not a power of 2
#endif
#if ( USART_TX_BUFFER_SIZE & USART_TX_BUFFER_MASK )
   #error TX buffer size is not a power of 2
#endif 
// 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 );
/////////////////////////////// USART OPTIONS ///////////////////////////////////
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 //<---IS THAT TRUE ????///////////////
                
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)
////////////////////////////////////////////BUFFER FUNCTIONS//////////////////////////////////
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--;
  }
}

Outcomes