Can't program ATMega

For Adafruit customers who seek help with microcontrollers

Moderators: adafruit_support_bill, adafruit

Can't program ATMega

Postby mikegrey28 » Tue Jul 19, 2011 8:25 pm

Hi Adafruit funs,

My name is Mike; I am a soft dev and I started working on electronics a few weeks ago.
I got a ATMega168p micro controller (not from a kit or anything) and I am using a breadboard to mount my mini project on it.

I got a simple example that should blink a led.
Here is the code:

Code: Select all
#include <avr/io.h>
#include <util/delay.h>

void Wait()
{
   uint8_t i=0;
   for(;i<23;i++)
      _delay_loop_2(0);
}

void main()
{
   //Set PORTC0 as output

   DDRC=0b00000001;

   while(1)
   {
      //Set PC0=High(+5v)
      PORTC|=0b00000001;
      Wait();

      //Set PC0=Low(GND)
      PORTC&=0b11111110;
      Wait();
   }
}


I compiled the code using AVR Studio 5.0.
I uploaded the code using avrdude but something is missing.

Questions:
-As a brand new micro controller, should I upload a bootloader? What is it good for?
-How should I update the fuses (can avrdude do that for me?)
-What am I missing to upload?

uhe wrote:AFAIK it just runs when you apply power.

-How does the micro know I want to program it with avrdude or any other uploader.
If I provide power the micro should start running the code right?
Does it stop running it when I start uploading software to it?
(Kind of) Does the ship say "ok, I stop running the program and let you upload new firmwork"

A few specifics:
-I use winXP, my uploaded is a serial COM2; I had no problem with AVRdude (write, read, check OK)
-The AtMega168 is a new one, no pre-burned.

Thanks a lot for your feedback uhe.
I appreciate all the help I can to get me started on this new programming avenue.
Mike.
Last edited by mikegrey28 on Wed Mar 07, 2012 9:59 pm, edited 2 times in total.
mikegrey28
 
Posts: 6
Joined: Tue Jul 19, 2011 7:46 pm

Re: After programming, what then?

Postby uhe » Thu Jul 21, 2011 3:04 pm

mikegrey28 wrote: How do I "start" the minipov after uploading the firmware?
AFAIK it just runs when you apply power.

When your're using different hardware you need to adapt the code. Grab the datasheets, compare the code you have and adapt it to what you need.
For example the ATtiny has a register called TIMSK (Timer/Counter Interrupt Mask Register), the ATmega doesn't. It's more powerfull and has three of them (TIMSK0, TIMSK1 and TIMSK2)...
"I have not failed. I've just found 10,000 ways that won't work." - Thomas Edison
uhe
 
Posts: 178
Joined: Mon Sep 03, 2007 3:50 pm
Location: metric world

Re: Can't program ATMega

Postby scott-42 » Fri Jul 22, 2011 8:57 am

mikegrey28 wrote:-As a brand new micro controller, should I upload a bootloader? What is it good for?
In general you don't need a boot loader for the style of programming you are doing. If for example you wanted to do some Arduino programming, then you could use one of the Arduino boot loaders like optiboot. In general a boot loader will provide for reprogramming the chip memory without having to program the entire chip and it is also responsible for running the main program.
mikegrey28 wrote:-How should I update the fuses (can avrdude do that for me?)
You can change the fuses with avrdude. Check out the fuse calculator here: http://www.engbedded.com/fusecalc/
mikegrey28 wrote:-What am I missing to upload?
You said it was uploading fine and verifying, so your not missing anything.
mikegrey28 wrote:-How does the micro know I want to program it with avrdude or any other uploader.
Generally it has something to do with the RESET line on the chip and then sending some clock pulses with the ISP port. For the most part you probably don't care about the details of this, if you do, then read the data sheet.
mikegrey28 wrote:If I provide power the micro should start running the code right?
It actually runs from the reset vector which then tells the CPU what else to run. This is why most people start off with something simple like Arduino before venturing into the unknown.
mikegrey28 wrote:Does it stop running it when I start uploading software to it?
(Kind of) Does the ship say "ok, I stop running the program and let you upload new firmwork"
Short answer Yes. For more info, read the data sheet it is covered in there.

Also I would recommend you check Arduino and try out that environment.
scott-42
 
Posts: 123
Joined: Mon May 30, 2011 9:46 am
Location: Santa Clara, CA

Re: Can't program ATMega

Postby mikegrey28 » Fri Jul 22, 2011 11:04 am

Thanks a lot uhe and scott-42.
You where right all along. After uploading the program with AVRDude it should be working right?
So I re-checked all the cables in my breadboard and guess what, there was a missing connection to the chip.
Fixed it, power ON, and I could see my blinky LED.

Thanks for your help!
mikegrey28
 
Posts: 6
Joined: Tue Jul 19, 2011 7:46 pm

Lets go on...

Postby mikegrey28 » Fri Jul 22, 2011 1:40 pm

After programming my first LED ON/OFF program I created a few more.
I added 5 LEDS to port C and turned theme ON in order; and then turned theme OFF in the same ORDER.

Then I played arround with my 7 segment LED and got it to work.
Also I used the include <avr/eeprom.h> to save the last number. This allowed me to restart my board and the last number was displayed.


Now I am looking into different source codes (i.e. the adafruit DIGG code) to see how to handle a "button".
I wish to use a switch to send a pulse to a port and increment my counter.
Also I am using the function _delay_loop_2(0); but I am not sure how many ms that really is lol.

Let's see if I can tune this little circuit =)
Help and suggestions are always welcome as a noob :mrgreen:
Last edited by mikegrey28 on Wed Mar 07, 2012 10:00 pm, edited 1 time in total.
mikegrey28
 
