Tyari Taylor
/
SPI_MCP3002_OS
operational SPI with OS5
Diff: main.cpp
- Revision:
- 67:f141b2ad5fcd
- Parent:
- 66:0a43d8aeeb76
--- a/main.cpp Tue Jun 26 17:52:09 2018 +0000 +++ b/main.cpp Thu Aug 23 14:07:02 2018 +0000 @@ -1,14 +1,44 @@ #include "mbed.h" +// Bits are: Start (1), SGL/DIFF (1 for single), ODD/SIGN (0 for channel 0), MSBF (1 for MSB first) +#define MCP3002_CH0 0x68 // 0110 1000 +#define MCP3002_CH1 0x78 // 0111 1000 -DigitalOut led1(LED1); +Serial pc(USBTX, USBRX); +SPI spi(p5, p6, p7); // mosi, miso, sclk +DigitalOut cs(p20); // Chip select, active low +AnalogOut aout(p18); -// main() runs in its own thread in the OS +// Note that using CS means that the chip itself has no address + int main() { - while (true) { - led1 = !led1; - wait(0.02); - led1 = !led1; - wait(0.02); + pc.printf("Starting program \r\n"); + // Chip must be deselected + cs = 1; + // Setup the spi for 8 bit data, CPOL = 0, CPHA = 0 + // with a 10KHz clock rate + spi.format(8,0); + spi.frequency(10000); + int data1, data2, mask, channel; + // Supply voltage is nominally 3.3V + float VCC=3.3, vIn; + while(1) { + for (channel = 0; channel<2; channel++){ + if(channel == 0){ + mask = MCP3002_CH0; + }else{ + mask = MCP3002_CH1; + } + cs = 0; + data1 = spi.write(mask); + data2 = spi.write(0); + cs = 1; +// Format for MSB set + data1 &= 0x03; // keep only three bits + data2 |= data1 << 8 ; // shift and add to LSB +// data2 ranges from 0 to 1023 for voltage from 0 to VCC + vIn = VCC*data2/1000.; + pc.printf("Channel %i Voltage is %f \r\n",channel,vIn); + } + wait(1); } } -