University of Plymouth - Stages 1, 2 and 3
/
Task442-FZ429ZI
Version for FZ420ZI
Fork of Task442 by
main.cpp@4:50e9ae883931, 2019-10-10 (annotated)
- Committer:
- noutram
- Date:
- Thu Oct 10 13:52:33 2019 +0000
- Revision:
- 4:50e9ae883931
- Parent:
- 3:36ab439835c7
- Child:
- 5:09b2e4c3659e
spelling error
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 | 4:50e9ae883931 | 47 | //Display 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 |