Reputation: 2334
I previously used Keil for programming 8051 microcontrollers. For some reason I have to code in SDCC, but today I am facing very strange behavior in Compiler. I am using Code blocks IDE 12.11 and SDCC 3.4 version.
I am compiling this simple piece of code.
#include <mcs51/8051.h>
#include "Serial.h"
unsigned char digits[5]={0};
void main(void)
{
serial_init(-13);
digits[2]='a';
serial_send(digits[2]);
serial_send('a');
while(1)
{
}
}
and here is the defination of serial_send function.
void serial_send(unsigned char dat){
while(!TI);
TI = 0;
SBUF = dat;
}
The problem is that, according to the code it should print 'a' character two times on terminal but it is printing only one time. The problem is in global veriable digits[] array.
The function only work properly with constant value but not with variable bassed by argument.
I am posting this question here because I think this problem with is about some C language trick, which I am unable to figure out.
I tried re-installing the compiler and IDE both but the problem remains the same. will some body please explain why this is happening. I have tried different codes and in all code the constant and local variables works fine but global variables providing strange behaviors.
Upvotes: 1
Views: 759
Reputation: 21
Check that your empty while loop while(!TI);
is not being optimised out by the compiler. The main code is ambiguous as you can't tell which 'a'
you're actually seeing being sent.
Change of them to a 'b'
and try swapping the order of the two serial_send()
calls to verify that it's not simply a case of SBUF being prematurely overwritten with new data before the uart has sent the current byte.
Upvotes: 2