|
Content: By Guido Socher |
An AVR microcontroller based electronic dice
Abstract:
Travel to outer space sounds very exciting but now we are here
in our space ship and we have about 10 square meters for 5 people.
It will take another week until we reach the first space station.
I took my mp3 player with me and after so many days of travel
I have heard every song at least a dozen times. |
unsigned char i=0;
initLEDports();
DDRB &= ~(1<<PINB0); // input line
PORTB|= (1<<PINB0); // internal pullup resistor on
while (1) {
if (bit_is_clear(PINB,PINB0)){
// button pressed, switch off the LEDs and count
allLEDsOff();
i++;
i %= 6; // numbers from 0 to 5
}else{
// no button press. display result:
displayNumber(i);
}
}
What you can learn here is how to check if a button was pressed. The button
is connected between pin PB0 and GND. After initializing the LED connections
as output (initLEDports) we write a zero to the data direction register on
the position responsible for PB0. This causes the pin to be a digital input line.
There is also the possibility to switch on an internal pull-up resistor such
that the input line has a defined state when nothing is connected.
Now we enter an endless loop (while(1)) and check the state of the button all
the time.
bit_is_clear(PINB,PINB0) reads the input line and checks if zero volts are
detected. This happens when the button is pressed.
2007-07-30, generated by tuxgrparser version 2.55