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

Reading Switches with ATtiny2313
There have been several Instructables dealing with outputs from the ATtiny2313 and similar AVR devices. For example, http://www.instructables.com/id/Ghetto-Programming%3a-Getting-started-with-AVR-micro/, http://www.instructables.com/id/Drive-a-Stepper-Motor-with-an-AVR-Microprocessor/. Working on the latest one from The Real Elliot, which showed how to control stepper motors, I found that it would be really helpful to be able to run alternate sections of code in the same program so I didn't have to reprogram the ATtiny2313 each time I wanted to try a slight code variation (such as half-stepping or running the stepper in reverse). While it is easy to write code using a switch/case statement to allow selection of alternate variations, some way of selecting the case is needed. That means some sort of input device has to be read to control the case.

Fortunately, the ATtiny2313 has plenty of I/O pins and is well-designed for reading inputs from switches. This Instructable will show how to read inputs and make decisions based on their state. Since that alone would make a pretty boring Instructable, I'll explain a simple way of using the timer/counter capability of the ATtiny2313 to drive a small speaker as a beeper. There will also be a small digression on simple debugging techniques.
 
Remove these adsRemove these ads by Signing Up
 

Step 1The Input Device

The Input Device
This Instructable builds on the excellent work of The Real Elliot and uses the ATtiny2313 Ghetto development system he describes. The ATtiny2313 data sheet from Atmel is the ultimate reference for all functions, but it is not necessarily easy to read. http://www.atmel.com/dyn/products/datasheets.asp?family_id=607 (Link has all AVR data sheets, locate the 2313.)

The figure shows a simple set of input switches. This is simply a package of four on/off switches; also known as single pole, single throw switches (SPST). Typically, one connection, or pole, of each switch is tied to ground while the other connection is pulled high through a current limiting resistor (10K or so). A microcontroller input is connected to the pole with the resistor. If the switch is open, the microcontroller will read the input as HI. If the switch is closed, the microcontroller will read the input LO. Refer to the schematic for details.

The ATtiny2313 simplifies things by providing programmable pull-up resistors on I/O pins when they are configured as inputs. This means that the switches can simply have one pole tied to ground (LO) and the other pole connected to a processor input. The first example shows only two switches. The switches are read and configured with the following code.

Configure the switches as inputs:
(No code required; this is the default.)
Turn on the pull-up resistors:
PORTB = _BV(PB0) | _BV(PB1) ;
Read the inputs:
but1 = ~PINB & 0x03;
Note use of inversion and masking to get correct value.
« Previous StepDownload PDFView All StepsNext Step »
7 comments
Aug 1, 2010. 12:41 AMnm17 says:
Great instructable, thanks! Any suggestions on a next one to learn more programming?
Feb 3, 2009. 2:13 PMsotsirh194 says:
How would you read a digital input like one 0 to 255, like that from a digital sensor or adc.
Feb 4, 2009. 2:32 PMsotsirh194 says:
an external adc connected to a sharp infrared sensor.
Feb 4, 2009. 4:19 PMsotsirh194 says:
this is the one from sparkfun
ADC

Sep 7, 2008. 8:08 AMtwenglish1 says:
awesome instructible i have been trying to do this for a while thank you!!!

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