Home Projects AVR Programming Guides Electrical Fundamentals Insteon Saving Electricity

Recent Updates

EasyGrow Greenhouse
Follow our adventure of assembling an 8x12 EasyGrow Greenhouse.
AVR Programming Guides
We have some nice AVR code samples to share. Lots of AVR goodies like timers, interrupts, and pin I/O for the beginner.
Watts Tables
Use these tables to help you determine how much various items in your house cost you by the day, month, and year.
Saving Electricity
A list of a variety of ways to help you save money by learning how much it costs to run things.
What is a Kilowatt Hour
Saving money on your electric bill starts with understanding what a kilowatt hour is.
How To Measure Watts
Learn a variety of ways to measure watts in your house.
Read Your Power Meter
By reading your power meter you can get a good idea of how much various electrical devices cost you.
Electronics Fundamentals
Learn some basic electronics fundamentals and see if you enjoy the field of electronics.














AVR Port Input in C

Getting Started

If you are just stumbling on this page, you might want to check out the entire series of AVR related programming guides here.

Connect PortB To Switches

Remember how we connected PORTA to the LEDS? Now we are going to connect PORTB to the SWITCHES. Just be sure that you connect pin 1 of PORTB also known as PB0 to SW0 by orienting the cable so that the red stripe is on the correct side on both headers.

AVR Port Input

Before you can read input from a port on an AVR microcontroller you need to configure the pins that you are going to read to be input pins. In our AVR Port Output Using C guide we talked about the Data Direction Registers. Review that guide if you are not familiar with the concept.

You should also write 0's out to PORTB to ensure that it is in high impedance mode. If PORTB has 1's in it, then the AVR will source current if pulled externally low. Lets set our PORTB up as an input port.

// setup PORTB data direction as an input

DDRB = 0;

// make sure it is high impedance and will not source

PORTB = 0;

Map Switches to LED's

For a really simple test application, lets map the PORTB switch inputs to the PORTA lights. This way, any time you press a button on the STK500, the corresponding LED will turn on. Remember that the LEDs on the STK500 are wired in reverse logic. So are the switches. This means that an open switch is feeding a 1 in to the corresponding pin, which will send a 1 out to the corresponding LED and that LED will be off. When you press a switch a 0 will be read in, which is sent out PORTA and the LED will light up.

To read the input from a pin, you read the PINx value. In this case, we are going to read PINB and map it to PORTA, like this.

PORTA = PINB;

Do It In The Loop

If you just put the above instruction in your program outside of a loop, then it would only happen once, and your PORTA lights will reflect the state of your PINB inputs at the time you turn on the STK500. To make the LEDs mimic the switches for ever, put the above line inside your main loop.

Below is the complete code to map PINB switches to PORTA LEDs.

/********************************************************************************
Includes
********************************************************************************/

#include <avr/io.h>
#include <stdbool.h>


/********************************************************************************
Main
********************************************************************************/


int main(void) {

    // configure PORTA as output

    DDRA = 0xFF;

    // configure PORTB as input

    DDRB = 0;

    // make sure PORTB is high impedance and will not source

    PORTB = 0;

    // main loop

    while (true) {

        // map PINB switches to PORTA

        PORTA = PINB;

    }

}

You can download the complete source code here.

Inverting the Values

If you want to invert the lights so that they are all on, and only turn off when you press a switch, simply replace PORTA = PINB; with this:

PORTA = ~PINB;

The tilde ~ will invert the inputs from PINB before mapping them out to PORTA.

Making Decisions

Now that we can read the input from an AVR pin, we can start doing some AVR Port Input Decision Making.

Or head back to our index of AVR Guides here.

Efundies.com - Making Electronics Fun
 
Saturday, 31-Jul-2010 03:49:55 PDT