Author Options:

Help Programming on an AVR Answered

I am looking at getting an USBtinyISP to start programing ATtiny 13's. I plan on using avrdude. I already have some experience with the Basic stamp, arduino and c++ when writing computer programs. My main concern is how to state and use an input or output. In examples I have seen I do not understand the inputs and outputs.


I don't understand commands like PORTB=0xfe or DDRB=0xfe.

PORTB is an AVR I/O port; the only one available on the ATtiny13.

DDRB is the Data Direction Register for PORTB. This sets the pins to be either input or output.

Both PORTB and DDRB are 8 bit registers, and each individual bit represents an I/O pin (actually, only 6 bits are used, since that's all that's addressable on an 8 pin AVR.)

0xfe is hexadecimal notation. It's a base-16 numbering system, and is extremely common in programming.

so what would be an example of how to put pin 3 high then. Also, what does the 0xfe mean.

Also, what does the 0xfe mean.

The prefix 0x is C coding for "the number that follows is hex." So 0xfe is the hexadecimal number FE.

You must have a calculator program on your computer. In XP, start the calculator, select "Hex" and enter FE. Now select "Dec", and the number will be converted to decimal. For more on hex, follow the link on hexadecimal notation...

Pin 3 is PB4 (straight from the datasheet), which is a predefined constant in avrgcc. To convert the constant to an actual numeric value use the _BV() macro thusly:


You never need to know the actual byte value of pin 3. In fact, the definitions are designed to keep that abstract.

But if you have to know, PB4 means "bit #4 in PORTB," (the fifth bit, since the first bit is #0.) In binary notation that's 00010000, or 16 in decimal (you can use your calculator for binary->decimal conversion , too.)

All of this is covered in the links. The existing threads I gave you explain how to set the port for output, and how to set individual bits.

I am still unsure how to state the different pins or make them either high or low.

Well, copied straight from the first link is an example of setting PB4 high or low:

Setting PORTB for output on PB4:

DDRB |= _BV(DDB4);

Setting PB4:

PORTB |= _BV(PB4); // HI
PORTB &= ~_BV(PB4); // LO

so t |= and &= ~ tell high and low and the pb4 tells which pin. what does the portb mean?

Yes, those are bitwise operators that set or unset individual bits.

PORTB? As explained in both this thread and the others, it's the I/O (in / out) port itself. AVRs with more pins have PORTA, PORTB, PORTC, etc.

Since different AVR ports have specific architecture, they used PORTB arch only for the ATtiny13. For instance, you normally find the ISP port on PORTB.

As noted above:
PB4 means "bit #4 in PORTB"

How would you use pwm. also, how many pwm pins does the attiny 13 have?

There are two pins on the '13 usable for hardware pwm. You'll have to google it. Any pwm I've done was done with bit-banging (toggling the bits with software.) If you need more than two pins, that's the only way.

what would be an example of code for the pwm and how does it work?

I can only point you to guyfrom7up's Lucid Dream Machine (I wrote the AVR code.)

If you're wrestling with the port/DDR information, you'd be better off googling for simpler examples, though. It's IRQ (interrupt) driven, and that's fairly complex.

How would you send data to eeprom?

The internal eeprom memory on many AVRs, or an external serial eeprom?


9 years ago

Check out some existing threads on the subject first; like this, and this and this one.

Ask again if it's not happenin'...

gmoon gives a ton of good I/O info in the first link, just totally skip the main topic thing and go to teh comments.