You are on page 1of 9

CodeVision Tutorial

0. Getting Started
Folder GetStarted
led.prj, led.c,

AVR033: Getting Started with the CodeVisionAVR C Compiler


Preparation
Install the CodeVisionAVR C Compiler in the default directory: C:\cvavr.
Install the Atmel AVR Studio debugger in the default directory:
C:\Program Files\Atmel\AVR Studio.
The demonstration program to be developed in the next few pages requires an Atmel
AT90S8515 (or ATmega163) microcontroller and the STK500 starter kit.
Set up the starter kit according to the instructions in the STK500 User Guide.
Make sure the power is off and insert the AT90S8515 (ATmega163) chip into the appropriate socket
marked SCKT3000D3 (SCKT3100A3).
Set the VTARGET, RESET, and XTAL1 jumpers. Also set the OSCSEL jumper
between pins 1 and 2.
Connect one 10-pin ribbon cable between the PORTB and LEDs headers.
This will allow displaying the state of AT90S8515s (ATmega163s) PORTB outputs.
Connect one 6-pin ribbon cable between the ISP6PIN and SPROG3 headers.
This will allow the CodeVisionAVR IDE to automatically program the AVR chip after a
successful compilation.
In order to use this feature, one supplementary setting must be done:
Open the CodeVisionAVR IDE and select the Settings|Programmer menu option.
The Programmer Settings window will open. Choose Atmel STK500 as the chip programmer type and
COM1 as the communication port.

Creating a New Project


There are two ways of doing it: using or not the CodeWizard AVR.
Well use the CodeWizard to create led.prj in Folder GetStarted
and tutor1_163.prj in Folder Tutor1_163.
In the CodeVisionAVR IDE, pull down File menu select New, choose
Project as File Type, and then click OK.
Click Yes to use the CodeWizard in response to the question, Do
you want to use the CodeWizardAVR?
In the CodeWizardAVR window,
Click Chip Tab and select ATmega163 as the device and 3.68MHz as
the clock frequency.
Click Ports Tab to initialize PortB:
1

Select OUT as data direction set output value as 1 for all


