PDA

View Full Version : Systm Episode 12: AVR101 [Discussion]


chuckles
07-14-2007, 12:46 AM
Episode 12: AVR101

So you've heard about the DIGG button and P.O.V. toys, and maybe even LCD drivers. What drives them? How do they work? Why should you care? On this episode of Systm, we tackle that very question as Patrick and Dave take a look at AVR microcontrollers. What should be simple becomes an overly complicated project that only a super NASA engineer would even think of handling if the world's fate was hanging in the balance and Bruce Willis was our only hope. So sit back and enjoy the ride as we make an LED blink on and off.

xibalba
07-14-2007, 02:11 AM
Damn wish I wasn't saving up for a laptop or else I would try some of these things. :D

rabidbadger
07-14-2007, 03:06 AM
Sounds like a fun ep, what I expect systm to be all about. will watch in the morn.

vahnx
07-14-2007, 01:11 PM
I'm not up to making any blinking LED's.

masherscf
07-14-2007, 01:35 PM
I really liked this show. You know I started out studying physics (in about 1986) and one of the skills they teach you is building your own instrumentation. We had to build stuff using all stock ICs and it was a pain. I remember having to sit and sketch out endless circuit diagrams. This would have been much easier. Not to mention way less soldering.

vincent12234
07-14-2007, 01:36 PM
Yay! New episode downloading now, will try to watch soon.

