Hexiware_voltmeter_code_MCP3201_ADC

main.cpp

Committer:
agaikwad
Date:
2017-03-22
Revision:
0:459e9fc6e491

File content as of revision 0:459e9fc6e491:

#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);
    }    
    
   
}