|
|
View previous topic :: View next topic |
Author |
Message |
delifisek Guest
|
rs232 problem |
Posted: Mon Aug 21, 2006 9:55 am |
|
|
Hi;
I m trying to get 8 consecutive char s from pc and send it to antenna. After that get a char from antenna and send it back to pc. Program can carry out this mission once however i want it to continue for unlimited time. I m not sure what the problem is caused by actually. My opinion is that buffer of pic is full and it doesn't accept any more character! However even if I use "reset_cpu", it doesn t sort it out! Code is below!
Pic 16f877
used components; max232, easy radio transceiver and a wire as antenna
Thanks in advance!
Code: | #include <16F877.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=4000000)
#use rs232(baud=19200, xmit=PIN_C6, rcv=PIN_C7)
#use fast_io(D)
#define KEYHIT_DELAY 50000
void put_to_a( char c ) {
putc(c);
}
char get_from_a() {
return getc(); }
#USE RS232(BAUD=19200, XMIT=PIN_B2, RCV=PIN_B3)
void put_to_b( char b ) {
putc(b);
}
char b_timed_getc() { //Wait for the response from second board for a limited time!!
long timeout;
char retval;
timeout=0;
while(!kbhit() && (++timeout< KEYHIT_DELAY*100))
delay_us(10);
if(kbhit())
retval = getc();
else
retval = 0;
return(retval);
}
main() {
int i=0;
char c1;
SET_TRIS_D( 0x01 );
while(TRUE) {
for(i=0;i<9;i++) {
output_low(PIN_D5);
while (!input(PIN_D0) );
put_to_b(get_from_a());}
while (!input(PIN_D0));
c1=b_timed_getc();
if(c1==0) {
reset_cpu();
// printf("please swipe your card again");
}
put_to_a(c1);
i=0;
reset_cpu();
}
} |
|
|
|
grasspuddle
Joined: 15 Jun 2006 Posts: 66
|
|
Posted: Mon Aug 21, 2006 10:20 am |
|
|
one main thing is that you shouldn't reset the cpu, the while loop will repeat the code for you
definately remove that |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Aug 21, 2006 1:10 pm |
|
|
Quote: | My opinion is that buffer of pic is full and it doesn't accept any more character! |
The standard way to fix the problem that you describe is to add the
ERRORS parameter as shown in bold below:
Quote: | #use rs232(baud=19200, xmit=PIN_C6, rcv=PIN_C7, ERRORS) |
This will clear any overrun errors that occur in the hardware UART.
(This only works with a hardware UART. It has no effect on a soft UART).
Also, this "fix" will only prevent the UART from locking up. You will still
lose any characters that are overrun, because you didn't check the UART
often enough in your code.
I didn't look at your whole program. I just looked at this one problem. |
|
|
bwgames
Joined: 21 Jul 2005 Posts: 36
|
|
Posted: Tue Aug 22, 2006 5:52 am |
|
|
I am using very similar components (easy radio and PIC18F452).
Something like below should do the job much more simply?
Code: |
while(1)
{
char c[8];
char d;
for(i=0;i<8;i++)
{
c[i] = fgetch(PC); //Getting PC chars
}
fprintf(Wireless,"%s",c); //send to wireless easyradio
d = fgetch(Wireless);
fprintf(PC,"%c",d);
} //end of while1
|
|
|
|
delifisek Guest
|
|
Posted: Tue Aug 22, 2006 8:11 am |
|
|
Thanks to u all guys!
Especially to u,bwgames;
I combined all your suggestions and it is working now!
I want to suggest smt which is not relevant to my problem!
I think every one should post the fixed program so that we can see the problem and see the fixed situation! My fixed code is below!
Code: | #include <16F877.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=4000000)
#use rs232(baud=19200, xmit=PIN_C6, rcv=PIN_C7)
#use fast_io(D)
#define KEYHIT_DELAY 50000
void put_to_a( char c ) {
putc(c);
}
char get_from_a() {
return getc(); }
#USE RS232(BAUD=19200, XMIT=PIN_B2, RCV=PIN_B3)
void put_to_b( char b ) {
putc(b);
}
char b_timed_getc() { //Wait for the response from second board for a limited time!!
long timeout;
char retval;
timeout=0;
while(!kbhit() && (++timeout< KEYHIT_DELAY*100))
delay_us(10);
if(kbhit())
retval = getc();
else
retval = 0;
return(retval);
}
main() {
int i=0;
SET_TRIS_D( 0x01 );
while (!input(PIN_D0) );
output_low(PIN_D5);
while(TRUE) {
char c[8];
char c1;
for(i=0;i<8;i++) {
c[i]=get_from_a();}
i=0;
for(i=0;i<8;i++) {
put_to_b(c[i]);}
c1=b_timed_getc();
if(c1==0) {
reset_cpu();
// printf("please swipe your card again");
}
put_to_a(c1);
i=0;
}
} |
|
|
|
delifisek Guest
|
|
Posted: Tue Aug 22, 2006 9:43 am |
|
|
Hi again;
This time it is expected my program to carry out the same mission by using interrupt routines! I wrote a code by the help of Pic example programs! However it is not working and I think the cause of problem might be interrupt routines!
1.)I wonder if we can use interrupts "int_ext" or "int_rda" or else "int_tbe" with B2 and B3 port or not? If not how can we empty buffer via B2 and B3 using interrupt?
2.)If buffer gets full and char continue to be sent, will the program go to "#int_rda" routine? If so buffer value will change and it might cause a problem.
Regards! Delifisek!
Code is below!
#include <16F877.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=4000000)
#use rs232(baud=19200, xmit=PIN_C6, rcv=PIN_C7)
#define KEYHIT_DELAY 50000 // in milliseconds
#use fast_io(D)
#define BUFFER_SIZE 8
BYTE buffer[BUFFER_SIZE];
BYTE next_in = 0;
BYTE next_out = 0;
int k=0;
#int_rda //If there is char on serial, detect it!!
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;
enable_interrupts(int_tbe);} // Buffer full !!
}
void put_to_a( char c ) { //send the response of second board!!
putc(c);
}
#USE RS232(BAUD=19200, XMIT=PIN_B2,RCV=PIN_B3)
#int_tbe
void serial_isrb() {
putc(buffer[next_out]);
next_out=(next_out+1) % BUFFER_SIZE;
if(next_in==next_out){
k=1;
disable_interrupts(int_tbe);}
}
char b_timed_getc() { //Wait for the response from second board for a limited time!!
long timeout;
char retval;
timeout=0;
while(!kbhit() && (++timeout< KEYHIT_DELAY*100))
delay_us(10);
if(kbhit())
retval = getc();
else
retval = 0;
return(retval);
}
void main() {
char c1;
enable_interrupts(global);
SET_TRIS_D( 0x01 );
while (!input(PIN_D0) );
output_low(PIN_D5);
{
enable_interrupts(int_rda);
while(k==1);
k=0;
next_in=0;
next_out=0;
c1=b_timed_getc();
if(c1==0) {
reset_cpu();
// printf("please swipe your card again");
}
put_to_a(c1);
} while (TRUE);
}
[/code] |
|
|
|
|
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
|