|
|
View previous topic :: View next topic |
Author |
Message |
Kowal Guest
|
Problem with recieving code |
Posted: Mon Feb 06, 2006 12:04 pm |
|
|
Hi, I know that there are codes for sending and recieving serial data on this forum. I have seen them and tried to use. unfortunatelly I can't recieve data. Transmiter works fine but recieving side not.
Here is my code, what I have done wrong ?
Code: |
#include <16F84A.h>
#use delay(clock=4000000)
#fuses NOWDT,XT, NOPUT, NOPROTECT
#use rs232(baud=1200,parity=N,xmit=PIN_B1,rcv=PIN_B0,bits=8)
#include "C:\Documents and Settings\pk\My Documents\test8.h"
#int_EXT
byte data1,data2;
void SEND_DATA(BYTE txbyte)
{
int i,j,b,me;
b = txbyte;
for (i=0; i<2; i++) {
me = 0; // manchester encoded txbyte
for (j=0 ; j<4; j++) {
me >>=2;
if (bit_test(b,0) )
me |= 0b01000000; // 1->0
else
me |= 0b10000000; // 0->1
b >>=1;
}
putc(me);
}
}
BYTE DECODE_DATA(BYTE encoded)
{
BYTE i,dec,enc,pattern;
enc = encoded;
if (enc == 0xf0) // start/end condition encountered
return 0xf0;
dec = 0;
for (i=0; i<4; i++) {
dec >>=1;
pattern = enc & 0b11;
if (pattern == 0b01) // 1
bit_set(dec,3);
else if (pattern == 0b10)
bit_clear(dec,3); // 0
else
return 0xff; // illegal code
enc >>=2;
}
return dec;
}
EXT_isr()
{
disable_interrupts(INT_EXT);
data1 = getc();
data2 = getc();
data1 = (data1 << 4) + data2;
data1 = DECODE_DATA(data1);
putc(data1);
if (data1 == 0b01010101){output_high(PIN_A2);delay_ms(2000);}
enable_interrupts(INT_EXT);
}
void main()
{
int current,butt1;
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
enable_interrupts(INT_EXT);
enable_interrupts(GLOBAL);
set_tris_a(0b00000011);
butt1=0;
while(1)
{
current=input_a();
if (bit_test(current,0)) {SEND_DATA(0b01010101);butt1=0;delay_ms(200);}
}
}
|
Thanks for any help |
|
|
rnielsen
Joined: 23 Sep 2003 Posts: 852 Location: Utah
|
|
Posted: Mon Feb 06, 2006 3:01 pm |
|
|
First, the 16F84 does not have a hardware serial port. It also appears that you are relying on the external interrupt to detect the negative edge of the serial transmission. Second, you are attempting to disable the ext. interrupt while you are already in it. Disabling interrupts while inside one is a definate No-No. The way you are trying to receive data, in my opinion, will not work. I stronly suggest you try a PIC that has a hardware RS232 and utilize the #RDA_isr interrupt to receive your data. Otherwise, you will need to do some serious polling of the port to get your data in. And trying to use the Ext_isr just won't give you what you are trying to do.
Ronald |
|
|
kender
Joined: 09 Aug 2004 Posts: 768 Location: Silicon Valley
|
|
Posted: Mon Feb 06, 2006 5:01 pm |
|
|
rnielsen wrote: | Second, you are attempting to disable the ext. interrupt while you are already in it. Disabling interrupts while inside one is a definate No-No. |
Ronald, I don't see why disabling interrupt X inside inerrupt X itself is a bad idea. Since PICs don't have interrupt nesting and queing, this is the way of preventing re-entrance of the inerrupt. In a sence PIC hardware itself disables the interrupt X while the ISR for it is executing by virtue of priorities, but disabling an interrupt within it's own ISR explicitly shouldn't hurt as long as it's re-enbled at the end of the ISR.
Last edited by kender on Wed Feb 08, 2006 10:36 pm; edited 1 time in total |
|
|
Humberto
Joined: 08 Sep 2003 Posts: 1215 Location: Buenos Aires, La Reina del Plata
|
|
Posted: Mon Feb 06, 2006 5:59 pm |
|
|
Hi Kowal,
Before start looking for code errors, I guess you are trying to comunicate using an RF link
- that you didn't mention it - am I right ?
If so, the very first aproach you will need to do is to comunicate both ends in a reliable
way using regular wiring, ie: the Tx output pin wired directly to the Rx receiver pin.
Pls tell us how are linked the Tx and Rx in your tests, a minimal hardware description.
Humberto |
|
|
|
|
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
|