📣📪 🚚 Adafruit will not be shipping USPS orders Monday October 14, 2019 🗓. Expedited USPS orders placed after 11am ET 🕚 Friday October 11 will go out Tuesday October 15 📣📪🚚
0

Really basic help with ATtiny2313
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Really basic help with ATtiny2313

by magician13134 on Sat Apr 12, 2008 11:51 pm

Hi, I was looking through a 2313 datasheet and it said there were 18 programmable I/O pins. This is great because I'm working on a project that needs 18 programmable LEDs, preferably without Charlie-plexing... The problem is, I've only seen MiniPOV code, so I don't know how to use any more than eight pins.

Can someone write a program for the 2313 that scrolls through all the LEDs, lighting them one-by-one?

Thank you very much!
magician13134
 
Posts: 1119
Joined: Wed Jun 13, 2007 9:17 am
Location: Ann Arbor

by The_Don125 on Sun Apr 13, 2008 1:15 am

(links removed, website is down)

Well, the above code is the .hex file for what you want, minus the LED attached to pin1. I'm having trouble getting PA2 (the reset pin) to output. Somebody else here might know how, I don't.

And since the .hex will do absolutely nothing to teach you how to do it yourself, below is the C code.

(links removed, website is down)

And I am releasing both as public domain.

Its not very elegant, but it gets the job done.

edit:
Ok, I figured out why PA2 isn't lighting. As mentioned before, its the reset pin. In order to use it for i/o, the reset state must be disabled via fuse. This eliminates its ability to be programmed via ISP.

So, to use the code as intended as it stands, the fuses need to be programmed as:
lfuse: 0xE4
hfuse: 0xDE
efuse: 0xFF

And you'll also need a high voltage programmer in order to make adjustments to the code, after the first write.
Last edited by The_Don125 on Mon Apr 21, 2008 8:21 pm, edited 1 time in total.
The_Don125
 
Posts: 373
Joined: Tue Mar 06, 2007 11:51 pm
Location: Illinois

by mtbf0 on Sun Apr 13, 2008 10:32 am

you might could try a mega168. 23 i/o pins available, although as with the tiny2313 one of them doubles as the reset pin and two of them are used for the oscillator. so you can use the internal clock and have 22 i/o's and still have isp.

and you can get a boarduino to prototype with. the problem with the boarduino is that it does require a 5v supply since it's running above 8MHz, but the good news is that it's got a regulator on board, or can get it's 5v from a usb interface.

(you can leave off the resonator and change the fuses to use the internal oscillator which gives you back two i/o lines and lets you run at as low as 2.7v, but at 8MHz max.)

and the arduino api provides a slightly braindamaged programming interface for those of us too lazy to write proper c. (personally i love not having to prototype functions, but feel really bad about it).

anyway with the boarduino you get 18 i/o's plus two for a serial interface, with a bootloader, regulated 5v, really snappy 16MHz operation. or not.
User avatar
mtbf0
 
Posts: 1645
Joined: Sat Nov 10, 2007 12:59 am
Location: oakland ca

by magician13134 on Sun Apr 13, 2008 5:07 pm

Ok, cool, thanks both of you. Question though:
Couldn't I program the code with my USBTinyISP, then program the fuses with it? If I did that, I could eliminate the need for the high voltage programmer, right? Thanks very much!

The_Don125, your code is great, just what I was looking for, thanks for the comments and organization!!! Just one thing though, what is this doing?
Code: Select all | TOGGLE FULL SIZE
switch (i)

Thanks!
magician13134
 
Posts: 1119
Joined: Wed Jun 13, 2007 9:17 am
Location: Ann Arbor

by The_Don125 on Sun Apr 13, 2008 5:11 pm

Well, you could certainly program the code and the fuses, but as soon as you program the hfuse (the one controlling the RESET pin), that chip will no longer program via ISP. Why? Because ISP programming requires the use of the RESET pin doing its reset function.

The switch (i) is just the method I used for controlling which lights are on. The i is a counter variable that increments once every 100ms. The switch then executes the case corresponding with the current value of i. Then, in case 18, i is set back to 0, incremented once, and the process begins again.
The_Don125
 
Posts: 373
Joined: Tue Mar 06, 2007 11:51 pm
Location: Illinois

by magician13134 on Sun Apr 13, 2008 6:19 pm

Oh, right! I forgot what switch was since I always use if/else if/else

Anyway, I'm still a bit confused... When you say "as soon as hfuse is programmed", does that mean it won't finish burning the fuses, or I just won't be able to UPDATE the code again? As in, can I do this project with only the USBTinyISP if I never plan on updating the code after programming it once?

Thanks
magician13134
 
Posts: 1119
Joined: Wed Jun 13, 2007 9:17 am
Location: Ann Arbor

by KyleK on Sun Apr 13, 2008 6:23 pm

