Akash Gaikwad
/
Hexiware_ammeter_Code_MCP3201_ADC
Hexiware Ammeter Code using MCP3201 ADC
Diff: main.cpp
- Revision:
- 0:dbba16496f3e
diff -r 000000000000 -r dbba16496f3e main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Wed Mar 22 00:57:10 2017 +0000 @@ -0,0 +1,39 @@ +#include "mbed.h" + +//#define ADC_SCALE_VALUE 24/4096 + +SPI spi(PTC6,PTC7,PTC5); // (MOSI MISO CLK)setup SPI interface on pins PTC5,PTC6,PTC7 + +DigitalOut cs1(PTC4); + +Serial pc(USBTX, USBRX); + + + +int main() +{ + spi.format(8,0); + spi.frequency(1000000); + pc.printf("Analog read test from spi.\n"); + while (true) + { + + cs1=0; + //spi.write(); + //pc.printf("%d\r\n",k); + int high_byte = spi.write(0); + int low_byte = spi.write(0); + cs1=1; + float x = ((high_byte & 0x1f) << 7) | ((low_byte >> 1)); + + float r= (float)((x*1)/4096); // show value in volts. + + pc.printf("AD channel value: %f Amps\r\n", r-0.5); + pc.printf("Analog Value: %.2f\n\r",x); + wait_ms(500); + } + + +} + + \ No newline at end of file