AnsweredAssumed Answered

USART & Timer Interrupt

Question asked by Mujtaba Hossaini on Nov 17, 2016
Latest reply on Nov 21, 2016 by sharief.adil
Hello,

I am trying to use USART1 to transmit and receive data in every 100ms. I've used both USART1 TXE and RXNE interrupts and Timer2 update interrupt but unfortunately it's not working and I don't have any data transmission nor reception!
I am using stm32f103rbt6 microcontroller.
Could you help me and tell me what I have to do ?
Any helps would be appreciated.

The main code is shown bellow:


001.#include "stm32f10x.h"
002.#include "stm32f10x_it.h"
003.#include "stm32f10x_gpio.h"
004.#include "stm32f10x_tim.h"
005.#include "stm32f10x_usart.h"
006.#include "misc.h"
007.void USART_config(void);
008.void RCC_config(void);
009.void NVIC_config(void);
010.void GPIO_config(void);
011.void Timer2_config(void);
012.USART_InitTypeDef USART_InitStructure;
013.GPIO_InitTypeDef GPIO_InitStructure;
014.NVIC_InitTypeDef NVIC_InitStructure;
015.TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
016.unsigned short write_led1_on[]={0x79,0x06,0x00,0x10,0x00,0x01,0x43,0xB7};
017.unsigned short write_led2_on[]={0x79,0x06,0x00,0x11,0x00,0x01,0x12,0x77};
018.unsigned short write_led_off[]={0x79,0x10,0x00,0x10,0x00,0x02,0x00,0x00,0x00,0x00,0x07,0xC2};
019.unsigned short read_2button_panel_touch_status[]={0x79,0x03,0x00,0x00,0x00,0x02,0xCE,0x73};
020.unsigned short *TxBuffer;
021.unsigned short *RxBuffer;
022.uint8_t TxLength=0x00;
023.uint8_t TxCount=0x00;
024.uint8_t ReceiveComplete=0,START=0;
025.uint8_t RxCount=0x00;
026.uint8_t Length;
027.ErrorStatus HSEStartUpStatus;
028.int main(void)
029. {
030.   RCC_config();
031.   GPIO_config();
032.   USART_config();
033.   Timer2_config();
034.   NVIC_config();
035.   //Enable the TIM2 Update Interrupt source:
036.   TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE);
037.   USART_ITConfig(USART1,USART_IT_TXE,ENABLE);
038.   USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);
039.   while(1)
040.   {
041.    TxBuffer=read_2button_panel_touch_status;
042.    TxLength=8;
043.    START=0;
044.    //Enable the TIM2:
045.    TIM_Cmd(TIM2,ENABLE);
046.    while(START!=1);
047.    while(ReceiveComplete!=1);
048.    if(RxBuffer[4]==0x01)
049.     {
050.      TxBuffer=write_led1_on;
051.      TxLength=8;
052.      }
053.    else if(RxBuffer[6]==0x01)
054.     {
055.      TxBuffer=write_led2_on;
056.      TxLength=8;
057.     }
058.   else
059.     {
060.      TxBuffer=write_led_off;
061.      TxLength=12;
062.      }
063.   //Enable the TIM2:
064.   TIM_Cmd(TIM2,ENABLE);
065.   while(!START);
066.   while(!ReceiveComplete);
067.  }
068.}
069.void RCC_config(void)
070.{
071. RCC_DeInit();
072. RCC_HSEConfig(RCC_HSE_ON);
073. HSEStartUpStatus = RCC_WaitForHSEStartUp();
074. if(HSEStartUpStatus == SUCCESS)
075. {
076.  RCC_PLLConfig(RCC_PLLSource_HSE_Div1,RCC_PLLMul_9);
077.  RCC_PLLCmd(ENABLE);
078.  while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET);
079.  RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
080.  while(RCC_GetSYSCLKSource() != 0x08);
081.  RCC_HCLKConfig(RCC_SYSCLK_Div1);
082.  RCC_PCLK1Config(RCC_HCLK_Div2);
083.  RCC_PCLK2Config(RCC_HCLK_Div1);        RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO|RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOC|RCC_APB2Periph_USART1,ENABLE);
084.  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
085. }
086.}
087.void GPIO_config(void)
088. {
089.   //USART1 TX GPIO Configuration:
090.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
091.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
092.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
093.   GPIO_Init(GPIOA,&GPIO_InitStructure);
094.   //USART1 RX GPIO Configuration:
095.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
096.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
097.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
098.   GPIO_Init(GPIOA,&GPIO_InitStructure);
099.    // LED1 GPIO Configuration:
100.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
101.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
102.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
103.   GPIO_Init(GPIOA,&GPIO_InitStructure);
104.    // DE & RE signal Configuration:
105.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
106.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
107.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
108.   GPIO_Init(GPIOC,&GPIO_InitStructure);   
109. }
110.void USART_config(void)
111. {
112.   //USART1 Configuration:
113.   USART_InitStructure.USART_BaudRate = 19200;
114.   USART_InitStructure.USART_WordLength = USART_WordLength_8b;
115.   USART_InitStructure.USART_StopBits = USART_StopBits_1;
116.   USART_InitStructure.USART_Parity = USART_Parity_No;
117.   USART_InitStructure.USART_HardwareFlowControl =
118.   USART_HardwareFlowControl_None;
119.   USART_InitStructure.USART_Mode = USART_Mode_Tx|USART_Mode_Rx;
120.   USART_Init(USART1,&USART_InitStructure);
121. }
122.void NVIC_config(void)
123. {
124.  NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
125.  //Enable the USART1 Interrupt:
126.  NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
127.  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
128.  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
129.  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
130.  NVIC_Init(&NVIC_InitStructure);       
131.  //Enable the TIM2 Interrupt:
132.  NVIC_InitStructure.NVIC_IRQChannel =TIM2_IRQn;
133.  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
134.  NVIC_InitStructure.NVIC_IRQChannelSubPriority =1;
135.  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
136.  NVIC_Init(&NVIC_InitStructure);       
137. }
138.void Timer2_config(void)
139.{
140.  TIM_TimeBaseStructure.TIM_Period =16000-1;
141.  TIM_TimeBaseStructure.TIM_Prescaler =450-1;
142.  TIM_TimeBaseStructure.TIM_ClockDivision = 0;
143.  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
144.  TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);   
145.}

