0% found this document useful (0 votes)
135 views10 pages

RF22 Transmit DEMO

The document describes an RF22 transmitter demo program for a PIC16F690 microcontroller. It initializes ports and timers, transmits predefined payload data using SPI every second, and puts the device to sleep in between transmissions.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
135 views10 pages

RF22 Transmit DEMO

The document describes an RF22 transmitter demo program for a PIC16F690 microcontroller. It initializes ports and timers, transmits predefined payload data using SPI every second, and puts the device to sleep in between transmissions.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

RF22 TX Program V1.

/******************************************************************************
Title: RFM22 transmitter demo program
Current version: V1.2
Function: Package send Demo
Processor PIC16F690 DIP-20
Clock: internal 8M.
Operate frequency: 434MHz
Data rate: 2.4kbps
modulation: FSK
deviation: 45K
bandwidth: 94.8K
frame mode: PH + FIFO
payload 0x30, 0x31...0x3f, 0x78(chksum)
Author : Simon.Yang
Company: HopeRF microelectroni
Contact: +86-0755-82973806
Date: 2010-07-14
The example is only for your reference.
******************************************************************************/
#include<pic.h>

typedef unsigned char uchar;


typedef unsigned int uint;

const uchar tx_buf[17] =


{0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x3a,0x3b,0x3c,0x3d,0x3e,0x3f,0x78};
// ch8 is 140

#define RF22_PWRSTATE_READY 01
#define RF22_PWRSTATE_TX 0x09
Tel: +86-755-82973805 Fax: +86-755-82973550 E-mail: [email protected] 1
http://www.hoperf.com
RF22 TX Program V1.2
#define RF22_PACKET_SENT_INTERRUPT 04
#define RF22_PWRSTATE_POWERDOWN 00

#define nIRQ RA2


#define nSEL RC6
#define SDO RC7
#define SDI RB4
#define LED_RED RB5
#define SCK RB6
#define TXEN RA4
#define RXEN RA5

uchar count_50hz;
uchar ItStatus1, ItStatus2;

typedef struct
{
uchar reach_1s : 1;
} FlagType;
FlagType Flag;

uchar read_8bit_data(void);
void to_tx_mode(void);
void to_ready_mode(void);
void send_8bit_data(uchar i);
void send_read_address(uchar i);
void spi_write(uchar address, uchar data);
void RF22_init_parameter(void);
void delay_50ms(void);
void delay_5ms(void);
void delay_1ms(void);
void port_init(void);
void power_on_delay(void);
uchar spi_read(uchar address);
void Write0( void );
void Write1( void );
void timer2_init(void);
void Write8bitcommand(uchar command);
void to_sleep_mode(void);

__CONFIG(0x3F04);

