Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp@2:535d154caa12, 2019-11-12 (annotated)
- Committer:
- reedas
- Date:
- Tue Nov 12 10:16:26 2019 +0000
- Revision:
- 2:535d154caa12
- Parent:
- 1:768b8bd42e33
CITY1082 debounced button example
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
4180_1 | 0:cc87c48aa43c | 1 | #include "mbed.h" |
4180_1 | 0:cc87c48aa43c | 2 | #include "PinDetect.h" |
4180_1 | 0:cc87c48aa43c | 3 | // must import Cookbook PinDetct library into project |
4180_1 | 0:cc87c48aa43c | 4 | // URL: http://mbed.org/users/AjK/libraries/PinDetect/lkyxpw |
4180_1 | 0:cc87c48aa43c | 5 | |
4180_1 | 0:cc87c48aa43c | 6 | DigitalOut myled(LED1); |
reedas | 2:535d154caa12 | 7 | DigitalOut myled2(LED5); |
reedas | 2:535d154caa12 | 8 | DigitalOut myled3(LED4); |
reedas | 2:535d154caa12 | 9 | DigitalOut myled4(LED3); |
4180_1 | 0:cc87c48aa43c | 10 | |
reedas | 2:535d154caa12 | 11 | PinDetect pb1(P0_4); |
reedas | 2:535d154caa12 | 12 | //PinDetect pb2(p7); |
4180_1 | 0:cc87c48aa43c | 13 | // SPST Pushbutton debounced count demo using interrupts and callback |
4180_1 | 0:cc87c48aa43c | 14 | // no external PullUp resistor needed |
4180_1 | 0:cc87c48aa43c | 15 | // Pushbutton from P8 to GND. |
4180_1 | 1:768b8bd42e33 | 16 | // Second Pushbutton from P7 to GND. |
4180_1 | 0:cc87c48aa43c | 17 | // A pb hit generates an interrupt and activates the callback function |
4180_1 | 0:cc87c48aa43c | 18 | // after the switch is debounced |
4180_1 | 0:cc87c48aa43c | 19 | |
4180_1 | 0:cc87c48aa43c | 20 | // Global count variable |
reedas | 2:535d154caa12 | 21 | int volatile countit=0; |
4180_1 | 0:cc87c48aa43c | 22 | |
4180_1 | 1:768b8bd42e33 | 23 | // Callback routine is interrupt activated by a debounced pb1 hit |
4180_1 | 1:768b8bd42e33 | 24 | void pb1_hit_callback (void) { |
reedas | 2:535d154caa12 | 25 | // printf("Count is %d\n", ++countit); |
reedas | 2:535d154caa12 | 26 | countit--; |
reedas | 2:535d154caa12 | 27 | myled4 = countit & 0x01; |
reedas | 2:535d154caa12 | 28 | myled3 = (countit & 0x02)>>1; |
reedas | 2:535d154caa12 | 29 | myled2 = (countit & 0x04)>>2; |
4180_1 | 0:cc87c48aa43c | 30 | } |
4180_1 | 1:768b8bd42e33 | 31 | // Callback routine is interrupt activated by a debounced pb2 hit |
reedas | 2:535d154caa12 | 32 | /*void pb2_hit_callback (void) { |
4180_1 | 1:768b8bd42e33 | 33 | count--; |
4180_1 | 1:768b8bd42e33 | 34 | myled4 = count & 0x01; |
4180_1 | 1:768b8bd42e33 | 35 | myled3 = (count & 0x02)>>1; |
4180_1 | 1:768b8bd42e33 | 36 | myled2 = (count & 0x04)>>2; |
reedas | 2:535d154caa12 | 37 | }*/ |
4180_1 | 0:cc87c48aa43c | 38 | int main() { |
4180_1 | 0:cc87c48aa43c | 39 | |
4180_1 | 1:768b8bd42e33 | 40 | // Use internal pullups for pushbutton |
4180_1 | 1:768b8bd42e33 | 41 | pb1.mode(PullUp); |
reedas | 2:535d154caa12 | 42 | // pb2.mode(PullUp); |
4180_1 | 0:cc87c48aa43c | 43 | // Delay for initial pullup to take effect |
4180_1 | 0:cc87c48aa43c | 44 | wait(.01); |
4180_1 | 1:768b8bd42e33 | 45 | // Setup Interrupt callback functions for a pb hit |
4180_1 | 1:768b8bd42e33 | 46 | pb1.attach_deasserted(&pb1_hit_callback); |
reedas | 2:535d154caa12 | 47 | // pb2.attach_deasserted(&pb2_hit_callback); |
4180_1 | 1:768b8bd42e33 | 48 | // Start sampling pb inputs using interrupts |
4180_1 | 1:768b8bd42e33 | 49 | pb1.setSampleFrequency(); |
reedas | 2:535d154caa12 | 50 | // pb2.setSampleFrequency(); |
4180_1 | 0:cc87c48aa43c | 51 | //Blink myled in main routine forever while responding to pb changes |
4180_1 | 0:cc87c48aa43c | 52 | // via interrupts that activate the callback counter function |
4180_1 | 0:cc87c48aa43c | 53 | while (1) { |
4180_1 | 0:cc87c48aa43c | 54 | myled = !myled; |
4180_1 | 0:cc87c48aa43c | 55 | wait(.5); |
4180_1 | 0:cc87c48aa43c | 56 | } |
4180_1 | 0:cc87c48aa43c | 57 | |
4180_1 | 0:cc87c48aa43c | 58 | } |