CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

What am I doing wrong?

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
hobby_85



Joined: 17 Aug 2009
Posts: 50

View user's profile Send private message

What am I doing wrong?
PostPosted: Fri Aug 21, 2009 9:20 pm     Reply with quote

I bought a RF kit, transmitter and receiver, and the website of a similar product is below. Now, I'm trying to write code for it, just to make sure it is working.

http://www.sparkfun.com/commerce/product_info.php?products_id=8950

I'm using a PIC 16f688 and if the LED lights up to a correct pattern, I know the code is working. However, it is not.

Here is the TX code:
Code:

#include <16F688.h>
//---------------------------------------------------------
#define WireTX PIN_C4 //
#define WireRX PIN_C5
//---------------------------------------------------------
#fuses XT, NOWDT, NOPROTECT,NOBROWNOUT, PUT
#use delay (clock = 4000000)
#use rs232(baud=2400,xmit=WireTX , rcv=WireRX , STREAM=COM_A )

void main(){

for(;Wink{

if(input(PIN_C2)==0) //if button pressed
{
output_high(PIN_A1); //output high Led
delay_ms(20); //delay 20ms
fputc('T',COM_A); //send data
delay_ms(20);
delay_ms(1000); //delay some ms
output_low(PIN_A1); // output low led
}

output_high(PIN_A1); // if button not pressed then just on off led on pin D1

delay_ms(50);
output_low(PIN_A1);
delay_ms(50);

}
}

So if the button is pressed, the letter T is meant to be sent across, and the LED should come on, then off then on a longer time.

Here is the corresponding Receiver code:
Code:

#include <16F688.h>
#fuses XT, NOWDT, NOPROTECT,BROWNOUT, PUT
#use delay (clock = 4000000)
//------------------------------
#define WireTX PIN_C4
#define WireRX PIN_C5
//------------------------------
#use rs232(baud=2400, xmit=WireTX, rcv=WireRX, STREAM=COM_A)
unsigned int8 data;
int1 flag=0;

#int_rda
void rd_isr(void){
disable_interrupts(INT_RDA); // Disable Serial Recieve Interrupt
disable_interrupts(GLOBAL); // Disable Global Interrupts

data= fgetc(COM_A);
if(data=='T'){
flag=1;
}

enable_interrupts(GLOBAL);
enable_interrupts(INT_RDA);

}

void main(){

enable_interrupts(global);
enable_interrupts(int_rda);

for(;Wink{

if(flag==1){
output_high(PIN_A1);
delay_ms(1000);
output_low(PIN_A1);
flag=0;
}

output_high(PIN_A1);
delay_ms(50);
output_low(PIN_A1);
delay_ms(50);
}
}

So if the message is received, the LED should come on/off in 1 sec intervals. If not, it should just flash on and off.

I have an oscilloscope, so I can look at the signals being transmitted. Here are the problems:

1) The switch - When I checked the RF signal being sent when the button was pushed, I saw a step on the oscilloscope. The message would be sent forever and the led would light as coded, as long as the button was pushed.
So I know something was sent across. However, after a couple of seconds, even when the switch was off, the message was sent automatically.
I have connected a 220k resister from the output of the MC to the LED, and then to GND.

2) The receiver - The led turns on/off, as though it didn't receive anything. However, when I connected the osc to the receiver/RX of the MC, I can see a jump in the signal, matching the signal sent by the tx. So why isn't the led lighting as expected?

Any ideas? The code itself is from the CCS library, and it works apparently.

Thanks
hobby_85



Joined: 17 Aug 2009
Posts: 50

View user's profile Send private message

PostPosted: Fri Aug 21, 2009 10:06 pm     Reply with quote

update:

When I changed the RF receiver, the LED lights up as per normal...like its picking up a signal. However, I've bought about 5 RF transmitters and receivers, and somehow, the other receivers don't pick up the signal either. Just one is picking up the signal.

