This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Send and receive simultaneously via USART

Hello
Here is my USART's settings:


        USART_InitTypeDef USAR;
        GPIO_InitTypeDef GPIOStruc;

        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB |RCC_APB2Periph_AFIO,ENABLE);
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
        GPIO_PinRemapConfig(GPIO_Remap_USART1,ENABLE);

        GPIOStruc.GPIO_Mode=GPIO_Mode_AF_PP ;
        GPIOStruc.GPIO_Speed=GPIO_Speed_50MHz;
        GPIOStruc.GPIO_Pin=GPIO_Pin_6;
        GPIO_Init(GPIOB,&GPIOStruc);

        GPIOStruc.GPIO_Mode=GPIO_Mode_IN_FLOATING ;
        GPIOStruc.GPIO_Pin=GPIO_Pin_7;
        GPIO_Init(GPIOB,&GPIOStruc);

        USAR.USART_BaudRate=115200;
        USAR.USART_StopBits=USART_StopBits_1;
        USAR.USART_WordLength=USART_WordLength_8b;
        USAR.USART_Parity=USART_Parity_No ;
        USAR.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
        USAR.USART_Mode=USART_Mode_Rx | USART_Mode_Tx;
        USART_Init(USART1,&USAR);
        USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);
  NVIC_EnableIRQ(USART1_IRQn);
        USART_Cmd(USART1,ENABLE);

and I send 200 characters with printf function each 20ms(that created by timer).

timer settings

TIM_TimeBaseInitTypeDef TimeStruct;
NVIC_InitTypeDef nvicStructure;
nvicStructure.NVIC_IRQChannel = TIM2_IRQn;
nvicStructure.NVIC_IRQChannelPreemptionPriority = 0;
nvicStructure.NVIC_IRQChannelSubPriority = 1;
nvicStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&nvicStructure);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
TimeStruct.TIM_Prescaler=7200;
TimeStruct.TIM_Period=200;
TimeStruct.TIM_ClockDivision=TIM_CKD_DIV1;
TimeStruct.TIM_CounterMode=     TIM_CounterMode_Up ;
TimeStruct.TIM_RepetitionCounter        =0;
TIM_TimeBaseInit(TIM2, &TimeStruct);
TIM_Cmd(TIM2, ENABLE);
TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE);

printf("LOng string......");

struct __FILE { int handle;} ;
FILE __stdout;
FILE __stdin;
FILE __stderr;
int fputc(int ch, FILE *f)  {
        while(!USART_GetFlagStatus(USART1,USART_FLAG_TXE));
        USART_SendData(USART1,ch);
        return ch;

but when I send a 120 characters string to my device it doesn't do anything

void USART1_IRQHandler(void){
if (USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
{
        temp=USART_ReceiveData(USART1);
}
}

but when I increase timer step (for example 100ms) it works fine.
what's wrong with it ? what is my mistake?

Parents
  • Make use of UART transmit interrupts to send.

    If each message to send should differ, then have the main loop create the strings.

    Let the timer interrupt activate the UART transmission - the UART ISR will then pick up new characters as they are consumed. When the last character has transmitted, then turn off the UART TX interrupt and leave off until the timer ISR kick-starts the next transmission.

    With a quick timer ISR, you get less latency issues from concurrently pending interrupt sources.

Reply
  • Make use of UART transmit interrupts to send.

    If each message to send should differ, then have the main loop create the strings.

    Let the timer interrupt activate the UART transmission - the UART ISR will then pick up new characters as they are consumed. When the last character has transmitted, then turn off the UART TX interrupt and leave off until the timer ISR kick-starts the next transmission.

    With a quick timer ISR, you get less latency issues from concurrently pending interrupt sources.

Children