Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Need Help with RF remote coding

Status
Not open for further replies.

pophip897

Newbie level 2
Newbie level 2
Joined
Apr 25, 2013
Messages
2
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Visit site
Activity points
1,295
Need Help with RF remote coding -/\- pls

Hello!

I have some trouble with my transmitter & receiver code via UART.
I want to check start byte first then receive the data for my real function.
I'm using PIC16F628A for Both transmitter & receiver(4 MHz clock).
Transmitter Module : TWS-BS3 (433.92 MHz)
Receiver Module : RWS-374 (433.92 MHz)
(Coding with Mikroc)


Thanks

Thanapat.

Origin: 27/5/2013
Updated 1: 28/5/2013:
Change baud rate from 300.5 to 1202

Transmitter Code:
Code:
void main() {
TRISA = 0x03;
TRISB = 0xF9;
SPBRG = 51;
TXSTA.TXEN = 1;
RCSTA.SPEN = 1;
Delay_ms(500);                 // Wait for setting 
USART_Write (172) ;         // Send data for receiver to check [start byte]
Delay_ms(15);		    [B][COLOR="#FF0000"]// *** Not sure that I need to have this one ?[/COLOR][/B]
USART_Write(0);              // Send command [Clear port]
while (1)
{
int tx1 = 0;
int tx2 = 0;
int tx3 = 0;
int tx4 = 0;
int trans = 0;
while ((PORTB.F6==1)&&(PORTB.F7==0))  // Choose Function (my remote have 3 function)
{
if(PORTB.F0==1)
{
USART_Write (172) ;     // sent start byte
Delay_ms(15);		[B][COLOR="#FF0000"]***[/COLOR][/B]
USART_Write(65) ;        // send command
Delay_ms(200);
}
if(PORTB.F3==1)
{
USART_Write (172) ;     // sent start byte
Delay_ms(15);		[B][COLOR="#FF0000"]***[/COLOR][/B]
USART_Write(72) ;          // send command
Delay_ms(200);
}
if(PORTB.F4==1)
{
USART_Write (172) ;     // sent start byte
Delay_ms(15);		[B][COLOR="#FF0000"]***[/COLOR][/B]
USART_Write(80) ;         // send command
Delay_ms(200);
}
if(PORTB.F5==1)
{
USART_Write (172) ;     // sent start byte
Delay_ms(15);		[B][COLOR="#FF0000"]***[/COLOR][/B]
USART_Write(96) ;         // send command
Delay_ms(200);
}
if(PORTA.F0==1)
{
USART_Write (172) ;     // sent start byte
Delay_ms(15);		[B][COLOR="#FF0000"]***[/COLOR][/B]
USART_Write(0) ;          // send command [Clear port]
Delay_ms(200);
}
}
while ((PORTB.F6==0)&&(PORTB.F7==1))         // Choose Function
{
if(PORTB.F0==1)
{
USART_Write (172) ;      // sent start byte
Delay_ms(15);		[B][COLOR="#FF0000"]***[/COLOR][/B]
USART_Write(129) ;         // send command
Delay_ms(200);
}
if(PORTB.F3==1)
{
USART_Write (172) ;      // sent start byte
Delay_ms(15);		[B][COLOR="#FF0000"]***[/COLOR][/B]
USART_Write(136) ;        // send command
Delay_ms(200);
}
if(PORTB.F4==1)
{
USART_Write (172) ;      // sent start byte
Delay_ms(15);		[B][COLOR="#FF0000"]***[/COLOR][/B]
USART_Write(144) ;        // send command
Delay_ms(200);
}
if(PORTB.F5==1)
{
USART_Write (172) ;      // sent start byte
Delay_ms(15);		[B][COLOR="#FF0000"]***[/COLOR][/B]
USART_Write(160) ;        // send command
Delay_ms(200);
}
if(PORTA.F0==1)
{
USART_Write (172) ;       // sent start byte
Delay_ms(15);		[B][COLOR="#FF0000"]***[/COLOR][/B]
USART_Write(0) ;            // send command [Clear port]
Delay_ms(200);
}
}
while ((PORTB.F6==1)&&(PORTB.F7==1))           // Choose Function
{
if(PORTB.F0==1)
{
tx1 = 1;
Delay_ms(50);
}
if(PORTB.F3==1)
{
tx2 = 8;
Delay_ms(50);
}
if(PORTB.F4==1)
{
tx3 = 16;
Delay_ms(50);
}
if(PORTB.F5==1)
{
tx4 = 32;
Delay_ms(50);
}
if(PORTA.F1==1)
{
trans = tx1+tx2+tx3+tx4;
USART_Write (172) ;              // sent start byte
Delay_ms(15);			[B][COLOR="#FF0000"]***[/COLOR][/B]
USART_Write(trans);               // send command
Delay_ms(200);
}
if(PORTA.F0==1)
{
tx1 = 0;
tx2 = 0;
tx3 = 0;
tx4 = 0;
trans = 0;
USART_Write (172) ;      // sent start byte
Delay_ms(15);		[B][COLOR="#FF0000"]***[/COLOR][/B]
USART_Write(0);            // send command [Clear port]
Delay_ms(200);
}
}
}
}



