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