Tyari Taylor
/
SPI_MCP3002_OS
operational SPI with OS5
main.cpp@67:f141b2ad5fcd, 2018-08-23 (annotated)
- Committer:
- Tyari21
- Date:
- Thu Aug 23 14:07:02 2018 +0000
- Revision:
- 67:f141b2ad5fcd
- Parent:
- 66:0a43d8aeeb76
operational SPI with OS5
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Jonathan Austin |
0:2757d7abb7d9 | 1 | #include "mbed.h" |
Tyari21 | 67:f141b2ad5fcd | 2 | // Bits are: Start (1), SGL/DIFF (1 for single), ODD/SIGN (0 for channel 0), MSBF (1 for MSB first) |
Tyari21 | 67:f141b2ad5fcd | 3 | #define MCP3002_CH0 0x68 // 0110 1000 |
Tyari21 | 67:f141b2ad5fcd | 4 | #define MCP3002_CH1 0x78 // 0111 1000 |
Jonathan Austin |
0:2757d7abb7d9 | 5 | |
Tyari21 | 67:f141b2ad5fcd | 6 | Serial pc(USBTX, USBRX); |
Tyari21 | 67:f141b2ad5fcd | 7 | SPI spi(p5, p6, p7); // mosi, miso, sclk |
Tyari21 | 67:f141b2ad5fcd | 8 | DigitalOut cs(p20); // Chip select, active low |
Tyari21 | 67:f141b2ad5fcd | 9 | AnalogOut aout(p18); |
Jonathan Austin |
0:2757d7abb7d9 | 10 | |
Tyari21 | 67:f141b2ad5fcd | 11 | // Note that using CS means that the chip itself has no address |
Tyari21 | 67:f141b2ad5fcd | 12 | |
Jonathan Austin |
0:2757d7abb7d9 | 13 | int main() { |
Tyari21 | 67:f141b2ad5fcd | 14 | pc.printf("Starting program \r\n"); |
Tyari21 | 67:f141b2ad5fcd | 15 | // Chip must be deselected |
Tyari21 | 67:f141b2ad5fcd | 16 | cs = 1; |
Tyari21 | 67:f141b2ad5fcd | 17 | // Setup the spi for 8 bit data, CPOL = 0, CPHA = 0 |
Tyari21 | 67:f141b2ad5fcd | 18 | // with a 10KHz clock rate |
Tyari21 | 67:f141b2ad5fcd | 19 | spi.format(8,0); |
Tyari21 | 67:f141b2ad5fcd | 20 | spi.frequency(10000); |
Tyari21 | 67:f141b2ad5fcd | 21 | int data1, data2, mask, channel; |
Tyari21 | 67:f141b2ad5fcd | 22 | // Supply voltage is nominally 3.3V |
Tyari21 | 67:f141b2ad5fcd | 23 | float VCC=3.3, vIn; |
Tyari21 | 67:f141b2ad5fcd | 24 | while(1) { |
Tyari21 | 67:f141b2ad5fcd | 25 | for (channel = 0; channel<2; channel++){ |
Tyari21 | 67:f141b2ad5fcd | 26 | if(channel == 0){ |
Tyari21 | 67:f141b2ad5fcd | 27 | mask = MCP3002_CH0; |
Tyari21 | 67:f141b2ad5fcd | 28 | }else{ |
Tyari21 | 67:f141b2ad5fcd | 29 | mask = MCP3002_CH1; |
Tyari21 | 67:f141b2ad5fcd | 30 | } |
Tyari21 | 67:f141b2ad5fcd | 31 | cs = 0; |
Tyari21 | 67:f141b2ad5fcd | 32 | data1 = spi.write(mask); |
Tyari21 | 67:f141b2ad5fcd | 33 | data2 = spi.write(0); |
Tyari21 | 67:f141b2ad5fcd | 34 | cs = 1; |
Tyari21 | 67:f141b2ad5fcd | 35 | // Format for MSB set |
Tyari21 | 67:f141b2ad5fcd | 36 | data1 &= 0x03; // keep only three bits |
Tyari21 | 67:f141b2ad5fcd | 37 | data2 |= data1 << 8 ; // shift and add to LSB |
Tyari21 | 67:f141b2ad5fcd | 38 | // data2 ranges from 0 to 1023 for voltage from 0 to VCC |
Tyari21 | 67:f141b2ad5fcd | 39 | vIn = VCC*data2/1000.; |
Tyari21 | 67:f141b2ad5fcd | 40 | pc.printf("Channel %i Voltage is %f \r\n",channel,vIn); |
Tyari21 | 67:f141b2ad5fcd | 41 | } |
Tyari21 | 67:f141b2ad5fcd | 42 | wait(1); |
Jonathan Austin |
0:2757d7abb7d9 | 43 | } |
Jonathan Austin |
0:2757d7abb7d9 | 44 | } |