Task 4.4.2

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 
00003 //Function prototype
00004 void doSample1Hz();
00005 
00006 //Global objects
00007 Serial pc(USBTX, USBRX);
00008 AnalogIn POT_ADC_In(A0);
00009 DigitalOut led(LED1);
00010 
00011 //Shared variables
00012 volatile static unsigned short sample16 = 0;
00013 
00014 //The ticker, used to sample data at a fixed rate
00015 Ticker t;
00016 
00017 //Main function
00018 int main()
00019 {
00020     //Set baud rate to 115200
00021     pc.baud(115200);
00022 
00023     //Set up the ticker - 100Hz
00024     t.attach(doSample1Hz, 1.0);
00025 
00026     while(1) {
00027 
00028         //Sleep
00029         sleep();
00030 
00031         //Displauy the sample in HEX
00032         pc.printf("ADC Value: %X\n", sample16);
00033  
00034     } //end while(1)
00035 } //end main
00036 
00037 //ISR for the ticker - simply there to perform sampling
00038 void doSample1Hz()
00039 {
00040     //Toggle on board led
00041     led = !led;
00042 
00043     //READ ADC as an unsigned integer.
00044     //Shift right 4 bits (this is a 12bit ADC) & store in static global variable
00045     sample16 = POT_ADC_In.read_u16() >> 4;
00046 }
00047