bits of PORTB.
Click Timers Tab and then click Timer 1:
Choose system clock as clock source,
Choose 3.594kHz (the system clock of 3.68MHz divided by 1024)
as clock value,
Choose Normal top=FFFFh as mode,
Check on the Interrup On box and choose Timer1 Overflow,
Type in f8fb in the Val box (To obtain the frequency of LED
movement of two per second we need to reinitialize the Timer1
value to 0x10000-(3594/2)=0xF8FB on every overflow.
Pull down the File menu of the CodeWizardAVR menu, and click
Generate, Save and Exit.
In the Souce File dialog box, select or creat a new folder, type
a name for the source file, led.c.
In the Project File box, type the name (usually the same name as
the C file) for the project file, led.prj.
In the cwp File box, type the name for the cwp file, led.cwp.
Now we have a skeleton C program in led.c which is ready to be
edited any time.

Project Configuration
In the CodeVisionAVR IDE, pull down Project menu and select
Configure.
In the Configure Project led.prj window,
Select After Make Tab and check on gthe box Program the Chip.
Select C Compile Tab to view and make changes if necessary.
//C Source File: led.c
/*********************************************
This program was produced by the
CodeWizardAVR V1.23.6a Standard
Automatic Program Generator
Copyright 1998-2002 HP InfoTech s.r.l.
http://www.hpinfotech.ro
e-mail:office@hpinfotech.ro , hpinfotech@xnet.ro
Project :
Version :
Date
: 6/14/2003
Author : Dr. B.C. Chang
Company : Drexel University
Comments:

Chip type
: ATmega163
Program type
: Application
Clock frequency
: 3.680000 MHz
Memory model
: Small
Internal SRAM size : 1024
External SRAM size : 0
Data Stack size
: 256
*********************************************/
//led.c
#include <mega163.h>
//the LED 0 on PORTB will be ON
unsigned char led_status=0xFE;

//11111110

// Timer 1 overflow interrupt service routine


interrupt [TIM1_OVF] void timer1_ovf_isr(void)
{
// Reinitialize Timer 1 value
// Frequency od LED movement = two per second
//0x10000-(3594/2)=0xF8FB
TCNT1H=0xF8;
TCNT1L=0xFB;
// Place your code here
//move the LED
led_status<<=1;
//led_status=led_status << 1, i.e., shift to the left by 1
led_status|=1;
//led_status=led_status OR 1
if(led_status==0xFF) led_status=0xFE;
//Turn on the LED
PORTB=led_status;
}
// Declare your global variables here
void main(void)
{
// Declare your local variables here
// Input/Output Ports initialization
// Port B initialization
// Func0=Out Func1=Out Func2=Out Func3=Out Func4=Out Func5=Out Func6=Out
Func7=Out
// State0=1 State1=1 State2=1 State3=1 State4=1 State5=1 State6=1 State7=1
PORTB=0xFF;
DDRB=0xFF;
// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: 3.594 kHz
// Mode: Normal top=FFFFh
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
TCCR1A=0x00;
TCCR1B=0x05;
TCNT1H=0xF8;

TCNT1L=0xFB;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
// External Interrupt(s) initialization
// INT0: Off
// INT1: Off
GIMSK=0x00;
MCUCR=0x00;
// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x04;
//Timer/Counter1 Overflow interrupt Enable
//UBRRHI=0x00; //It was generated by CodeWizardAVD,
// seems to be irerelavant.
// Global enable interrupts
#asm("sei")
while (1);
}

1. Tutor1_163
Folder Tutor1_163
tutor1_163.prj,

tutor1_163.c,

Creating a New Project


There are two ways of doing it: using or not the CodeWizard AVR.
Well use the CodeWizard to create tutor1_163.prj in Folder
Tutor1_163.
In the CodeVisionAVR IDE, pull down File menu select New, choose
Project as File Type, and then click OK.
Click Yes to use the CodeWizard in response to the question, Do
you want to use the CodeWizardAVR?
In the CodeWizardAVR window,
Click Chip Tab and select ATmega163 as the device and 3.68MHz as
the clock frequency.
Click Ports Tab to initialize PortB and PORTD:
Select OUT as data direction set output value as 1 for all
bits of PORTB.
Leave PORTD as default, i.e., IN as data direction and T as
value for all bits of PORTD.
Pull down the File menu of the CodeWizardAVR menu, and click
Generate, Save and Exit.
In the Souce File dialog box, select or creat a new folder, type
a name for the source file, tutor1_163.c.
In the Project File box, type the name (usually the same name as
the C file) for the project file, tutor1_163.prj.
In the cwp File box, type the name for the tutor1_163 file,
led.cwp.
Now we have a skeleton C program in tutor1_163.c which is ready
to be edited.

Project Configuration
In the CodeVisionAVR IDE, pull down Project menu and select
Configure.
In the Configure Project tutor1_163.prj window,
Select After Make Tab and check on gthe box Program the Chip.
Select C Compile Tab to view and make changes if necessary.
5

//C Source File: tutor1_163.c


/*********************************************
This program was produced by the
CodeWizardAVR V1.23.6a Standard
Automatic Program Generator
Copyright 1998-2002 HP InfoTech s.r.l.
http://www.hpinfotech.ro
e-mail:office@hpinfotech.ro , hpinfotech@xnet.ro
Project :
Version :
Date
: 6/14/2003
Author : Dr. B.C. Chang
Company : Drexel University
Comments:

Chip type
: ATmega163
Program type
: Application
Clock frequency
: 3.680000 MHz
Memory model
: Small
Internal SRAM size : 1024
External SRAM size : 0
Data Stack size
: 256
*********************************************/
#include <Mega163.h>
// Declare your global variables here
void main(void)
{
// Declare your local variables here
unsigned char data;
// Input/Output Ports initialization
// Port B initialization
// Func0=Out Func1=Out Func2=Out Func3=Out Func4=Out Func5=Out Func6=Out Func7=Out
// State0=1 State1=1 State2=1 State3=1 State4=1 State5=1 State6=1 State7=1

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

PORTD=0x00;
DDRD=0x00;
while (1)
{
data=PIND; //read the switches
PORTB=data; //write the data to the output
};
}

2. Tutor2_163
Folder Tutor2_163
Tutor2_163.prj,

tutor2_163.c,

Creating a New Project


There are two ways of doing it: using or not the CodeWizard AVR.
Well NOT use the CodeWizard to create tutor2_163.prj in Folder
Tutor2_163.
Create a new C Source file
In the CodeVisionAVR IDE, pull down File menu select New, choose
Source as File Type, and then click OK.
After finishing typing the C source code onto the untitled.c
window, use Save As to save the c file as tutor2_163.c in the
folder Tutor2_163.
Create a new Project file
In the CodeVisionAVR IDE, pull down File menu select New, choose
Project as File Type, and then click OK.
Click No to not use the CodeWizard in response to the question,
Do you want to use the CodeWizardAVR?
Type the new project name as tutor2_163.prj.
In the Configure Project tutor2_163.prj window,
Click File Tab, click Add and choose tutor_163.c.
Click C Compiler Tab, select ATmega163 as the chip and 3.68MHz
as the clock frequency.
Click After Make Tab, check the Program the Chip box.
Click OK.

//C Source File: tutor2_163.c


/*********************************************
Revised version of tutor1_163.c in which the LEDs stay on after
release of the switch
Project :
Version :
Date
: 6/15/2003
Chip type
: ATmega163
Clock frequency
: 3.680000 MHz
Memory model
: Small
Internal SRAM size : 1024
External SRAM size : 0
Data Stack size
: 256
*********************************************/

//tutor2_163.c
#include <mega163.h>
// Declare your global variables here
// global character declarations
//
unsigned char data; // global byte giving last switch press
// Prototype declarations
//
void initialize(void);
unsigned char read_switch_bank(void);
void write_to_LEDs(unsigned char ch);
//
// main program
//
void main(void)
{
unsigned char ch;
initialize();
while (1)
{
ch = read_switch_bank();
if ((ch != data) && (ch != 0xff)) // see if it has changed
{
data = ch;
write_to_LEDs(data);
}
}
}
//
// procedure and function definitions
//
void initialize(void)
{
data = 0xff; // starting value
DDRD = 0x00; // all inputs
DDRB = 0xff; // all outputs
PORTB = 0xff; // start by turning all LEDs off
}
unsigned char read_switch_bank(void)
{
unsigned char ch;
ch = PIND;
return (ch);
}
void write_to_LEDs(unsigned char ch)
{
PORTB = ch;
}

You might also like