darkfate
07-14-2007, 03:03 PM
The ~= is a bitwise complement (also called a one's complement) turning all the 0s (off) into all 1s (on). So the code turns it off, waits 1000 milliseconds, then turns it on, and waits another 1000 milliseconds. The while(1) just makes it loop forever.

Doing it in assembly would be more complicated, but make the code execute slightly faster. Although it doesn't matter much in this specific case.

sacha_komputes
07-14-2007, 06:26 PM
This is trippy. Try to brush your teeth with an electric toothbrush while looking at a 7 Segment LED (like a clock). Pre-tty kule.

"This is really, this is a great way if you, if y.., if you think, I want to grow up and make incredible robots!"

-Patrick Norton on AVR micro controllers [made me lol]

FireFalcon
07-14-2007, 11:27 PM
The ~= is a bitwise complement (also called a one's complement) turning all the 0s (off) into all 1s (on). So the code turns it off, waits 1000 milliseconds, then turns it on, and waits another 1000 milliseconds. The while(1) just makes it loop forever.

Doing it in assembly would be more complicated, but make the code execute slightly faster. Although it doesn't matter much in this specific case.

First, doing it in assembly wouldn't be faster as when the C code gets compiled, it will be in assembly, then machine code format. For something so simple, I'm sure there would be no difference in code. Many of the macros like _BV() are written in assembly anway.

Actually, the code is misleading in how it works with the PORTD register. The code "PORTD = _BV(PD4)" leads one to believe that PORTD doesn't assign but XORs (switches) all locations that have a 1 in the assignment string. It doesn't help that the host also alluded to this type of behavior. This contrasts with, "PORTD &= ~_BV(PD4)" which is supposed to turn the led off. The code can also be written as, "PORTD = PORTD & (~_BV(PD4))" which, following how the first statement was described would turn all on bits of PORTD off except 4. This means that, in PORTD's case, assignment means assignment not XOR (switching).

So, it is better to write "PORTD |= _BV(PD4)" to turn the led at pin four on because it won't affect the value of the other pins in PORTD. It is also more clear in conjunction with the second line of code for altering PORTD.


---Mattthew Hicks

adlerweb
07-14-2007, 11:29 PM
Really nice ep. Im working now for some month with AVRs and its easier than i thought first. Now nearly every device in my room is hooked up to a AVR. It also gives an good look into the systems working in your PC or other devices - especially when you use ASM.

First, doing it in assembly wouldn't be faster as when the C code gets compiled, it will be in assembly, then machine code format.

Well - i have not checked it, but i think assembly would be smaller/faster because you can optimize it better.

titaniumlizzard
07-15-2007, 04:13 AM
Great episode!

Need more along the lines of this one.

vbook
07-15-2007, 05:32 AM
Well - i have not checked it, but i think assembly would be smaller/faster because you can optimize it better.

Larger style projects yes doing it in asm will result in a smaller footprint and run faster as when writing in asm you should know your cycle times for each instruction for optimization.

I have been working on micros for years and its only been within the last 4-6 years that the majority of the work that comes across my desk is a c/asm combination. My job is to take the routines from others and optimize them from a pure asm mindset.

I think more people should be learning exactly what goes on in assembly the more and more products become smaller and hand held computers become the norm.

For those that want to learn more about assembly, there is a great book both in stores and online (in html and pdf) called "The Art of Assembly". Great book for beginners teaching them basic binary and assembly techniques.

For something this small they could have gotten away with using a tiny12 but I understand they are just showing the basics to get you going and have the 2313 for larger style projects and having a hardware UART is always a plus

drandolph
07-15-2007, 06:26 PM
You know a lot of people have a hard time with programming, even in C but assembly is a tricky language to learn. So with my limited experience with programming in general I usually like to recommend people start with C or Visual Basic not because they are better languages (Most of the time they are not) but because it helps to get people thinking in the right direction and they can start getting the pay off pretty quickly. Now if your an adventurous person and want to tackle Assembly then a great way to start is with the Parallax Hydra Kit.
I got mine a week ago and the fun hasn't stopped. It has a great book and all the hardware you need to get started. Considering most other books on assembly i have browsed through I am finding that the reward structure in this one is fairly quick considering the subject matter.

http://www.parallax.com/detail.asp?product_id=32360

After reading my post, i thought maybe some people would not understand what a pay off is or what i mean by that. When ever someone is introduced to a new subject, hobby, language, program, etc. There is the upward learning curve. The point when patience is equal the the excitement that one person has. Then the crest which is where if the person does not grasp the concept as a whole they usually give up. This it the pay off point. If a person can not reach the crest without a reward of some kind they will usually give up on the subject matter.

Case Study:
SMB V Zelda

Both games were extremely popular and a lot of people enjoyed them. However SMB killed Zelda in sales and penetration in the market. Why?
SMB had a slight lead in pay off thru the game. Remember level 1-1? slow creatures you can usually kill the first time you play, mushroom right there at the start? Almost from the second you hit start you knew exactly what to do. Then there was Zelda, a great game and full of fun but the first time you began there was a moment of "what do I do?" This drove people away and only left it for the more serious gamers.

Why help people with a faster reward system and temper there knowledge?
Simple, the more people working with a product the better it will be.

Case Study:
Linux

Slowly but surely people are starting to pick up on Linux OS's. At the same time Linux is getting better, because of more feedback. Not because of better hardware (which helps but not the main reason) but because of developers hearing peoples problems and addressing them.


Sorry everyone I was just going to suggest a good way to start assembly programming but it turned into a giant rant about how to help people learn a subject.

So in short. "Hello World"

Rivive
07-15-2007, 06:38 PM
Thanks for the mention in the show drandolph, (I was the guy who interfaced the digg software with an lcd) I got a bit of a kick out of it. The software I use is Melabs Pic Basic Pro, the code you write in this software is more intuitive than most basic compilers out there. So you can quickly have a complex programme up and running in no time. Plus they have a trial software so you can try before you buy. I do agree that C and ASM are faster languages, but if need be you can integrate them in to your basic code. So if you are into Basic and microcontrolers then this is the way to go.

Rivive...:)

ToadX
07-16-2007, 03:40 AM
GREAT show! Loved it! I'd love to see a follow-up episiode that goes through some more advanced functionality such as interfacing the chip to an LCD as you mentioned at the beginning of the episode.

harshbarj
07-16-2007, 06:16 AM
looks like just the thing to interface with a ps/2 keyboard / mouse. Great show and keep stuff like this coming (perhaps dedicate a new show just to diy hardware hacks like this).

ExtremeCoder
07-16-2007, 04:12 PM
Loved this episode, the second it finished i bought all the components and I am just troubled with what am I supposed to do with power, which pins do I connect the power too? the + and the - cable where do I soder?

drandolph
07-16-2007, 04:32 PM
If you check out
http://www.atmel.com/dyn/resources/prod_documents/2543S.pdf

Under the PDIP/SOIC drawing you will notice that pin 20 is for VCC/ + Power and pin 10 is for GND/Negative Power.

You can use some batteries, i used 3AAA batteries but you could also just wire it up from USB power too.

ExtremeCoder
07-16-2007, 04:57 PM
thanks alot mate that is usefull to know... xD, I am using a parralel to usb adapter... will that work? it looks ok ... do you know if it can work?

drandolph
07-16-2007, 08:49 PM
I haven't found one that will because most will only do printer emulation and not support the other comm functions available in parallel.

If you do not have native parallel support on your PC then you would be better off going with a USB or serial programmer.


adafruit.com has a cute cheap $22 USB programmer and a $7.50 Serial programmer which will work with a usb-serial converter.

But like i said in the show when you get really serious about it there are full development boards with all the bells and whistles out there.

Rossimo
07-17-2007, 08:50 PM
What were the 2 magazines they mentioned on the show? I remember them mentioning some parts catalogs, but I can't find the reference.

masherscf
07-17-2007, 09:00 PM
Assembly code isn't difficult. The trouble is that you're existing on a kind of primitive level. Of course, I haven't done any of that since the bad ol' 6502 days. Why not take advantage of those who went before?

harshbarj
07-18-2007, 02:40 AM
What were the 2 magazines they mentioned on the show? I remember them mentioning some parts catalogs, but I can't find the reference.

http://www.digikey.com/
http://www.grainger.com/Grainger/wwg/start.shtml


Also I use jameco for a lot of parts.

http://www.jameco.com/

AscendedDaniel
07-18-2007, 06:10 AM
I hope this episode will pave the way for future episodes based on this. Now that the basics of programming microcontrollers is on the show, Systm can now move on to more complicated projects that use the AVR as a component.

Blue25
07-19-2007, 05:02 AM
Just wanted to say: Great episode! :D and I hope Systm will go in this direction showing more complicated project.

Maybe you could make a converter for my HD44780-based Character-LCD to work on USB. Currently it's using the LPT port.
More info on this:
http://www.electronics-lab.com/projects/pc/020/index.html

BTW: here is my LCD:
http://www.blue25.net/page/my-hd44780-character-lcd.html

One again - great show guys!

andrewgee
07-19-2007, 01:32 PM
Hi there! UK viewer here. After watching I became very interested and I'm currently trying to locate all the components. I found digi-key have a UK site, which I have been able to find a lot of the components from. I'm having problems finding an IC PC Board like the one from radio shack that was linked on the podcast page.

Two other questions...

What sort of cable should I use to link connections? For example for the parallel cable to the processor.

And the other is what type of solder should I be using? I've found that there are multiple types.

Thanks for the great show! Keep it up!

harshbarj
07-20-2007, 12:45 AM
What sort of cable should I use to link connections? For example for the parallel cable to the processor.

Any slim wire should work fine. I'd stick with multi strand wire and not solid as it's simpler to solder(from my experience) and easer to work with (its flexible while solid wire is stiff). I have a ton of old parallel cables that are perfect for most all low voltage projects like this.


And the other is what type of solder should I be using? I've found that there are multiple types.

Any rosin core solder should do as long as it's electrical and not pluming.
look for something like this-\/
http://www.radioshack.com/product/index.jsp?productId=2062715&cp=&sr=1&origkw=solder&kw=solder&parentPage=search

Olorcain
07-20-2007, 07:30 PM
I definitely plan on learning how to program in Assembly... I was at my local college campus and there were some physics professors that were retiring and they had some old programming IBM 360 and assembly programming books they were giving away free. They're quite old indeed but I grabbed them up anyway. I plan on checking out the other aforementioned resources on programming, thanks for all the information on how to get started with Assembly

MasterQ
07-20-2007, 11:10 PM
Hey Dave, great show on the AVR! I went out and bought a ATtiny2313 from Jameco right after watching and I'm playing around with it now.

I noticed that the diagram in the article for this episode doesn't match the info that The Real Elliot gave. It looks like the pins on the diagram should be flipped horizontally to match The Real Elliot's info.

EDIT: I'm no expert at this stuff but I'm 99.9% sure that the Rev3 diagram is incorrect because pin 10 on the 2313 is the ground and that pin isn't grounded in the diagram

brokentoaster
07-21-2007, 10:30 AM
EDIT: I'm no expert at this stuff but I'm 99.9% sure that the Rev3 diagram is incorrect because pin 10 on the 2313 is the ground and that pin isn't grounded in the diagram

It does look wrong comparing to the Datasheet for the AT90S2313 and the newer ATtiny2313. Perhaps the original example used a different chip?

Dave: Loved the episode a great way to get people intereseted in micro controllers. Isn't atmel pronounced at-mel rather than a-t-mel? ;) sorry just being picky ( I'm from NZ myself so am always pronouncing things funny :) )

