View previous topic :: View next topic |
Author |
Message |
hemnath
Joined: 03 Oct 2012 Posts: 242 Location: chennai
|
problem with rs232 |
Posted: Tue Dec 31, 2013 1:42 am |
|
|
I'm using PIC18F2520, oscillator: Internal type 4Mhz, compiler: 4.114,
controlling LED's using RF transmitter and receiver.
Tx code:
Code: | #include "18F2520.h"
#fuses INTRC_IO
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6)
#define Button1 PIN_A1
#define Button2 PIN_A2
void main()
{
while(1)
{
if(input(button1) == 0)
{
putc('a');
delay_ms(1000);
}
if(input(button1) == 1)
{
putc('b');
delay_ms(1000);
}
if(input(button2) == 0)
{
putc('c');
delay_ms(1000);
}
if(input(button2) == 1)
{
putc('d');
delay_ms(1000);
}
}
}
|
RX code:
Code: |
#include "18F2520.h"
#fuses INTRC_IO
#use delay(clock = 4000000)
#use rs232(baud=9600, rcv=PIN_C7)
#define LED1 PIN_A4
#define LED2 PIN_A5
unsigned char value;
void main()
{
while(1)
{
if(kbhit())
{
value = getc();
delay_ms(1000);
}
if(value == 'a')
{
output_high(LED1);
}
if(value == 'b')
{
output_low(LED1);
}
if(value == 'c')
{
output_high(LED2);
}
if(value == 'd')
{
output_low(LED2);
}
}
} |
When i press the button, LED is not glowing.. Please help |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19513
|
|
Posted: Tue Dec 31, 2013 2:22 am |
|
|
Because you are only specifying one pin of the UART port, the compiler will say 'he is not using the UART', and switch to _software_ RS232 mode.
Now, software mode has no buffering. The test 'kbhit' with a software UART, just tests for the RX line being low _at the instant it is called_. Any characters arriving at any other time, are lost.
So first thing, specify both UART pins in the #USE RS232 statements, so the hardware UART is used.
Then have you actually verified that your buttons are seen?. How are they wired?. Pull up resistors on A1, and A2?. Have you also verified your LED's on the Rx chip work. Current limiting resistors?. |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Tue Dec 31, 2013 10:25 am |
|
|
Besides the #USE RS232 line I suggest that you break down your large project into smaller parts that you can test and proof to be working as expected.
For example, do you know for sure the LEDs on the receiver are working?
I think you will fined LED2 on the receiver not working.
PIN_A5 on the receiver is an analog input by default. See table 10-1 in the data sheet. You will have to disable the analog port and the comparator 2 output.
So there are more small tests you can do. For example test that both boards are running and at the expected speed by adding a LED that is blinking at 1 second intervals. A very simple test but one that fails more often than you would think.
Test the transmitter and receiver board apart. When you make a loop back test, i.e. the transmitted data is connected directly to the receiver on the same board, are you then receiving the same data as is being transmitted?
What happens when you connect the transmitter board and the receiver board directly, so without the RF circuits?
Perhaps you can think of more tests yourself. Start with something as simple as possible. When that works expand in small steps and proof every step is working. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9226 Location: Greensville,Ontario
|
|
Posted: Tue Dec 31, 2013 12:44 pm |
|
|
comment: as CK says, breakdown the project into smaller, provable parts. You don't tell us what RF modules you're using but the cheap 433MHz ones can be 'fun' to get up and running properly.
At the very least remove the RF modules and go 'direct' PIC to PIC to eliminate the RF units.You'll be able to quickly debug.
hth
jay |
|
|
hemnath
Joined: 03 Oct 2012 Posts: 242 Location: chennai
|
|
Posted: Tue Dec 31, 2013 10:10 pm |
|
|
Thanks guys. I have debugged the code and it is working now.
Now I'm in my home. As i don't have 18F2520 now, and changed the controller to PIC18F4550 for testing, which i have it.
made a simple LED blinking test for 18F4550 controller.
On PIN_RA4, LED is not glowing. Why is it?
I have configured the pin as digital output. But still not working. Any help? |
|
|
ezflyr
Joined: 25 Oct 2010 Posts: 1019 Location: Tewksbury, MA
|
|
Posted: Tue Dec 31, 2013 10:50 pm |
|
|
Hi,
Have you consulted the datasheet? If you had, under the port I/O section you'd note that RA4 is an open-drain pin. That mean that this pin can only
Sink to GND, it cannot go 'hi', ie. source to Vdd.
Use another pin, or change your LED connection to be compatible.
John |
|
|
neochrome32
Joined: 09 Jun 2013 Posts: 153
|
|
Posted: Wed Jan 01, 2014 9:17 pm |
|
|
Hello,
i found this sometimes helps
ensure you have ..... setup_adc(ADC_OFF);
also its a bit strange, sometimes some pics dont like it when you do
setup_adc_ports (NO_ANALOGS);
setup_comparator(NC_NC_NC_NC);// This device COMP currently not supported by the PICWizard
its wierd i know, but try commenting these out and turn your IO's to FAST
#use fastio(a)
and double check set_tris_a(0x00);
its a bit legacy but most of my code has this and ive never yet figured out why it works like this, "you just keep going, no matter how crazy it seems"
good luck |
|
|
|