View previous topic :: View next topic |
Author |
Message |
thiam
Joined: 06 Apr 2010 Posts: 13
|
problem with send and transmit data by using serial port |
Posted: Tue Apr 06, 2010 1:44 am |
|
|
HI, I'm a new user for CCS compiler. Can anyone can check the following code? Why the PIN_B6 can't be turned on although I have sent the char "1" from my computer to PIC?? Anyone can guide me about it?? Thanks
Code: |
#include <16F877A.h>
#include <string.h>
#use delay(clock=20000000)
#define REDLED PIN_B4
#define GREENLED PIN_B6
#define YELLOWLED PIN_B3
#use rs232 (baud = 9200,xmit=PIN_C6, rcv=PIN_C7)
#fuses HS,NOWDT,NOPROTECT,NOLVP
char ch;
void main()
{
ch= getc();
if (ch == "1")
{
set_tris_b(0b00);
output_High(GREENLED);
}
}
|
|
|
|
hayee
Joined: 05 Sep 2007 Posts: 252
|
|
Posted: Tue Apr 06, 2010 2:03 am |
|
|
Try this.
Code: |
#include <16F877A.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
#use rs232 (baud = 9200,xmit=PIN_C6, rcv=PIN_C7)
#include <string.h>
#define REDLED PIN_B4
#define GREENLED PIN_B6
#define YELLOWLED PIN_B3
char ch;
void main()
{
set_tris_b(0x00);
while(1)
{
ch= getc();
if (ch == "1")
{
output_toggle(GREENLED);
}
}
}
|
|
|
|
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
|
Posted: Tue Apr 06, 2010 2:09 am |
|
|
Your first problem is that in C to represent a single char you use single quotes '
So you should do:-
if (ch == '1') |
|
|
thiam
Joined: 06 Apr 2010 Posts: 13
|
|
Posted: Tue Apr 06, 2010 8:56 am |
|
|
i have try the code but still have the problem... ... what wrong with the code?? anyone pls help me?? |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19552
|
|
Posted: Tue Apr 06, 2010 8:59 am |
|
|
Start with the code:
Have you corrected the test, as Wayne pointed out. This is vital.
Then, are you sure about your baud rate?. 9200bps, is 'non standard'. 9600bps, is the nearest standard setting.
Then are you sure about your hardware. RS232 transceiver chip?. Correctly wired?. Have you tested that the processor is actually running, and running at the right speed (toggle an LED at one second intervals - is it actually working at the right interval?). Can you send a 'hello world' message to the PC over the serial?.
Best Wishes |
|
|
thiam
Joined: 06 Apr 2010 Posts: 13
|
|
Posted: Tue Apr 06, 2010 10:13 am |
|
|
Thanks for you all. I have corrected the code and changed the baud rate to 9600. About the hardware, I use UC00A which is USB-UART converter from Cytron. I have tried the code again but still have the same problem. Ttelmah, can you give me a sample code?? Thank you very much. |
|
|
bkamen
Joined: 07 Jan 2004 Posts: 1615 Location: Central Illinois, USA
|
|
Posted: Tue Apr 06, 2010 10:32 am |
|
|
Do you have an RS232 interface transceiver on the PIC though?
In order to hook a PIC to an RS232 port, say on a PC, you must meet the voltage requirements needed by RS232 standards.
This is typically accomplished with an interface IC like the MAX232 or in the needs of only TX/RX, a MAX221.
If you are connecting your PIC directly to an RS232 port (via USB or backpanel connector), you are most likely injecting >+5VDC and <0VDC to the pins on your RX line, while your TX line is seen as a marginal/erroneous transmitter to the PC.
-Ben _________________ Dazed and confused? I don't think so. Just "plain lost" will do. :D |
|
|
thiam
Joined: 06 Apr 2010 Posts: 13
|
|
Posted: Tue Apr 06, 2010 11:32 am |
|
|
thanks guys... finally i have done.. the problem is the baud rate and the hardware... thanks |
|
|
|