Instructables
Picture of 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 1: The Input Device

Picture of The Input Device
SwBeep.jpg
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.
mickcaulton4 months ago

Well explained - You make it very easy and clear to understand for someone getting started with micro controllers.

nm174 years ago
Great instructable, thanks! Any suggestions on a next one to learn more programming?
sotsirh1945 years ago
How would you read a digital input like one 0 to 255, like that from a digital sensor or adc.
doctek (author)  sotsirh1945 years ago
Good question, but I need a little more detail. Can you give me an example of the sort of sensor you'd like to read? There's a lot of different interfaces used so I can't really give you a generic answer.
an external adc connected to a sharp infrared sensor.
doctek (author)  sotsirh1945 years ago
You still need to tell me more about the ADC. You could be using one with a data bus, or one that uses the I2C bus. Also, how do you clock the ADC? What rate is going to run at? I hate to put you off here, but I can't really give you any details without knowing what you're using. A specific part from a specific vendor would be best, or at least a few alternatives.
this is the one from sparkfun
ADC

twenglish16 years ago
awesome instructible i have been trying to do this for a while thank you!!!