|
|
View previous topic :: View next topic |
Author |
Message |
simons
Joined: 15 Jan 2009 Posts: 18
|
Having troubles with 16F876A and RS232 |
Posted: Thu Jan 15, 2009 4:50 am |
|
|
Hello!! I'm new to this forum..
I'm having some troubles handling serial communication between a 16f876A with max232 and a pc
These are the fuses and other initialization stuff..
Code: |
#include <16F876A.h>
#device adc=8
#use delay(clock=20000000)
#fuses NOWDT,HS, NOPUT, NOPROTECT, NODEBUG, NOBROWNOUT, NOLVP, NOCPD, NOWRT
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)
|
I have this interrupt routine:
Code: | #int_RDA
RDA_isr()
{
ibuff_Rx = getc(); //catch the incoming char
interrupt=1; //set a variable to 1
output_low(PIN_C4); //light on the RX led on the board
enable_interrupts(INT_RDA); //make sure the RDA int is active for new char
} |
Then I have this in the main statement:
Code: |
char check='r';
if(interrupt==1)
{
if(ibuff_Rx==check)
{
printf("\cCOMMAND RECEIVED: %C",ibuff_Rx); //send back the command
output_high(PIN_B4); //light on the check led
}
else
{
printf("\cWRONG COMMAND: %C",ibuff_Rx);
output_low(PIN_B4); //light off the check led
}
interrupt=0;
}
|
I'm sending the 'r' character with hyperterminal and this is correctly recognised because the check led lights on.
When the MCU sends back the character to pc, in the hyperterminal i see garbage characters, but the string "COMMAND RECEIVED" displays correctly. It's only the %C part that doesn't display correctly.
I don't know how to fix this!!!
I tried puts instead of printf .. same result, garbage characters...
This this is driving me nuts.. help me please |
|
|
ECACE
Joined: 24 Jul 2006 Posts: 94
|
|
Posted: Thu Jan 15, 2009 1:21 pm |
|
|
What type is your ibuff_Rx? I don't see it defined.
What happens if you change your %c to %s?
What are some examples of the junk data.. if you put in a Y, what does it display?
Lastly, what version of the compiler are you using? _________________ A HW Engineer 'trying' to do SW !!! Run!!! |
|
|
Guest
|
|
Posted: Thu Jan 15, 2009 2:13 pm |
|
|
Hi!
ibuff_Rx is defined as char.
if I change %c to %s the garbage still remains but it's longer..
some examples of junk data are three vertical lines or a +/- or or question mark and stuff like this...
the compiler version is 3.181. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Jan 15, 2009 3:23 pm |
|
|
Make a very simple test program and see if it works. Type characters
in from Hyperterminal and see if they are sent back properly to the PC.
Example:
Code: | #include <16F876A.h>
#fuses XT, NOWDT, BROWNOUT, PUT, NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
#int_rda
void rda_isr(void)
{
char c;
c = getc(); // Get character from PC
putc(c); // Send it back to the PC
}
//========================
void main()
{
enable_interrupts(INT_RDA);
enable_interrupts(GLOBAL);
while(1);
} |
|
|
|
simons
Joined: 15 Jan 2009 Posts: 18
|
|
Posted: Thu Jan 15, 2009 3:33 pm |
|
|
the example works great! every character is sent back to the pc..
but i can't figure out what's going wrong in my code |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Jan 15, 2009 4:05 pm |
|
|
Your program is not complete. For example, you don't show the
declaration of iBuff_Rx.
Post the complete program (but about the same length) and post it in
one Code box. Don't split it up over 3 code boxes. Compile it first.
Make it has no errors. Then post it.
Also, add the ERRORS parameter to your #use rs232() statement. |
|
|
simons
Joined: 15 Jan 2009 Posts: 18
|
|
Posted: Fri Jan 16, 2009 9:45 am |
|
|
Ok here's the complete code, I made a little change but basically it's the same code.
PIN_B4 is check led
PIN_C3 is TX led
PIN_C4 is RX led
all leds are active low.
Code: |
#include <16F876A.h>
#device adc=8
#use delay(clock=20000000)
#fuses NOWDT,HS, NOPUT, NOPROTECT, NODEBUG, NOBROWNOUT, NOLVP, NOCPD, NOWRT
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8, ERRORS)
#include "lcd_new.h" //my own lcd driver
BOOLEAN interrupt=0;
char ibuff_Rx;
#int_RDA
RDA_isr()
{
ibuff_Rx = getc();
interrupt=1;
output_low(PIN_C4); //rx led on
}
void main()
{
int i;
char check='r';
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_OFF);
setup_spi(FALSE);
setup_counters(RTCC_INTERNAL,RTCC_DIV_1);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
set_tris_a(0b00000000);
set_tris_b(0b00000000);
set_tris_c(0b00000000);
lcd_init();
delay_ms(100);
lcd_init();
delay_ms(100);
printf(send_char,"\c-WELCOME-");
delay_ms(1000);
printf(send_char,"\c !Work In Progress!\nTest Board by Simons");
delay_ms(1000);
clear_display();
output_high(PIN_C3);
output_high(PIN_C4);
enable_interrupts(INT_RDA);
enable_interrupts(global);
printf("-WELCOME-\n\r");
while(1)
{
output_high(PIN_C4); //all leds off
output_high(PIN_C3);
if(interrupt==1)
{
if(ibuff_Rx=='r')
{
output_low(PIN_B4);
output_low(PIN_C3);
//31 is a test value
printf(send_char,"\cRECEIVED: %C \nTEMP: %u",ibuff_Rx,31);
printf("RECEIVED: %C \rTEMP: %u\r",ibuff_Rx,31);
ibuff_Rx=' ';
interrupt=0;
}
else
{
output_high(PIN_B4);
output_low(PIN_C3);
printf(send_char,"\cRECEIVED: %C",ibuff_Rx);
ibuff_Rx=' ';
interrupt=0;
}//if ibuff scope
}//if interrupt scope
}//while scope
}//main scope
|
Ok now I notice a thing:
if I remove the printf function that sends out data in rs232, the character is recognised and printed correctly to the lcd.
When I put the printf back, seems like that when the function is called, the interrupt is fired again even if it's a transmission and not a reception.
Thanks for help! |
|
|
Ttelmah Guest
|
|
Posted: Fri Jan 16, 2009 10:51 am |
|
|
Are you sure you have a good ground connection between the systems?. What you describe is exactly what you will see, if the ground is not properly connected, and the system effectively gets grounded through the second data line.
Best Wishes |
|
|
simons
Joined: 15 Jan 2009 Posts: 18
|
|
Posted: Fri Jan 16, 2009 5:57 pm |
|
|
The board where the pic is installed is a demo board, I didn't build it... It has it's own DC Adapter and a rs232 port.
I checked and everything seems fine.. no floating points or non-properly grounded areas.. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Jan 16, 2009 6:16 pm |
|
|
Quote: | void main()
{
int i;
char check='r';
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_OFF);
setup_spi(FALSE);
setup_counters(RTCC_INTERNAL,RTCC_DIV_1);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
set_tris_a(0b00000000);
set_tris_b(0b00000000);
set_tris_c(0b00000000); |
Get rid of the line in bold. You're setting the Rx pin to be an output.
My advice is to get rid of all the TRIS statements and let the compiler
set the TRIS. It does it automatically when you specify the #use rs232
library. It will do it correctly.
If you get rid of that line I believe it will fix your problem. |
|
|
simons
Joined: 15 Jan 2009 Posts: 18
|
|
Posted: Sat Jan 17, 2009 4:44 am |
|
|
OMG!! Now it works reeeeally well!!!
Thanks a lot man!!! This is very much appreciated!!! |
|
|
|
|
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
|