View previous topic :: View next topic |
Author |
Message |
jpage
Joined: 23 Jun 2004 Posts: 24
|
Receiving RS232 Data from PC using PIC18F4220 |
Posted: Thu Jun 24, 2004 10:06 am |
|
|
I am trying to test a very simple program that will receive a character ('?') and once it is received it will blink a Data LED a couple times.
If it is not the correct character, it will blink the the Power LED.
It is not working. Everytime I send a '?' character it, blinks the Power LED, which indicates that it is not the correct char.
I am using the built-in USART in the PIC.
I would appreciate any help. Thanks
Here is the code:
Code: |
#include <18f4220.h>
#fuses HS,NOWDT,NOPROTECT,NOBROWNOUT
#use delay (clock=20000000)
#use rs232(baud = 9600, parity=n, bits=8, xmit=pin_c6, rcv=pin_c7)
//defining LEDs
#define POWER PIN_E2
#define DATA PIN_D7
void main()
{
//input character from VB or SIOW.exe from CCS//
char data_in;
//Power ON Indicator
output_high(POWER);
delay_ms(250);
output_low(POWER);
delay_ms(250);
output_high(POWER);
delay_ms(250);
output_low(POWER);
delay_ms(250);
//endless loop
while(1)
{
//receive character from PC
data_in = getc();
//Checks received character
if (data_in == '?')
{
//blink Data LED
output_high(DATA);
delay_ms(250);
output_low(DATA);
delay_ms(250);
output_high(DATA);
delay_ms(250);
output_low(DATA);
delay_ms(250);
}
//if incorrect character, blink Power LED
else{
output_high(POWER);
delay_ms(250);
output_low(POWER);
delay_ms(250);
output_high(POWER);
delay_ms(250);
output_low(POWER);
delay_ms(250);
}
}
}
|
|
|
|
Ttelmah Guest
|
|
Posted: Thu Jun 24, 2004 10:59 am |
|
|
There are a lot of obvious 'hardware' things that could cause this, but have to be checked.
Is the PC setup to use 9600bps?. On Hyperterm, you can sometimes get a situation that if you change the baud rate, it doesn't start using the 'new' rate, till the next time the package is used. How is the wiring done (what chip are you using to convert from RS232 levels to TTL?. Is your crystal 20MHz?. Is it possible that the oscillator has locked onto an 'overtone' (this sometimes happens if the crystal is overdriven, resulting in operation at an unexpected frequency...)?.
The behaviour is what you would expect if the baud rate was wrong at one end or the other (note that a lot of new PC's seem to have timing problems on their internal UART's - especially if the 'spread spectrum' option is enabled in the BIOS!...).
Best Wishes |
|
|
jpage
Joined: 23 Jun 2004 Posts: 24
|
Receiving RS232 Data from PC using PIC18F4220 |
Posted: Thu Jun 24, 2004 11:21 am |
|
|
Thanks for the reply.
I have my PC setup exactly the same as the PIC as far as communications (9600, N, 8, 1).
I think I can rule out any problems with PC sending the correct baud rate because I just recently sent ASCII data to a different device that I am working on and that device was able to capture the data OK and it also worked at 9600 baud.
I have confirmed the PIC Xtal is operating at 20Mhz.
The RS232 to TTL conversion is done using a MAX323E.
I'm not absolutely convinced that it is not a hardware problem.
I would like to capture the converted TTL signal going into the PIC and verify that it is the correct ASCII char.
Any other suggestions would be welcome. Thanks. |
|
|
Humberto
Joined: 08 Sep 2003 Posts: 1215 Location: Buenos Aires, La Reina del Plata
|
|
Posted: Thu Jun 24, 2004 11:54 am |
|
|
Hi jpage,
Just to know the char received, add a putc() function.
Code: |
//endless loop
while(1)
{
//receive character from PC
data_in = getc();
putc(data_in); // ++++++++++ add this line ++++++++++++
//Checks received character
if (data_in == '?')
{
//blink Data LED
output_high(DATA);
delay_ms(250);
output_low(DATA);
delay_ms(250);
output_high(DATA);
delay_ms(250);
output_low(DATA);
delay_ms(250);
}
|
If you can borrow a scope, that will be the best way to "see" what´s going on.
Regards,
Humberto
. |
|
|
jpage
Joined: 23 Jun 2004 Posts: 24
|
|
Posted: Thu Jun 24, 2004 12:12 pm |
|
|
Thanks, I'll add the line and try it out.
I should be able to obtain a digital scope by tomorrow and look at the RS232 and TTL lines.
I'll keep you posted.
Thanks again. |
|
|
Ttelmah Guest
|
Re: Receiving RS232 Data from PC using PIC18F4220 |
Posted: Thu Jun 24, 2004 2:39 pm |
|
|
jpage wrote: | Thanks for the reply.
I have my PC setup exactly the same as the PIC as far as communications (9600, N, 8, 1).
I think I can rule out any problems with PC sending the correct baud rate because I just recently sent ASCII data to a different device that I am working on and that device was able to capture the data OK and it also worked at 9600 baud.
I have confirmed the PIC Xtal is operating at 20Mhz.
The RS232 to TTL conversion is done using a MAX323E.
I'm not absolutely convinced that it is not a hardware problem.
I would like to capture the converted TTL signal going into the PIC and verify that it is the correct ASCII char.
Any other suggestions would be welcome. Thanks. |
Er. Hopefully you mean a MAX232, not a '323' (the latter is a solid state multiplexer).
Look forward to hearing how you get on with some 'trace' diagnostics using putc, and a scope.
Best Wishes |
|
|
jpage
Joined: 23 Jun 2004 Posts: 24
|
Re: Receiving RS232 Data from PC using PIC18F4220 |
Posted: Thu Jun 24, 2004 2:47 pm |
|
|
Uh oh..typo on my part.
I did mean MAX232.
Thanks. |
|
|
|