# include <stdio.h> # include <aduc842.h> #pragma registerbank(2) void sendmoduleinit(void); void InitByte_Char (volatile unsigned long); void print2uart_hex(int); void displaytemp1(void); void displaycal(void); void sendremaining(void); volatile unsigned char moduleinit; int ucRxChar1; int check_sum; int check_sum_tx; void main() { EA = 1; ES = 1; T3CON = 0x83; T3FD = 0x2D; SCON = 0x50; if(ucRxChar1 == 0xCC) { sendmoduleinit(); } if (ucRxChar1 == 0xFA && ucRxChar1 == 0xFA && ucRxChar1 == 0x05 && ucRxChar1 == 0xAA && ucRxChar1 == 0x2F) {displaytemp1(); } if (ucRxChar1 == 0x05 && ucRxChar1 == 0xAB) {displaycal(); } } void ISR_sc(void) interrupt 4 using 2 { if (TI==1) { TI=0; //clear interrupt } else { ucRxChar1 = SBUF; //put value on pins RI=0; //clear interrupt } /*REN = 1; while(RI == 0) {} ucRxChar1 = SBUF; RI = 0; */ } void sendmoduleinit(void) { check_sum = (0xF5 ^ 0xF5 ^ 0x06 ^ 0xFF ^ 0xFF); check_sum_tx = (check_sum & 0x7F); InitByte_Char(0xF5); InitByte_Char(0xF5); InitByte_Char(0x06); InitByte_Char(0xFF); InitByte_Char(0xFF); print2uart_hex(check_sum_tx); } void InitByte_Char (volatile unsigned long result) { REN = 0; SBUF = result; while (TI == 0) { } TI = 0; REN = 1; } void print2uart_hex(int chksum) { REN = 0; SBUF = chksum; while (TI == 0) { } TI = 0; REN = 1; } void displaytemp1(void) { check_sum =(0xA0 ^ 0xE2 ^ 0x07 ^ 0x00 ^ 0x62 ^ 0x00); check_sum_tx = (check_sum & 0x7F); InitByte_Char(0xA0); InitByte_Char(0xE2); InitByte_Char(0x07); InitByte_Char(0x00); sendremaining(); //InitByte_Char(0x62); // InitByte_Char(0x00); // print2uart_hex(check_sum_tx); } void sendremaining(void) { InitByte_Char(0x62); InitByte_Char(0x00); print2uart_hex(check_sum_tx); } void displaycal(void) { check_sum =(0xA0 ^ 0xE2 ^ 0x07 ^ 0x00 ^ 0x25 ^ 0x00); check_sum_tx = (check_sum & 0x7F); InitByte_Char(0xA0); InitByte_Char(0xE2); InitByte_Char(0x07); InitByte_Char(0x00); InitByte_Char(0x25); InitByte_Char(0x00); print2uart_hex(check_sum_tx); }
This is my code. Over here I am trying to read bytes into SBUF and depending upon bytes received I am trying to send some packets. My code is working but reading into SBUF is taking time... Reading a single byte is not taking time but reading an entire packet takes time can u suggest some alternative soln regards Mayuri
Some people define "working" as: tools managed to compile/link to a binary.
if (ucRxChar1 == 0xFA && ucRxChar1 == 0xFA && ucRxChar1 == 0x05 && ucRxChar1 == 0xAA && ucRxChar1 == 0x2F)
Some better compilers would notice that above expression can never be true, and warn about unreachable code.
But there are lots of interesting things with this code - for example why it constantly turns on/off the UART receiver. Is RX and TX of the UART connected together, so you need to stop the UART from hearing its own transmission?
Working? Really?
How can ucRxChar1 equal 0xFA, 0x05, 0xAA, and 0x2F all that the same time?
Also there's that little problem of running off the end of main().
My code is working but reading into SBUF is taking time...
It's not the reading into SBUF that is taking time. It's the way your grabbing it and then checking for a specific sequence of characters.
You need to revisit this code and consider how characters are received by the processor.
The speed of the UART is controlled by the baudrate.
If you want to be able to send data quicker, you must select a higher baudrate.
If you want to receive data quicker, you must select a higher baudrate.
Note, that a higher baudrate just means that a single character gets transmitted faster. The other side can still decide to wait before it sends the next character.
When transmitting, you can add a ring-buffer. This allows your program to just enqueue the outgoing data and have your interrupt handler pick up the next enqueued character when you get an interrupt that the UART is ready for more.
This allows "instant" transmission, as seen by your main program loop, unless it tries to send so much data that the ringbuffer gets full - then the speed will slow down to the speed decided by the UART baudrate.
Your code is almost impossible to read because of the indentation issues. You should switch your editor to indent with spaces, and then clean up all indentations and post again.
But unless I have missed counting { and } in your code, it seems like your UART interrupt handler have a busy-loop for receiving data. You do not (!) want any busy-loop in the interrupt handler. Let the UART generate an interrupt when it has data. Don't try to get data when none is there. Remember that it can take hours, or even years, until a character arrives.
Another interesting thing is that you let the main loop use polling for sending data. But your UART ISR makes use of both RI and TI in a rather interesting way. Do you really think you can look at TI to decide if there is data to read?
View all questions in Keil forum