Introduction: Getting Started With Atmel AVR and BASCOM


I have seen plenty of Instructables showing how to work with microprocessors, but they all assume that you have worked with them before and know what you are doing. I have not seen an Instructable that takes you from nothing and builds on each step.

What we will do here is to start with a bare breadboard and build each connection and each component until we have everything we need to program a microcontroller to do something. In this Instructable we will blink some LEDs in sequence... then if you build this circuit... your first project can be to change the code slightly to make it into a traffic light.

I picked an older Atmel chip, the Tiny-26 to get started. It is a smaller microprocessor, very inexpensive, and easy to understand. Once you understand what we are doing here, you may want to try a more powerful chip like the Mega-328P which has more pins and more memory.

Note: The Tiny-261, Tiny-461, and Tiny-861 are pin compatible newer versions of the Tiny-26. They have 2K, 4K, and 8K of memory. If used, simply change the header by selecting the appropriate chip and recompile the program to use the newer version. The new chips have more functions that can be assigned to each pin. See the datasheets for more details.
Tiny 261, 461, 861 Datasheet (PDF)
Tiny 26Datasheet (PDF)

Below is an image with the pins for the chip we will be using... we will be connecting the power and ground... in this case 5v. So where do we get 5 volts? We will build a power supply from a 9v battery.

Let's get started!

Video posted in a larger size at: http://www.youtube.com/watch?v=Jxica6Yenh8


Step 1: Breadboards and Building Circuits.

Below you can see a breadboard... they come in many sizes depending on how complex the circuit you want to build will be. We will use a medium sized breadboard with enough room for our power supply, the microprocessor, and the LEDs we want to control.

Step 2: Starting With the Power Supply

We will use a voltage regulator called a 7805. It has three pins, the first is the input, next is the ground, and last is the output voltage... in this case 5 volts. The chip needs to have greater than 6 volts to be able to regulate it down to 5 volts... but it cannot have more than 36 volts. The 9 volt battery works well in this application.

We also want to install a diode into the circuit... it will allow current to flow in only one direction. The reason we install it is so that if we connect the battery backwards it will not let the current flow and will protect our circuit from damage. The diode has a line printed on it, this should be on the most negative side, in our case... it connects to the input of the voltage regulator.

The 9 volt battery clip uses stranded wires... they do not plug into a breadboard very well so we need to make it so that we can install the connections properly. We will use what is called a SIP header. (Single Inline Pins) The wires are soldered to the pins and then can be inserted into the breadboard.

If you look at image #3, you can see that the wires are soldered to TWO pins. This is done to provide a stronger connection that won't turn or jiggle loose. (It's a good tip to remember! )

We insert the pins into the breadboard and install the diode. (Image #4)

In Image #5 you can see that we have added a capacitor... it is a 47uF rated at least 6 volts... these are 25 volts... so they are fine. They act to smooth out the voltage when things like LEDs turn on and off. One way to think of them is like the water tank in your toilet... when you flush you need a large flow of water all at once... but the supply line is very small. The tank holds enough water to even out the flow. Likewise, the capacitor holds extra power for when there is a surge from things starting and stopping.

One capacitor goes between the input to the voltage regulator and the ground, the other goes between the ground and the regulated 5 volts. The capacitor has a marking to show which side connects to ground.

In Image #6 and Image #7 you can see the completed power supply.

Step 3: Wires and Connections

You will notice that my connections to the board are very neat and easy to follow. (At least I hope so!) I am using a wire kit that is available online from several places. The kits are about $10 to $20 depending where you look... and refills are available from places like Digikey .

The kits make your wire connections short, clean, and easy to follow when you come back to them in the future. The refills are a bit pricey, but if you get into electronics quite a bit you will find that you don't want to live without them.

A great way to get started is to find some 22 gauge to 24 gauge SOLID wire. You don't want stranded wires in a breadboard... they just don't work well. Solid wire is useful when you want it stiff and bendable as we are doing here. Stranded wire is used when it must be flexible.

If you see the local telephone repair person ask if they have any scrap 25 pair cable. They often get it in 500 foot rolls... and if it has less than 20 or so feet will often throw it away. The cable has a string that when pulled will cut the jacket and give you a whole bunch of wires as shown in Image #3 .

Step 4: How Do We Know If It's Working???

So now we have a power supply for the microprocessor and out LEDs. But how do we know if it is working? We can install an LED so that when the power supply is on it will illuminate. This is also a good time to teach you about resistors. We will be working with 5 volts... but an LED is designed to run on only 2 volts. (Approximate, they vary depending on the type. )

If we were to connect a 2v LED to the 5v supply, it would get very bright for a very short period of time... then POOF! Here is where you need a little math... don't worry... it's not too bad.

If we look at the specification sheet for the LED, it says that it runs on 2 volts at 20mA. Okay... let's start with the voltage... 5 volts minus 2 volts is 3 volts. So we have 3 volts too much for the LED. (Told you the math wasn't too hard.)

