1 | #include <p18f46k20.h>
|
2 | #include <usart.h>
|
3 | #include <delays.h>
|
4 | #include <spi.h>
|
5 | #include <stdlib.h>
|
6 |
|
7 | /* Definitions */
|
8 | #define BAUD 9600
|
9 | #define CLK 8000000
|
10 | #define UREF 1,17
|
11 | #define ADSTEPS 16777216
|
12 | #define STEPRANGE (UREF/ADSTEPS)
|
13 |
|
14 | #define AD_IOREG_WRITE 0b00101000 //
|
15 | #define AD_IOREG_READ 0b01101000 //
|
16 | #define AD_STATUSREG_READ 0b01000000
|
17 | #define AD_CONFREG_WRITE 0b00010000
|
18 | #define AD_CONFREG_READ 0b01010000 //
|
19 | #define AD_MODEREG_WRITE 0b00001000
|
20 | #define AD_MODEREG_READ 0b01001000
|
21 | #define AD_DATAREG_READ 0b01011000 //
|
22 |
|
23 | /**/
|
24 | void getVREF();
|
25 | void getCHECK();
|
26 | void ADreset();
|
27 |
|
28 | signed char WriteSPI( unsigned char data_out )
|
29 | {
|
30 | SSPBUF = data_out;
|
31 | while( !SSPSTATbits.BF);
|
32 | return SSPBUF;
|
33 | }
|
34 |
|
35 | void getsSPI( unsigned char *rdptr, unsigned char length )
|
36 | {
|
37 | while ( length ) // stay in loop until length = 0
|
38 | {
|
39 | *rdptr++ = getcSPI(); // read a single byte
|
40 | length--; // reduce string length count by 1
|
41 | }
|
42 | }
|
43 |
|
44 | void ADreset()
|
45 | {
|
46 | PORTCbits.RC0 = 0; // Chip select
|
47 | putcSPI( 0xFF );
|
48 | putcSPI( 0xFF );
|
49 | putcSPI( 0xFF );
|
50 | putcSPI( 0xFF );
|
51 | PORTCbits.RC0 = 1; // Chip deselect
|
52 | Delay1KTCYx(7); // wait 500 us
|
53 | }
|
54 |
|
55 | void getVREF()
|
56 | {
|
57 | unsigned char byte[3];
|
58 | unsigned long int result = 0;
|
59 | char buffer[24];
|
60 |
|
61 | // Setup AD7793
|
62 | PORTCbits.RC0 = 0; // Chip select
|
63 | Delay1KTCYx(14); // Wake up time 1 ms
|
64 |
|
65 | /* write ioreg */
|
66 | putcSPI( AD_IOREG_WRITE ); // write IO Register
|
67 | putcSPI( 0x02 ); // 210uA at IOUT1 and IOUT2
|
68 |
|
69 | putcSPI( AD_CONFREG_WRITE ); // 00010000 Write conf register
|
70 | putcSPI( 0x01 ); // 00000000 BiasOff, gain 1
|
71 | putcSPI( 0x82 ); // 10000010 int ref, buf off, CH anin3
|
72 |
|
73 | putcSPI( AD_MODEREG_WRITE ); // 00001000 Write mode register
|
74 | putcSPI( 0x20 ); // 00100000 single conversation
|
75 | putcSPI( 0x0A ); // 00001010 16,7 Hz fADC
|
76 |
|
77 | PORTCbits.RC0 = 1;
|
78 |
|
79 | PORTCbits.RC0 = 0;
|
80 | Delay1KTCYx(14); // Wake up time 1 ms
|
81 |
|
82 | while( PORTCbits.SDA ); // wait till RDY is low
|
83 | putcSPI( AD_DATAREG_READ ); //
|
84 | byte[2] = ReadSPI();
|
85 | byte[1] = ReadSPI();
|
86 | byte[0] = ReadSPI();
|
87 |
|
88 | PORTCbits.RC0 = 1; // Chip deselect
|
89 |
|
90 | result = (unsigned long int)byte[2] * 256 * 256 + (unsigned long int)byte[1] * 256 + (unsigned long int)byte[0];
|
91 | ultoa( result, buffer );
|
92 | putsUSART( buffer ); // send back A/D value
|
93 | }
|
94 |
|
95 | /* getCHECK is now working */
|
96 | void getCHECK()
|
97 | {
|
98 | unsigned char byte[3];
|
99 | char buffer[24];
|
100 |
|
101 | // Setup AD7793
|
102 | PORTCbits.RC0 = 0; // Chip select
|
103 | Delay1KTCYx(14); // Wake up time 1 ms
|
104 |
|
105 | /* read ioreg */
|
106 | putcSPI( AD_IOREG_READ ); // 01101000 Read IO Register
|
107 | byte[2] = ReadSPI();
|
108 |
|
109 | itoa( (unsigned int)byte[2], buffer );
|
110 | putrsUSART( "\nIOREG: " );
|
111 | putsUSART( buffer ); // send back
|
112 |
|
113 | /* read conf reg */
|
114 | putcSPI( AD_CONFREG_READ ); // 00010000 read conf register
|
115 | byte[2] = ReadSPI();
|
116 | byte[1] = ReadSPI();
|
117 |
|
118 | itoa( (unsigned int)byte[2], buffer );
|
119 | putrsUSART( "\nCONFREG: " );
|
120 | putsUSART( buffer ); // send back
|
121 | itoa( (unsigned int)byte[1], buffer );
|
122 | putsUSART( buffer ); // send back
|
123 |
|
124 | /* read mode reg */
|
125 | putcSPI( AD_MODEREG_READ ); // 00010000 read conf register
|
126 | byte[2] = ReadSPI();
|
127 | byte[1] = ReadSPI();
|
128 |
|
129 | itoa( (unsigned int)byte[2], buffer );
|
130 | putrsUSART( "\nMODEREG: " );
|
131 | putsUSART( buffer ); // send back
|
132 | itoa( (unsigned int)byte[1], buffer );
|
133 | putsUSART( buffer ); // send back
|
134 |
|
135 | PORTCbits.RC0 = 1; // Chip select
|
136 | }
|
137 |
|
138 | void main( void )
|
139 | {
|
140 | /* 9600, */
|
141 | OpenUSART(USART_TX_INT_OFF &
|
142 | USART_RX_INT_OFF &
|
143 | USART_ASYNCH_MODE &
|
144 | USART_EIGHT_BIT &
|
145 | USART_BRGH_HIGH,
|
146 | 77);
|
147 |
|
148 | /* SPI idle is high, FOSC/64, */
|
149 | OpenSPI( SPI_FOSC_64, MODE_11, SMPEND );
|
150 |
|
151 | TRISCbits.TRISC0 = 0; // RC0/CS - output
|
152 | TRISDbits.TRISD7 = 0; // D7 - output (LED)
|
153 | TRISCbits.TRISC4 = 1; // RC4/SDI - input
|
154 | TRISCbits.TRISC5 = 0; // RC5/SDO - output
|
155 | TRISCbits.TRISC3 = 0; // RC3/CLK - output
|
156 | TRISDbits.TRISD0 = 1; // Taster - input
|
157 | TRISDbits.TRISD1 = 1; // Taster - input
|
158 |
|
159 | ADreset();
|
160 |
|
161 | while( 1 )
|
162 | {
|
163 | // Blinklicht
|
164 | PORTDbits.RD7 = 0;
|
165 | Delay10KTCYx(100);
|
166 | PORTDbits.RD7 = 1;
|
167 | Delay10KTCYx(100);
|
168 |
|
169 | if( !PORTDbits.RD0 )
|
170 | {
|
171 | Delay1KTCYx(14); // Wake up time 1 ms
|
172 | while( !PORTDbits.RD0 );
|
173 | getVREF();
|
174 | }
|
175 |
|
176 | if( !PORTDbits.RD1 )
|
177 | {
|
178 | Delay1KTCYx(14); // Wake up time 1 ms
|
179 | while( !PORTDbits.RD1 );
|
180 | getCHECK();
|
181 | }
|
182 | }
|
183 | }
|