Receiver Code:
Code:
void main()
{
int receive = 0;
TRISB = 0xC6;
SPBRG = 51;
RCSTA.SPEN = 1;
RCSTA.CREN = 1;
Delay_ms(500);
PORTB = 0;
while (1)
{
 if (Usart_Data_Ready() == 1)
 {
 receive = Usart_Read();
 if (receive==172)        // Check start byte
 {
 Delay_ms(15);            [B][COLOR="#FF0000"]*** I don't know why but it need to match the intput delay[/COLOR][/B] 
 if (Usart_Data_Ready() == 1) {
 receive = Usart_Read();  // receive command
 if (receive<128)
 {
 PORTB = receive;         // main function
 Delay_ms(200);
 }
 if (receive==129)       
 {
 PORTB.F0=~PORTB.F0;      // main function
 Delay_ms(200);
 }
 if (receive==136)
 {
 PORTB.F3=~PORTB.F3;	  // main function
 Delay_ms(200);
 }
 if (receive==144)
 {
 PORTB.F4=~PORTB.F4;	  // main function
 Delay_ms(200);
 }
 if (receive==160)
 {
 PORTB.F5=~PORTB.F5;      // main function
 Delay_ms(200);
 }
 }
 }
 }
}
}
 
Last edited:

I'm not sure why you have used all those delays but your real problem is that you have to pre-condition the data before sending it and do the opposite when receiving. In an AM system the '1' is transmitted as 'transmitter on' and the '0' as 'transmitter off'. As far as the receiver is concerned, the presence of a signal is seen as a '1' (correct) but the absence is seen as background noise and could be a one or a zero. This is further complicated by the serial data being framed by a start bit and stop bit which could be mistakenly detected in the random noise. There are things you can try, firstly, increase the baud rate from 300 to at least 1200, this will shorten the time between '1' bits, giving the receiver less time to adjust to the background noise. Secondly, try turning the power to the TX on with the data line held high, then wait for say 50mS before sending the data. This will force a signal to be transmitted which will quietent the channel and allow the receiver time to adjust to it before the data itself is transmitted. Try not to leave gaps beween the bytes you are sending.

If you post code again, please copy it between code tags so it retains it's original format. Use the ' # ' icon in the message menu bar.

Brian.
 

Thanks for your advice Brian. But it still only working in proteus. I believe that the main problem is the receiver checking code that need to have same delay as input delay [Red word]
Are there any coding method that better than mine. I think my coding in that part is wrong.

Best regards,
Thanapat
 

I don't use Mikro C or Proteus but based on users experience I read here I have little trust in it.

Please check to see if your USART_Write() function checks to see if it is safe to write to the TXREG register. If it doesn't, I suggest you check the TRMT flag or TXIF bit to see if the previous byte transmission has finished. This should be quicker and more reliable than just leaving a delay for it to be sent.

Brian.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top