Search This Blog

Powered By Blogger

Sunday, April 18, 2010

Softwares needed

Hmm...this is the part where we will know which softwares are to be used for uC coding,simulation and programming.Well...the architecture of uC and your PC is different and so you can't use your computer compiler softwares to program your uC.Well...there are several compilers for programming avr like avr-gcc,bascom avr etc.they support the coding architecture of PC(For instance,avr-gcc is a c code compiler for avr uCs) but compiled output is different.But you can configure your programming editor(Hope you know the editor you use to write the code and the compiler you use to compile your code is different) with this uC compilers.Well,uCs can also be programmed with assembly language(a low level language) in which you can directly access memory addresses etc. but it's not as user friendly as a mid level language like c or high level language like basic(bascom avr).In this tutorial we will use avr-gcc as our compiler(for assembly,there is a good tutorial here).
Here is the list of softwares.First I am giving you the list for Windows OS,then Linux(Ubuntu,other distributions should also be compatible).

Softwares for windows:

1.AVR Studio:
This is a very good software and a complete IDE for avr uCs by Atmel with built in assembler(assembly compiler).You can also use it to compile C(if you have WinAVR installed) codes.It has a simulator in which you can run your codes to see if they are bug free.You can even program your uC right through this IDE if you have a AVR Studio compatible programmer(List of programmers can be found in it's menu) .This great software is absolutely free and requires only a free registration.The link is here.
Visit the link and download "AVR Studio 4.18"(requires a free registration) then download the service pack(SP2 or SP3).Install AVR Studio 4.18 first then the service pack(because service pack is not the complete software,just an update).Check "install USB driver"(When shown).

2.WinAVR: A collection of compiler and all the files you need(some you don't).A software for programming uC(avrdude) is integrated with it(This is highly necessary if you are using USBasp as programmer).Can be downloaded from here.(That's a open source one,so you can modify it if you want).An integrated editor(Programmer's Notepad) can be used as a code editor for avr-gcc.
N.B. :- It may show some errors if you are using windows vista or 7 (64 bit version). To fix it,you need to patch winavr.The patch can be found here.Download it and follow the instructions(May be you need to replace a file given there as patch).Recent releases of WinAVR should be free from this problems.If not...then write to me.

Proteus VSM:A simply awesome software for simulating circuit you want to make(means you can just draw the circuit in it's workspace and run the circuit in you PC virtually).This software supports a lot of avr uCs and you can just add the compiled file there.You can even design PCBs for your circuits here.Unfortunately...this is not a free software and too much costly(>$2000 or $3000).There are many cracked versions available online.Search them(I can't give it's link here...the admin guys may shut my blog up!).

Well...there are other IDE's available...such as Eclipse.To know how to use it with winavr,click here.

Ponyprog:You can use this one if you are planning to build a parallel/serial programmer stated in the previous chapter.This is a very handy tool and can be downloaded from here.

AVR-Burn-O-Mat:AVRdude is a command line software(aahh...I hate command typing).This is a GUI(Graphical User Interface) for avrdude(means you give command to avrdude via this software by simply clicking things,REMEMBER,this is not an alternative to avrdude,just a GUI for it).Link is here.

Well...I am writing a GUI for avrdude.You will get it soon!It's is not as versatile as Burn-O-Mat but a simple one to use.

Softwares for Linux:

Hmm...the OS with ever growing popularity.Here is the list of softwares for it.

avr-libc,avrdude,uisp : there is no collection like WinAVR available for Linux.In ubuntu,you will get them in your Synaptic Package Manager(Preference menu).Download them and this manager software will automatically mark necessary extra apps needed before download.After downloading,they will be installed automatically.

Kontrollerlab:A very good IDE for Linux.You can even add any avrdude compatible programmer in it.That means it can even be used to program your uC.Link is here.
Download the compatible package from the link.This is the one I am using in my Ubuntu OS.

Eclipse can be used as a IDE for Linux too.

Installing Driver for programmer(Only if you are using USBasp):Hmm...something else I still need to to install the driver for the latest zip archive from then extract it.When you insert the USB cable of your USBasp hardware,a "Found New Hardware Box appears"(If you have already written the firmware into Atmega8 of USBasp and your connections are alright).There you show the "bin/win-driver" folder of your extracted USBasp folder.Then the setup will automatically locate driver for USBasp.
Now,time to test the hardware.Go to command prompt and type "avrdude -c usbasp -p m16" or replace the "m16" with "m8" or "m32" if you are using them as target uC.(No need to worry,I'll explain that!).Most probably,you will see "avrdude: error: could not find USB device "USBasp" with vid=0x16c0 pid=0x5dc".Well, this is because of the version difference between the driver given with avrdude and the one you installed.If you're lucky and you have set everything up,you should see an ok message.Otherwise,if you haven't connected the target uC or if there is any connection error in the connection between the uC and USBasp (Or in the power connection of target uC) and the driver version is perfect then you should see "target doesn't answer" message.If you haven't connected the target uC,then you can jump saying "yeppi,my USB device works!" or if you have,then you can say to yourself,"No problem...just to fix the target uC USBasp is alright!".
Now,if you have got an error message like "could not find USBasp",then unplug the hardware and reconnect it.If this does't work,copy the libusb0.dll & libusb.sys file to "WinAVR/bin" directory.A message will appear saying that these files already exist...but just press yes.Now test it,you should see a positive result.If not,then check your USBasp for connection errors.

Saturday, April 17, 2010

Hardwares needed we are preparing for our voyage to uC world!This tutorial is about the hardwares or precisely programming hardware you need to insert the program into your avr chip.Well... what about other parts you need to make project circuits or your own circuits,right? will get their values in schematics of projects and when you are making a project of your own...that's up to you!
First we need to build a power supply for our uC.uCs are mostly operated with +5V.This should be regulated(specially when you are using timing or communication features or ADC etc. because voltage ripples can cause incorrect data outputs).
This is the Schematic of the power supply for uC.This is actually not a full power supply.It's just the voltage regulator part.Build it according to schematic and don't forget to use a heatsink with 7805 IC(It may get terribly hot when used for a long time)
N.B. :- There are two ground pins in your uC,connect both of them to your circuit ground.
Parts list(picture is fair as schematics , not for seeing parts list):
  1. IC-7805(see Datasheet for pin config)
  2. Capacitor-220uF,16V(others voltage ratings will work)
  3. Resistor-1k
  4. LED
  5. Power connector
  6. Switch
Now let's come to program the uC.avr uCs can be programmed in two ways:
  1. Parallel programming(don't mistake,it doesn't mean programming with parallel port of pc)
  2. Programming through SPI bus(this is what we will discuss,because this the way of in system programming.For informations on parallel programming,see datasheet)
In system programming hardware:Well...there are several programming hardwares for that.We will give schematics of the most popular hardwares.Some of them are commercially available(STK500,AVRISP mk II etc.).If you can afford them...then buy one(But we advise you to have the experience of building one of these hardwares)Here are the schematics:
1.Parallel port programmer:If you have an old PC(Old is gold!),then this one can be the starting programmer for you.There are several parallel port programmers.This one was tested by us.Schematic:
N.B. : SCK,MOSI,MISO are the pins of SPI bus of your uC.Find their pin number from Datasheet(same applicable for RESET)In this circuit, a buffer IC has been used(74HC125).You may be thinking about the strange connection pattern of this circuit(You should be,if not experienced).Don't worry, that circuit is easy.But you need parallel port diagram to make this.Ok,here is it:This is the female side of the DB25 Connector Cable used here.You can directly connect the circuit without using the cable but I recommend you shouldn't(You may get some shocks due to earthing).
The Whole circuit can be built on a breadboard(including the power supply).
After building it,we shall use a program Ponyprog to send the program from PC.(will be discussed in the next chapter)
2.Serial port programmerI haven't seriously tested this one but many use it(may be!).It's a bit complex than the first one.
N.B. : This one is not supported by USB 2 Serial Converters
Here is the Schematic:Here,Serial port config is already given.So I haven't shown it.
N.B. : I don't guarantee if it works but since I know,this is a popular one.
USB programmer: we are getting modern.Well...the programmers stated above are backdated as serial and parallel ports are not available in today's PCs or Laptops(However,if you have an old one,you can still build them...after a will know why I said "Old is gold")To do something with USB port,Hardware driver is needed and to let the PC know what kind of hardware is connected to USB,some id codes and high/low operations are needed.That's why,in almost every USB device,there is a microcontroller or dedicated USB controller chip. Now you may have felt why I said "Old is Gold",because to make a USB programmer,you need a USB controller Chip or uC as driver in which you need to write the hardware program(firmware).Here is a diagram on how USB programmers work.
To know why we need to do that for USB,search google with the following keyword "USB in a nutshell".This is a very good book on USB.
Luckily , if you have an old PC/Laptop,you can make any of the first two programmers(they don't require a uC or controller chip to program your uC) and to be modern,you may use them temporarily to write the driver program(also called firmware) to your USB programmer's driver uC and forget them.Here is the Schematic of one of the most popular(may be the most) USB programmer(that I use at present),USBasp by Thomas Fischl( looks horrible may be but quite easy to make.Here,as driver uC an Atmega48 has been used.But you can also use Atmega8(Even others if you understand the firmware code).This programmer requires a driver.It's available at the website stated above the schematic.
To build this, you need to know the usb pin know this,visit this site.
N.B. :- Some connections are not very necessary in this circuit.For example...the connection between pin 1 and pin 16(self programing) is for firmware upgrading and not important so it can be deducted from the circuit(But NOT THE ONE FROM PIN 16 TO x2).Connections from pin 2 and 3 to X2 are also not important(keep them if you want).X2 is a connector.You can simply use serial rail to make this one(this would be better if you want to keep your uC circuit on breadboard).This circuit can be built on breadboard but I recommend you build it on a veroboard or PCB.PCB Layouts are available at the site stated above schematic.
Some useful pics: My USBasp board The ISP connector of the board(just serial rail....X2 in the schematic)
These pins are connected to Atmega8's MOSI,MISO,SCK,pin 16 and ground....
Others are unnecessary

One thing to tell you about this circuit is that if you are planning to build a PCB in which you will put your target uC to program,then keep the VCCINT(the target uC will be powered by USB).Otherwise,for ISP operation,deduct it.

Getting Started with avr microcontroller

Well...time to stop this history lecture and come to the point.We are using atmel avr microcontroller.Many of you may ask why we are using this one.Well...actually I have already answered it but missed one point.Atmel guys write their datasheets very well and they are easy to understand(Datasheet is the documentation of the device you are using...means pin configs,register descriptions etc.).
Atmega16 is an 8bit avr microcontroller with 16k in system programmable flash.Well...after all these almost non-technical stuffs,the aforesaid one is like instantly changing speed from as slow as a cycle to as fast as a bullet train!!!Well, avr is a computer architecture developed by Atmel.It's a modified harvard architecture system.8bit means the register size(a temporary memory where data can be stored,exchanged etc.) is limited to 8bits and more precisely...the arithmetic logic unit(CPU) can process 8bit data at a time(maximum).Register is like a set of memory blocks.Each of them can store one bit(0 or 1).So each register of avr should have 8blocks for 8bits.There are some 10bit,16bit registers too,but they are actually collections of several 8bit registers(We shall see them in datasheet).
Now...what is that "in system programmable flash" thing?Well...flash is a non-volatile memory and in avr microcontroller(and in many others),this is the memory where the program you have written is kept."In system" means you can program the microcontroller even after installing it in your circuit.That's a great thing because you don't need to remove uC from circuit to program it.You just power your circuit and connect your programming hardware to it and just send the program from your PC or Laptop!
Now...I have stated something new here what I haven't stated before:"Programming Hardware".Well...Since the beginning of this tutorial I have been saying that uC can be "programmed".But how?You can't just load the program into a pendrive and then insert it into uC!You need a circuit that will work as an interfacing medium between the uC and your PC.This is the programming hardware.There are many kinds of programming hardwares available for avr(We will discuss it soon).
Besides, you also need a software that will use the hardware to send the program to uC.Well...we will discuss it too!
Now...let's come to datasheet.I said you need it to know about the uC you are using.Seeing it you can know the features of your uC,pin config, internal devices and how to access them through your code etc.A very good video on accessing the datasheet is about to be published(It's on atmega8 but you can easily get the main theme).Well...let's move to our next chapter.

Friday, April 16, 2010

How to select a perfect microcontroller for your work

Well...after having a basic knowledge of uCs (microcontroller) , we move to an essential part...there are many uC models and manufacturers....which uC should we select for our work,right?'s a bit like choosing other electronic components.For example, for timing pulse operation,we select NE555 IC , not CD4017.Like this, we will select our uC according to what we want to do with it....means what integrated facilities it must have to meet our demands.Cost is also a big factor if you want to go commercial.If your desired work can be done with a $1 uC,then it is impractical to choose a $5 uC for that.Size can also be a big factor.two uCs can be of same price but different size and sometimes you may have to choose the smaller one to make your circuit compact.
Well...internal facilities or hardwares play the main role so this factor has the first priority in case of choosing a suitable uC.Like,for your work , you need an analog to digital converter or a higher clock rate or built in USB hardware(this stuffs will be discussed don't get scared seeing them) etc. etc. etc. There are some uCs that may meet all your demands...means you may use one like them in every application but they may be costly so you have to choose cheaper , specially dedicated uCs("dedicated" doesnt mean these uCs can only do a single job,they can do other jobs but are "specialized" for doing that single job).
A good example is this we are explaining every basic thing of we have to choose a uC in which every general hardware is present(means the common hardwares found in most of the uCs).So we are using Atmega16.There is another reason behind choosing this one.This is a general purpose uC and...almost all the tools you need(software,circuit schematics) to work is free(except hardware) and AVR microcontrollers are good to be used in novice level.

What is microcontroller and why it is used

We live in electronics age.Electronic stuffs are all around us and doing a lot of works for us every day...every moment.Many of them are quite smart and complex too.For example,let's take a look at a very familiar phone!They are like small computers and have so many functionalities. Of course these matter how complex... are built with many know what logic is...simply visit this link. make this logic gates "electronically",there are many logic gate IC's available or you may make them using simple equipments like relays,transistors etc.You can do this when you are building a very simple circuit. But...what about complex ones...huh?Like your mobile phone?there you need to control the LCD,display various informations on it...that requires very complex logic operation,let alone controlling other thing such as network communication,phone book,internet ,application...bla bla bla.Well,if u want to make a phone without microcontroller, then the size would be as big as a truck(!!!!!!!!!!!!!) and to add a new function,you will have to change the entire circuit.Well...there is one thing that will stand with you to face such challenges...microcontroller. Image : Atmega16 Microcontroller (We will use it)
Microcontroller is a small computer in a single chip.I mean,it has almost everything a computer consists of...CPU,RAM,Non- Volatile memory like Hard disk(not actually like that,in a solid state form without any rotating parts).Like a computer you can program it and through it's I/O pins,you can send command or take datas for processing.And logics can be programmed,so you don't need a truck size board now(Yeppi!) .So, your circuits can now be extremely small...highly customizable...and very easy...and secure.Now where is this security stuff coming from...right? want to sell a complex (with many logics) circuit you have built with great labour.You sell it...and someone buys it and copies it and...sells it at a lower price..because he has just copied he can cut the extra cost you added for research(ohhhh)...but if that was a microcontroller based could just "lock" the microcontroller code and sell it and...that copier guy cannot make your circuit because he doesn't have your code to run the circuit(Now do that...copier).So it makes your work easy and safe too.
Now...where can we find microcontrollers?Well...they are everywhere,mobile phone,washing machine,microwave oven,television,printer,photocopier,modem,toys(aaahhh)...and many other.
So,we can say that...they have become a part of our life