Hexiware Ammeter Code using MCP3201 ADC

main.cpp

Committer:
agaikwad
Date:
2017-03-22
Revision:
0:dbba16496f3e

File content as of revision 0:dbba16496f3e:

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