|
|
View previous topic :: View next topic |
Author |
Message |
camy990
Joined: 25 Mar 2008 Posts: 2
|
PIC16F876A can't interpret RX data |
Posted: Wed Mar 26, 2008 11:46 pm |
|
|
I'm unable to make pins C0 and C1 to respond to incoming commands over rs232, (device=PIC16F876A, compiler = PCW). Am I missing something in the following codes? Please help!!! Any info would be appreciated.
#include <16f876A.h>
#fuses HS,NOLVP,NOWDT,PUT
#USE delay(clock=20000000) #use rs232 (baud=4800, PARITY=E, xmit=PIN_C6, rcv=PIN_C7)
#include <input.c>
#include <string.h>
#define CAM_TYPE_TX PIN_B1
#define NEAR PIN_C0
#define FAR PIN_C1
#define CAM_TYPE_RX PIN_A5
#define CAM_TYPE 200
#define SWITCH_MODE PIN_C4
char rx[20],n[20],f[20];
void RZ5_MODE ()
{
strcpy(n,":WFCBBAA");
strcpy(f,":WFCBBA9");
rx[0] = getc();
putc(rx[0]);
if (strncmp(rx,n,8)==0)
{
output_low(FAR);
output_high(NEAR);
}
else if (strncmp(rx,f,8)==0)
{
output_high(NEAR);
output_low(FAR);
}
else
{
output_low(NEAR);
output_low(FAR);
}
}
void CAM_TYPE_TX_send ()
{
output_high(CAM_TYPE_TX);
delay_ms(50);
output_low(CAM_TYPE_TX);
}
void main()
{
int reading;
output_high(PIN_C4);
output_low(PIN_C0);
output_low(PIN_C1);
setup_adc_ports (RA0_ANALOG);
setup_adc (adc_clock_internal);
set_adc_channel (0);
CAM_TYPE_TX_send();
while(TRUE)
{
delay_ms(50);
read_adc (0);
reading = read_adc();
if ( reading < CAM_TYPE)
{
output_high(SWITCH_MODE);
RZ5_MODE();
}
else
{
output_low(SWITCH_MODE);
}}} |
|
|
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
|
Posted: Thu Mar 27, 2008 2:22 am |
|
|
You are missing quite a lot, using the code tags in your post would make it readable for a start but your main problem is that you are constantly overwriting index 0 of your rx array with the character from the rs232 port.
basically you will never create the required string.
A quick fix is to have a global index variable and increment it everytime you read in a char:-
Code: |
int8 index = 0;
void RZ5_MODE () {
strcpy(n,":WFCBBAA");
strcpy(f,":WFCBBA9");
rx[index] = getc();
putc(rx[index]);
if (++index >= 20) // increment index and check to see if we have reached the end of the buffer.
index = 0;
if (strncmp(rx,n,8)==0)
{
output_low(FAR);
output_high(NEAR);
index = 0; // reset index
} else if (strncmp(rx,f,8)==0) {
output_high(NEAR);
output_low(FAR);
index = 0; // reset index
}
else
|
A lot more needs to be done for it to work properly. Look at using rs232 interrupts and a propper routine for recieving a command. |
|
|
Douglas Kennedy
Joined: 07 Sep 2003 Posts: 755 Location: Florida
|
|
Posted: Thu Mar 27, 2008 2:24 am |
|
|
Learn to break your code down into functional pieces. Look at rx are you filling it or just writing to rx[0]. After you have rx the way you expect add in another functional piece like comparing it to an expected value. When that works move to the next functional piece. Test the pieces independently for example test the ADC before merging it with your RS232 code. Writing code and throwing at the wall to see what sticks rarely is going to work. Taking another's code and modifying it without first understanding it also rarely works. |
|
|
camy990
Joined: 25 Mar 2008 Posts: 2
|
|
Posted: Fri Mar 28, 2008 10:39 pm |
|
|
I got it to work!!! Thank you all for your excellent direction. |
|
|
|
|
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
|