# COMMUNITY : FORUMS : TECH

## Help Programming on an AVR

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.

sort by: active | newest | oldest
 1-10 of 18 Next »
sotsirh194 (author) 8 years ago
I don't understand commands like PORTB=0xfe or DDRB=0xfe.
8 years ago
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.
sotsirh194 (author)  gmoon8 years ago
so what would be an example of how to put pin 3 high then. Also, what does the 0xfe mean.
8 years ago
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:

_BV(PB4)

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.
sotsirh194 (author)  gmoon8 years ago
I am still unsure how to state the different pins or make them either high or low.
8 years ago
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
sotsirh194 (author)  gmoon8 years ago
so t |= and &= ~ tell high and low and the pb4 tells which pin. what does the portb mean?
8 years ago
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"
sotsirh194 (author)  gmoon8 years ago
How would you use pwm. also, how many pwm pins does the attiny 13 have?
8 years ago
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.
 1-10 of 18 Next »