Simple blink program won't run from Atmel Studio 6

Go To Last Post
6 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I am slowly moving from Arduino to AVR C. I purchased an atmega32u4 breakout board from Adafruit (this one here) which comes preloaded with a bootloader that allows programming through the USB interface. I setup Atmel Studio 6 to write and build the code and avrdude to upload the hex file onto the micro controller. These steps work well and I am able to build and upload without issue. The problem I am having is that the hex file built by Atmel Studio does not appear to run on the mega32:

 

#ifndef F_CPU
#define F_CPU 16000000L // 16 MHz clock speed
#endif

#include <avr/io.h>
#include <util/delay.h>

int main(void)
{
   DDRC = 0xFF; //Makes all pins on PORTC outputs
   while(1) //infinite loop
   {
      PORTC = 0xFF;
      _delay_ms(1000); //1 second delay
      PORTC = 0x00;
      _delay_ms(1000); //1 second delay
   }
}

There is a simple circuit of a LED and a resistor to verify the operation of the code above.

 

I have taken the same code above and put it into the Arduino IDE as such:

 

#include <avr/io.h>
#include <util/delay.h>

void setup()
{
  DDRC = 0xFF;
}

void loop()
{
   PORTC = 0xFF;
   _delay_ms(1000);
   PORTC = 0x00;
   _delay_ms(1000);
}

This works as expected and the LED lights on and off at about once per second. I am using the Arduino IDE only to build the hex file using the Leonardo profile (runs an atmega32u4). However, in both cases I am using avrdude to upload the hex file to the micro controller as such:

 

avrdude.exe -F -v -c avr109 -p m32u4 -U flash:w:Blinky.hex:i -P COM5

 

I have a high degree of confidence I am doing something wrong in Atmel Studio.

 

Thanks for listening.

This topic has a solution.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I have a high degree of confidence that you are using the -F flag in avrdude.  Never override the invalid signature check.  Post the output of avrdude command:

 

avrdude.exe -vv -c avr109 -p m32u4 -U flash:w:Blinky.hex:i -P COM5

Have you selected the right device in Atmel Studio?

 

EDIT:

There is a simple circuit of a LED and a resistor to verify the operation of the code above.

On what Port and Pin?  You are toggling all the Pins on the PORT.   Try that on PORTB instead.  Hint: Look at the data sheet to see how many pins you have available on PORTC.

 

"I may make you feel but I can't make you think" - Jethro Tull - Thick As A Brick

"void transmigratus(void) {transmigratus();} // recursio infinitus" - larryvc

"It's much more practical to rely on the processing powers of the real debugger, i.e. the one between the keyboard and chair." - JW wek3

"When you arise in the morning think of what a privilege it is to be alive: to breathe, to think, to enjoy, to love." -  Marcus Aurelius

Last Edited: Sat. Mar 14, 2015 - 04:02 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I started by calling out a single pin (PC6) on PORTC in code. That is where the LED is plugged into. I switched the code to toggle the entire port because I wanted to make sure it was not me using the wrong pin ID.

 

I moved the LED to PB4 and switched the code accordingly:

int main(void)
{
    DDRB = 0xFF;// |= _BV(PC5); // output
    while(1)
    {
        //TODO:: Please write your application code 
		PORTB = 0xFF;//|= _BV(PC6);
		_delay_ms(1000);
		PORTB = 0x00; //&= ~(_BV(PC6));
		_delay_ms(1000);
    }
}

I've put the LED on all the PB* pins available and still no joy from the Atmel Studio hex. I did also remove the -F flag from avrdude:

 

avrdude.exe: Version 5.10, compiled on Jan 19 2010 at 10:45:23
             Copyright (c) 2000-2005 Brian Dean, http://www.bdmicro.com/
             Copyright (c) 2007-2009 Joerg Wunsch

             System wide configuration file is "C:\Program Files (x86)\WinAVR\bin\avrdude.conf"

             Using Port                    : COM5
             Using Programmer              : avr109
             avr910_devcode (avrdude.conf) : none
             AVR Part                      : ATmega32U4
             Chip Erase delay              : 9000 us
             PAGEL                         : PD7
             BS2                           : PA0
             RESET disposition             : dedicated
             RETRY pulse                   : SCK
             serial program mode           : yes
             parallel program mode         : yes
             Timeout                       : 200
             StabDelay                     : 100
             CmdexeDelay                   : 25
             SyncLoops                     : 32
             ByteDelay                     : 0
             PollIndex                     : 3
             PollValue                     : 0x53
             Memory Detail                 :

                                      Block Poll               Page                       Polled
               Memory Type Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack
               ----------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
               eeprom        65    10     8    0 no       1024    8      0  9000  9000 0x00 0x00
               flash         65     6   128    0 yes     32768  128    256  4500  4500 0x00 0x00
               lfuse          0     0     0    0 no          1    0      0  9000  9000 0x00 0x00
               hfuse          0     0     0    0 no          1    0      0  9000  9000 0x00 0x00
               efuse          0     0     0    0 no          1    0      0  9000  9000 0x00 0x00
               lock           0     0     0    0 no          1    0      0  9000  9000 0x00 0x00
               calibration    0     0     0    0 no          1    0      0     0     0 0x00 0x00
               signature      0     0     0    0 no          3    0      0     0     0 0x00 0x00

             Programmer Type : avr910
             Description     : Atmel AppNote AVR109 Boot Loader

