View previous topic :: View next topic |
Author |
Message |
mshearer
Joined: 12 Jan 2009 Posts: 33
|
reading RCREG 18f4520 |
Posted: Fri Jan 23, 2009 5:00 am |
|
|
In the 18f4520 datasheet, it says how the RCIF flag can be used to alert incoming data, and this flag is cleared everytime the RCREG is read.
my code
Code: | for (i=0;i<60;i++)
{
if(bit_RCIF == TRUE)
string[i] = reg_RCREG;
} |
For some reason i can use this technique elsewhere, but when done here the RCIF bit never gets cleared.
matt |
|
|
Ttelmah Guest
|
|
Posted: Fri Jan 23, 2009 6:18 am |
|
|
Show your definitions of reg_RCREG, and bit_RCIF.
Also consider adding error checking. This code will fail if the OERR bit has become set.
Best Wishes |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Fri Jan 23, 2009 8:19 am |
|
|
Your code example is too short to give a definitive answer, but what makes me suspicious is: why are you looping 60 times?
It will take some time for a character to be received over a serial line. Even 115kbaud is slow in computer terms. If you are sending a stream of data to the PIC it will read about 2 or 3 characters and then the 60-times loop will be finished. Checking the RCIF flag again, for example, a millisecond later will show the RCIF flag to be set again by the next character being received.
Not exactly sure what you want to achieve, but you'll have to redesign this piece of code. One of the best ways to receive data is to do it in the background and periodically have your main routine check data to be received. A good example of how to implement this is the ex_sisr.c example program supplied with your compiler.
Note that instead of: Code: | if(bit_RCIF == TRUE)
string[i] = reg_RCREG; | you can write: Code: | if (kbhit())
string[i] = getc(); | I favour the second version as it is easier to read (more 'self explaining') and easier to port to another processor because you don't have to worry about register addresses. |
|
|
mshearer
Joined: 12 Jan 2009 Posts: 33
|
|
Posted: Tue Jan 27, 2009 3:05 am |
|
|
using kbhit is a great idea thanks
What i am trying to do is take receive 60bytes of Hex and place them all into an array.
This seems to work
Code: | while (k<60)
{
if (kbhit())
{
string[k] = getc();
k=k+1;
}
} |
except my terminal program hangs up after about the 6th byte if i enter
0102030405060708091011121314151617-----60
I'll have a proper interface set up soon though.
thanks for the help,
matt |
|
|
|