Up: Hands On AVR-GCC programming


Button

This program takes AVR-GCC to the next level of programming. If you carefully read the program you will find function prototypes for new functions as in structured programming. The new functions are checkButton() and setLED(). Also in this program two ports, PORTC and PORTG are being used. Note that PORTC is set as an input by using the function ' outb(DDRC, 0x00)'. '0x00' is a hexadecimal for '0000 0000'. This means that all pins in PORTC are set as INPUT. Getting familiar with hexadecimal might be useful while programming with AVR-GCC . On Fedora 8, Gnome calculator provides DEC-TO-HEX or HEX-TO-DEC conversion. The file,

 '~/avrlib/examples/basic_io/basiciotest.c'

gives a good introduction on using hexadecimal numbers for manipulating ports on ATMegas.

After reading and understanding objectives and description of this program, do the circuit with a button on a breadboard. There is a nice diagram and description for doing a button circuit on the Wiring documentation. Try to follow the steps outlined on the previous examples, First create a 'button' sub directory on the 'wiring' directory. Next copy 'global.h', and 'makefile' to this directory. Edit 'makefile' so that it reflects the name of this program. Understand which libraries this program is using, its purpose and make the changes in the 'makefile' lines accordingly.


 		 
 
//------------------------------------------------------
//------------------------------------------------------
//   
//  AVRLIB-DEMO
//  For avrlib and Wiring development board.
//  
//
//  File:     button.c
//  Author:   Juan Reyes    juanig-at-ccrma-stanford_edu
//  Based on: 'button.c' by Michael Gurevich \& Wendy Ju
//  Date:     June 25, 2008
//
//  Notes:
//-----------------------------------------------------
//  USING THE WIRING DEVELOPMENT BOARD, BUILT-IN LED
//  IN PIN 48 PORT D SHOULD LIGHT WHEN BUTTON PRESSED.
//
//-----------------------------------------------------
//  More information about Wiring board:
//
//  http://wiring.org.co/hardware/index.html
//  
//-----------------------------------------------------
//  Information about functions used here and avrlib:
// 
//  ~/avrlib/examples/basic_io/basiciotest.c
//  
//  
//
//*********************************************************
//   Description:
//   ============
//
//   This program will turn on/off the built in led (pin48) 
//   using a hardware swicth on a breadboard. For circuit 
//   diagram look at:
//   http://wiring.org.co/learning/examples/switch.html
//
//*********************************************************


//compiler includes
#include <avr/io.h>
#include <avr/interrupt.h>

//avrlib includes
#include "global.h"


//function prototypes:

int checkButton(int button);
void setLED(u08 whichLED, u08 on);

int main(void)
{

  //  PORTC (Port1 Wiring) is set as INPUT.
  outb(DDRC, 0x00);
  //  PORTG is set as OUTPUT.
  outb(DDRG, 0xFF);
	  

  //loop forever
  while (1) {
    
    setLED(0, checkButton(0));

	} 
  return 0;
}
int checkButton(button) {
  
  return (! bit_is_set(PINC,button));
  /* Logical negation is because when the button is pushed, the pin
     is drawn to ground, so the button is "on" when the bit is zero. */
}

void setLED(u08 whichLED, u08 on) {
  if (on) {
    //light the LED
    cbi(PORTG,whichLED);
  } else {
    //turn off the LED
    sbi(PORTG,whichLED);
  }
}

After editing 'makefile' lines reflecting, 'TRG' and 'SRC =', just compile and load hex files.

  1. Compile:

    
     		 [host] make        
    
  2. Press the '<reset>' button.

  3. Load to ATMega:

    
     		 [host] make load       
    
  4. Press the '<reset>' button. If you see the progress output and everything goes fine, press the '<reset>' button again, and when you press the button (switch) on the breadboard, built-in LED on the Wiring boards should light.


next up previous
Next: Analog Inputs Up: Hands On AVR-GCC programming Previous: LEDs Traffic Light

© Copyright 2001-2008 CCRMA, Stanford University. All rights reserved.
Created and Mantained by Juan Reyes