EDIT: A thorough check revels that the pin numbers are incorrect but the layout is correct for looking at the underside of the chip.
an image I edited with the consistent pin numbers is at http://brokentoaster.com/parallel.jpg

Olorcain
07-21-2007, 02:17 PM
http://www.digikey.com/
http://www.grainger.com/Grainger/wwg/start.shtml


Also I use jameco for a lot of parts.

http://www.jameco.com/

I just ordered all three of these catalogs....I used to have a digikey catalog several years ago but thanks for mentioning these other ones. ;)

Dougman
07-22-2007, 03:28 AM
Does anyone know where i can get the IC (ATtiny2313) in Ontario, Canada?

MasterQ
07-22-2007, 11:38 PM
Does anyone know where i can get the IC (ATtiny2313) in Ontario, Canada?

Any good electronics store should carry either the ATtiny2313 or another atmel avr. They don't carry them at RipoffShack though. You can always order it from jameco or digikey too.

harshbarj
07-26-2007, 04:08 PM
I have 5 of these little babies coming. Can't wait to start tinkering. I even still have a 2x20 lcd I can play with. I'm planning on trying to get a ps/2 keyboard working with one. :D

harshbarj
08-01-2007, 06:04 AM
Got them in Monday. These things are fast (got the 20mhz versions). Only problem I'm having is making the led light at full brightness. The example single led code works at full brightness. I'll keep looking around and see if I can find a fix. Once I figure out how to activate each port correctly I'm going to try getting my 2x20 lcd working.

