Thursday, March 17, 2011

MY FIRST PROGRAM with CodeVisionAVR


Let us try to write a program with codevisionAVR, with our previous knowledge.
Let’s SAY HELLO TO EMBEDDED WORLD.

Here we consider that we are having hardware setup with LEDs connected on the PORTB. We'll try to blink those LEDs

STPE 1:
--------------------------------------------------------
Start CoveVisionAVR.

STEP 2:
--------------------------------------------------------
Click on the GEAR symbol. This will start the CODE Wizard AVR.

STEP 3:
--------------------------------------------------------
Select the chip: Atmega32
Set clock to 16.000000 MHz
Do not disturb the other things!!

STEP 4:
--------------------------------------------------------
Switch to Ports tab.
Change the sub-tab to Port B.
Change the data direction to out by clicking on the In button in front of Bit0 to Bit7 (Encircled with RED circle below!)
By default Output value will be initialized to zero (0)

STEP 5:
--------------------------------------------------------
Click on the File menu for Code Wizard AVR.
Click on the Generate, Save and Exit.

STEP 6:
--------------------------------------------------------
This will open the Save C Compiler Source File, window. As shown below.
Type File name as Hello_embedded and hit save button.

STEP 7:
--------------------------------------------------------
This will open the Save C Compiler Project file, window. As shown below.
Type identical file name as above: Hello_embedded and hit save button.

STEP 8:
-------------------------------------------------------
This will open the Save Code Wizard Project file .cwp, window. As shown below.
Type identical file name as above: Hello_embedded and hit save button.

IMP points
-------------------------------------------------------
NOTE that file names must be identical but file extension must be different. If you check the bin folder
carefully you will find the 3 files as Hello_embedded.c, Hello_embedded.prj, and Hello_embedded.cwp.


STEP 8:
--------------------------------------------------------
This will open the Hello_embedded.c file in codevision window.
(If not so then note pad window is opened click on the minimize button to view Hello_embedded.c (Encircled with RED circle below!))
Here the codevisioAVR generates the automatic code. Just scroll down to see the creation of the CodeVisionAVR.

At first you will encounter Input/output Ports initializations, as shown below! You can notice that CodevisionAVR has auto-initialized the PORTB as output port with initial value 0x00.



STEP 9:
--------------------------------------------------------
Scroll down till you encounter the while loop at the bottom of the file saying ->
// Place your code here



STEP 10:
--------------------------------------------------------
Let us write Code here!!
Logic is ->

  • Wait for few milliseconds
  • Change the status of the PORTB to all ones
  • Wait for few milliseconds
  • Change the status of the PORTB to all zeros

delay_ms(500);
PORTB = 0xFF;
delay_ms(500);
PORTB = 0x00;


STEP 11:
--------------------------------------------------------
Click on the PROJECT > Build All as shown below!!

This will build the project and check it for errors. OPS!! It says 2 errors??


In Step 10: we use one function delay_ms(500);
but we didn't include file in which it is defined !!
  • So to correct those 2 errors click on the OK button in neighboring window.
  • Scroll up the Hello_embedded.c. At the start of file you will find the line saying ->
    #include <mega32.h>
  • Add #include<delay.h> below it.

  • Now again build the project!
  • PROJECT > BUILD ALL
  • And here will not be any error!!
  • To ensure successful compilation check
  • cvavr2>Bin>Exe. This folder must have Hello_embedded.hex file.






7 comments:

  1. thank you so much.. very helpful site for beginner,s like me. please continue your good work. you will have our support. please do add more examples. i am new to codevisionavr. i dont know how to work with ADC. Please post an example of ADC.
    Thank you...
    Saikiran

    ReplyDelete
  2. Hi,
    ADC tutorial will be there soon !!
    check out after 2 or 3 hrs....around 12.30 PM 2da.y

    ReplyDelete
  3. Hi,
    Nice for Beginner's like me. I would like see all the examples on this site, like LCD, RS-232, USB, ADC, DAC, I2C, RTC, Multiplexed 7-Segment, Matrix Keypad, etc.

    ReplyDelete
  4. @ Yogesh,
    With educational version of CV AVR we can have RS-232, ADC, Multiplexed 7-Segment, Matrix Keypad but they don't have support for USB, RTC etc.

    I'll update this as early as possible.

    ReplyDelete
  5. Just take a look at this
    ADC tutorial :

    http://codevisionavr.blogspot.com/2011_05_01_archive.html

    ReplyDelete
  6. I was wondering if you can post examples on using AND, OR and trigonometric functions. It would really help me because I'm having a hard time migrating from AVRSTUDIO structure code...

    ReplyDelete