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
- Committer:
- reedas
- Date:
- 2019-11-12
- Revision:
- 2:535d154caa12
- Parent:
- 1:768b8bd42e33
File content as of revision 2:535d154caa12:
#include "mbed.h"
#include "PinDetect.h"
// must import Cookbook PinDetct library into project
// URL: http://mbed.org/users/AjK/libraries/PinDetect/lkyxpw
DigitalOut myled(LED1);
DigitalOut myled2(LED5);
DigitalOut myled3(LED4);
DigitalOut myled4(LED3);
PinDetect pb1(P0_4);
//PinDetect pb2(p7);
// SPST Pushbutton debounced count demo using interrupts and callback
// no external PullUp resistor needed
// Pushbutton from P8 to GND.
// Second Pushbutton from P7 to GND.
// A pb hit generates an interrupt and activates the callback function
// after the switch is debounced
// Global count variable
int volatile countit=0;
// Callback routine is interrupt activated by a debounced pb1 hit
void pb1_hit_callback (void) {
// printf("Count is %d\n", ++countit);
countit--;
myled4 = countit & 0x01;
myled3 = (countit & 0x02)>>1;
myled2 = (countit & 0x04)>>2;
}
// Callback routine is interrupt activated by a debounced pb2 hit
/*void pb2_hit_callback (void) {
count--;
myled4 = count & 0x01;
myled3 = (count & 0x02)>>1;
myled2 = (count & 0x04)>>2;
}*/
int main() {
// Use internal pullups for pushbutton
pb1.mode(PullUp);
// pb2.mode(PullUp);
// Delay for initial pullup to take effect
wait(.01);
// Setup Interrupt callback functions for a pb hit
pb1.attach_deasserted(&pb1_hit_callback);
// pb2.attach_deasserted(&pb2_hit_callback);
// Start sampling pb inputs using interrupts
pb1.setSampleFrequency();
// pb2.setSampleFrequency();
//Blink myled in main routine forever while responding to pb changes
// via interrupts that activate the callback counter function
while (1) {
myled = !myled;
wait(.5);
}
}