tokenuser
08-01-2007, 12:19 PM
Any good electronics store should carry either the ATtiny2313 or another atmel avr. They don't carry them at RipoffShack though. You can always order it from jameco or digikey too.Dont discount what you can get at the Radioshack (calling them the Ripoffshack is just as lame as telling people not to get Microsux Windoze from WorstBuy - it just makes you look like a ****ing idiot).

Advantage of RadioShack - they don't stock everything in the store, BUT they can order just about any part you need. How is this different to digikey or jameco? Delivery. RadioShack parts can be ordered for instore pickup. This is free.

Consider that Digikey and Jameco add something like a $5 S&H fee for delivery to you. Suddenly the part that cost $1.50 at Radioshack that costs you $0.50 at Digikey is costing you $5.50.

You need to be a savvy shopper and consider shipping costs, but Radioshack will generally win out for the one off small parts.

tempesta
08-03-2007, 02:04 PM
or spelling microsoft with an $

harshbarj
08-07-2007, 04:11 PM
Got my first working project. I made a 5x5 led display and it works. I'll post pictures / code later tonight (be kind on my coding :D )

harshbarj
08-08-2007, 08:37 PM
ok. I can't get the camera working but I do have the code. It's quickly written so it's sloppy (and it's based off the led blinker from the show).