yup you can do it, thats exactly what he said
KyleK
 
Posts: 13
Joined: Tue Apr 08, 2008 4:25 pm

by The_Don125 on Sun Apr 13, 2008 6:26 pm

Yeah, it will burn the hfuse, and then stop working with the USBTinyISP until you find another method of programming the chip, such as a high voltage programmer. avrdude will probably throw a warning asking if you are sure you want to set that fuse. So yes, you could do this project with the USBTinyISP if you never plan on updating the firmware or fuses. Personally, I'd do everything except program the hfuse until you are done testing. You'll have to live with PB2 not lighting up during this time, but it at least gives you room for error.

Yeah, if/else would have been an option for the code as well, but I've read that for long lists like that, switch is more efficient.
The_Don125
 
Posts: 373
Joined: Tue Mar 06, 2007 11:51 pm
Location: Illinois

by magician13134 on Sun Apr 13, 2008 6:32 pm

Ok, cool, thanks much. And then, one more thing... As for lighting multiple lights at once... If they're on separate ports (is that what they're called, or is that an abbreviation or something, like PORTB?) I can do that, but on the same port... do I just add the numbers? I think I remember doing something like this before, but I don't want to over-complicate things...

Thanks again!

(Oh, and yes, I do plan on doing all testing without pin 1 lighting, I'll live)
magician13134
 
Posts: 1119
Joined: Wed Jun 13, 2007 9:17 am
Location: Ann Arbor

by The_Don125 on Sun Apr 13, 2008 6:50 pm

Yup. Adding the values will get you multiple lights lit on the same port.
Code: Select all | TOGGLE FULL SIZE
PORTA = 0x7
PORTB = 0xff;
PORTD = 0x7f;


The above code should light all 18 pins.
The_Don125
 
Posts: 373
Joined: Tue Mar 06, 2007 11:51 pm
Location: Illinois

by magician13134 on Sun Apr 13, 2008 8:01 pm

..? I'm sorry that confused me even more... Why aren't all the values the same for all three ports? And I add them in hex?
Thanks, you're being very helpful and patient!
magician13134
 
Posts: 1119
Joined: Wed Jun 13, 2007 9:17 am
Location: Ann Arbor

by The_Don125 on Sun Apr 13, 2008 8:18 pm

The maximum values aren't the same.

PORTA is only 3 bits (PB0, PB1, PB2), so its maximum value is 0x7, or 0b111.
PORTB is 8 bits, so its maximum value is 0xFF, or 0b11111111
PORTD is 7 bits, so its maximum value is 0x7F, or 0b1111111

You can add them in either hex or decimal, whichever you prefer. I can convert hex to binary much easier than I can convert decimal to binary, so I use hex.

Think of the values like this: each bit represents one pin. The pins are numbered from highest to lowest, from left to right. So in the case of PORTA, PA2 is controlled by 0bX00 (the bit with the X in it), PA1 is controlled by 0b0X0, and PA0 is controlled by 0b00X. This holds true for PORTB and PORTD as well, they just have more bits.

So, say I wanted to light alternating lights on PORTB, starting on PB0 and finishing on PB6. That corresponds to:
Code: Select all | TOGGLE FULL SIZE
PORTB = 0x55
or
0b01010101


If I wanted to do the same on PORTA, I'd do:
Code: Select all | TOGGLE FULL SIZE
PORTA = 0x5
or
0b101


And PORTD:
Code: Select all | TOGGLE FULL SIZE
PORTD = 0x55
or
0b1010101

PORTA has the smaller value because setting it above 0x7 will either a)do nothing or b) cause an overflow (not sure which).

Unfortunately, C doesn't allow for inputing values in binary, so you'll have to choose hex or decimal.
The_Don125
 
Posts: 373
Joined: Tue Mar 06, 2007 11:51 pm
Location: Illinois

by magician13134 on Sun Apr 13, 2008 8:30 pm

Oh, yeah, I never noticed that there wasn't a PORTC.

Wow, that was REALLY helpful and informative!!!

So, the data sheet tells me which pins PA2, PA1, PA0 are, right? If so, that's all I need to know, thanks again, very much!
magician13134
 
Posts: 1119
Joined: Wed Jun 13, 2007 9:17 am
Location: Ann Arbor

by The_Don125 on Sun Apr 13, 2008 8:36 pm

Yup, datasheet has all the info. Good luck with your project.
The_Don125
 
Posts: 373
Joined: Tue Mar 06, 2007 11:51 pm
Location: Illinois

by magician13134 on Sun Apr 13, 2008 8:51 pm

Thanks! I'll let you guys in on what it is if I don't butcher it in the process! :wink:

I've been known to do that to many-a-project
magician13134
 
Posts: 1119
Joined: Wed Jun 13, 2007 9:17 am
Location: Ann Arbor

Please be positive and constructive with your questions and comments.


cron