Step 7Using Four Switches
Reading the switches is mostly unchanged. The mask value is changed to 0x1B (00011011 binary) to mask bit 2 along with 5, 6, and 7. One further trick is used to create a 4-bit binary number. Shift bits 3 and 4 right one bit and combine them with bits 0 and 1 into a 4 bit binary number. This is standard C syntax for shifting and combining bits, but may not be well known to the novice.
but1a = (but1 & 0x03) | ((but1 & 0x18) >> 1); // but1 has switch reading
In operation, the program will blink twice and beep twice to signal initialization. Anytime the switches are changed, the number they represent will be beeped. When the switches aren't changing, the LED will blink.
To run this code, create a new directory (call it Beep if you like) and download the following C code file and makefile into it. Rename Makefile2.txt to just Makefile. Using WinAVR, compile the program and load it into your Attiny2313.
| « Previous Step | Download PDFView All Steps | Next Step » |
![]() |
Add Comment
|








































