|
|
View previous topic :: View next topic |
Author |
Message |
ntrungtruc2003
Joined: 16 Feb 2011 Posts: 42
|
can not receive the true character from pc |
Posted: Fri Feb 18, 2011 11:27 am |
|
|
Dear Sir
I sent an ascii character(ex 'A') from PC to PIC16F877A and store the received value to char c. Both sent value and received value are equal.
(pc:A-----rs232------pic:A).
But when i store the received value to buffer[next_in]. The reveived value not equal to sent value.
(pc:A(41H)-----rs232------pic:1)
I use a lcd(16x2) to display the received value for test.
Can you teach me how to solve it. I also try to use %c or %d or %x in printf(lcd_putc,"%c", buffer[next_in])
Code: |
#include <16f877A.h>
#fuses HS, NOLVP, NOWDT, NOPROTECT
#use delay (clock=8000000) //Use built-in function: delay_ms() & delay_us()
#use rs232(baud=9600,parity=N, xmit=PIN_C6, rcv=PIN_C7, bits=9)
#include "LCD_TM.c" //use module function
#define BUFFER_SIZE 32
char buffer[BUFFER_SIZE];
int next_in = 1;
#int_rda
void receiver_isr(void)
{
buffer[next_in]=getch();
putc(buffer[next_in]);
next_in++;
}
void main(void)
{
lcd_init();
enable_interrupts(int_rda);
enable_interrupts(GLOBAL);
printf("hello");
while(true){
//lcd_init();
lcd_gotoxy(2,1);
printf(lcd_putc,"%x",buffer[next_in]); // here is problem.
lcd_gotoxy(1,2);
printf(lcd_putc,"");
delay_ms(20);
}
} |
|
|
|
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Fri Feb 18, 2011 11:45 am |
|
|
buffer[next_in] is not the last character received. |
|
|
ntrungtruc2003
Joined: 16 Feb 2011 Posts: 42
|
|
Posted: Fri Feb 18, 2011 12:14 pm |
|
|
Thanks for your reply.
Could you explain me?. The first received value is also different.
I just send one character and terminate. |
|
|
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Fri Feb 18, 2011 12:20 pm |
|
|
After next_in++ buffer[next_in] is an empty location. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19496
|
|
Posted: Fri Feb 18, 2011 3:26 pm |
|
|
First comment. In C, arrays start at '0', not 1.
Then think about it. One character received starting from '1' as you currently have:
Character received - > buffer[1]
next_in now equals 2 - This is the _next_ location, where a character is _going_ to be put. Trying to read 'buffer[2]', just gets garbage (depends what happens to be sitting in the memory, when the chip wakes up.....).
Look at ex_sisr, for an example of how to handle a 'circular buffer' like this. _Two_ addresses are needed. 'next_in', and 'next_out'. The numbers 'chase' one another, with next_out incrementing _after a character is read_, and next_in incrementing after a character is written.
Best Wishes |
|
|
ntrungtruc2003
Joined: 16 Feb 2011 Posts: 42
|
|
Posted: Sat Feb 19, 2011 4:09 am |
|
|
It's worked. He he Thanks your very much.
here is code
Code: | #include <16f877A.h>
#fuses HS, NOLVP, NOWDT, NOPROTECT
#use delay (clock=8000000) //Use built-in function: delay_ms() & delay_us()
#use rs232(baud=9600,parity=N, xmit=PIN_C6, rcv=PIN_C7, bits=9)
#include "LCD_TM.c" //use module function
#define BUFFER_SIZE 32
BYTE buffer[BUFFER_SIZE];
BYTE next_in = 0;
BYTE next_out = 0;
byte c;
#int_rda
void serial_isr() {
int t;
buffer[next_in]=getc();
t=next_in;
next_in=(next_in+1) % BUFFER_SIZE;
if(next_in==next_out)
next_in=t; // Buffer full !!
}
#define bkbhit (next_in!=next_out)
BYTE bgetc() {
BYTE c;
while(!bkbhit) ;
c=buffer[next_out];
next_out=(next_out+1) % BUFFER_SIZE;
return(c);
}
void main(void)
{
lcd_init();
enable_interrupts(int_rda);
enable_interrupts(GLOBAL);
printf("hello");
while(true){
//lcd_init();
lcd_gotoxy(2,1);
c = bgetc();
printf(lcd_putc,"%c",c); // here
lcd_gotoxy(1,2);
printf(lcd_putc,"hello");
delay_ms(20);
}
} |
|
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|