Okay... the LED runs on 20mA... that's the same as 0.020 amps. (mA are 1/1000 of an amp... so 1000mA = 1A) We know that we have 3 volts too much... so divide that by the current that the LED is supposed to run at...  3 divided by 0.020 = 150.

So we know that we need to have a resistance of 150 ohms to slow down the flow of electricity to a point where it won't burn out the led. Voltage / Current = Ohms.

We install our resistor (150 ohms) and our LED onto the board... now we know if we have power or not!

Step 5: Adding the Microprocessor

We now have a working power supply, we understand how a breadboard works, and we have the wires... let's connect up a microprocessor and do something!

On order to make the microprocessor do anything, we need a way to get the software into it. There are four signals that send the data to and from the microprocessor during programming, they need to be connected to a programmer. If you have a PC or Laptop with a parallel printer port, the BASCOM software we will be using later has plans to make a very simple programmer. If you only have USB ports, then you can purchase a USB programmer. There are links on the BASCOM page.

To connect your programmer to the microprocessor, there is usually a 6-pin (or sometimes an older 10-pin) connector. The folks at Sparkfun have made a great adapter as shown in images #1 and #2. Once you solder in your pins, simply plug it into the breadboard and connect your wires. You can get yours here: http://www.sparkfun.com/products/8508

Image #3 and #4 show how we are installing the Tiny-26 microprocessor into the breadboard. Note that we rotated the breadboard 180 degrees so the text would face correctly in the photographs.

Next we connect the power and ground connections, then make the jumpers for the following signals from the Sparkfun board to the programming pins of the chip.

* MISO - Data into the chip from the PC.
* SCK - System Clock and timing.
* RESET - Reset, tells the chip to enter programming mode.
* MOSI - Data out form the chip to the PC.

We also have connections for power and ground. Image #7 shows where we want to install the programmer, Image #8 shows the connections, and #9 shows it all assembled.

Image #10 shows the names and connections of the Tiny-26 Pins. You can see that we connected MOSI to MOSI on the chip for example. If you get MOSI and MISO reversed it won't work... not that I ever did that. :-/

Step 6: Connecting the LEDs

We need something to control with this microprocessor... let's add three LEDs to the circuit.

This chip has two ports named PORTA and PORTB as can be seen in image #1 . (Look familiar?)

We could have connected the LEDs to PORTA , but I wanted to show that you can use the programming port to also control things. Connecting something like a motor controller to the programming port would not be a good idea... the motor would turn on and off uncontrollably during programming. But with the LEDs connected, you will see them blink as we program the chip. I just think that looks kool too.

