Nicholas Outram
/
Task442
Task 4.4.2
main.cpp@1:b0f53d2975bd, 2017-07-13 (annotated)
- Committer:
- noutram
- Date:
- Thu Jul 13 14:55:06 2017 +0000
- Revision:
- 1:b0f53d2975bd
- Parent:
- 0:44a74c1bc812
updated for mbed-os 5.5
Who changed what in which revision?
User | Revision | Line number | New 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 | 0:44a74c1bc812 | 23 | //Set up the ticker - 100Hz |
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 | 0:44a74c1bc812 | 30 | |
noutram | 0:44a74c1bc812 | 31 | //Displauy the sample in HEX |
noutram | 0:44a74c1bc812 | 32 | pc.printf("ADC Value: %X\n", sample16); |
noutram | 0:44a74c1bc812 | 33 | |
noutram | 0:44a74c1bc812 | 34 | } //end while(1) |
noutram | 0:44a74c1bc812 | 35 | } //end main |
noutram | 0:44a74c1bc812 | 36 | |
noutram | 0:44a74c1bc812 | 37 | //ISR for the ticker - simply there to perform sampling |
noutram | 0:44a74c1bc812 | 38 | void doSample1Hz() |
noutram | 0:44a74c1bc812 | 39 | { |
noutram | 0:44a74c1bc812 | 40 | //Toggle on board led |
noutram | 0:44a74c1bc812 | 41 | led = !led; |
noutram | 0:44a74c1bc812 | 42 | |
noutram | 0:44a74c1bc812 | 43 | //READ ADC as an unsigned integer. |
noutram | 0:44a74c1bc812 | 44 | //Shift right 4 bits (this is a 12bit ADC) & store in static global variable |
noutram | 0:44a74c1bc812 | 45 | sample16 = POT_ADC_In.read_u16() >> 4; |
noutram | 0:44a74c1bc812 | 46 | } |
noutram | 0:44a74c1bc812 | 47 |