Task 4.4.2

Fork of Task442 by Nicholas Outram

Committer:
noutram
Date:
Mon May 11 13:54:36 2020 +0000
Revision:
5:6e92769a3ed8
Parent:
4:2c20353c0656
Fixed comment

Who changed what in which revision?

UserRevisionLine numberNew contents of line
noutram 0:44a74c1bc812 1 #include "mbed.h"
noutram 0:44a74c1bc812 2
noutram 0:44a74c1bc812 3 //Function prototype
noutram 0:44a74c1bc812 4 void doSample1Hz();
noutram 0:44a74c1bc812 5
noutram 0:44a74c1bc812 6 //Global objects
noutram 0:44a74c1bc812 7 Serial pc(USBTX, USBRX);
noutram 0:44a74c1bc812 8 AnalogIn POT_ADC_In(A0);
noutram 0:44a74c1bc812 9 DigitalOut led(LED1);
noutram 0:44a74c1bc812 10
noutram 0:44a74c1bc812 11 //Shared variables
noutram 0:44a74c1bc812 12 volatile static unsigned short sample16 = 0;
noutram 0:44a74c1bc812 13
noutram 0:44a74c1bc812 14 //The ticker, used to sample data at a fixed rate
noutram 0:44a74c1bc812 15 Ticker t;
noutram 0:44a74c1bc812 16
noutram 0:44a74c1bc812 17 //Main function
noutram 0:44a74c1bc812 18 int main()
noutram 0:44a74c1bc812 19 {
noutram 0:44a74c1bc812 20 //Set baud rate to 115200
noutram 0:44a74c1bc812 21 pc.baud(115200);
noutram 0:44a74c1bc812 22
noutram 5:6e92769a3ed8 23 //Set up the ticker - 1Hz
noutram 0:44a74c1bc812 24 t.attach(doSample1Hz, 1.0);
noutram 0:44a74c1bc812 25
noutram 0:44a74c1bc812 26 while(1) {
noutram 0:44a74c1bc812 27
noutram 0:44a74c1bc812 28 //Sleep
noutram 0:44a74c1bc812 29 sleep();
noutram 2:5914bea4068e 30
noutram 2:5914bea4068e 31 //READ ADC as an unsigned integer.
noutram 2:5914bea4068e 32 //Shift right 4 bits (this is a 12bit ADC) & store in static global variable
noutram 2:5914bea4068e 33 sample16 = POT_ADC_In.read_u16() >> 4;
noutram 2:5914bea4068e 34
noutram 4:2c20353c0656 35 //Display the sample in HEX
noutram 0:44a74c1bc812 36 pc.printf("ADC Value: %X\n", sample16);
noutram 3:22fb972026c5 37 //Wait for 20 characters to clear
noutram 3:22fb972026c5 38 wait(0.0014);
noutram 0:44a74c1bc812 39
noutram 0:44a74c1bc812 40 } //end while(1)
noutram 0:44a74c1bc812 41 } //end main
noutram 0:44a74c1bc812 42
noutram 0:44a74c1bc812 43 //ISR for the ticker - simply there to perform sampling
noutram 0:44a74c1bc812 44 void doSample1Hz()
noutram 0:44a74c1bc812 45 {
noutram 0:44a74c1bc812 46 //Toggle on board led
noutram 0:44a74c1bc812 47 led = !led;
noutram 0:44a74c1bc812 48 }
noutram 0:44a74c1bc812 49