NUCLEO-F042K6 Simple demo with buttons intterupt changing PWM DC - with autoincrement when button pressed longer than 1s
Dependencies: mbed
main.cpp@3:5edb1ea41029, 2018-02-17 (annotated)
- Committer:
- vodsejak
- Date:
- Sat Feb 17 17:26:47 2018 +0000
- Revision:
- 3:5edb1ea41029
- Parent:
- 2:b5b8bda0bcda
v 3.0; added internal pull ups;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
vodsejak | 1:127786980842 | 1 | #include "mbed.h" // import of mbed library (required) |
vodsejak | 1:127786980842 | 2 | /******************************************************************************* |
vodsejak | 1:127786980842 | 3 | |
vodsejak | 1:127786980842 | 4 | EXAMPLE DESCRIPTION |
vodsejak | 1:127786980842 | 5 | |
vodsejak | 3:5edb1ea41029 | 6 | Uses two buttons connected to PA_0 - plus and PA_1 - minus with internal pull |
vodsejak | 3:5edb1ea41029 | 7 | up resistors. On PA_8 is PWM output with variable duty cycle (DC) and frequncy |
vodsejak | 3:5edb1ea41029 | 8 | of 500 Hz.DC can be changed true buttons. On press of button DC is incremented |
vodsejak | 1:127786980842 | 9 | or decremented by 0.01. Interrupt is attached to falling edge on appropriate |
vodsejak | 1:127786980842 | 10 | pin and process of incrementation/decrementation is driven throu this |
vodsejak | 1:127786980842 | 11 | interrupt. Programm also demonstrates auto incrementation/decrementation while |
vodsejak | 1:127786980842 | 12 | button is pressed for longer than 1 s. After button is pressed timer is |
vodsejak | 1:127786980842 | 13 | started. Ticker periodically (every 10 ms) calls function that checks timer |
vodsejak | 1:127786980842 | 14 | value. If the timer had counted more than 1 s, It is stopped and auto |
vodsejak | 1:127786980842 | 15 | incementation/decrementation is stared (+-0.01 every 500 ms). After button |
vodsejak | 1:127786980842 | 16 | release auto incementation/decrementation is stopped and timer reset. |
vodsejak | 1:127786980842 | 17 | Programm is written in way, that there can NOT be simultaneously active both |
vodsejak | 1:127786980842 | 18 | auto incrementation and decrementation. Note that there are also prints to |
vodsejak | 1:127786980842 | 19 | UART to check states of buttons and change of DC. |
vodsejak | 1:127786980842 | 20 | |
vodsejak | 1:127786980842 | 21 | *******************************************************************************/ |
vodsejak | 0:602adf33625c | 22 | |
vodsejak | 3:5edb1ea41029 | 23 | InterruptIn buttonPlus(PA_0); // deffition of interrupt |
vodsejak | 0:602adf33625c | 24 | InterruptIn buttonMinus(PA_1); // deffition of interrupt |
vodsejak | 0:602adf33625c | 25 | |
vodsejak | 0:602adf33625c | 26 | PwmOut PWM(PA_8); // definition of PWM pin |
vodsejak | 0:602adf33625c | 27 | |
vodsejak | 0:602adf33625c | 28 | Timer tim; // Timer definition |
vodsejak | 0:602adf33625c | 29 | |
vodsejak | 1:127786980842 | 30 | Ticker tick; // Ticker definition |
vodsejak | 1:127786980842 | 31 | |
vodsejak | 1:127786980842 | 32 | bool autoIncrement=false; // true - do autoincrement |
vodsejak | 1:127786980842 | 33 | // false - do not autoincrement |
vodsejak | 0:602adf33625c | 34 | |
vodsejak | 1:127786980842 | 35 | bool plus=true; // true - auto increment |
vodsejak | 1:127786980842 | 36 | // false - auto decrement |
vodsejak | 0:602adf33625c | 37 | |
vodsejak | 1:127786980842 | 38 | // called after falling edge on PA_0 |
vodsejak | 1:127786980842 | 39 | // starts timer for auto increment check |
vodsejak | 1:127786980842 | 40 | // increments DC |
vodsejak | 0:602adf33625c | 41 | void pressedPlus() { |
vodsejak | 0:602adf33625c | 42 | tim.start(); // start Timer |
vodsejak | 1:127786980842 | 43 | plus=true; // incrementation |
vodsejak | 0:602adf33625c | 44 | if (PWM.read()+0.01 <= 1) { |
vodsejak | 1:127786980842 | 45 | PWM.write(PWM.read()+0.01); |
vodsejak | 3:5edb1ea41029 | 46 | printf("Presed plus button. DC: %f.\n",PWM.read()); |
vodsejak | 0:602adf33625c | 47 | }else{ |
vodsejak | 0:602adf33625c | 48 | PWM.write(1); |
vodsejak | 3:5edb1ea41029 | 49 | printf("Presed plus button. Already maximum DC.\n"); |
vodsejak | 0:602adf33625c | 50 | } |
vodsejak | 0:602adf33625c | 51 | } |
vodsejak | 0:602adf33625c | 52 | |
vodsejak | 1:127786980842 | 53 | // called after rising edge on PA_0 |
vodsejak | 1:127786980842 | 54 | // stops and resets auto increment timer |
vodsejak | 1:127786980842 | 55 | // stops auto increment |
vodsejak | 0:602adf33625c | 56 | void releasedPlus(){ |
vodsejak | 0:602adf33625c | 57 | tim.stop(); |
vodsejak | 0:602adf33625c | 58 | tim.reset(); |
vodsejak | 0:602adf33625c | 59 | autoIncrement=false; |
vodsejak | 3:5edb1ea41029 | 60 | printf("Plus button released.\n"); |
vodsejak | 0:602adf33625c | 61 | } |
vodsejak | 0:602adf33625c | 62 | |
vodsejak | 1:127786980842 | 63 | // called after falling edge on PA_1 |
vodsejak | 1:127786980842 | 64 | // starts timer for auto decrement check |
vodsejak | 1:127786980842 | 65 | // decrements DC |
vodsejak | 0:602adf33625c | 66 | void pressedMinus() { |
vodsejak | 0:602adf33625c | 67 | tim.start(); // start Timer |
vodsejak | 1:127786980842 | 68 | plus=false; // decrementation |
vodsejak | 0:602adf33625c | 69 | if (PWM.read()-0.01 >= 0) { |
vodsejak | 0:602adf33625c | 70 | PWM.write(PWM.read()-0.01); |
vodsejak | 3:5edb1ea41029 | 71 | printf("Presed minus button. DC: %f\n",PWM.read()); |
vodsejak | 0:602adf33625c | 72 | }else{ |
vodsejak | 0:602adf33625c | 73 | PWM.write(0); |
vodsejak | 3:5edb1ea41029 | 74 | printf("Presed minus button. Already minimum DC.\n"); |
vodsejak | 0:602adf33625c | 75 | } |
vodsejak | 0:602adf33625c | 76 | } |
vodsejak | 0:602adf33625c | 77 | |
vodsejak | 1:127786980842 | 78 | // called after rising edge on PA_1 |
vodsejak | 1:127786980842 | 79 | // stops and resets auto decrement timer |
vodsejak | 1:127786980842 | 80 | // stops auto decrement |
vodsejak | 0:602adf33625c | 81 | void releasedMinus(){ |
vodsejak | 0:602adf33625c | 82 | tim.stop(); |
vodsejak | 0:602adf33625c | 83 | tim.reset(); |
vodsejak | 0:602adf33625c | 84 | autoIncrement=false; |
vodsejak | 3:5edb1ea41029 | 85 | printf("Minus button released.\n"); |
vodsejak | 0:602adf33625c | 86 | } |
vodsejak | 0:602adf33625c | 87 | |
vodsejak | 1:127786980842 | 88 | |
vodsejak | 1:127786980842 | 89 | // checks timer value |
vodsejak | 1:127786980842 | 90 | // if > 1 s starts auto incerement/decrement |
vodsejak | 0:602adf33625c | 91 | void checkTimer(){ |
vodsejak | 0:602adf33625c | 92 | if(tim.read_ms()>1000){ |
vodsejak | 0:602adf33625c | 93 | tim.stop(); |
vodsejak | 0:602adf33625c | 94 | autoIncrement=true; |
vodsejak | 0:602adf33625c | 95 | } |
vodsejak | 0:602adf33625c | 96 | } |
vodsejak | 0:602adf33625c | 97 | |
vodsejak | 0:602adf33625c | 98 | int main() |
vodsejak | 0:602adf33625c | 99 | { |
vodsejak | 0:602adf33625c | 100 | |
vodsejak | 0:602adf33625c | 101 | // Set PWM |
vodsejak | 0:602adf33625c | 102 | PWM.period_ms(2); // 500 Hz |
vodsejak | 0:602adf33625c | 103 | PWM.write(0); // duration of active pulse |
vodsejak | 0:602adf33625c | 104 | |
vodsejak | 1:127786980842 | 105 | // Set buttons interrupts |
vodsejak | 0:602adf33625c | 106 | buttonPlus.fall(&pressedPlus); |
vodsejak | 0:602adf33625c | 107 | buttonMinus.fall(&pressedMinus); |
vodsejak | 0:602adf33625c | 108 | buttonPlus.rise(&releasedPlus); |
vodsejak | 0:602adf33625c | 109 | buttonMinus.rise(&releasedMinus); |
vodsejak | 0:602adf33625c | 110 | |
vodsejak | 3:5edb1ea41029 | 111 | // internal pull ups |
vodsejak | 3:5edb1ea41029 | 112 | buttonPlus.mode(PullUp); |
vodsejak | 3:5edb1ea41029 | 113 | buttonMinus.mode(PullUp); |
vodsejak | 3:5edb1ea41029 | 114 | |
vodsejak | 1:127786980842 | 115 | // set ticker interrupt |
vodsejak | 0:602adf33625c | 116 | tick.attach(&checkTimer,0.01); |
vodsejak | 0:602adf33625c | 117 | |
vodsejak | 0:602adf33625c | 118 | while (1) { |
vodsejak | 1:127786980842 | 119 | // check if auto increment/decrement |
vodsejak | 0:602adf33625c | 120 | if(autoIncrement && plus && PWM.read()+0.01 <= 1){ |
vodsejak | 0:602adf33625c | 121 | PWM.write(PWM.read()+0.01); |
vodsejak | 3:5edb1ea41029 | 122 | printf("Autoincrement. DC: %f.\n",PWM.read()); |
vodsejak | 0:602adf33625c | 123 | } |
vodsejak | 0:602adf33625c | 124 | else if(autoIncrement && !plus && PWM.read()-0.01 >= 0){ |
vodsejak | 0:602adf33625c | 125 | PWM.write(PWM.read()-0.01); |
vodsejak | 3:5edb1ea41029 | 126 | printf("Autodecrement. DC: %f.\n",PWM.read()); |
vodsejak | 0:602adf33625c | 127 | } |
vodsejak | 0:602adf33625c | 128 | wait_ms(500); |
vodsejak | 0:602adf33625c | 129 | } |
vodsejak | 0:602adf33625c | 130 | } |