and the interrupt routine code is shown as bellow:


001.void USART1_IRQHandler(void)
002.{
003. if((USART1->SR & 0x80)==0x80)
004. {
005.  //If the buffer is fully transmitted:
006.  if(TxCount == TxLength)
007.  {
008.   //Set DE pin to low level (TC interrupt is occured ):
009.   GPIOA->BRR = GPIO_Pin_2;
010.   GPIOC->BSRR=GPIO_Pin_6;
011.   //Disable the USART1 Transmit Complete interrupt:
012.   USART_ITConfig(USART1,USART_IT_TC, DISABLE);
013.   USART_ClearFlag(USART1,USART_FLAG_TC);
014.   TxCount=0;
015.   START=0;
016.   ReceiveComplete=0;
017.   //Enable USART1 TXE Interrupt:
018.   USART_ITConfig(USART1, USART_IT_TXE, ENABLE);
019.   }
020. else if (TxCount == TxLength-1)
021.  {
022.   if(USART_GetFlagStatus(USART1,USART_FLAG_TXE)!=RESET)
023.    {
024.     USART1->DR = TxBuffer[TxCount++];
025.     //Disable USART TXE interrupt:
026.     USART_ITConfig(USART1, USART_IT_TXE, DISABLE);
027.     //Enable TC interrupt at the last byte to transmit:
028.     USART_ITConfig(USART1, USART_IT_TC , ENABLE);
029.     }
030.  }
031. else if(TxCount != 0) //if it's not the first byte
032. {
033.  //USART1 Tx empty interrupt has occured:
034.  if(USART_GetFlagStatus(USART1,USART_FLAG_TXE)!=RESET)
035.   {
036.    //Continue the buffer transmission via USART:
037.    USART1->DR = TxBuffer[TxCount++];
038.    }
039. }
040.else if(TxCount==0)//The first byte to be transmitted:
041. {
042.  //USART1 Tx empty interrupt has occured:
043.  if(USART_GetFlagStatus(USART1,USART_FLAG_TXE)!=RESET)
044.  {
045.   USART_ITConfig(USART1, USART_IT_RXNE, DISABLE);
046.   //Set DE pin to high level:
047.   GPIOA->BSRR = GPIO_Pin_2;
048.   GPIOC->BRR=GPIO_Pin_6;
049.   //Transmit the first byte:
050.   USART1->DR = TxBuffer[TxCount++];
051.  }
052. if(TxLength == 1 )
053. {
054.  //Enable TC interrupt at the last byte to transmit (single byte):
055.  //Enable USART TC interrupt:
056.  USART_ITConfig(USART1, USART_IT_TC , ENABLE);
057.  //Disable USART TXE interrupt:
058.  USART_ITConfig(USART1, USART_IT_TXE, DISABLE);
059. }
060.}
061.}
062.else if((USART1->SR & 0x20)==0x20)
063.{
064.  if(RxCount>2)
065.   {
066.    if(RxCount<RxBuffer[2]+2+3-1)
067.    {
068.     if(USART_GetFlagStatus(USART1,USART_FLAG_RXNE)!=RESET)
069.     {
070.      //Continue the buffer transmission via USART:
071.      RxBuffer[RxCount++]=USART1->DR;
072.      }
073.    }   
074.    else if(RxCount==RxBuffer[2]+2+3)
075.    {
076.     if(USART_GetFlagStatus(USART1,USART_FLAG_RXNE)!=RESET)
077.     {
078.      RxBuffer[RxCount++]=USART1->DR;
079.      ReceiveComplete=1;
080.      USART_ITConfig(USART1, USART_IT_RXNE,DISABLE);
081.      RxCount=0;
082.      START=0;
083.      GPIOA->BSRR=GPIO_Pin_2;
084.      GPIOC->BRR=GPIO_Pin_6;
085.      }
086.    }
087.   }
088.   else
089.    {
090.     if(USART_GetFlagStatus(USART1,USART_FLAG_RXNE)!=RESET)
091.     {
092.      //Continue the buffer transmission via USART:
093.      RxBuffer[RxCount++]=USART1->DR;
094.     }
095.    }
096.  }
097.}
098.void TIM2_IRQHandler(void)
099.{
100. if(TIM_GetITStatus(TIM2, TIM_IT_Update) == SET)
101.    {
102.     TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
103.     START=1;
104.     //USART_ITConfig(USART1,USART_IT_TXE,ENABLE);
105.     USART_Cmd(USART1,ENABLE);
106.     TIM_Cmd(TIM2,DISABLE);
107.    }   
108.}

Outcomes