The chip can support up to 20mA per pin... so a single LED on the pin is fine. If you wanted to connect multiple LEDs to a pin, then you would need to install something to drive the needed power... like a 2N2222 transistor or similar. (But that's for another Instructable!)

If you look at the remaining images you will see that the LEDs are connected and the resistors are installed. Remember that the LED needs to be installed correctly, the FLAT side is connected to ground via the resistor.

Step 7: Programming the Chip

There are many programming languages to choose from for programming the Atmel series of chips. Some people like to use assembly, others prefer C. I have been programming in BASIC since 1978 so I like to use that language. There is a GREAT version of BASIC for the Atmel that is very powerful and easy to learn, it's called BASCOM . You can download it and get more information here: http://www.mcselec.com/index.php?option=com_content&task=view&id=14&Itemid=41

The demo version will allow you to program up to 4K of memory space... and since this is a 2K microprocessor... that will never be an issue. When your programs get bigger and you migrate to more powerful chips, the program only costs about $80 which is a real bargain for all it does.

Once you install BASCOM , the screen will look something like image #1

Image #2: Select options, compiler, then chip. a menu screen will open up.

Image #3: Select the TINY26 from the list. then click the ADD TO CODE button which will add the commands tot he code so that you won't have to keep selecting the chip type. It defaults with a speed of 4MHZ for the crystal... and needs to be changed to 1MHZ since we will use the internal clock of the chip. The line should read...

$CRYSTAL = 1000000

Image #4: Here you can see the code that was generated. It tells the software what kind of chip is selected, what speed we are going to run it at, and it has some other (optional) data to define how the hardware is configured. Once this is in the software, it knows everything needed to program the chip. It wouldn't do anything we would call useful... but it would program okay.

Image #5: This is our program... let's go through it.
------------------------------------------------------
$regfile = "attiny26.dat"
$crystal = 1000000
$hwstack = 32
$swstack = 8
$framesize = 24

Config PORTA = Output
Config PORTB = Output

RED Alias PORTB.0
YEL Alias PORTB.1
GRN Alias PORTB.2

Begin:

Red = 1 : Yel = 0 : Grn = 0
Wait 1

Red = 0 : Yel = 1 : Grn = 0
Wait 1

Red = 0 : Yel = 0 : Grn = 1
Wait 1

Goto Begin
------------------------------------------------------

The first section sets up the chip, then we need to configure the two ports. A port can be an INPUT or an OUTPUT. Since we want to run some LEDs, we set the port to be an OUTPUT. May as well define them all at one time... so we did.

The next section is where we define the pin names. I don't know about you... but I would forget which pin the RED LED was connected to, or the green, or the yellow. I don't feel like typing in PORTB.0 for the first pin every time... so we told the software that it's name was "RED". Now all we need to do is reference it by it's name.

Once defined, if we make them equal a "1" the LED will turn ON, and if we make it equal to a "0" it would turn OFF. The next series of lines defines how we want the LEDs to be set, then waits 1 second. (The WAIT command.)

After we change the state of the LEDs 3 times... we jump back to the beginning and do it all over again... over and over.

Image #6: To get the software into the chip we must first COMPILE it into something it understands. Clicking on the black chip will run the compiler... this makes a HEX file that can be loaded into the chip. If there are any errors they will be shown at the bottom of the screen and you will need to correct them.

Image #7 : When you click the green chip, the programmer opens up. If the chip is connected properly, the programmer screen will display. If not, it will say that it can't find chip FFFFFF and you will need to correct the problem.

Image #8: Once you get the programming screen to show up, simply click the green chip on that display and the program will be loaded into your chip... once finished, your chip will start running your program. You can disconnect the PC or Laptop and your chip will run your program all by it's-self.

Step 8: It's Alive!

At this point your microprocessor will be running the LEDs in sequence...

Red --> Yellow ---> Green ---> Red...

They will each light for 1 second. If you change this...

WAIT 1

to this...

WAITMS 100

Then it will blink every 100 milliseconds (1/10 of a second.)

Can you change the code to make a traffic light? (Hint, change the time settings. )

With the addition of only 2 more parts we were able to have an LCD screen running. You can see some images of that on my BLOG page .

Stay tuned for more information, downloads, and videos. If you like... visit me at http://askjerry.info to see my tutorials, blogs, and more.

Jerry

Just in...

I got with Mark Alberts (author of BASCOM) and asked him which USB programmers he recommended for use with BASCOM. Here are his favorites.

1) Atmel AVRISP mkII In-System Programmer (ATAVRISP2)
2) USBasp - USB programmer for Atmel AVR controllers

