Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
/* *****************************************************************************
; *
; Filename: *
; Date: *
; File Version: 001 *
; *
; Author: Jason Lopez *
; Company: AtomSoft *
; *
;***************************************************************************** */
#include <p18f2525.h>
#include <delays.h>
#include <string.h>
#pragma config WDT = OFF, LVP = OFF, OSC = HSPLL
/************************************
Prototypes
*************************************/
void main(void);
/*
void SendUART(unsigned char data);
void init_uart(void);
*/
void delay_ms(int count);
void SendByte(unsigned char data);
void SendZero(void);
void SendOne(void);
#define BTN1 PORTBbits.RB1
#define BTN2 PORTBbits.RB2
#define LED LATCbits.LATC3
#define TX LATCbits.LATC6
#define ADDRESS 0
#define DATA 1
#define CHECKSUM 2
unsigned char PACKET[3];
void main(void){
ADCON1 = 0x0F;
TRISB = 0b00000110;
TRISC = 0b10000000;
//init_uart();
PACKET[ADDRESS] = 0xAA;
PACKET[CHECKSUM] = 0xAA;
while(1){
if(BTN1){
LED = 1;
delay_ms((int)500);
while(BTN1);
SendByte('J');
LED = 0;
}
if(BTN2){
LED = 1;
delay_ms((int)500);
while(BTN2);
SendByte('a');
LED = 0;
}
}
}
void SendByte(unsigned char data){
volatile unsigned char x;
volatile unsigned char ADDT;
volatile unsigned char CHKT;
PACKET[DATA] = data;
ADDT = PACKET[ADDRESS];
CHKT = PACKET[CHECKSUM];
//SEND HEADER
TX = 0;
for(x=0;x<20;x++){
SendOne();
}
SendZero();
//SEND ADDRESS
for(x=0;x<8;x++){
if(ADDT & 0x80){
SendOne();
} else {
SendZero();
}
ADDT <<= 1;
}
SendOne();
SendZero();
//SEND DATA
for(x=0;x<8;x++){
if(data & 0x80){
SendOne();
} else {
SendZero();
}
data <<= 1;
}
SendOne();
SendZero();
//SEND CHECKSUM
for(x=0;x<8;x++){
if(CHKT & 0x80){
SendOne();
} else {
SendZero();
}
CHKT <<= 1;
}
SendOne();
SendZero();
}
void SendOne(void){
TX = 0;
Delay100TCYx(10);
Delay10TCYx(4);
TX = 1;
Delay100TCYx(10);
Delay10TCYx(4);
}
void SendZero(void){
TX = 0;
Delay100TCYx(10);
Delay10TCYx(4);
TX = 1;
Delay100TCYx(10);
Delay10TCYx(4);
}
void delay_ms(int count){
int x;
for(x=0;x<count;x++){
Delay100TCYx(99);
}
}
wow i cant even wrap my head around the receive code heh... must be the headache i have.. or maybe it gave me a headache.. who knows... break time... any thoughts would be nice
I might get a couple of these ... (attached image)
I really recommend using a chip that does the encoding for you. This frees up time on the PIC and means you can do useful other things than looping forever waiting for the pre-amble.
Whilst the RFM does not do data "encoding" the in-build modulation using FSK does a lot of work for you. This is much better and less prone to interference than the simple TX/RX modules. Clocking and other necessary (but less interesting!) tasks are also done for you.
Most of the code implementations I have seen do something sensible like send a few bytes of 0b01010101 before the syncronisation byte, which should ensure a steady state in the receiver.
So yes, the modules which simply interface to UART with a huge buffer and provide encoding/modulation/etc are better. But the RFM12 modules are very suitable too.