Hi everyone!
I'm a n00b to microcontroller programming so have patience with me :)
The mission is to handle some uart (rs232) communication, both rx and tx. My part is a small module going into a rest of the software already developed.
The issue is the following: When I use serial ISR it seems to not go into the "main while loop" (which is required).
(I've also tried to poll my self but reliability seems somewhat unstable, loosing a byte here and there)
I've tried the simplest demo/test programs and if I for instance use a printf in the while loop, it will not reach it as soon as ES = 1.
Is it just that any printf will not issue an interrupt and hence no output to my terminal? At the moment I can not test my module with the rest of the software so I'm a bit in the blind here, well could of course try to blink a led or something... but I reacon a answer from here is faster and more reliable :)
Thanks in advance
/Mike
Of course here it is:
#include "at89c5131.h" #include "stdio.h"
char uart_data
void main (void) { int i=0; uartInit(); // Global interrupt EA = 1; // Serial interrupt // Comment out below and printf in while will work ES = 1; while(1) { printf("..."); } } // The ISR function echos the received character void serial_IT(void) interrupt 4 { if(RI==1) { RI=0; uart_data = SBUF; //Read received data SBUF = uart_data; //Send back same data on uart } else TI=0; }
uart_init() would also be of interest.
Also, as I mentioned below, using printf() and using interrupt-driven operation on the UART at the same time is most likely a bad idea. You may need to find a different method to indicate that the program has entered the main loop (e.g. toggle a port pin or blink a LED - or use a breakpoint if your emulator/debugger supports it).
void uartInit(void); { SCON = 0x50; TMOD |= 0x20; TH1 = 0xF7; TR1 = 1; PCON |= 0x80; TI = 1; }
By the way, thanks for your interest in my problem :)
Okay, I think we're getting somewhere. Your problem is a good example why interrupt-driven and polling operation on a peripheral should not be mixed.
I've added some comments to your code that should show what is happening.
void uartInit(void); { SCON = 0x50; TMOD |= 0x20; TH1 = 0xF7; TR1 = 1; PCON |= 0x80; TI = 1; } void main (void) { int i=0; uartInit(); // This sets TI to 1 // Global interrupt EA = 1; // Serial interrupt // Comment out below and printf in while will work ES = 1; // Since TI is 1, MCU will jump to UART ISR immediately while(1) { printf("..."); // printf will call putchar(), and putchar will enter an endless loop since TI is 0 } } // The ISR function echos the received character void serial_IT(void) interrupt 4 { if(RI==1) { RI=0; uart_data = SBUF; //Read received data SBUF = uart_data; //Send back same data on uart } else TI=0; // If TI was 1, then TI is set to 0 }