Rev Author Line No. Line
1088 mija 1 /* mija 2008
2 demo for RFM01 - RX 868MHz
3  
4 PIC 16F876A
5 xtal = 4MHz
6 */
7 #include "RX_RFM01.h"
8  
9 //************************************************************************
10  
11 #define SDI PIN_B0
12 #define SCK PIN_B1
13 #define SDO PIN_B2 // input for PIC
14 #define nIRQ PIN_B3
15 #define nSEL PIN_B4
16 #define LED PIN_B5
17  
18 //************************************************************************
19 unsigned char RF_RXBUF[22];
20 int1 led_trg;
21  
22 //************************************************************************
23  
24 void RF_INIT(void)
25 {
26 output_high(nSEL);
27 output_high(SDI);
28 output_low(SCK);
29 input(SDO);
30 }
31  
32 void RF_WRITE_CMD(unsigned int16 cmd)
33 {
34 unsigned int8 i;
35  
36 output_low(SCK);
37 output_low(nSEL);
38 for (i=0;i<16;i++)
39 {
40 output_low(SCK);
41 if (cmd & 0x8000) output_high(SDI);
42 else output_low(SDI);
43 output_high(SCK);
44 cmd <<= 1;
45 }
46 output_low(SCK);
47 output_high(nSEL);
48 }
49  
50 int8 RF_RDFIFO(void)
51 {
52 unsigned int8 i,Result;
53  
54 output_low(SCK);
55 output_low(nSEL);
56 output_low(SDI);
57 for (i=0;i<16;i++) // skip satus bits
58 {
59 output_high(SCK);
60 output_high(SCK);
61 output_low(SCK);
62 output_low(SCK);
63 }
64 Result = 0;
65 for (i=0;i<8;i++)
66 {
67 Result <<= 1;
68 if (input(SDO)) Result |= 1;
69 output_high(SCK);
70 output_high(SCK);
71 output_low(SCK);
72 output_low(SCK);
73 }
74 output_high(nSEL);
75 return Result;
76 }
77  
78 void main()
79 {
80 unsigned int8 i,j,ChkSum;
81  
82 setup_adc_ports(NO_ANALOGS);
83 setup_adc(ADC_OFF);
84 setup_spi(SPI_MASTER|SPI_L_TO_H|SPI_CLK_DIV_4);
85 setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
86 setup_timer_1(T1_DISABLED);
87 setup_timer_2(T2_DISABLED,0,1);
88 setup_comparator(NC_NC_NC_NC);
89 setup_vref(FALSE);
90  
91 output_high(LED);
92 delay_ms(1000);
93 output_low(LED);
94  
95 RF_INIT();
96  
97 RF_WRITE_CMD(0x0000);
98 RF_WRITE_CMD(0x918A); // 868BAND,134kHz
99 RF_WRITE_CMD(0xA640); // 876Mhz
100 RF_WRITE_CMD(0xC847); // 4.8kbps
101 RF_WRITE_CMD(0xC69B); // AFC setting
102 RF_WRITE_CMD(0xC42A); // Clock recovery manualcontrol,Dig. filter, DQD=4
103 RF_WRITE_CMD(0xC240); // output 1.66Mhz
104 RF_WRITE_CMD(0xC080);
105 RF_WRITE_CMD(0xCE88); // use FIFO 8bit
106 RF_WRITE_CMD(0xCE8B);
107 RF_WRITE_CMD(0xC081); // open RX
108  
109 led_trg = 0;
110 i = 0;
111 while (1)
112 {
113 while (!input(nIRQ))
114 {
115 RF_RXBUF[i++] = RF_RDFIFO();
116 putc(RF_RXBUF[i-1]);
117 if (i == 18)
118 {
119 i = 0;
120 RF_WRITE_CMD(0xCE88);
121 RF_WRITE_CMD(0xCE8B);
122 ChkSum = 0;
123 for (j=0;j<16;j++) ChkSum += RF_RXBUF[j];
124 if (ChkSum == RF_RXBUF[16])
125 {
126 led_trg = ~led_trg;
127 output_bit(LED,led_trg);
128 }
129 }
130 }
131 }
132 }
133  
134  
135