# 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
Are you understanding what I'm writing? At all?
Actually I am a fresher and there is no one to help me out.
pls help me out
So, have you read the answer above? They contain all the help you need!
My problem is solved now.
I have used while loop instead of for loop
I am helping you. I am saying that the ISR gets notified if there exists one (1) received byte. So the ISR should only try to pick up one (1) character.
It's irrelevant what the protocol looks like - let the main loop worry about what characters are received - and when.
Next thing - haven't you realized that you can receive one random character (maybe after insert of cable) and then get your 0xCC as second character. But your code think it should look at first entry of array for 0xCC. If you get a spurious character before the 0xCC, you get into a deadlock until the other side for some reason sends enough characters to get you out of your 5-character loop.
Felicitations !
thnx a lot every1 for ur help...
My problem is solved..
u ppl wer very supportive...:)
regards Mayuri
It doesn't matter if you have do { ... } while (x) or while (x) { ... } or for (;;) { ... }
The ISR should not contain any loop to receive multiple characters. It should pick up one (1) character if you got there because of a receive interrupt. It should pick up zero (0) if you got there because of a transmit interrupt.
Only for processors that have a FIFO functionality in the UART, should an ISR pick up multiple received characters. But such a UART can wait with generating the interrupt until the receive FIFO gets past the configured watermark. For a chip without a receive FIFO - or with a receive FIFO not activated - the ISR should never ever try to receive more than one character. Never! Never! Never! And again - never!
An ISR should be quick. Instant in. Instant out. No loop waiting an unknown time until there _maybe_ comes more characters for the ISR to pick up.
Throw away the loop from the ISR.
Google for code for a ring buffer.
Let the ISR insert bytes one-by-one into the ring buffer.
Let the main loop consume bytes one-by-one from the ring buffer.
Let the main loop consider what bytes that may represent the start of a packet - and how many more characters you may require based on how the packet started.
But no: NO LOOP WAITING FOR MORE CHARACTERS IN THE ISR. No "while". No "for". Instant in. Instant out.
My code is working NO, it is My code is 'working'
working without quotes means working. 'working' with quotes means appears to work
no code can be WORKING with wait loops in an ISR
NEVER share routines between mani and ISR KISS Keep ISRs Short and Simple
Erik