Task 4.4.4 Solution
Dependencies: mbed
main.cpp@0:83a9911ab6ec, 2015-09-24 (annotated)
- Committer:
- noutram
- Date:
- Thu Sep 24 12:31:15 2015 +0000
- Revision:
- 0:83a9911ab6ec
Initial version 24-09-2015
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 | 0:83a9911ab6ec | 14 | volatile static unsigned short sample16 = 0; |
noutram | 0:83a9911ab6ec | 15 | |
noutram | 0:83a9911ab6ec | 16 | unsigned int sum = 0; |
noutram | 0:83a9911ab6ec | 17 | unsigned int count = 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 | 0:83a9911ab6ec | 40 | count++; |
noutram | 0:83a9911ab6ec | 41 | |
noutram | 0:83a9911ab6ec | 42 | //Enough to calculate the average? |
noutram | 0:83a9911ab6ec | 43 | if (count == 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 | 0:83a9911ab6ec | 49 | |
noutram | 0:83a9911ab6ec | 50 | //Reset sum and count |
noutram | 0:83a9911ab6ec | 51 | count = sum = 0; |
noutram | 0:83a9911ab6ec | 52 | } |
noutram | 0:83a9911ab6ec | 53 | |
noutram | 0:83a9911ab6ec | 54 | } //end while(1) |
noutram | 0:83a9911ab6ec | 55 | } //end main |
noutram | 0:83a9911ab6ec | 56 | |
noutram | 0:83a9911ab6ec | 57 | //ISR for the ticker - simply there to perform sampling |
noutram | 0:83a9911ab6ec | 58 | void doSample1Hz() |
noutram | 0:83a9911ab6ec | 59 | { |
noutram | 0:83a9911ab6ec | 60 | //Toggle on board led |
noutram | 0:83a9911ab6ec | 61 | led = !led; |
noutram | 0:83a9911ab6ec | 62 | |
noutram | 0:83a9911ab6ec | 63 | //READ ADC as an unsigned integer. |
noutram | 0:83a9911ab6ec | 64 | //Shift right 4 bits (this is a 12bit ADC) & store in static global variable |
noutram | 0:83a9911ab6ec | 65 | sample16 = POT_ADC_In.read_u16() >> 4; |
noutram | 0:83a9911ab6ec | 66 | } |
noutram | 0:83a9911ab6ec | 67 | |
noutram | 0:83a9911ab6ec | 68 |