Posts: 6
Joined: Tue Jul 19, 2011 7:46 pm

Re: Can't program ATMega

Postby uhe » Fri Jul 22, 2011 2:56 pm

This tutorial might help:
http://www.ladyada.net/learn/arduino/lesson5.html
The code is arduino specific but the theory about switches is not.
"I have not failed. I've just found 10,000 ways that won't work." - Thomas Edison
uhe
 
Posts: 178
Joined: Mon Sep 03, 2007 3:50 pm
Location: metric world

Re: Can't program ATMega

Postby mikegrey28 » Sun Jul 24, 2011 7:53 am

Hi uhe!

It has been a while, a few days I think.
I have been busy making a few test on my breadboard.
For example:
1. Using two 7 segment leds
2. Blinking 3 LEDS in order faster and faster by a switch and so on.
3. Reading temperature using Analog port with a LM35 (her in argentina we use Celsius).
All this without any bootloader, meaning, no arduino or other "OS" :mrgreen:

Here are the following things to crack.
I have been using the function _delay_loop_2 witch is included in the file #include <util/delay.h>.
This function is nice; I used it to blink faster and faster my 3 LEDS using a switch, and then slower and slower.
Now I wish to use the function _delay_ms, but there for I need to define F_CPU. For example:

Code: Select all
#define F_CPU 8000000 //This currently is set for 8MHz


So what frequency should I put in? (this code snip is from a forum).
I have an atmega168. In the data sheet it staits: Up to 20 MIPS Throughput at 20 MHz
So could I set a value between 1MHz and 20Mhz?
What's the effect? I guess 1MHz is slower computing (less computing actually), and 20 is more (much more :) )

As always...
Thanks for your help!

Regards,
Mike
mikegrey28
 
Posts: 6
Joined: Tue Jul 19, 2011 7:46 pm

Re: Can't program ATMega

Postby scott-42 » Sun Jul 24, 2011 8:35 am

mikegrey28 wrote:
Code: Select all
#define F_CPU 8000000 //This currently is set for 8MHz

So what frequency should I put in? (this code snip is from a forum).
I have an atmega168. In the data sheet it staits: Up to 20 MIPS Throughput at 20 MHz
So could I set a value between 1MHz and 20Mhz?
What's the effect? I guess 1MHz is slower computing (less computing actually), and 20 is more (much more :) )

According to the datasheet for a 168P (make sure you include the P when talking about the chip, it makes a difference; the data sheets are different for a 168 versus a 168P):
9.2.1 Default Clock Source - The device is shipped with internal RC oscillator at 8.0MHz and with the fuse CKDIV8 programmed, resulting in 1.0MHz system clock. The startup time is set to maximum and time-out period enabled. (CKSEL = "0010", SUT = "10", CKDIV8 = "0"). The default setting ensures that all users can make their desired clock source setting using any available programming interface.

This means, unless you changed the clock speed by setting the clock bits or fuse settings, you're really running at 1MHz by default.
scott-42
 
Posts: 123
Joined: Mon May 30, 2011 9:46 am
Location: Santa Clara, CA

Re: Can't program ATMega

Postby uhe » Sun Jul 24, 2011 9:50 am

Hi mikegrey28,
You need to set F_CPU to what you're actually using, not what's possible. Maybe what scott-42 said, I don't know.
To be sue, read the fuses somehow with avrdude (avrdue docu or google knows the parameters to use ;)).
When you got them you can put the valuses into a fuse calculator to decode them.

If you set F_CPU you might want to guard it with an #ifdef like:
Code: Select all
#ifndef F_CPU
#define F_CPU <your_mhz value>
#endif

Regarding 168 vs. 168P, P has a temperature sensor and pico power. AFAIK the fuse settings are different.
"I have not failed. I've just found 10,000 ways that won't work." - Thomas Edison
uhe
 
Posts: 178
Joined: Mon Sep 03, 2007 3:50 pm
Location: metric world


Return to Microcontrollers

Who is online

Users browsing this forum: No registered users and 1 guest

Stuff to buy from the Adafruit store and links to product documentation!


New Products [102]

Raspberry Pi[80]
 
FLORA[23]
 
Bunnie Studios[9]
 
FPGA[1]
 
mbed[11]
Arduino[60]
 
NETduino[14]
 
BeagleBone[24]
 
Android[6]
 
XBee[10]
More Dev Boards[30]


 
BoArduino[8]
 
SpokePOV[4]
 
TV-B-Gone[4]
 
MiniPOV[3]
 
SIM reader[3]
 
Microtouch[5]
 
Clocks & Watches[18]
 
Drawdio[4]
 
Brain Machine[1]
 
Game of Life[2]
 
MintyBoost[2]
More DIY Kits[16]


 
MaKey MaKey[3]
 
Tweet-a-Watt[5]
 
Young Engineers[33]
 
Discover Electronics[2]
 
Snap Circuits[4]
 
littleBits[3]
 
Project packs[8]


 
Breakout Boards[33]
LCDs & Displays[48]
Components & Parts[69]
Batteries & Power[49]
EL Wire/Tape/Panel[52]
LEDs[109]
 
Wireless[14]
Cables[60]
 
Lasers[6]
Sensors/Parts[145]
 
Enclosures/Cases[11]
 
Solar[11]
 
RFID / NFC[13]
Prototyping[70]
 
iDevices[13]
Tools[71]
 
Wearables[39]
 
CNC[37]
 
Robotics[29]
 
3D printing[1]
 
Materials[24]


 
Stickers[41]
 
Skill badges[55]
 
Books[25]
 
Circuit Playground[7]
 
Gift Certificates[4]