So get one and dive in!

Comments

author
frank26080115 (author)2011-02-09

What would you say is the advantage of using BASIC over C?

author
askjerry (author)frank260801152011-02-09

Speaking strictly of the language, it's whatever you are familiar with. From the standpoint of the IDE (programming software) the BASCOM is much easier to install and implement than something like the AVR Studio.

I installed BASCOM, typed in a demo program, hit compile, hit program, and it worked the first time. I installed AVR Studio, took about 40 minutes to configure it, another 20 to figure out how to load the software, then never really did figure out how to compile it, which files needed to be included, etc.

Frankly, I gave up and went back to programming in BASCOM because I knew that I could use it and complete my projects. So far the only C type IDE that even comes close to being easy to understand is the one for the Arduino. (I'm currently learning how to use that now.)

Someone who started off with C may have a different opinion... but I have taught about 350 to 400 people how to program in BASCOM... the youngest was about 7 at the time. So the advantage... easy to set up and learn while still being powerful. If there is enough interest, I'll do another session showing how to make an LCD run. (That's what I did in my BLOG.)

author
Mariel KateL (author)askjerry2016-10-01

Hello. I'm new in programming. I used a sketch I got from an instructable that used BASCOM. I always seem to have this error when I click the program button:

stk500_2_ReceiveMessage(): timeout:-1

and

ser_send: port not open or ser_read: port not open

I'm using Arduino Uno r3 (328) and WIndows 10.

Hope you can help. Thanks.

author
askjerry (author)Mariel KateL2016-10-01

It sounds like you have an incorrect serial port referenced... so it is attempting to send the commands, but not to the correct location. If you are using BASCOM, then try looking for information on the BASCOM forum.

http://www.mcselec.com/index2.php?option=com_forum&Itemid=59

author
janw (author)askjerry2011-02-13

+1

BASCOM is so userfriendly and very intuïtive. It only takes about 5 minutes to setup the program for your programmer and off you go.

Bascom also makes working with all kinds of different communicationprotocols very easy. I2C, 1wire,.... they have all their set of easy to use commands.

author
askjerry (author)janw2011-02-13

I know... I have a compass module that uses I2C, all I need to do is use the I2CSEND and I2CRECEIVE commands... very easy. Likely I'll do more tutorials on this as I don't see very many.

Most people go to the Arduino boards... which are okay, but you need to have a pre-programmed Atmel chip. With BASCOM you just get it from the factory and go. Plus... if you make a project that you want to mas produce, you can order the chips pre-programmed from the factory with your HEX code already in them ready to be installed.

author
frank26080115 (author)askjerry2011-02-09

Ah, I overcome the bad IDE problem by just writing my own
http://code.google.com/p/avr-project-ide/
Now I use NetBeans too

About "which files needed to be included", check out http://www.nongnu.org/avr-libc/user-manual/modules.html

author
askjerry (author)frank260801152011-02-09

A bit over my head at the moment... but I'm going to pass the information along to the folks at the robot group and see what they have to say about it.

Austin - http://therobotgroup.org
St Louis - http://robomo.com

Thanks,
Jerry

author

It's more easy to learn, use and debug. The problem with basic is that you can't do evrything with it.

author
mohammadsalem94 (author)2015-03-04

I'm trying to make it with atmega8L
but bascom avr can't recognize to mcu or to programer
the problem is just from atmega 8 or what?

author
askjerry (author)mohammadsalem942015-03-04

Be sure that you select the proper chip type from the drop-down menu and add it to the code. (Replacing the Tiny-26 code data.)