Connecting to programmer: .
Found programmer: Id = "LUFACDC"; type = S
    Software Version = 1.0; No Hardware Version given.
Programmer supports auto addr increment.
Programmer supports buffered memory access with buffersize=128 bytes.

Programmer supports the following devices:
    Device code: 0x44

avrdude.exe: devcode selected: 0x44
avrdude.exe: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.00s

avrdude.exe: Device signature = 0x1e9587
avrdude.exe: safemode: lfuse reads as FC
avrdude.exe: safemode: hfuse reads as D0
avrdude.exe: safemode: efuse reads as C3
avrdude.exe: NOTE: FLASH memory has been specified, an erase cycle will be performed
             To disable this feature, specify the -D option.
avrdude.exe: erasing chip
avrdude.exe: reading input file "C:\Users\mquezada\Documents\Atmel Studio\6.2\Blinky\Blinky\Debug\Blinky.hex"
avrdude.exe: writing flash (158 bytes):

Writing | ################################################## | 100% 0.02s

avrdude.exe: 158 bytes of flash written
avrdude.exe: verifying flash memory against C:\Users\mquezada\Documents\Atmel Studio\6.2\Blinky\Blinky\Debug\Blinky.hex:
avrdude.exe: load data flash data from input file C:\Users\mquezada\Documents\Atmel Studio\6.2\Blinky\Blinky\Debug\Blinky.hex:
avrdude.exe: input file C:\Users\mquezada\Documents\Atmel Studio\6.2\Blinky\Blinky\Debug\Blinky.hex contains 158 bytes
avrdude.exe: reading on-chip flash data:

Reading | ################################################## | 100% 0.00s

avrdude.exe: verifying ...
avrdude.exe: 158 bytes of flash verified

avrdude.exe: safemode: lfuse reads as FC
avrdude.exe: safemode: hfuse reads as D0
avrdude.exe: safemode: efuse reads as C3
avrdude.exe: safemode: Fuses OK

avrdude.exe done.  Thank you.

I also uploaded this version using the Arduino IDE. The only difference I notice between avrdude outputs is the field called StabDelay. Uploading the Arduino hex it says 150 instead of 100 from the Atmel Studio hex.

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

This works fine on my atmega32u4 breakout board from Adafruit

/*
 * CapacitiveSmokeBlinky.c
 *
 * Created: 3/14/2015 10:35:12 AM
 *  Author: Larry
 */ 


#include <avr/io.h>
#define F_CPU 16000000L // 16 MHz clock speed
#include <util/delay.h>

int main(void)
{
 DDRB = 0xFF;
 while(1)
 {
  PORTB = 0xFF;
  _delay_ms(1000);
  PORTB = 0x00;
  _delay_ms(1000);
 }
}

 

"I may make you feel but I can't make you think" - Jethro Tull - Thick As A Brick

"void transmigratus(void) {transmigratus();} // recursio infinitus" - larryvc

"It's much more practical to rely on the processing powers of the real debugger, i.e. the one between the keyboard and chair." - JW wek3

"When you arise in the morning think of what a privilege it is to be alive: to breathe, to think, to enjoy, to love." -  Marcus Aurelius

Last Edited: Sat. Mar 14, 2015 - 05:59 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

OK, I'll check it out as soon I get back to the bench. I see the CPU speed definition is after the io.h include and before delay.h. This gives me hope and frustration :). Would you mind sharing your fuse settings? Just in case.

In Atmel Studio I selected Atmega32 as the device.

Last Edited: Sat. Mar 14, 2015 - 07:40 PM
This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Nevermind, I figured it out. I was following a tutorial and it said to load the Atmega32 device in Atmel Studio, so I did..... I looked further into the datasheet and realized that was not the device I have and indeed an Atmega32U4 device is further down the list. Go figure now it works. 

 

Larry, I am sorry I wasted your time, I do appreciate you taking a look at it to try to help me out.