Atmega UART buffering problem

Status
Not open for further replies.

agreatstar

Junior Member level 2
Joined
May 14, 2012
Messages
20
Helped
2
Reputation
4
Reaction score
2
Trophy points
1,283
Activity points
1,464
Code:

Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
int main()
{
DDRD=0xFE;
uart0_init();
char y=0,myScore;
_delay_ms(3000);
up:
while(1){
y=rx_char2();
 
 if(y==('x')){
myScore++;
sendInfo(currlife,myScore);
}
}//while
}//main
 
 
unsigned char rx_char2(void)
{
if(!(UCSRA & (1<<RXC)))
return 0;
else
return (UDR);
}
 
 void uart0_init(void)
{
 
 UBRRL = 51; 
 UBRRH = 0x00;
 UCSRA = 0x20;
 UCSRC = 0x86;
 UCSRB = 0x18;
}


Here my sendInfo function is such that it displays the core on LCD. I encounter a strange behaviour here. If I press 'x' with sufficient time gap, the score is perfectly incremented and displayed on the LCD. But if I keep 'x' pressed such that say 10 'x' are transmitted to atmega almost simultaneously, the score increments only by 4. Then if I transmit one more 'x' , score suddenly increments by the missing amount, ie the correct value(11 in this case) is displayed. SO it seems that atmega is buffering the incoming characters yet the RXC flag is not being set if a bunch of characters are received simultaneously. I have tried it on hardware as well as simulation(Proteus with virtual terminal), and get the same result on both.
I dont know what I am missing here.
 
Last edited by a moderator:

Test this code
Code:
unsigned char rx_char2(void)
{
while(!(UCSRA & (1<<RXC)));
return (UDR);
}
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…