Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of Task442 by
main.cpp@2:5914bea4068e, 2018-02-13 (annotated)
- Committer:
- noutram
- Date:
- Tue Feb 13 11:49:20 2018 +0000
- Revision:
- 2:5914bea4068e
- Parent:
- 0:44a74c1bc812
- Child:
- 3:22fb972026c5
Uses mbed classic library as mbed-os does not seem to work with sleep
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 | 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 | 0:44a74c1bc812 | 35 | //Displauy the sample in HEX |
| noutram | 0:44a74c1bc812 | 36 | pc.printf("ADC Value: %X\n", sample16); |
| noutram | 0:44a74c1bc812 | 37 | |
| noutram | 0:44a74c1bc812 | 38 | } //end while(1) |
| noutram | 0:44a74c1bc812 | 39 | } //end main |
| noutram | 0:44a74c1bc812 | 40 | |
| noutram | 0:44a74c1bc812 | 41 | //ISR for the ticker - simply there to perform sampling |
| noutram | 0:44a74c1bc812 | 42 | void doSample1Hz() |
| noutram | 0:44a74c1bc812 | 43 | { |
| noutram | 0:44a74c1bc812 | 44 | //Toggle on board led |
| noutram | 0:44a74c1bc812 | 45 | led = !led; |
| noutram | 0:44a74c1bc812 | 46 | } |
| noutram | 0:44a74c1bc812 | 47 |
