Akash Gaikwad
/
Hexiware_voltmeter_code_MCP3201_ADC
Hexiware_voltmeter_code_MCP3201_ADC
Diff: main.cpp
- Revision:
- 0:459e9fc6e491
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Wed Mar 22 01:13:04 2017 +0000 @@ -0,0 +1,34 @@ +#include "mbed.h" + +SPI spi(PTC6,PTC7,PTC5); // (MOSI MISO CLK)setup SPI interface on pins PTC5,PTC6,PTC7 + +DigitalOut cs2(PTC3); + +Serial pc(USBTX, USBRX); + + + +int main() +{ + spi.format(8,0); + spi.frequency(1000000); + pc.printf("Analog read test from spi.\n"); + while (true) + { + + cs2=0; + int high_byte = spi.write(0); + int low_byte = spi.write(0); + cs2=1; + float x = ((high_byte & 0x1f) << 7) | ((low_byte >> 1)); + + float r= (float)((x*33)/4096); // show value in volts. + + pc.printf("AD Voltage channel value: %f Volt\r\n", r-16.5); + pc.printf("Voltage Analog Value: %.2f\n\r",x); + wait_ms(500); + } + + +} + \ No newline at end of file