/* Blinker Demo */

/* Include useful pre-defined functions */
#include <avr/interrupt.h> // Defines pins, ports, etc to make programs easier to read
#define F_CPU 100000UL // Sets up the default speed for delay.h
#include <util/delay.h>
int main(){

DDRD = _BV(PD0)+_BV(PD4)+_BV(PD3)+_BV(PD2)+_BV(PD1); /* enable output on port D, pin 4 */
DDRB = _BV(PB0)+_BV(PB1)+_BV(PB2)+_BV(PB3)+_BV(PB4);
int x;
int y;
int m1[5][5][2];
//A
m1[1][1][1]=0;
m1[1][2][1]=1;
m1[1][3][1]=1;
m1[1][4][1]=1;
m1[1][5][1]=0;

m1[2][1][1]=1;
m1[2][2][1]=0;
m1[2][3][1]=0;
m1[2][4][1]=0;
m1[2][5][1]=1;

m1[3][1][1]=1;
m1[3][2][1]=1;
m1[3][3][1]=1;
m1[3][4][1]=1;
m1[3][5][1]=1;

m1[4][1][1]=1;
m1[4][2][1]=0;
m1[4][3][1]=0;
m1[4][4][1]=0;
m1[4][5][1]=1;

m1[5][1][1]=1;
m1[5][2][1]=0;
m1[5][3][1]=0;
m1[5][4][1]=0;
m1[5][5][1]=1;
//B
m1[1][1][2]=1;
m1[1][2][2]=1;
m1[1][3][2]=1;
m1[1][4][2]=1;
m1[1][5][2]=0;

m1[2][1][2]=1;
m1[2][2][2]=0;
m1[2][3][2]=0;
m1[2][4][2]=1;
m1[2][5][2]=0;

m1[3][1][2]=1;
m1[3][2][2]=1;
m1[3][3][2]=1;
m1[3][4][2]=1;
m1[3][5][2]=1;

m1[4][1][2]=1;
m1[4][2][2]=0;
m1[4][3][2]=0;
m1[4][4][2]=0;
m1[4][5][2]=1;

m1[5][1][2]=1;
m1[5][2][2]=1;
m1[5][3][2]=1;
m1[5][4][2]=1;
m1[5][5][2]=1;
int f=0;
int z=2;
while(1){
for (y=1; y<=5;y++){
for (x=1; x<=5;x++){
f++;
if (f>=5000){
f=0;
z++;
}
if (z>=3){
z=1;
}
if (y==1){
PORTB=_BV(PB0);
}
if (y==2){
PORTB=_BV(PB1);
}
if (y==3){
PORTB=_BV(PB2);
}
if (y==4){
PORTB=_BV(PB3);
}
if (y==5){
PORTB=_BV(PB4);
}
if (m1[y][x][z]==1){
if (x==1){
PORTD=_BV(PD0);
}

if (x==2){
PORTD=_BV(PD1);
}

if (x==3){
PORTD=_BV(PD2);
}

if (x==4){
PORTD=_BV(PD3);
}

if (x==5){
PORTD=_BV(PD4);
}
}


}
}
}

return(0);
}

Drevious
11-07-2007, 04:54 PM
Has anyone noticed that the reference image on the Revision3 site is wrong? The pin outs on the parallel port go to the wrong pins on the attiny2313. I've tried contacting systm@revision3.com but they are non responsive.