If not... when you compile... you tell it to look for a Tiny-26 and anything else will fail.

I didn't see a Mega 8L in the drop down... but I did see the Mega-8...

$regfile = "m8def.dat"
$crystal = 8000000
$hwstack = 20
$swstack = 8
$framesize = 24

See if that works for you... this presumes the chip is running at 8MHz.

Jerry

author
mohammadsalem94 (author)2014-08-13

Great work.. thanks

author
Qtechknow (author)2011-09-17

Which program should c/c++ programmers use for the atmega 328?

author
askjerry (author)Qtechknow2012-08-31

You can use the AVR Studio, the current build is Version 5.
http://www.atmel.com/microsite/avr_studio_5/

author
Qtechknow (author)askjerry2012-09-01

Thanks! Is it compatible with Mac OSX?

author
jamilm9 (author)Qtechknow2012-09-07

Use Eclipse for Mac

author
askjerry (author)Qtechknow2012-09-01

This is the download page: http://www.atmel.com/tools/ATMELSTUDIO.aspx

I did not see anything that referenced Linux nor MAC OS, sorry.

author
Mic100 (author)2012-01-19

Good system with the 9Volts Battery
Tanks :)

author
askjerry (author)Mic1002012-01-19

I have been doing it that way for a very long time. There are other more efficient chips out there... but this uses the least components and is easy for everyone to understand.

Many components today use 3 volts, two AA type batteries provide this (1.5v each) but if you use rechargables they can be 1.2v each or 2.4v total... so you need to watch that. Sometimes I'll use 3 of the AAA rechargables... 3.6v... that seems to work... then use a ZENNER diode to clamp it at 3.0v.

...probably another Instructable!

author
janw (author)2011-02-13

Great instructable!

author
AgeingHippy (author)2011-02-09

Hi Jerry

Nice and clear. Thanks.

I am wondering though, PB0-PB7 can only all be input or all be output?

I have a little (very little) experiance with arduino and my inderstanding was that one can define a pin (as apposed to a port = 8 pins) to input or output.

Is this perhaps a diferent type of chip or is defining the port as input /output a shortcut for defining all 8 pins seperately?

Also, what might be usefull would be a circuit diagram to match to your boards.

Thanks

author
AgeingHippy (author)AgeingHippy2011-02-09

Sorry - also wanted to ask more.

Does the chip already have a bootloader on it when programming it with BASCOM or does it not need one?

author
askjerry (author)AgeingHippy2011-02-09

No bootloader is needed. We are programming directly into the chip as it comes from the factory. In fact, we could actually load the bootloader HEX file into the proper chip and make our own Arduino chips. (But I prefer to have more control over how each pin and function is used.)

author
AgeingHippy (author)askjerry2011-02-09

Great.

Thanks for your informative replies :)

author
askjerry (author)AgeingHippy2011-02-09

Any pin can be an input or an output. Different chips in the series can also have pins assigned as analog inputs instead of just digital 1 or 0. If you look at the pin connections for this chip, you can see that PORTA has other functions (ADC0 through ADC6) which are analog to digital converters.

I set the whole port for simplicity, but I could have used DDRA and DDRB to configure each pin.

DDRB =&B00000111

"0" is an input, and "1" is an output... in our example "&B00000111" will set PORTB.0, PORTB.1, and PORTB.2 to OUTPUTS, (right to left) the remainder being INPUTS.

Video added... drawing in the works now.

The Arduino is built using an Atmel AVR, but they have loaded software (a bootloader) into the chip already. This defines certain pins and functions for you. We are going a step above that... we are taking a raw chip and programming and defining it the way we want to.

About This Instructable

32,884views

45favorites

License:

Bio: I like to create YouTube videos and have a channel. I also like to work with CNC, electronics, and I build robots. It's been ... More »
More by askjerry:Learning to Program the Arduino - Part 1Steadycam Adjustment and Display StandRobot Kits For Kids
Add instructable to: