View previous topic :: View next topic |
Author |
Message |
eagerstudent Guest
|
RS232 Interrupt |
Posted: Tue Dec 07, 2004 4:24 pm |
|
|
Hi,
I am trying to use a serial port to read. I am using pins 25 and 26 of PIC16f874 since they are usart ports. Here's a test code
#include <16F874.H>
#include <stdlib.h>
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
#define BUFFER_SIZE 20
#BIT RCIF = 0x0C.5 //address of the rcif bit
byte buffer[BUFFER_SIZE];
byte next_in = 0;
byte tbits =0;
int dataok=0;
#int_rda
void serial_isr() {
int t;
dataok=1;
tbits=tbits+1; //counts the total number of bytes read.
buffer[next_in]=getc();
t=next_in;
next_in=(next_in+1) % BUFFER_SIZE; //% gives remainder
if(next_in==0)
next_in=0; } // Buffer full !!
int main()
{
enable_interrupts(global);
enable_interrupts(int_rda);
if(dataok==1)
{
byte inar=0;
while(tbits>0)
{
putc(buffer[inar]);
tbits = tbits-1;
}
}
}
This is all the code that I would need right? I am using hyperterminal to test and it does not read any data back. Any help is greatly appreciated. By the way, if you didn't know, I am new to PIC programming.
Thank you |
|
|
Guest
|
|
Posted: Tue Dec 07, 2004 4:37 pm |
|
|
you should first enable int_rda and then enable globle inturrupt. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Dec 07, 2004 4:47 pm |
|
|
Your program is too complicated for your first program.
Do something like this:
Code: | #include <16F874.h>
#use delay(clock=20000000)
#fuses HS, NOWDT,PUT, NOPROTECT, BROWNOUT, NOLVP
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
main()
{
char c;
while(1)
{
c = getc(); // Wait for a character from the PC
putc(c); // Then send it back to the PC
}
} |
Note: Edited to change the fuse to HS, per ckielstra's post below.
Last edited by PCM programmer on Tue Dec 07, 2004 5:48 pm; edited 1 time in total |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Tue Dec 07, 2004 4:56 pm |
|
|
Quote: | you should first enable int_rda and then enable globle inturrupt. | This is not required. But it is more logical.
You are trying to implement a circular buffer, which is a good method for buffering the incomming data. Problem is that you are using the same pointer for writing to and for reading from the buffer. You'll have to add an extra pointer for reading from the buffer. You know the buffer is empty when both pointers are equal. |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Tue Dec 07, 2004 4:59 pm |
|
|
Code: | #use delay(clock=20000000)
#fuses XT, NOWDT,PUT, NOPROTECT, BROWNOUT, NOLVP
| A slip of PCM's finger: At 20MHz you need the HS fuse instead of the XT fuse |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Dec 07, 2004 5:19 pm |
|
|
Actually, I modified my normal 16F877 test program to make it look like
his program and I missed that part. Thanks for catching it. |
|
|
Guest
|
|
Posted: Tue Dec 07, 2004 6:39 pm |
|
|
ckielstra wrote: | Quote: | you should first enable int_rda and then enable globle inturrupt. | This is not required. But it is more logical.
You are trying to implement a circular buffer, which is a good method for buffering the incomming data. Problem is that you are using the same pointer for writing to and for reading from the buffer. You'll have to add an extra pointer for reading from the buffer. You know the buffer is empty when both pointers are equal. |
I am not sure what you mean by I am using the same pointer to read and write to buffer. Thank You |
|
|
eagerstudent Guest
|
|
Posted: Tue Dec 07, 2004 7:47 pm |
|
|
I tried that simple program and it worked. It does not work if I have buffer. It does not even go into the interrupt. To check if it does go into interrupt, I had a led connected to pin C0 and setting it to high. If I pressed a key on keyboard, the led should stay on as i am not setting it to low anywhere else. Thanks in advance |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Wed Dec 08, 2004 2:56 am |
|
|
Quote: | I am not sure what you mean by I am using the same pointer to read and write to buffer. Thank You | Reading the data is now based on the counter 'tbits', which is used in both the ISR and in your main(), this is an error waiting to happen; the instruction for modifying tbits in main() can be interrupted by a received character and the resulting value for tbits is undefined. The common solution to this is not to use the same variables in your isr and main but use two seperate variables. The first variable is a pointer to your buffer for writing the next character in the buffer, this one you already have (next_in). In your main you introduce a similar new variable for reading the next character from the buffer, e.g. next_out.
Also your value for 'inar' is never modified, so you'll output the same character every time. |
|
|
rwyoung
Joined: 12 Nov 2003 Posts: 563 Location: Lawrence, KS USA
|
|
Posted: Wed Dec 08, 2004 8:34 am |
|
|
If you do want to use buffered I/O for the UART I suggest you study the example programs EX_SISR.C and EX_STISR.C that came with the compiler (or at least should have). The will be in the EXAMPLES directory.
If you are not familiar with C code, these look a little complicated because of the #define and #include stuff at the top but that is just there to support multiple versions of the CCS compilers. _________________ Rob Young
The Screw-Up Fairy may just visit you but he has crashed on my couch for the last month! |
|
|
|