NerdKits - electronics education for a digital generation

You are not logged in. [log in]

NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.

Project Help and Ideas » What I would like to Have

December 07, 2009
by timk
timk's Avatar

I am trying to come to grips with all this stuff. Don't get me wrong I think this kit is the greatest thing since sliced bread. But after burning up most of my first kit just hooking up a power supply I have been a little afraid of experimenting. I now have it back together and working as expected. I have power supply connected and the backlight on the display working.

What I would like to have is a simple example of A button being pressed, waiting a length of time and turning on an LED. I would like the c source code and the make file and any other files needed all with comments explaining what each line is for. Along with instructions for connecting the Switch and the LED.

I know that most of this is probably simple and covered in the tutorials. But to me it would be simpler/safer if I had all this info in one spot. And with this information I think I can learn and build from it.

I have seen the simple blinking led thread on the site but was not sure how to create the Makefile. I think most of the compile errors I recieved was because of the different include files.

Thanks for any help Timk

December 07, 2009
by FWSquatch
FWSquatch's Avatar

I don't have my stuff in front of me, but here is some code that should do what you want. The LED anode should connect like this: anode to pin 27 (row 12 of your breadboard on the right side) and the cathode should hook up to ground. I can't exactly remember how the Nerdkits pushbutton hooks up off the top of my head, but basically, it should hook up to pin 28 (row 11 on the right side of your breadboard) and the ground. The following is the code for a file named button_led_blink.c. Create it and then save it into the led_blink folder

 // button_led_blink.c
 #define F_CPU 14745600
 #include <avr/io.h>
 #include <inttypes.h>
 #include "../libnerdkits/delay.h"

 // PIN DEFINITIONS:
 // PC5 -- push button (pulled to ground when pressed)
 // PC4 -- LED anode

 int main() {
   // LED as output
   DDRC |= (1<<PC4);
   // enable internal pullup resistor on PC5 (the button)
   PORTC |= (1<<PC5);

   // loop keeps looking forever
   while(1) {
     // check the walk button
     if((PINC & (1<<PC5)) == 0)  //button is pushed
     //delay 5 seconds
       delay_ms(5000); 
       // turn on LED
       PORTC |= (1<<PC4);
       //delay for 1 second
       delay_ms(1000);
       // turn off LED
       PORTC &= ~(1<<PC4);  
   }

   return 0;
 }

Next, go into your Makefile and replace all of the "led_blink" with "button_led_blink". It should look like this:

GCCFLAGS=-g -Os -Wall -mmcu=atmega168 
LINKFLAGS=-Wl,-u,vfprintf -lprintf_flt -Wl,-u,vfscanf -lscanf_flt -lm
AVRDUDEFLAGS=-c avr109 -p m168 -b 115200 -P /dev/ttyUSB0
LINKOBJECTS=../libnerdkits/delay.o ../libnerdkits/lcd.o ../libnerdkits/uart.o

all:    button_led_blink-upload

button_led_blink.hex:   button_led_blink.c
        make -C ../libnerdkits
        avr-gcc ${GCCFLAGS} ${LINKFLAGS} -o button_led_blink.o button_led_blink.c ${LINKOBJECTS}
        avr-objcopy -j .text -O ihex button_led_blink.o button_led_blink.hex

button_led_blink.ass:   button_led_blink.hex
        avr-objdump -S -d button_led_blink.o > button_led_blink.ass

button_led_blink-upload:    button_led_blink.hex
        avrdude ${AVRDUDEFLAGS} -U flash:w:button_led_blink.hex:a

Now you are ready to make.

December 07, 2009
by timk
timk's Avatar

Thanks, This is perfect Worked great, I have already been playing around with code to work more LEDs and am now trying to add the LCD stuff to display which Led is on and off.

Thanks again

Happy Holidays

TimK

December 07, 2009
by FWSquatch
FWSquatch's Avatar

Glad to hear it! I wrote it at work and was worried that I'd put a typo in it that would make it all grind to a halt. :)

July 12, 2011
by uml_12
uml_12's Avatar

I'm having a small issue with this code that is a reoccurring problem in most of my projects. I have copy/pasted the code exactly as above and loaded it onto my 168.

Connected to Pin28 is the switch that connects to ground when pressed.

Connected to Pin27 is a resistor followed by an led anode. The cathode is connected to ground.

Now I run the code and for some reason the led is already on. When the button is pressed, the led stays on briefly, turns off and then turns back on. So I modified the code after line 13 to do:

PORTC &= ~(1<<PC4);

Basically I want to have the led off before the switch is pressed. I run this new code and nothing changes. This has been reoccurring in a lot of my projects. I feel like I'm overlooking a basic concept. Any possible solution? Here is the code I'm using .. not much different than yours:

#define F_CPU 14745600

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

#include "../libnerdkits/delay.h"

int main(){

// LED as output
  DDRC |= (1<<PC4);
  PORTC &= ~(1<<PC4);
  // enable internal pullup resistor on PC5 (the button)
  PORTC |= (1<<PC5);

  // loop keeps looking forever
  while(1) {
    // check the walk button
    if((PINC & (1<<PC5)) == 0)  //button is pushed
    //delay
      delay_ms(500);
      // turn on LED
      PORTC |= (1<<PC4);
      //delay for 1 second
      delay_ms(1000);
      // turn off LED
      PORTC &= ~(1<<PC4); 
  }

  return 0;
  }
July 12, 2011
by Noter
Noter's Avatar

Are you missing the braces to block the indented code for your if statement?

July 12, 2011
by Noter
Noter's Avatar

Also it takes a few uSec for the voltage to rise after enabling the pull-up so you may need to delay briefly or your 1st time in the loop may see the pin still grounded. And there is nothing to return to so you don't need the return 0; at the end of main.

 int main() {
   // LED as output
   DDRC |= (1<<PC4);
   // enable internal pullup resistor on PC5 (the button)
   PORTC |= (1<<PC5);
   delay_ms(1);

   // loop keeps looking forever
   while(1) {
        // check the walk button
        if((PINC & (1<<PC5)) == 0){  //button is pushed
            //delay 5 seconds
            delay_ms(500); 
            // turn on LED
            PORTC |= (1<<PC4);
            //delay for 1 second
            delay_ms(1000);
            // turn off LED
            PORTC &= ~(1<<PC4);  
        }
    }

 }

Post a Reply

Please log in to post a reply.

Did you know that LEDs (light emitting diodes) only conduct current in one direction, like normal diodes? Learn more...