loading

Microcontroller question!!!!

Non of the online tutorials are helping me, How do i set a high/low output to a SPECIFIC pin on my AT2313 tiny??? All I fine alone is OUT PORTD,0xFF which sets ALL ports on portD to high 255 aka OFF. But How can i set like pin 4 (PD4 aka PortD 4) and not all portD? someone help me!!

sort by: active | newest | oldest
zachninme9 years ago
Don't think of 0xFF as the decimal number, 255, instead think of it as the binary number 11111111. See the correlation?
0x01 = 00000001 (pin 1)
0x02 = 00000010 (pin 2)
0x04 = 00000100 (pin 3)
0x08 = 00001000 (pin 4)
0x10 = 00010000 (pin 5)
0x20 = 00100000 (pin 6)
0x40 = 01000000 (pin 7)
0x80 = 10000000 (pin 8)

If you're doing basic things, you can just add up the values of the pins you want on. For instance, pins 7 & 8 would be 0x80 + 0x40, or 0xC0.
Or, you can do boolean logic.
PORTD = 0x80;PORTD |= 0x40; //PORTD = 0xC0PORTD &= ~0x80; //PORTD = 0x40
Killa-X (author)  zachninme9 years ago
Yeah, I think I get it now. Tutorials I seen said to use FF and 00 to set High and Low.

So what your saying is, You need to state what PORTD is. sample:

PORTD = 0x80
OUT PORTD,0xFF

And that will set pin 8, to it's on. Correct? If i'm wrong, I'd like a example mini code of what I just showed, a simple on with a port. Thank you!
gmoon zachninme9 years ago
Another option is to use the avrlibc "bit value" macro: _BV(bit) , which is just a shift operation (1 << (bit))

The equivilent of Zachninme's statement PORTD |= 0x40; is--

PORTD |= _BV(6);

( uses the conventional bit numbering of 7-0 )

The _BV() macro is used for all the AVR bit definitions now...( sbi() and cbi() functions are depreciated in avrlibc....)
zachninme gmoon9 years ago
Oh, don't tell me, I got the pin numberings wrong! Whoops!
gmoon zachninme9 years ago
Not really wrong in any fundamental way....

(but uCs usually count from zero)
westfw9 years ago
There are a several ways:

  • See sbi and cbi instructions. (presence of single-bit pin set instructions like these are one of the things that tends to distinguish between a microcontroller (most have something like this) and a microprocessor (most don't have this; you should see what the ARM does to allow similar functionality; it's... mind boggling.)) On AVR, SBI/CBI only work on the first 32 IO registers, but that does seem to include all the basic IO pins.
  • read the port, modify the bit, write the port. (may be dangerous if some bits in the port are inputs, or if reading an output port will give you data other than what was last written.)
  • Keep a "shadow" copy of what you've written to the port, modify bits there and output whole port from the shadow.