Akash Gaikwad
/
Hexiware_voltmeter_code_MCP3201_ADC
Hexiware_voltmeter_code_MCP3201_ADC
main.cpp@0:459e9fc6e491, 2017-03-22 (annotated)
- Committer:
- agaikwad
- Date:
- Wed Mar 22 01:13:04 2017 +0000
- Revision:
- 0:459e9fc6e491
Hexiware_voltmeter_code_MCP3201_ADC
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
agaikwad | 0:459e9fc6e491 | 1 | #include "mbed.h" |
agaikwad | 0:459e9fc6e491 | 2 | |
agaikwad | 0:459e9fc6e491 | 3 | SPI spi(PTC6,PTC7,PTC5); // (MOSI MISO CLK)setup SPI interface on pins PTC5,PTC6,PTC7 |
agaikwad | 0:459e9fc6e491 | 4 | |
agaikwad | 0:459e9fc6e491 | 5 | DigitalOut cs2(PTC3); |
agaikwad | 0:459e9fc6e491 | 6 | |
agaikwad | 0:459e9fc6e491 | 7 | Serial pc(USBTX, USBRX); |
agaikwad | 0:459e9fc6e491 | 8 | |
agaikwad | 0:459e9fc6e491 | 9 | |
agaikwad | 0:459e9fc6e491 | 10 | |
agaikwad | 0:459e9fc6e491 | 11 | int main() |
agaikwad | 0:459e9fc6e491 | 12 | { |
agaikwad | 0:459e9fc6e491 | 13 | spi.format(8,0); |
agaikwad | 0:459e9fc6e491 | 14 | spi.frequency(1000000); |
agaikwad | 0:459e9fc6e491 | 15 | pc.printf("Analog read test from spi.\n"); |
agaikwad | 0:459e9fc6e491 | 16 | while (true) |
agaikwad | 0:459e9fc6e491 | 17 | { |
agaikwad | 0:459e9fc6e491 | 18 | |
agaikwad | 0:459e9fc6e491 | 19 | cs2=0; |
agaikwad | 0:459e9fc6e491 | 20 | int high_byte = spi.write(0); |
agaikwad | 0:459e9fc6e491 | 21 | int low_byte = spi.write(0); |
agaikwad | 0:459e9fc6e491 | 22 | cs2=1; |
agaikwad | 0:459e9fc6e491 | 23 | float x = ((high_byte & 0x1f) << 7) | ((low_byte >> 1)); |
agaikwad | 0:459e9fc6e491 | 24 | |
agaikwad | 0:459e9fc6e491 | 25 | float r= (float)((x*33)/4096); // show value in volts. |
agaikwad | 0:459e9fc6e491 | 26 | |
agaikwad | 0:459e9fc6e491 | 27 | pc.printf("AD Voltage channel value: %f Volt\r\n", r-16.5); |
agaikwad | 0:459e9fc6e491 | 28 | pc.printf("Voltage Analog Value: %.2f\n\r",x); |
agaikwad | 0:459e9fc6e491 | 29 | wait_ms(500); |
agaikwad | 0:459e9fc6e491 | 30 | } |
agaikwad | 0:459e9fc6e491 | 31 | |
agaikwad | 0:459e9fc6e491 | 32 | |
agaikwad | 0:459e9fc6e491 | 33 | } |
agaikwad | 0:459e9fc6e491 | 34 |