No clue what is happening here. Any ideas?

Please Help.

Thanks
hobby_85



Joined: 17 Aug 2009
Posts: 50

View user's profile Send private message

PostPosted: Fri Aug 21, 2009 10:20 pm     Reply with quote

update:

I attached a small (10cm) antenna to the transmitter and now the receiver picks it up every few seconds. Even the tha transmitter sends out the signal continuously. It can sometimes go for a minute before it picks it up.

It looks like the receiver is picking up garbage copntinusoutly, and by fluke, it picks up the 'T' and lights the LED. Still don't have a concrete working circuit/code.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Aug 21, 2009 10:38 pm     Reply with quote

Quote:
#int_rda
void rd_isr(void){
disable_interrupts(INT_RDA);
disable_interrupts(GLOBAL);

data= fgetc(COM_A);
if(data=='T'){
flag=1;
}

enable_interrupts(GLOBAL);
enable_interrupts(INT_RDA);

}

Delete the lines shown in bold. They are not needed. Nested interrupts
are not supported. The compiler and the PIC's hardware handle all of
this for you. Re-enabling Global interrupts inside an isr can cause
the program to crash, if an another interrupt occurs while you're still
inside the isr (because nested interrupts are not supported).

Quote:
for(;Wink{

What is this ? Was part of the line cut off ? (in both Rx and Tx code)
hobby85
Guest







PostPosted: Fri Aug 21, 2009 11:26 pm     Reply with quote

hey pcm, thanks for replying. I'm on my phone so wont be able to write much. I will delete those lines like you said. The smiley is a mistake in copying and pasting. Its meant to be a for loop which goes forever. So for{;;} is what its meant to be. When I left home, I noticed that when I turned the switch on and off quickly, the receiver would pick up the signal and light the led properly. Almost all the time. But if the switch stayed on forever, meaning the signal is meant to be sent across in an infinite loop, then the receiver would pick up mostly noise but sometimes the signal too. When I mean sometimes, I mean once in like a minute. Do you think its because I'm not using any encoder or decoder?
hobby
Guest







PostPosted: Fri Aug 21, 2009 11:39 pm     Reply with quote

Also, sometimes a signal is being transmitted by the transmitter regardless of the switch is being pressed. Any ideas? Thanks all.
hobby_85



Joined: 17 Aug 2009
Posts: 50

View user's profile Send private message

PostPosted: Sun Aug 23, 2009 10:03 am     Reply with quote

hey, can anyone point out the different between the two codes:

Code:

int data[9];
int STX = 0x02;
int CRC = 85;  //85 chosen randomly for test purposes
int CR = 0x0d;
int counter = 0;

sprintf(data,"%x%x%x%xAAA%x%x",0,0,0,STX,CRC,CR);
               
while(counter<9){
    fputc(data[counter],COM_A);
    counter = counter+1;
  }

AND
Code:

fputc('0',COM_A);
fputc('0',COM_A);
fputc('0',COM_A);
fputc(STX,COM_A);
fputc('A',COM_A);
fputc('A',COM_A);
fputc('A',COM_A);
fputc(CRC,COM_A);
fputc(CR,COM_A);

I'm trying to get the while loop working so I don't waste space and have extra lines, but nothing is being transmitted when I do. If I you fputc(), then it does.

Thanks all.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Aug 23, 2009 10:20 am     Reply with quote

Debug it. Put in a printf statement just after your sprintf line to display
the contents of your 'data' buffer. Verify that it holds what you think it
should hold.

Then, consider the size of your 'data' buffer. You have a 9-byte buffer.
Of those, up to 8 can be used for chars, and one for the string terminator
byte of 0x00. How many bytes are represented by your format string in
the sprintf line ? I'll bet that it's a lot more than just 8 chars. To code
in C, you have to start being suspicious. Question your code. "What I
have I done that's possibly wrong here ?" Put in printf statements to
display intermediate values and prove that each line is working correctly.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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