Embedded system Fun Blog
























































Find out all the best information, libraries and circuit about the latest Embedded systems.
Showing posts with label analog. Show all posts
Showing posts with label analog. Show all posts

Sunday, 8 January 2012

MBED SPI Sensor: How to read AD7904 output

.from: http://mbed.org/forum/mbed/topic/2444/

#include "mbed.h"

DigitalOut cs(p10);
SPI spi(p5, p6, p7);
Serial pc(USBTX, USBRX);

int adc;

int main()
{
    pc.baud(115200);
    pc.format(8,Serial::None,1);

    spi.format(16,3);
    spi.frequency(1000000);

   
    // dump read
    pc.printf("dump read\r\n");
    cs = 0;
    adc = spi.write(0x8300);
    cs = 1;
    pc.printf("ADC = %d\r\n", adc);

    // real reading
    pc.printf("real reading\r\n");
    cs = 0;
    adc = spi.write(0x0000);
    cs = 1;
    pc.printf("ADC = %d\r\n", adc);
}


MBED SPI Sensor: How to read AD7914 output

.from: http://mbed.org/forum/mbed/topic/2444/

#include "mbed.h"

DigitalOut cs(p10);
SPI spi(p5, p6, p7);
Serial pc(USBTX, USBRX);

int adc;

int main()
{
    pc.baud(115200);
    pc.format(8,Serial::None,1);

    spi.format(16,3);
    spi.frequency(1000000);

   
    // dump read
    pc.printf("dump read\r\n");
    cs = 0;
    adc = spi.write(0x8300);
    cs = 1;
    pc.printf("ADC = %d\r\n", adc);

    // real reading
    pc.printf("real reading\r\n");
    cs = 0;
    adc = spi.write(0x0000);
    cs = 1;
    pc.printf("ADC = %d\r\n", adc);
}


Saturday, 7 January 2012

MBED Example: How to read MaxSonar EZ1 using mbed AnalogIn

.from: http://mbed.org/users/shimniok/programs/MaxSonar_EZ1_Analog/llju06


#include "mbed.h"

// MaxSonar EZ1 test program
// by Michael Shimniok http://www.bot-thoughts.com/
//
// Based on datasheet here: http://www.maxbotix.com/uploads/LV-MaxSonar-EZ1-Datasheet.pdf
// Reads from AN (analog) pin connected to mbed p20, assumes 3.3V supply to EZ1 module.
//
// mbed -> EZ1
// -----------
// VOUT -> +5
// GND  -> GND
// p20  -> AN
//

AnalogIn ain(p20);
Serial pc(USBTX, USBRX); // tx, rx

int main() {
    float adc, volts, inches;
    int feet, in;
   
    pc.baud(115200);
   
    while (1){
        adc = ain.read();           // read analog as a float
        volts = adc * 3.3;          // convert to volts
        inches = volts / 0.0064;    // 3.3V supply: 6.4mV per inch
        feet = (int) inches / 12;   // inches to feet (trunc)
        in = (int) inches % 12;     // remainder -> in(ches)
       
        pc.printf("%8.2f adc %8.2fV %8.2f in %d'%d\"\n", adc, volts, inches, feet, in);

        wait(0.05);                 // 20Hz update rate ; note we aren't truly synchronized to the device or anything...  
    }
}