![](/media/cache/group/Embedded_software_in_context_icon.png.50x50_q85.png)
Task 4.3.3 Solution
Dependencies: mbed
main.cpp@0:5c5a623dc9e0, 2015-09-24 (annotated)
- Committer:
- noutram
- Date:
- Thu Sep 24 12:31:08 2015 +0000
- Revision:
- 0:5c5a623dc9e0
Initial version 24-09-2015
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
noutram | 0:5c5a623dc9e0 | 1 | #include "mbed.h" |
noutram | 0:5c5a623dc9e0 | 2 | |
noutram | 0:5c5a623dc9e0 | 3 | //Function prototype |
noutram | 0:5c5a623dc9e0 | 4 | void doSample1Hz(); |
noutram | 0:5c5a623dc9e0 | 5 | void printBinary12(unsigned short u16); |
noutram | 0:5c5a623dc9e0 | 6 | |
noutram | 0:5c5a623dc9e0 | 7 | //Global objects |
noutram | 0:5c5a623dc9e0 | 8 | Serial pc(USBTX, USBRX); |
noutram | 0:5c5a623dc9e0 | 9 | AnalogIn POT_ADC_In(A0); |
noutram | 0:5c5a623dc9e0 | 10 | DigitalOut led(LED1); |
noutram | 0:5c5a623dc9e0 | 11 | |
noutram | 0:5c5a623dc9e0 | 12 | //Shared variables |
noutram | 0:5c5a623dc9e0 | 13 | volatile static unsigned short sample16 = 0; |
noutram | 0:5c5a623dc9e0 | 14 | |
noutram | 0:5c5a623dc9e0 | 15 | //The ticker, used to sample data at a fixed rate |
noutram | 0:5c5a623dc9e0 | 16 | Ticker t; |
noutram | 0:5c5a623dc9e0 | 17 | |
noutram | 0:5c5a623dc9e0 | 18 | //Main function |
noutram | 0:5c5a623dc9e0 | 19 | int main() |
noutram | 0:5c5a623dc9e0 | 20 | { |
noutram | 0:5c5a623dc9e0 | 21 | //Set baud rate to 115200 |
noutram | 0:5c5a623dc9e0 | 22 | pc.baud(115200); |
noutram | 0:5c5a623dc9e0 | 23 | |
noutram | 0:5c5a623dc9e0 | 24 | //Set up the ticker - 100Hz |
noutram | 0:5c5a623dc9e0 | 25 | t.attach(doSample1Hz, 1.0); |
noutram | 0:5c5a623dc9e0 | 26 | |
noutram | 0:5c5a623dc9e0 | 27 | while(1) { |
noutram | 0:5c5a623dc9e0 | 28 | |
noutram | 0:5c5a623dc9e0 | 29 | //Sleep |
noutram | 0:5c5a623dc9e0 | 30 | sleep(); |
noutram | 0:5c5a623dc9e0 | 31 | |
noutram | 0:5c5a623dc9e0 | 32 | //Displauy the sample in HEX |
noutram | 0:5c5a623dc9e0 | 33 | printBinary12(sample16); |
noutram | 0:5c5a623dc9e0 | 34 | |
noutram | 0:5c5a623dc9e0 | 35 | } //end while(1) |
noutram | 0:5c5a623dc9e0 | 36 | } //end main |
noutram | 0:5c5a623dc9e0 | 37 | |
noutram | 0:5c5a623dc9e0 | 38 | //ISR for the ticker - simply there to perform sampling |
noutram | 0:5c5a623dc9e0 | 39 | void doSample1Hz() |
noutram | 0:5c5a623dc9e0 | 40 | { |
noutram | 0:5c5a623dc9e0 | 41 | //Toggle on board led |
noutram | 0:5c5a623dc9e0 | 42 | led = !led; |
noutram | 0:5c5a623dc9e0 | 43 | |
noutram | 0:5c5a623dc9e0 | 44 | //READ ADC as an unsigned integer. |
noutram | 0:5c5a623dc9e0 | 45 | //Shift right 4 bits (this is a 12bit ADC) & store in static global variable |
noutram | 0:5c5a623dc9e0 | 46 | sample16 = POT_ADC_In.read_u16() >> 4; |
noutram | 0:5c5a623dc9e0 | 47 | } |
noutram | 0:5c5a623dc9e0 | 48 | |
noutram | 0:5c5a623dc9e0 | 49 | //Function to print a 12-bit number in the binary format |
noutram | 0:5c5a623dc9e0 | 50 | //(I've kept this simple to avoid confusion) |
noutram | 0:5c5a623dc9e0 | 51 | void printBinary12(unsigned short u16) |
noutram | 0:5c5a623dc9e0 | 52 | { |
noutram | 0:5c5a623dc9e0 | 53 | unsigned int mask = 0x800; //Binary 1000000000000000 |
noutram | 0:5c5a623dc9e0 | 54 | for (unsigned int n=0; n<12; n++) { |
noutram | 0:5c5a623dc9e0 | 55 | //Test to see if the bit is set |
noutram | 0:5c5a623dc9e0 | 56 | if (u16 & mask) { |
noutram | 0:5c5a623dc9e0 | 57 | pc.printf("1"); |
noutram | 0:5c5a623dc9e0 | 58 | } else { |
noutram | 0:5c5a623dc9e0 | 59 | pc.printf("0"); |
noutram | 0:5c5a623dc9e0 | 60 | } |
noutram | 0:5c5a623dc9e0 | 61 | //Shift mask 1 bit right |
noutram | 0:5c5a623dc9e0 | 62 | mask >>= 1; |
noutram | 0:5c5a623dc9e0 | 63 | } |
noutram | 0:5c5a623dc9e0 | 64 | pc.printf("\n"); |
noutram | 0:5c5a623dc9e0 | 65 | } |