Task 4.4.4 Solution
Fork of Task444Solution by
main.cpp@2:8ec2bcaa0346, 2019-09-18 (annotated)
- Committer:
- noutram
- Date:
- Wed Sep 18 15:21:20 2019 +0000
- Revision:
- 2:8ec2bcaa0346
- Parent:
- 0:83a9911ab6ec
2019
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| noutram | 0:83a9911ab6ec | 1 | #include "mbed.h" |
| noutram | 0:83a9911ab6ec | 2 | |
| noutram | 0:83a9911ab6ec | 3 | //Function prototype |
| noutram | 0:83a9911ab6ec | 4 | void doSample1Hz(); |
| noutram | 0:83a9911ab6ec | 5 | |
| noutram | 0:83a9911ab6ec | 6 | //Global objects |
| noutram | 0:83a9911ab6ec | 7 | Serial pc(USBTX, USBRX); |
| noutram | 0:83a9911ab6ec | 8 | AnalogIn POT_ADC_In(A0); |
| noutram | 0:83a9911ab6ec | 9 | DigitalOut led(LED1); |
| noutram | 0:83a9911ab6ec | 10 | |
| noutram | 0:83a9911ab6ec | 11 | #define N 16 |
| noutram | 0:83a9911ab6ec | 12 | |
| noutram | 0:83a9911ab6ec | 13 | //Shared variables |
| noutram | 2:8ec2bcaa0346 | 14 | volatile static uint16_t sample16 = 0; |
| noutram | 0:83a9911ab6ec | 15 | |
| noutram | 2:8ec2bcaa0346 | 16 | uint32_t sum = 0; |
| noutram | 2:8ec2bcaa0346 | 17 | uint32_t counter = 0; |
| noutram | 0:83a9911ab6ec | 18 | |
| noutram | 0:83a9911ab6ec | 19 | //The ticker, used to sample data at a fixed rate |
| noutram | 0:83a9911ab6ec | 20 | Ticker t; |
| noutram | 0:83a9911ab6ec | 21 | |
| noutram | 0:83a9911ab6ec | 22 | //Main function |
| noutram | 0:83a9911ab6ec | 23 | int main() |
| noutram | 0:83a9911ab6ec | 24 | { |
| noutram | 0:83a9911ab6ec | 25 | //Set baud rate to 115200 |
| noutram | 0:83a9911ab6ec | 26 | pc.baud(115200); |
| noutram | 0:83a9911ab6ec | 27 | |
| noutram | 0:83a9911ab6ec | 28 | //Set up the ticker - 100Hz |
| noutram | 0:83a9911ab6ec | 29 | t.attach(doSample1Hz, 0.01); |
| noutram | 0:83a9911ab6ec | 30 | |
| noutram | 0:83a9911ab6ec | 31 | while(1) { |
| noutram | 0:83a9911ab6ec | 32 | |
| noutram | 0:83a9911ab6ec | 33 | //Sleep |
| noutram | 0:83a9911ab6ec | 34 | sleep(); |
| noutram | 0:83a9911ab6ec | 35 | |
| noutram | 0:83a9911ab6ec | 36 | //Add new sample to the sum |
| noutram | 0:83a9911ab6ec | 37 | sum += sample16; |
| noutram | 0:83a9911ab6ec | 38 | |
| noutram | 0:83a9911ab6ec | 39 | //Increment the count |
| noutram | 2:8ec2bcaa0346 | 40 | counter = counter + 1; |
| noutram | 0:83a9911ab6ec | 41 | |
| noutram | 0:83a9911ab6ec | 42 | //Enough to calculate the average? |
| noutram | 2:8ec2bcaa0346 | 43 | if (counter == N) { |
| noutram | 0:83a9911ab6ec | 44 | //Divide sum by 16 |
| noutram | 0:83a9911ab6ec | 45 | unsigned short mean = (sum >> 4); |
| noutram | 0:83a9911ab6ec | 46 | |
| noutram | 0:83a9911ab6ec | 47 | //Display |
| noutram | 0:83a9911ab6ec | 48 | pc.printf("Mean = %hu\n", mean); |
| noutram | 2:8ec2bcaa0346 | 49 | wait(0.0014); //Wait for buffer to clear |
| noutram | 0:83a9911ab6ec | 50 | |
| noutram | 0:83a9911ab6ec | 51 | //Reset sum and count |
| noutram | 2:8ec2bcaa0346 | 52 | counter = 0; |
| noutram | 2:8ec2bcaa0346 | 53 | sum = 0; |
| noutram | 0:83a9911ab6ec | 54 | } |
| noutram | 0:83a9911ab6ec | 55 | |
| noutram | 0:83a9911ab6ec | 56 | } //end while(1) |
| noutram | 0:83a9911ab6ec | 57 | } //end main |
| noutram | 0:83a9911ab6ec | 58 | |
| noutram | 0:83a9911ab6ec | 59 | //ISR for the ticker - simply there to perform sampling |
| noutram | 0:83a9911ab6ec | 60 | void doSample1Hz() |
| noutram | 0:83a9911ab6ec | 61 | { |
| noutram | 0:83a9911ab6ec | 62 | //Toggle on board led |
| noutram | 0:83a9911ab6ec | 63 | led = !led; |
| noutram | 0:83a9911ab6ec | 64 | |
| noutram | 0:83a9911ab6ec | 65 | //READ ADC as an unsigned integer. |
| noutram | 0:83a9911ab6ec | 66 | //Shift right 4 bits (this is a 12bit ADC) & store in static global variable |
| noutram | 0:83a9911ab6ec | 67 | sample16 = POT_ADC_In.read_u16() >> 4; |
| noutram | 0:83a9911ab6ec | 68 | } |
| noutram | 0:83a9911ab6ec | 69 | |
| noutram | 0:83a9911ab6ec | 70 |