//*****************************************************************************
Tel: +86-755-82973805 Fax: +86-755-82973550 E-mail: [email protected] 2
http://www.hoperf.com
RF22 TX Program V1.2
void main()
{
uchar i,j,chksum;
OSCCON = 0X70; // 8M crystal
WDTCON = 0X00;

power_on_delay();
port_init();
timer2_init();
count_50hz = 0;
Flag.reach_1s = 0;
INTCON = 0xc0; // enable interrupt

RF22_init_parameter();
while(1)
{
//to_sleep_mode(); //enter sleep mode
if(Flag.reach_1s)
{
Flag.reach_1s = 0;
to_tx_mode(); // TRANSIMITTE DATA
}
}
}
//*****************************************************************************
//--------------------------------------------------------------
void Write0( void )
{
SCK=0;
NOP();

SDI=0;
NOP();

SCK=1;
NOP();
}
//--------------------------------------------------------------
void Write1( void )
{
SCK=0;
NOP();

Tel: +86-755-82973805 Fax: +86-755-82973550 E-mail: [email protected] 3


http://www.hoperf.com
RF22 TX Program V1.2
SDI=1;
NOP();

SCK=1;
NOP();
}
//--------------------------------------------------------------
void Write8bitcommand(uchar command) // keep sel to low
{
uchar n=8;
nSEL = 1;
SCK=0;
nSEL=0;
while(n--)
{
if(command&0x80)
Write1();
else
Write0();
command = command << 1;
}
SCK=0;
}
//--------------------------------------------------------------
void delay_50ms(void)
{
uchar j;
for(j = 0; j<10; j++)
{
delay_5ms();
}
}
//--------------------------------------------------------------
void delay_5ms(void)
{
int i;
for(i = 0; i<650; i++) // 85*5
{
;
}
}
//--------------------------------------------------------------
void delay_1ms(void)
Tel: +86-755-82973805 Fax: +86-755-82973550 E-mail: [email protected] 4
http://www.hoperf.com
RF22 TX Program V1.2
{
uchar i;
for(i = 0; i<130; i++)
{
;
}
}
//--------------------------------------------------------------
void timer2_init(void)
{
T2CON = 0x7f; // timer2 on and 16 pre, postscale
PR2 = 156; // 50hZ, 4m/4/16/16/50
TMR2IE = 1;
}
//--------------------------------------------------------------
void interrupt ISR_timer(void)
{
uchar i;
if(TMR2IF)
{
count_50hz++;
if(count_50hz==50) // REACH 1S
{
count_50hz=0;
Flag.reach_1s = 1;
}
TMR2IF=0;
}
}
//--------------------------------------------------------------
void port_init(void)
{
ANSEL = 0;
ANSELH = 0;
WPUA = 0; // NO PULL UP
IOCA = 0; // NO INTERRUPT ON CHANGE
TRISA = 0x0f; // all ra0-- ra3 input , RA4,5 OUTPUT
TRISB = 0x80; // rb7 input
WPUB = 0x00; // no pull up
TRISC = 0b10101111; // pc7,5,3,2,1,0 input
RC4=0;
LED_RED = 0;
TXEN = RXEN = 0;
Tel: +86-755-82973805 Fax: +86-755-82973550 E-mail: [email protected] 5
http://www.hoperf.com
RF22 TX Program V1.2
}
//--------------------------------------------------------------
void power_on_delay(void)
{
uint i;
for(i = 0; i<1000; i++)
{
delay_1ms();
}
}
//--------------------------------------------------------------
uchar spi_read(uchar address)
{
uchar result;
send_read_address(address);
result = read_8bit_data();
nSEL = 1;
return(result);
}
//--------------------------------------------------------------
void RF22_init_parameter(void)
{
ItStatus1 = spi_read(0x03); // read status, clear interrupt
ItStatus2 = spi_read(0x04);
spi_write(0x06, 0x00); // no wakeup up, lbd,
spi_write(0x07, RF22_PWRSTATE_READY); // disable lbd, wakeup timer, use internal
32768,xton = 1; in ready mode
spi_write(0x09, 0x7f); // c = 12.5p
spi_write(0x0a, 0x05);
spi_write(0x0b, 0xf4); // gpio0 for received data output
spi_write(0x0c, 0xef); // gpio 1 for clk output
spi_write(0x0d, 0xfd); // gpio 2 micro-controller clk output
spi_write(0x0e, 0x00); // gpio 0, 1,2 NO OTHER FUNCTION.
spi_write(0x70, 0x20); // disable manchest

//case RATE_24K: // 2.4k


spi_write(0x6e, 0x13);
spi_write(0x6f, 0xa9);

//PH+FIFO
spi_write(0x30, 0x8c); // enable packet handler, msb first, enable crc,
// 0x31 only readable
spi_write(0x32, 0xff); // 0x32address enable for headere byte 0, 1,2,3, receive header check
Tel: +86-755-82973805 Fax: +86-755-82973550 E-mail: [email protected] 6
http://www.hoperf.com
RF22 TX Program V1.2
for byte 0, 1,2,3
spi_write(0x33, 0x42); // header 3, 2, 1,0 used for head length, fixed packet length,
synchronize word length 3, 2,
spi_write(0x34, 64); // 64 nibble = 32byte preamble
spi_write(0x36, 0x2d); // synchronize word
spi_write(0x37, 0xd4);
spi_write(0x38, 0x00);
spi_write(0x39, 0x00);
spi_write(0x3a, 's'); // tx header
spi_write(0x3b, 'o');
spi_write(0x3c, 'n');
spi_write(0x3d, 'g');
spi_write(0x3e, 17); // total tx 17 byte

// 0x52, 53, 54, 55 set to default


// 0x56 ---------0x6c ??????????????????????????

spi_write(0x6d, 0x0f); // set power max power


spi_write(0x79, 0x0); // no hopping
spi_write(0x7a, 0x0); // no hopping

spi_write(0x71, 0x22); // Gfsk, fd[8] =0, no invert for Tx/Rx data, fifo mode, txclk -->gpio
spi_write(0x72, 0x38); // frequency deviation setting to 45k = 72*625
spi_write(0x73, 0x0);
spi_write(0x74, 0x0); // no offset

//band 434
spi_write(0x75, 0x53); // hbsel = 0, sbsel =1 ???, fb = 19
spi_write(0x76, 0x64); // 25600= 0x6400 for 434Mhz
spi_write(0x77, 0x00);
}
//--------------------------------------------------------------
void spi_write(uchar address, uchar data)
{
address |= 0x80;
Write8bitcommand(address);
send_8bit_data(data);
nSEL = 1;
}
//--------------------------------------------------------------
void send_read_address(uchar i)
{
i &= 0x7f;
Tel: +86-755-82973805 Fax: +86-755-82973550 E-mail: [email protected] 7
http://www.hoperf.com
RF22 TX Program V1.2
Write8bitcommand(i);
}
//--------------------------------------------------------------
void send_8bit_data(uchar i)
{
uchar n = 8;
SCK=0;
while(n--)
{
if(i&0x80)
Write1();
else
Write0();
i = i << 1;
}
SCK=0;
}
//--------------------------------------------------------------
uchar read_8bit_data(void)
{
uchar Result, i;

SCK=0;
Result=0;
for(i=0;i<8;i++)
{ //read fifo data byte
Result=Result<<1;
SCK=1;
NOP();
if(SDO)
{
Result|=1;
}
SCK=0;
NOP();
}
return(Result);
}
//--------------------------------------------------------------
void to_tx_mode(void)
{
uchar i;
to_ready_mode();
Tel: +86-755-82973805 Fax: +86-755-82973550 E-mail: [email protected] 8
http://www.hoperf.com
RF22 TX Program V1.2
//delay_50ms();
RXEN = 0;
TXEN = 1;
spi_write(0x08, 0x03); // disABLE AUTO TX MODE, enble multi packet clear fifo
spi_write(0x08, 0x00); // disABLE AUTO TX MODE, enble multi packet, clear fifo

// ph +fifo mode
spi_write(0x34, 64); // 64 nibble = 32byte preamble
spi_write(0x3e, 17); // total tx 17 byte
for (i = 0; i<17; i++)
{
spi_write(0x7f, tx_buf[i]);
}
spi_write(0x05, RF22_PACKET_SENT_INTERRUPT);
ItStatus1 = spi_read(0x03); //read the Interrupt Status1 register
ItStatus2 = spi_read(0x04);
spi_write(0x07, RF22_PWRSTATE_TX); // to tx mode
while(nIRQ);
to_ready_mode();
RXEN = 0;
TXEN = 0;
LED_RED= 1;
delay_50ms();
LED_RED= 0;
}
//--------------------------------------------------------------
void to_ready_mode(void)
{
ItStatus1 = spi_read(0x03);
ItStatus2 = spi_read(0x04);
spi_write(0x07, RF22_PWRSTATE_READY);
}
//--------------------------------------------------------------
void to_sleep_mode(void)
{
TXEN = RXEN = 0;
LED_RED = 0;
spi_write(0x07, RF22_PWRSTATE_READY);

ItStatus1 = spi_read(0x03); //read the Interrupt Status1 register


ItStatus2 = spi_read(0x04);
spi_write(0x07, RF22_PWRSTATE_POWERDOWN);
while(1)
Tel: +86-755-82973805 Fax: +86-755-82973550 E-mail: [email protected] 9
http://www.hoperf.com
RF22 TX Program V1.2
{
;
}
}
//--------------------------------------------------------------

Tel: +86-755-82973805 Fax: +86-755-82973550 E-mail: [email protected] 10


http://www.hoperf.com

You might also like