30
Jan
0

Avr atmega128 – Start Programming with CodeVision a tutorial

In this post, I will explain the base to create a program and load it on the AVR atmega 128.
The program is very simple and just used the leds and output ports.

What You Need

We need :

  • An avr atmega board (like stk128 as I use) and a atmega128 chip.
  • An USB AVR ISP to physically load the program on the chip
  • Code Vision (trial version)
  • AVR Studio

To know what is the Stk128+ board and the USB AVRISP wire please report to my first post about AVR.

AVR atmega 128 board2
AVR STK128+

USB ISP AVR
USB AVR ISP

Install

You need first to install code vision. This software is made for creating program for avr chip as fast as possible :)
You can download it at: http://www.hpinfotech.ro/html/cvavr.htm
Then go in the download page and take the trial limited version (3kbytes code size limited version which is sufficient).

After CV, you need to install AVR Studio by downloading it at: http://www.atmel.com/dyn/resources/prod_documents/AVRStudio4.18SP1.exe

Connect

We need to connect the different components together:

  • Use the USB wire to give supply to your card. This connection is used to give the power to your card. If you want to make your card/chip autonomous you have to change it by a battery.
  • Connect your USB AVR ISP to the ISP port on your card.
  • Connect the port you need from the chip to your card (the is some thing that change depending on your goal).

Here we connect the PORTC to the LED.
PortC to LED

Now we can program on the micro-controller :)

Start Programming

First, run CodeVision and create a new project:
code vision - project
Select to be supported by the code wizard:
code vision - wizard
Then it show you a config windows, here you can do so many things! But we will only change two things:

  • Select the right chip
  • Change Portc to be output

As shown in these pictures:
code vision - project config
code vision - project config portc
Then we can generate the project and the code files.
code vision - project config generate
By doing this, codevision will ask you where to save the files and what names to use. Please, put every thing in the same folder and give the same name to each file (only the type of each file is important).

Examples

As you can see, code vision generates a huge file :) with all you need to work.
My advice is “read the file” read it one time to understand approximately what code vision did.
For example, see the difference between portB (which is input mode) and portC (which is output), as you can see, the commentary and the DDRX change:

// Port B initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTB=0x00;
DDRB=0x00;

// Port C initialization
// Func7=Out Func6=Out Func5=Out Func4=Out Func3=Out Func2=Out Func1=Out Func0=Out
// State7=0 State6=0 State5=0 State4=0 State3=0 State2=0 State1=0 State0=0
PORTC=0x00;
DDRC=0xFF;

What to do now? Well let’s play with the led!
Go at the beginning of the main function and write:

void main(void)
{
// Declare your local variables here
//Personal data
unsigned char led = 0xF0;
unsigned long counter;

Then in the while function we can do what we want, but we have to use this lines:

  • for( counter = 0; counter < 100000 ; counter++); // this will wait a few time, you usually know this function as sleep(ms); I think some of you will say “hey that is tooooo ugly!! Active waiting is to bad what about a real sleep function?” That not wrong but you have to keep in mind that we are working on microcontroller, so if you are using the processor for nothing, it is okay, because your process are the only one on the chip.
  • PORTC = led; // It turns on/off the led. Where the bits of led are 1 the led corresponding are set to on (and 0 for off). For example, if led = 00010110, if you look to your card, the led will be like Off Off Off On Off On On Off
  • Another line to change the content of led in each loop.

Here is my while :

while (1)
{
// Place your code here

// WAIT
for( counter = 0; counter < 100000 ; counter++);
// WAIT MORE if needed uncomment next line
//for( counter = 0; counter < 100000 ; counter++); // SET LED VALUE PORTC = led; // CHANGE VARIABLE VALUE led = led ^ 0xFF;          // SWITCH //led = led >> 1 | led << 7; // LOOP
};

With the code above, you can uncomment/comment the two last lines depending on the behaviour you want. The first (switch) make the led flashing and the second (loop) create a snake.
To build the program you have to click on the build project icon, it will produce a .hex file in your project directory.
code vision - build icons

Load the program on the chip

Run AVR Studio, close the project window and select com (the small icon on the tool bar):
avr studio
Select like me if you have a USB ARVISP
avr studio - com
Then, click on the avr icon in the tool bar and chose your .hex. You can load it by clicking on Program. Be careful to use Flash memory!!
avr studio - isp

After that your program will run on the chip and you will see the led moving. You just need to modify you program in code vision, build it in code vision again, and click on program in avr studio to try your modification.

And After That?

Then, we will see how to use timers to get more precision and to play with buttons to change program behaviour.

SOURCE

Project 0 CodeVision Project

Enjoyed reading this post?
Subscribe to the RSS feed and have all new posts delivered straight to you.

Comments are closed.

Celadon theme by the Themes Boutique