languer
Joined: 09 Jan 2004 Posts: 144 Location: USA
|
Simple driver for OOK Wireless Messaging - Single Data Byte |
Posted: Tue Apr 23, 2013 1:04 am |
|
|
Simple driver to send and receive a single data byte using inexpensive OOK wireless modules (315MHz, 433MHz) and RS232. Following the driver is example code on how to use it for transmission and reception. In the example code the transmitter sends a single data byte from 0 to 100 (and keeps repeating it).
Please note that the maximum baud rate I've seen this properly work with is 2400bps.
Driver:
Code: | // Driver Code //
// definition of message structure
#define JUNK 0xAA
#define SYNC1 0x2D
#define SYNC2 0xD4
#define MSG_SIZE 4
#ifndef OOK_IO_TX
#define OOK_IO_TX PIN_B0
#endif
#ifndef OOK_IO_RX
#define OOK_IO_RX PIN_B1
#endif
#ifndef OOK_BAUD
#define OOK_BAUD 1200
#endif
#use rs232 (baud=OOK_BAUD, xmit=OOK_IO_TX, rcv=OOK_IO_RX, parity=N, bits=8, stream=WIRELESS, FORCE_SW)
//GLOBAL VARIABLES
int8 dataMsg[MSG_SIZE];
//PROTOTYPES
void ook_init();
int8 ook_receive();
void ook_send(int8 msg);
int8 crc8(int8 crc, int8 crc_data);
// INITIALIZE FUNCTION
void ook_init()
{
output_low(OOK_IO_TX); // initialize transmit pin to low state (low power)
}
// RECEIVE FUNCTION
int8 ook_receive()
{
int8 i = 0;
int8 msg = 0;
int8 crcMsg = 0;
int1 returnMsgFlag = False;
int8 msgState = 0;
int8 msgCnt = 0;
while (~returnMsgFlag)
{
if ( kbhit(WIRELESS) ) // buffer incoming characters
{
msg = fgetc(WIRELESS); // receive byte
switch (msgState)
{
case 0: // SYNC1
{
if (msg == SYNC1)
{
dataMsg[msgCnt] = msg;
msgState++;
msgCnt++;
}
break;
}
case 1: // SYNC2
{
if (msg == SYNC2)
{
dataMsg[msgCnt] = msg;
msgState++;
msgCnt++;
}
else
{
msgState = 0;
msgCnt = 0;
}
break;
}
case 2: // MSG
{
dataMsg[msgCnt] = msg;
msgState++;
msgCnt++;
break;
}
case 3: // CRC
{
dataMsg[msgCnt] = msg;
crcMsg = 0;
for (i=0;i<(MSG_SIZE-1);i++)
{
crcMsg = crc8(crcMsg, dataMsg[i]);
}
if (msg == crcMsg)
{
returnMsgFlag = True;
msg = dataMsg[2];
return msg;
}
else
{
msgState = 0;
msgCnt = 0;
returnMsgFlag = False;
}
break;
}
default:
{
msgState = 0;
msgCnt = 0;
returnMsgFlag = False;
}
}
}
}
}
// TRANSMIT FUNCTION
void ook_send(int8 msg)
{
int8 i = 0;
int8 crcMsg = 0;
dataMsg[0] = SYNC1;
dataMsg[1] = SYNC2;
dataMsg[2] = msg;
// calculate message CRC
crcMsg = 0;
for (i=0;i<(MSG_SIZE-1);i++)
{
crcMsg = crc8(crcMsg, dataMsg[i]);
}
dataMsg[3] = crcMsg;
// transmit data
fputc(JUNK,WIRELESS);
fputc(JUNK,WIRELESS);
fputc(JUNK,WIRELESS);
for (i=0;i<(MSG_SIZE);i++)
{
fputc(dataMsg[i],WIRELESS);
}
output_low(OOK_IO_TX); // default back to low power state until next transmission
}
// CRC-8 FUNCTION (Dattalo's CRC-8 Function)
int8 crc8(int8 crc, int8 crc_data)
{
int8 i;
i = (crc_data ^ crc) & 0xff;
crc = 0;
if(i & 1)
crc ^= 0x5e;
if(i & 2)
crc ^= 0xbc;
if(i & 4)
crc ^= 0x61;
if(i & 8)
crc ^= 0xc2;
if(i & 0x10)
crc ^= 0x9d;
if(i & 0x20)
crc ^= 0x23;
if(i & 0x40)
crc ^= 0x46;
if(i & 0x80)
crc ^= 0x8c;
return(crc);
} |
Transmitter Code:
Code: | // Example transmitter code //
// ---------------------------------------------------------------------------------
// PRE-PROCESSOR DEFINITIONS
// ---------------------------------------------------------------------------------
/*** PREPROCESSOR ***/
#include <12F683.h>
#device *=16 ADC=10
#fuses INTRC_IO,NOWDT,NOPUT,NOPROTECT,NOCPD,NOMCLR
#use delay(clock=8000000)
/*** BUILD INFORMATION ***/
#ORG 0x07F0,0x07FF
const char build_rev[] = {"Rev 1.0"};
// ---------------------------------------------------------------------------------
// GLOBAL VARIABLES AND DEFINITIONS
// ---------------------------------------------------------------------------------
/*** DEFINITIONS ***/
#define TRISA1 0b11111100
#define OOK_IO_TX PIN_A0
#define OOK_IO_RX PIN_A3
#define OOK_BAUD 1200
/*** VARIABLES / CONSTANTS ***/
// ---------------------------------------------------------------------------------
// INCLUDES, FUNCTIONS, MACROS & LIBRARIES
// ---------------------------------------------------------------------------------
/*** INCLUDES ***/
#include "drv_wireless_ook1.c"
/*** FUNCTION PROTOTYPES ***/
void init();
/*** FUNCTION - Main ***/
main()
{
init();
int8 cnt = 0;
while ( TRUE )
{
//send three times
ook_send(cnt);
delay_ms(20);
ook_send(cnt);
delay_ms(20);
ook_send(cnt);
delay_ms(20);
cnt = (cnt + 1) % 100;
delay_ms(1000);
}
}
/*** FUNCTION - Inititalize Hardware ***/
void init()
{
set_tris_a(TRISA1); // set GP1-GP4 to outputs, all other inputs
setup_comparator(NC_NC_NC_NC); // disable comparators
setup_adc_ports(NO_ANALOGS); // disable analog inputs
setup_adc(ADC_OFF); // disable A2D
ook_init(); // initialize wirelesss module
delay_ms(2500);
} |
Receiver Code:
Code: | // Example Receiver Code //
// ---------------------------------------------------------------------------------
// PRE-PROCESSOR DEFINITIONS
// ---------------------------------------------------------------------------------
/*** PREPROCESSOR ***/
#include <16F88.H>
#device *=16 ADC=8
#fuses INTRC_IO, NOWDT, PUT, NOMCLR, NOBROWNOUT, NOLVP, NOCPD, NOWRT, NODEBUG, NOPROTECT, NOFCMEN, NOIESO
#use delay(clock=8000000)
/*** BUILD INFORMATION ***/
#ORG 0xFF0,0xFFF
const char build_rev[] = {"Rev 1.0"}; // max length = 15
// ---------------------------------------------------------------------------------
// GLOBAL VARIABLES AND DEFINITIONS
// ---------------------------------------------------------------------------------
/*** DEFINITIONS ***/
// Hardware used by OOK module
#define OOK_IO_TX PIN_B3
#define OOK_IO_RX PIN_B0
#define OOK_BAUD 1200
// Other pin definitions
#define IO_PC PIN_B7
// TRIS Settings
#define TRIS_A 0b11111111
#define TRIS_B 0b01011111
/*** VARIABLES / CONSTANTS ***/
// ---------------------------------------------------------------------------------
// INCLUDES, FUNCTIONS, MACROS & LIBRARIES
// ---------------------------------------------------------------------------------
/*** INCLUDES ***/
#include "drv_wireless_ook1.c"
/*** FUNCTION PROTOTYPES ***/
#use rs232 (baud=9600,xmit=IO_PC,parity=N,bits=8,stream=PC)
void init();
/*** FUNCTION - Main ***/
void main()
{
init();
int8 dataNew = 255;
int8 dataOld = 255;
delay_ms(2500);
fprintf(PC,"%s\r\n",build_rev);
delay_ms(500);
while( true )
{
dataNew = ook_receive();
if (dataNew == dataOld)
{
// do nothing
}
else
{
dataOld = dataNew;
fprintf(PC,"%u\r\n",dataNew);
}
}
}
/*** FUNCTION - Initialization ***/
void init()
{
set_tris_a(TRIS_A);
set_tris_b(TRIS_B);
setup_comparator(NC_NC_NC_NC); // disable comparators
setup_adc_ports(NO_ANALOGS); // disable analog inputs
setup_adc(ADC_OFF); // disable A2D
} |
|
|