3 Simple Ways to
Share What You Make

With Instructables you can share what you make with the world — and tap into an ever-growing community of creative experts.

PhotosPhotos

Share one or more photos of a project, recipe, or whatever you've made, quickly and easily.

Step by StepStep-By-Step

Share your step-by-step photos with text instructions of what you made so others can do it too!

VideoVideo

Share your how-to video. You'll need your embed code from a video site such as YouTube.

Reading Switches with ATtiny2313

Step 7Using Four Switches

As we implement the beeper, let's extend our hardware and software to handle four switches. Since the output of Timer Counter 0A is on Port B, pin 2, we can't simply hook up more switches sequentially to Port B. An easy solution would be to use Port D, but let's keep that port available for other functions (perhaps a stepper motor). So let's hook up the additional switches to PB3 and PB4.

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 StepDownload PDFView All StepsNext Step »

Pro

Get More Out of Instructables

Already have an Account?

close

All Steps Viewing
View all steps of an Instructable on the same page when you're a Pro Member.

Upgrade to Pro today!
41
Followers
8
Author:doctek