Read 2 character from uart

Status
Not open for further replies.

bluelake

Newbie level 2
Joined
Aug 29, 2009
Messages
2
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,297
read 2 character pic16f877 serial interrupt

Hi,

I'm working with a pic16f877. I can't figure out how to read 2 characters (2 byte + 2 byte) from serial with no delay between them.
I'm using serial interrupt here a piece of code:

Code:
void interrupt ISR{

 if(RCIF) byte1 = RCREG;

}


main
{

 while(1){

   CMD = byte1;
   CHK = byte2;
 }

}

What I want to do is use serial interrupt and save the first character in CMD and the second one in CHK, But if I run the program, CMD always get the last character of the 2 characters.
I hope you can see my problem and tell me if there is some way to read all 2 characters and save them in CMD and CHK.

thank you
 

Four problems:
1. You need to clear the interrupt flag before you leave the interrupt routine
2. There is no way of telling which is the first byte and which the second - unless there will only ever be two.
3. You never assign a value to 'Byte2'
4. You missed the code tag off the beginning of your listing

Brian.
 

The interrupt flag is cleared by the hardware after RCREG is read.

Maybe I don't know if interrupt trigged after first character is received or after second character is received. The 2 characters are send by the master and there is no pause between them.

I havn't save any data in CHK because I don't know how to save 2 data separatly.
 

You cannot do it the way you are saying. It is easy to receive two bytes but unless you have some way to tell them apart, it is impossible to say which is CMD and which is CHK.

I would try this:
Code:
char ReceivedByte;
char ByteIsReceived;

void interrupt ISR
{
 if(RCIF) ReceivedByte = RCREG;
 ByteIsReceived = 1; 
}


void main()
{
 char ByteCount = 0;

 if(ByteIsReceived)
 {
  if(ByteCount++ == 0) CMD = ReceivedByte;
  if(ByteCount == 1) CHK = ReceivedByte;
 }

// your other code here
}

It will pick up the bytes as you want but they could be swapped over.

Brian.
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…