Skip to main content
lowbsmallfung
Associate
August 28, 2009
Question

USART1 recive problem(PLEase help)

  • August 28, 2009
  • 8 replies
  • 1340 views
Posted on August 28, 2009 at 02:03

USART1 recive problem(PLEase help)

    This topic has been closed for replies.

    8 replies

    lowbsmallfung
    Associate
    May 17, 2011
    Posted on May 17, 2011 at 13:21

    hi All,

    Does anyone can help me to solve the problem listed in the following:

    I am try to get a lot of date from hyperterminal and then send back e.g.10 char.

    I can collect correctly if only one character but if I send more than 2 char from the hyperterminal to my board....it doesn't work.

    I am using interrupt function to collect date.

    the code is shown in the followings:

    void USART1_IRQHandler(void)

    {u8 Rx_Char;

    while ( USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET);

    Rx_Char=(u8)USART1->DR;

    USART1->DR = (u8)Rx_Char;

    while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);

    }

    THX*100000000000000000000000000

    robertwood9
    Associate III
    May 17, 2011
    Posted on May 17, 2011 at 13:21

    Is the interrupt actually working? Can you break on character received? If so, what is happening? How is it losing characters? How do you know? More information please, be as specific as possible.

    lowbsmallfung
    Associate
    May 17, 2011
    Posted on May 17, 2011 at 13:21

    hi robert,

    The abnormal I mean is if I send ''abcde''.It will output unknown character. If I just send ''a'', the output is ''a''.

    The interrupt routine code is shown here.

    void USART1_IRQHandler(void)

    {u8 Rx_Char;

    while ( USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET);

    Rx_Char=(u8)USART1->DR;

    USART1->DR = (u8)Rx_Char;

    while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);

    }

    Would you please suggest how to use buffer to solve my problem.

    Many thx

    robertwood9
    Associate III
    May 17, 2011
    Posted on May 17, 2011 at 13:21

    What do you mean by abnormal? Are you saving the data in the interrupt routine into a buffer? Are you using parity checking? Is it possible that you have that wrong?

    lowbsmallfung
    Associate
    May 17, 2011
    Posted on May 17, 2011 at 13:21

    thx robert.

    The interrupt is 100% work. I have tested as the following steps:

    1. Type 1 char in the hyperterminal and send to my board.

    2. My board recevie the char and then send back to the hyperterminal

    3. The hyperterminal display the char which I send.

    My problem is when I type more than 1 char in the hyperterminal and send them as once.

    The date is abnormal.

    Would you please suggest some solution to solve it.

    Mant thx

    trevor1
    Associate III
    May 17, 2011
    Posted on May 17, 2011 at 13:21

    Your approach is wrong. For one you can't wait inside an interrupt for something to happen that is going to cause the same interupt -- in general don't wait inside an interrupt for anything (in, save, out).

    You either use polling or you use interrupts for serial comms. You don't need the interrupt in this case. Suggest you do the following.

    Code:

    for (;;)

    {

    u8 Rx_Char;

    while ( USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET);

    Rx_Char=(u8)USART1->DR;

    while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);

    USART1->DR = (u8)Rx_Char;

    }

    If you want to use an interrupt to achieve this post back and I'll suggest something else.

    Regards

    Trevor

    robertwood9
    Associate III
    May 17, 2011
    Posted on May 17, 2011 at 13:21

    Ah, so what you're trying to do is just echo the character back? I should have read your first posting more carefully. A buffer's not really the answer if that's all you're trying to do I guess.

    For the record a buffer would be something like this:

    RxBuff[RxBuffPtr] = (u8)USART1->DR;

    RxBuffPtr++;

    if (RxBuffPtr == MAX_BUFSIZE)

    RxBuffPtr = 0;

    What is the purpose of this line?

    while ( USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET);

    Is it possible this is just sat waiting for something when the next character comes in?

    picguy
    Associate III
    May 17, 2011
    Posted on May 17, 2011 at 13:21

    See my

    http://www.hmtown.com/fifo.htm

    It discusses a way to separate logical and physical RS-232 I/O. Amaze your friends - have data going both directions at once.

    The FIFO part of this page is not limited to I/O. As long as there is a single source and a single sink this process works well even if the data and pointers are in a multiprocessor system with shared RAM.

    As trevor points out, waiting for anything inside and ISR is a serious no-no. With my physical / logical separation your mainline code looks like

    for(;;)

    {

    read a byte - with hard wait for data

    write that byte - waiting if output buffer is full

    }