This blog/site is primarily to support the courses that I teach in digital design in particular embedded digital systems design using C and the Arduino MEGA hardware. I decided to post the course materials and code snippets here because:
a) Much of the knowledge, techniques and tools, that are used by real practitioners of embedded design, are freely available on the web and I have made extensive use of these in putting this material together. In publishing this blog I hope to also contribute to this community.
b) WordPress natively supports syntax highlighting of code snippets using the
[sourcecode language=”css”] your code here [/sourcecode] tags and makes even the work of an electronic engineer look beautiful.
The material here has been developed in C using AVR Studio 5 and the Arduino Mega 2560 microcontroller board.
For a comprehensive introduction to the need for switch debouncing in microprocessor applications and approaches to doing so. Take a look at this excellent guide by The Ganssle Group.
A simple software debounce routine for debouncing the press of an active low switch, such as a momentary push switch directly attached from the input pin (with pullups enabled) of the microcontroller down to ground, is shown below. In this case the switch is attached to pin0 on port B.
static char count=0;
if (PINB & (1<<PB0)) count=0;
if (count > 10) return 1;
else return 0;
This code simply implements a counter that requires the pin to be in the logic zero state for 10 successive samples, before returning a one. The count can be increased or decreased as necessary to ensure robust operation of the switch. Software debouncing can also be realised in an more efficient form by simply using a char variable and a logical shift left to count the successive samples. In this implementation the maximum count is 8.
static char state=0xFF;
state=(state << 1)|(PINB & (1 << PB0));
if state==0 return 1;
else return 0;