OSDN Git Service

11/11/30 12H serial csv out.
[scilog/cpu2010.git] / myad.c
1 /*
2  * ADS1259
3  * 
4  * ADデータSPIエンディアン MSB first
5  */
6 #include <p24FJ64GA004.h>
7 #include <libpic30.h>
8
9 #include "delay.h"
10 #include "myspi.h"
11 #include "myad.h"
12
13
14 #if 0
15 // AD DRDY0 INTフラグ 1=INTかかった 0=なし
16 static char ad_drdy0_int;
17 // AD DRDY0 INTフラグ
18 void ad_drdy0_intf_set(void)
19 {
20     ad_drdy0_int = 1;
21 }
22 void ad_drdy0_intf_clear(void)
23 {
24     ad_drdy0_int = 0;
25 }
26 char ad_is_drdy0_intf(void)
27 {
28     if (ad_drdy0_int == 1) {
29         return 1;
30     }
31     return 0;
32 }
33 #endif
34
35 void ad_cs_init(void)
36 {
37     asm("NOP");
38     CS_RCLK = 0;
39     asm("NOP");
40     CS_CLK = 0;
41     asm("NOP");
42     CS_DATA = 0;
43 }
44 # if 0
45 /*
46  int cs: 0〜15
47  */
48 void ad_cs(unsigned char cs)
49 {
50     unsigned char i;
51
52 //CS_CLK = 1;
53     for(i = 16; i > 0; i--) {
54         if (i == cs+1) {
55             CS_DATA = 0;
56         } else {
57             CS_DATA = 1;
58         }
59         asm("NOP");
60         CS_CLK = 1;
61         CS_CLK = 0;
62     }
63     asm("NOP");
64     CS_RCLK = 1;
65     asm("NOP");
66     CS_DATA = 0;
67     asm("NOP");
68     CS_RCLK = 0;
69 }
70
71 void ad_cs_dis(void)
72 {
73     unsigned char i;
74
75     CS_DATA = 1;
76     for(i = 16; i > 0; i--) {
77         CS_CLK = 1;
78         CS_CLK = 0;
79     }
80     asm("NOP");
81     CS_RCLK = 1;
82     asm("NOP");
83     CS_DATA = 0;
84     asm("NOP");
85     CS_RCLK = 0;
86 }
87 #endif
88
89 void ad_reset(void)
90 {
91     PORTBbits.RB10 = 1; // -AD RESET
92     delay_us(1);
93     PORTBbits.RB10 = 0; // -AD RESET
94     delay_ms(1);
95     PORTBbits.RB10 = 1; // -AD RESET
96 }
97 void ad_start(void)
98 {
99     PORTBbits.RB11 = 1; // +AD START
100     delay_us(1);
101     PORTBbits.RB11 = 0; // +AD START
102 }
103 // polling send
104 void ad_spi_send(unsigned char c)
105 {
106     while(spi1_tx_fifo_is_full());
107     spi1_putc(c);
108 }
109 // polling rcv
110 unsigned char ad_spi_rcv(void)
111 {
112     spi1_rx_overrun_clear();
113     while(spi1_tx_fifo_is_full());
114     // 前に受信したデータをクリア
115     spi1_getc();
116     // 送信
117     spi1_putc(0);
118     // 受信待ち
119     while(!spi1_rx_fifo_is_full());
120     return spi1_getc();
121 }
122
123 void ad_init(void)
124 {
125     int cs;
126     int i;
127 for(i = 0; i < AD_CHNUM; i++) {
128     cs = AD_CH1_CS + i;
129 //1. Send the SDATAC command <11h>. This command cancels the RDATAC mode. RDATAC mode must be
130 //cancelled before the register write commands.
131
132     ad_cs(cs);
133         ad_spi_send(ADCMD_SDATAC);
134     ad_cs_dis();
135 //2. Send the register write command. The following example shows the register write as a block of nine bytes,
136 //starting at register 0 (CONFIG0).
137     ad_cs(cs);
138         ad_spi_send(ADCMD_WREG | 0);    // Register write command
139         ad_spi_send(9 - 1);             // 9byte write
140         // CONFIG0: RFBIAS OFF, SPI timeout enable
141         // Bit 2 RBIAS: Internal reference bias
142         //  0 = Internal reference bias disabled
143         //  1 = Internal reference bias enabled (default)
144         // Bit 0 SPI: SCLK timeout of SPI interface
145         //  0 = SPI timeout disabled
146         //  1 = SPI timeout enabled (default), when SCLK is held low for 216 clock cycles
147         //         bit76543210
148         ad_spi_send(0b00000101);
149         // CONFIG1: sinc1 filter, EXTREF ON, START delay = 0
150         // Bit 6 CHKSUM: Checksum
151         //  0 = Disabled (default)
152         //  1 = Conversion data checksum byte included in readback
153         // Bit 4 SINC2: Digital filter mode
154         //  0 = sinc1 filter (default)
155         //  1 = sinc2 filter
156         // Bit 3 EXTREF: Reference select
157         //  0 = Internal
158         //  1 = External (default)
159         // Bits 2-0 DELAY[2:0]: START conversion delay
160         //  000 = No delay (default)
161         //         bit76543210
162         ad_spi_send(0b00001000);
163
164
165         // CONFIG2: SYNCOUT ON, Gate control, DataRate=10Hz
166         // Bit 5 SYNCOUT: SYNCOUT clock enable
167         //  0 = SYNCOUT disabled (default)
168         //  1 = SYNCOUT enabled
169         // Bit 4 PULSE: Conversion Control mode select
170         //  0 = Gate Control mode (default)
171         //  1 = Pulse Control mode
172         // Bits 2-0 DR[2:0] Data rate setting
173         //  000 = 10SPS (default)
174         //  001 = 16.6SPS
175         //  010 = 50SPS
176         //  011 = 60SPS
177         //  100 = 400SPS
178         //  101 = 1200SPS
179         //  110 = 3600SPS
180         //  111 = 14400SPS
181         //  NOTE: fCLK = 7.3728MHz
182         unsigned char rate_bit;
183         switch(AD_SAMPLE) {
184             case 50:
185                 rate_bit = 0b00000010;
186                 break;
187             case 400:
188                 rate_bit = 0b00000100;
189                 break;
190             default:
191             case 10:
192                 rate_bit = 0b00000000;
193                 break;
194         }
195         //         bit76543210
196         ad_spi_send(0b00100000 | rate_bit);
197
198         // OFC0,1,2: no offset correction
199         ad_spi_send(0);
200         ad_spi_send(0);
201         ad_spi_send(0);
202
203     // FSC0,1,2: no full scale correction
204         ad_spi_send(0);
205         ad_spi_send(0);
206         ad_spi_send(0b01000000);
207     ad_cs_dis();
208
209 }
210     // 4. Take the START pin high or send the START command to start conversions.
211     ad_start_ena(); // +AD START
212
213     for(i = 0; i < AD_CHNUM; i++) {
214         cs = AD_CH1_CS + i;
215         //5. Optionally, send the RDATAC command <10h>. This permits reading of conversion data without the need of
216         //the read data command. Otherwise, the read data opcode must be sent to read each conversion result.
217         ad_cs(cs);
218             ad_spi_send(ADCMD_RDATAC);
219         ad_cs_dis();
220     }
221
222 }
223 /*
224         PGA280 init
225         BUF OFF
226 */
227 void pga_init(void)
228 {
229     int cs;
230     int i;
231 for(i = 0; i < AD_CHNUM; i++) {
232     cs = PGA_CH1_CS + i;
233     //Register 1
234     //Soft Reset
235     ad_cs(cs);
236         ad_spi_send(0x41);
237         ad_spi_send(0x01);
238     ad_cs_dis();
239     //waitいる?
240     delay_ms(1);
241
242     //Register 0
243     //Gain=1/4
244     ad_cs(cs);
245         ad_spi_send(0x40);  //(BUF OFF)
246         ad_spi_send(0x08);
247     ad_cs_dis();
248
249     //Register 8: GPIO Configuration Register
250     //使わないpinはoutput(=1)にする
251     //bit6 GPIO6(SYNCIN) input(=0)
252     ad_cs(cs);
253         ad_spi_send(0x48);
254         ad_spi_send(0b00111111);
255     ad_cs_dis();
256
257     //Register 12: Special Functions Register
258     //bit6 SYNCin=1 SYNCIN(GPIO6) Enable
259     ad_cs(cs);
260         ad_spi_send(0x4C);
261         ad_spi_send(0b01000000);
262     ad_cs_dis();
263 }
264 }
265