Question

I am trying to get data over UART serial on PC from Silicon Labs C8051f320

I usually program in Java so correct me if i am wrong somewhere with C language or MCUs understanding.

Here is my code that handles UART Interrupts

Unsigned char Byte;
unsigned char TX_Ready =1;
void UART0_Interrupt (void) interrupt 4{
if (RI0 == 1)// Check if Rec flag is set
{
            // Do nothing 

  }

 if (TI0 == 1)  {                 // Check if transmit flag is set

  SBUF0 = Byte;          // copy the character from Byte to Buffer
  TI0 = 0;               // Set Transmit flag to 0

 }

 }

Here is my Method for Sending the Data

void SendData(){
unsigned char incoming_str[] = {'d','s','a','\r'};   //array of chars to send

TX_Ready = 0;

for(i=0;i<sizeof(incoming_str);i++) { 

 Byte = incoming_str[i];

 TI0=1;



 } 


 }

First Problem i am getting the data in reversed order (is it normal). Second Problem i am getting characters in wrong data

Here is an output sample:

a s d

a s d

a s d

a s s

a a d

a s d

a s d

a s d

a s d

a s d

a s s

a a d

a s d

a s d

a s d

a s d

a s d

a s s

a a d

a s d

a s d

a s d

a s

Was it helpful?

Solution

A fair miracle it works at all. You are not doing anything to ensure that the interrupt handler is in sync with the SendData() loop. Getting bytes to repeat or disappear is therefore expected.

Two basic schemes. If you don't worry about the delays, like you do now, then you don't use the interrupt. You simply loop until SCON.1 == 1, then write SBUF.

If you do care about delay then you need the interrupt. You'll need a circular buffer to store bytes. Your interrupt handler pulls a byte from the buffer and writes it to SBUF. If the buffer is empty it needs to set a bool to indicate that an interrupt was detected but not serviced. You now send bytes by disabling interrupts and append the byte(s) you want to send to the circular buffer. If the bool flag is set then immediately write a byte to SBUF and clear the flag. Re-enable interrupts.

OTHER TIPS

Also check the clock source - and what is the failure ratio calculating the baud rate/crytal ??

I once had this with two AVRs. As I could not stabilize the clock sources in my case I implemented a simple checking, by sending "byte ff-byte" - like that the receiver could verify whether the data was correct

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top