Vincent Cheung
/
HelloWorld
h
Fork of HelloWorld by
main.cpp@4:f0a11480f39f, 2016-11-14 (annotated)
- Committer:
- oopakhooo
- Date:
- Mon Nov 14 19:59:54 2016 +0000
- Revision:
- 4:f0a11480f39f
- Parent:
- 3:641eefd1110b
- Child:
- 5:afabac4fce1b
serial
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
simon | 0:fb6bbc10ffa0 | 1 | #include "mbed.h" |
Judorunner | 2:e2ae43e8acab | 2 | #include "PaceHeart.h" |
simon | 0:fb6bbc10ffa0 | 3 | |
oopakhooo | 3:641eefd1110b | 4 | //DigitalOut output_pin_A(LED1); |
simon | 0:fb6bbc10ffa0 | 5 | |
oopakhooo | 4:f0a11480f39f | 6 | PaceHeart* Pacer = new PaceHeart; |
oopakhooo | 4:f0a11480f39f | 7 | DigitalOut led(LED1); |
oopakhooo | 4:f0a11480f39f | 8 | Serial s(USBTX, USBRX); |
oopakhooo | 4:f0a11480f39f | 9 | void baud(int baudrate) { |
oopakhooo | 4:f0a11480f39f | 10 | |
oopakhooo | 4:f0a11480f39f | 11 | s.baud(baudrate); |
oopakhooo | 4:f0a11480f39f | 12 | } |
oopakhooo | 4:f0a11480f39f | 13 | |
Judorunner | 2:e2ae43e8acab | 14 | |
simon | 0:fb6bbc10ffa0 | 15 | int main() { |
oopakhooo | 4:f0a11480f39f | 16 | /* |
oopakhooo | 4:f0a11480f39f | 17 | baud(57600); |
oopakhooo | 4:f0a11480f39f | 18 | s.printf("Press 'u' to turn LED1 brightness up, 'd' to turn it down\n"); |
simon | 0:fb6bbc10ffa0 | 19 | while(1) { |
oopakhooo | 4:f0a11480f39f | 20 | char c = s.getc(); |
oopakhooo | 4:f0a11480f39f | 21 | pc.putc(pc.getc()); //echoes back input |
oopakhooo | 4:f0a11480f39f | 22 | if((c == 'u')) { |
oopakhooo | 4:f0a11480f39f | 23 | |
oopakhooo | 4:f0a11480f39f | 24 | led = !led; |
oopakhooo | 4:f0a11480f39f | 25 | } |
oopakhooo | 4:f0a11480f39f | 26 | if((c == 'd')) { |
oopakhooo | 4:f0a11480f39f | 27 | led = 0; |
oopakhooo | 4:f0a11480f39f | 28 | } |
oopakhooo | 4:f0a11480f39f | 29 | }*/ |
oopakhooo | 4:f0a11480f39f | 30 | Pacer-> pace_A(); |
oopakhooo | 4:f0a11480f39f | 31 | /* PwmOut out(PTA0); pulse width thingy |
oopakhooo | 4:f0a11480f39f | 32 | PwmIn in(PTD1); |
oopakhooo | 4:f0a11480f39f | 33 | float pe,pw,ds; |
oopakhooo | 4:f0a11480f39f | 34 | |
oopakhooo | 4:f0a11480f39f | 35 | // set the PwmOut in seconds |
oopakhooo | 4:f0a11480f39f | 36 | out.pulsewidth(2); |
oopakhooo | 4:f0a11480f39f | 37 | out.period(4); |
oopakhooo | 4:f0a11480f39f | 38 | |
oopakhooo | 4:f0a11480f39f | 39 | while (true) { |
oopakhooo | 4:f0a11480f39f | 40 | pe= in.period(); |
oopakhooo | 4:f0a11480f39f | 41 | pw= in.pulsewidth(); |
oopakhooo | 4:f0a11480f39f | 42 | ds= in.dutycycle(); |
oopakhooo | 4:f0a11480f39f | 43 | pc.printf("A period= %f, pulsewidth= %f, duty cycle= %f\n\r",pe, pw, ds); |
oopakhooo | 4:f0a11480f39f | 44 | |
oopakhooo | 4:f0a11480f39f | 45 | wait(1); |
oopakhooo | 4:f0a11480f39f | 46 | } |
oopakhooo | 3:641eefd1110b | 47 | |
oopakhooo | 4:f0a11480f39f | 48 | AnalogOut aout(p18); //sets up pin 18 as an analogue output |
oopakhooo | 4:f0a11480f39f | 49 | AnalogueIn ain(p15); //sets up pin 15 and an analogue input |
oopakhooo | 4:f0a11480f39f | 50 | |
oopakhooo | 4:f0a11480f39f | 51 | int main(){ |
oopakhooo | 4:f0a11480f39f | 52 | aout=0.5; //sets output to 0.5*VCC |
oopakhooo | 4:f0a11480f39f | 53 | while(1){ //sets up a loop |
oopakhooo | 4:f0a11480f39f | 54 | if (ain>0.3){ //tests whether the input is above 0.3 |
oopakhooo | 4:f0a11480f39f | 55 | aout=0; //sets the output to 0 |
oopakhooo | 4:f0a11480f39f | 56 | } |
oopakhooo | 4:f0a11480f39f | 57 | aout = 0.5; |
oopakhooo | 4:f0a11480f39f | 58 | wait(1); |
oopakhooo | 4:f0a11480f39f | 59 | aout = 0; |
oopakhooo | 4:f0a11480f39f | 60 | |
oopakhooo | 4:f0a11480f39f | 61 | } |
oopakhooo | 4:f0a11480f39f | 62 | |
oopakhooo | 4:f0a11480f39f | 63 | sine wave output |
oopakhooo | 4:f0a11480f39f | 64 | const double pi = 3.141592653589793238462; |
oopakhooo | 4:f0a11480f39f | 65 | const double amplitude = 0.5f; |
oopakhooo | 4:f0a11480f39f | 66 | const double offset = 65535/2; |
oopakhooo | 4:f0a11480f39f | 67 | double rads = 0.0; |
oopakhooo | 4:f0a11480f39f | 68 | uint16_t sample = 0; |
oopakhooo | 4:f0a11480f39f | 69 | |
oopakhooo | 4:f0a11480f39f | 70 | while(1) { |
oopakhooo | 4:f0a11480f39f | 71 | // sinewave output |
oopakhooo | 4:f0a11480f39f | 72 | for (int i = 0; i < 360; i++) { |
oopakhooo | 4:f0a11480f39f | 73 | rads = (pi * i) / 180.0f; |
oopakhooo | 4:f0a11480f39f | 74 | sample = (uint16_t)(amplitude * (offset * (cos(rads + pi))) + offset); |
oopakhooo | 4:f0a11480f39f | 75 | aout.write_u16(sample); |
oopakhooo | 4:f0a11480f39f | 76 | } |
oopakhooo | 4:f0a11480f39f | 77 | } |
oopakhooo | 4:f0a11480f39f | 78 | */ |
simon | 0:fb6bbc10ffa0 | 79 | } |