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@3:36ab439835c7, 2019-09-18 (annotated)
- Committer:
- noutram
- Date:
- Wed Sep 18 14:44:20 2019 +0000
- Revision:
- 3:36ab439835c7
- Parent:
- 2:6c9c8dfe36e6
- Child:
- 4:50e9ae883931
2019
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 | 2:6c9c8dfe36e6 | 9 | DigitalOut led1(LED1); |
| noutram | 2:6c9c8dfe36e6 | 10 | DigitalOut led2(LED2); |
| noutram | 0:44a74c1bc812 | 11 | |
| noutram | 0:44a74c1bc812 | 12 | //Shared variables |
| noutram | 0:44a74c1bc812 | 13 | volatile static unsigned short sample16 = 0; |
| noutram | 0:44a74c1bc812 | 14 | |
| noutram | 0:44a74c1bc812 | 15 | //The ticker, used to sample data at a fixed rate |
| noutram | 0:44a74c1bc812 | 16 | Ticker t; |
| noutram | 0:44a74c1bc812 | 17 | |
| noutram | 0:44a74c1bc812 | 18 | //Main function |
| noutram | 0:44a74c1bc812 | 19 | int main() |
| noutram | 0:44a74c1bc812 | 20 | { |
| noutram | 0:44a74c1bc812 | 21 | //Set baud rate to 115200 |
| noutram | 0:44a74c1bc812 | 22 | pc.baud(115200); |
| noutram | 0:44a74c1bc812 | 23 | |
| noutram | 2:6c9c8dfe36e6 | 24 | //Power on self test |
| noutram | 2:6c9c8dfe36e6 | 25 | led1 = 1; led2 = 1; |
| noutram | 2:6c9c8dfe36e6 | 26 | pc.printf("TESTING\n\r"); |
| noutram | 2:6c9c8dfe36e6 | 27 | wait(1.0); |
| noutram | 2:6c9c8dfe36e6 | 28 | led1 = 1; led2 = 1; |
| noutram | 2:6c9c8dfe36e6 | 29 | pc.printf("DONE\n\r"); |
| noutram | 2:6c9c8dfe36e6 | 30 | wait(1.0); |
| noutram | 2:6c9c8dfe36e6 | 31 | |
| noutram | 0:44a74c1bc812 | 32 | //Set up the ticker - 100Hz |
| noutram | 0:44a74c1bc812 | 33 | t.attach(doSample1Hz, 1.0); |
| noutram | 0:44a74c1bc812 | 34 | |
| noutram | 0:44a74c1bc812 | 35 | while(1) { |
| noutram | 0:44a74c1bc812 | 36 | |
| noutram | 0:44a74c1bc812 | 37 | //Sleep |
| noutram | 0:44a74c1bc812 | 38 | sleep(); |
| noutram | 0:44a74c1bc812 | 39 | |
| noutram | 2:6c9c8dfe36e6 | 40 | //READ ADC as an unsigned integer. |
| noutram | 2:6c9c8dfe36e6 | 41 | //Shift right 4 bits (this is a 12bit ADC) & store in static global variable |
| noutram | 2:6c9c8dfe36e6 | 42 | sample16 = POT_ADC_In.read_u16() >> 4; |
| noutram | 2:6c9c8dfe36e6 | 43 | |
| noutram | 2:6c9c8dfe36e6 | 44 | //Hardware debug to verify this is working |
| noutram | 2:6c9c8dfe36e6 | 45 | led2 = !led2; |
| noutram | 2:6c9c8dfe36e6 | 46 | |
| noutram | 0:44a74c1bc812 | 47 | //Displauy the sample in HEX |
| noutram | 0:44a74c1bc812 | 48 | pc.printf("ADC Value: %X\n", sample16); |
| noutram | 3:36ab439835c7 | 49 | //Wait for 20 characters to clear |
| noutram | 3:36ab439835c7 | 50 | wait(0.0014); |
| noutram | 0:44a74c1bc812 | 51 | |
| noutram | 0:44a74c1bc812 | 52 | } //end while(1) |
| noutram | 0:44a74c1bc812 | 53 | } //end main |
| noutram | 0:44a74c1bc812 | 54 | |
| noutram | 0:44a74c1bc812 | 55 | //ISR for the ticker - simply there to perform sampling |
| noutram | 2:6c9c8dfe36e6 | 56 | void doSample1Hz() { |
| noutram | 0:44a74c1bc812 | 57 | //Toggle on board led |
| noutram | 2:6c9c8dfe36e6 | 58 | led1 = !led1; |
| noutram | 0:44a74c1bc812 | 59 | } |
| noutram | 0:44a74c1bc812 | 60 |
