Voting resources, early voting, and poll worker information - VOTE. ... Adafruit is open and shipping.
0

AvrDude help
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

AvrDude help

by TabbyArtifact on Sat Sep 04, 2010 5:38 pm

Hi everyone. I just wanted to say that i love this site and all of its cool things that you help people like me out. (people with no experience with electronic stuff) Now I have just gotten hooked on the idea that I can control leds and stuff with a small chip. :) I read your tutorial on how to use avrdude, I bought a USBtinyISP, I bought all the stuff i need to make a target board and I cant wait to get some leds blinking!. :) Now the only thing i dont understand is how to write your own code? In your tutorial you gave the test_leds.hex file. How would one go about making there own .hex file? I was thinking about making the leds blink in patterns and stuff, but i cant for the life of me figure out how to write the program for it.
TabbyArtifact
 
Posts: 4
Joined: Sat Sep 04, 2010 5:32 pm

Re: AvrDude help

by uhe on Sun Sep 05, 2010 3:03 pm

To clarify a few things... The steps you need to go through are:
  1. Write code (-> text editor)
  2. Compile the code (-> avr-gcc)
  3. Upload the compiled code (-> avrdude)
To upload the binary (.hex) you need avrdude but before you can do that, the program needs to be written and compiled!

For AVR microcontrollers programs can be written in C or BASCOM. Most people prefer C and know that BASCOM exists ;)
After the code got written, it needs to be translated into binary-code. This is called compiling and can be done with avr-gcc.
On windows you can use WinAVR which should include everything you need.
Have a look at the projects around here, there are c programs with Makefile (all needed instructions for compiling) and already compiled hex-files, theese are good examples.
AFAIK a tutorial how to program is missing and i can't recommend any.
"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 4:50 pm
Location: metric world

Re: AvrDude help

by TabbyArtifact on Sun Sep 05, 2010 8:38 pm

Ha thanks for the information. I pretty much just found out about avrstudio and avr-gcc. Its like a whole new world has opened up to me. :) Now I just have to learn how to program I guess. I have taken one class of c++ in college, so the already completed codes I am seeing (mainly ones that blink an led) look vaguely familiar. I just dont understand all the PortB stuff, and the binary parts of it. I guess I better go learn. :)
TabbyArtifact
 
Posts: 4
Joined: Sat Sep 04, 2010 5:32 pm

Re: AvrDude help

by uhe on Mon Sep 06, 2010 6:23 am

PORTB, PINB, DDRB are registers which controls the pins of the uC (input or output, with or without pull-ups...).
To *configure* (enable or disable) features of your chip you just set or clear a bit in a register. Just grab the datasheet of your chip it's all described in there.
"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 4:50 pm
Location: metric world

Re: AvrDude help

by stinkbutt on Tue Oct 26, 2010 2:41 pm

Here's a stupid simple primer for trying to figure out AVR code:

This is the pinout diagram for the ATMega168. There are others, but this one is a decent starting point. Each AVR microcontroller has a number of PORTS which define up to 8 pins (0 through 7). You don't really directly address pins, you address ports.

Image

The codes immediately next to each pin, PB0, PC2, etc... tell you which PORT (B, C, D) and which PIN (0, 2, 4) it is.

DDRx is the Data Direction Register. It's an 8-bit number that defines the input/output status of an ENTIRE PORT. 1 is output, 0 is input. It's binary, so the c statements:

Code: Select all | TOGGLE FULL SIZE
DDRB = 0b1111111;
DDRB = 0xFF;
DDRB = 255;


would mean that every single pin on port B (PB0 through PB7) would be OUTPUT pins. All three of these statement do precisely the same thing, though the binary version's a hell of a lot clearer. The same applies, in this case for the '168, for DDRC and DDRD. They do it this way so if you move your code to a different chip it's still compatible, provided you're not attempting to address nonexistent ports.

PORTx is the actual pins of the port. You set a bit to 1 to pull the pin high and the bit to 0 to pull the pin low. So, let's augment our previous code:

Code: Select all | TOGGLE FULL SIZE
DDRB = 0b1111111;  // All pins on port B are output pins.
PORTB = 0b1111111; // All pins on port B are now HIGH.
PORTB = 0b10101010;  // Now half the pins on port B are HIGH, the other half, LOW.


Now, most of the time you WON'T want to set an entire port at once. Not only it it cumbersome, but you won't necessarily know what the other pins are supposed to be if you're, say, working inside of an interrupt. So instead you'll want to use bitwise operators.

Changing the previous code, let's start with all the pins LOW and not alternate them.:

Code: Select all | TOGGLE FULL SIZE
DDRB = 0b1111111;  // All pins on port B are output pins.
// PORTB = 0b1111111; We won't do this.
// PORTB = 0b10101010;  We won't do this.
PORTB = 0b00000000; // All pins on port B are now LOW.

PORTB |= ( 1 << 1 );  // Turn on the bit in the 2nd position (bit 1, numbered 0-7)
PORTB |= ( 1 << 3 );  // Turn on the bit in the 4th position (bit 3)
PORTB |= ( 1 << 5 );  // Turn on bit 5
PORTB |= ( 1 << 7 );  // Turn on bit 7 (the last bit)


In that code we end up doing the same thing, but now by individually using the OR assignment operator, "|=", which is like +=, -=, *=, only with bitwise arithmetic. The << is the bit shift operator, which takes a 1 (the first argument) and move it X bits down (the second argument.) So:

Code: Select all | TOGGLE FULL SIZE
1 << 1
0b00000001 << 1
0b00000010
_BV( 1 )


All three of these represent the number 2. The last line is a macro in avr-gcc that makes this simpler, the _BV() macro.

Turning OFF a bit is very similar, only instead of using an OR operator you use an AND operator and you negate a single-bit number. All of these lines of code do the same thing:

Code: Select all | TOGGLE FULL SIZE
PORTB &= 0b11111101;
PORTB &= ~(1 << 1);
PORTB &= ~_BV(1);


The tilda ~ in the last two lines takes a binary number and negates it. So:

Code: Select all | TOGGLE FULL SIZE
~(0b00000010) = 0b11111101


I'm sure other people can give you lots of more pointers for how to decipher some of the AVR code, but that was the stuff that threw me for a loop back in the day. (That is to say, about 6 weeks ago ;) )
Red M&M, Blue M&M: They all wind up the same color

stinkbutt
 
Posts: 593
Joined: Wed Feb 17, 2010 2:40 am

Re: AvrDude help

by stinkbutt on Tue Oct 26, 2010 2:48 pm

There are a couple of excellent tutorials that will start you off if you want to learn from scratch instead of re-jiggering existing code. A couple of good places to start, if you've already finished the ladyada.net tutorial, are:

http://www.sparkfun.com/commerce/tutori ... ials_id=93

http://www.protostack.com/blog/2009/07/ ... er-supply/
Red M&M, Blue M&M: They all wind up the same color

stinkbutt
 
Posts: 593
Joined: Wed Feb 17, 2010 2:40 am

Re: AvrDude help

by Entropy on Tue Oct 26, 2010 3:10 pm

If you want to start with a basic example, I'd look into:

The avr-libc examples

Also, make sure to have a datasheet for whatever chip you are targeting is! (For Arduino-compatible hardware, it's usually an Atmel ATMegaXX8 chip, where XX is typically 16 or 32, although Mega88s do exist too. XX is the size of the chip's flash memory in Kbytes - so 88 = 8k, 168 = 16k, 328 = 32k)
Entropy
 
Posts: 472
Joined: Tue Jan 08, 2008 12:43 am
Location: Owego, NY USA

Please be positive and constructive with your questions and comments.