ciband
12-20-2007, 03:41 AM
First off I loved this episode. I'm a software engineer by day and this makes me want to get into the hardware aspect of things. To tweak a bit more optimization out of the code given in the show we could change the

while(1)
{
...
}

to

for( ; ; )
{
...
}

This would eliminate the conditional check at the start of each iteration of the loop. Very minor but something I've picked up in my career so I'd thought I'd share. Please correct me if I'm missing something obvious.

scienceking
12-20-2007, 01:23 PM
while(1)
{
...
}

to

for( ; ; )
{
...
}

This would eliminate the conditional check at the start of each iteration of the loop. Very minor but something I've picked up in my career so I'd thought I'd share. Please correct me if I'm missing something obvious.

Not necessarily. When programming in a high level language like C, you don't know exactly how the compiler will optimize that. But you are right in general, as most popular microcontroller compilers do not do a very good job at even such obvious optimizations at all. Really all we want in this case in the assembly code is a non-conditional jump/branch at the end of the block to the starting address, and the compiler has to figure out(hopefully) to do this by either loop interpretation. The standard way the for loop is sort of mapped to assembler makes it pretty clear that for(;;) will always give us what we want, but many compilers are smart enough to map while(1) the same way.


//There are many similar more problematic hacks like the fact that some compliers will turn something like:
f=0



;by habit into a sequence like
mov ax,0
;when it is common knowledge that on many CPUs the more efficient way would be
xor ax,ax


This is why its important to code performance critical stuff in assembler. But remember, performance optimization on a micro usually only matters if it will let you run the micro's clock at a lower speed or something like that, its not like a computer where the developer can't assume he can abuse 100% of the hardware resources just for himself. So if you are running at the same clock frequency(power usage level), there really is no such thing as one program being more "efficient" than another if both of them work fine in the embedded world.

vartok
01-09-2008, 09:02 AM
Hi guys... i just recently discovered Systm and was going back an watching all of the episodes... I saw this one and remembed an old case mod i wanted to do.

what im wanting to do is have 20 or 25 LEDs running up the side of my case and have them turn in in sinquence, representing the current CPU usage and maybe another set on the other side for RAM usage.... would that be possible to do with this type of IC?

If it is, could someone point me in the right direction on where a good plase to start would be. If not... any ideas on how i would go about doing this?

AscendedDaniel
01-09-2008, 09:39 AM
Hi guys... i just recently discovered Systm and was going back an watching all of the episodes... I saw this one and remembed an old case mod i wanted to do.

what im wanting to do is have 20 or 25 LEDs running up the side of my case and have them turn in in sinquence, representing the current CPU usage and maybe another set on the other side for RAM usage.... would that be possible to do with this type of IC?

If it is, could someone point me in the right direction on where a good plase to start would be. If not... any ideas on how i would go about doing this?

I've had great success using the Diolan U2C-12 (http://www.diolan.com/i2c/u2c12.html). It has 23 GPIO pins, among other interfaces, and has open source drivers for Linux, Mac OS X, and Windows. I got it running under Debian. I have also heard a lot about the Arduino (http://blog.makezine.com/archive/arduino/). Post back if you want help getting the U2C-12 online. As for the Arduino, Make has a lot of stuff on it. Specifically, check out this post (http://blog.makezine.com/archive/2007/12/arduinopowered_holiday_li.html), which shows how to only use a few pins to control a great number of lights. I'd be interested to hear how your project turns out.

zeppelin06
01-11-2008, 03:58 PM
hi guys, I'm having trouble getting the hex spit out from the programmers notepad. I keep getting an error that says something like no makefile target. I have the makefile in the same dir as led.c. Can someone please post/send me the hex. Also skipping that step, pony prog does not see the avr. Is this do to bad soldering? I can post pics to flickr or something if someone wants to see them. Thanks

this is my first project and I have the tiny USB kit from adafruit waiting to be put together