You are on page 1of 8

Contents 

 
About this Tutorial .......................................................................................................................... 2 
Important Information .................................................................................................................... 2 
Create Codevision Project ............................................................................................................... 3 
Include .H and .C files in CodeVision ............................................................................................... 6 
Sample .H File .................................................................................................................................. 7 
Sample .C File .................................................................................................................................. 8 
 

   

SJVE    Page 1 of 8 
 
About this Tutorial 
 
This tutorial will guide you on how to create a project in CodeVision.  Furthermore, it explains how to 
burn the code onto an Atmega chip using STK500 board and program the microcontroller to output 
serial data that you can read to and from the microcontroller.  Finally, there is a section that explains 
how to setup header files in CodeVision 

Important Information 
 

1. The clock on the STK500 kit is 3.686Mhz 
The clock on the PCB Kit is 16.000Mhz 
2. Do not change the fuse bit settings in CodeVision unless you know what you are doing.  
Changing fuse bit setting can disable the Atmega microcontroller 

   

SJVE    Page 2 of 8 
 
Selecting the chip type lets you choose 
features of the chip.  You must select 
Create Codevision Project  the correct frequency of the Atmega 
  otherwise many functions like UART 
and delay function calls will not work as 
1. Open CodeVision 
intended 
2. Click File Æ New  
4. In the next figure, user can choose the 
settings for UART.  This will enable the 
use of printf and scanf statements.  
UART enables you to perform 
input/output functions for the Atmega 
microcontroller and it is also useful to 
debug code. 

 
3. After clicking Yes in the previous 
prompt, you can now use CodeVision to 
automatically generate template code 
for Atmega microcontroller. 

 
5. Select File Æ Generate, Save and Exit  
6. You can now name your project files 
and save them into a project folder. 
7. CodeVision will generate the code and 
now you can burn the code into the 
Atmega, but first, a few settings need to 
be changed. 
 

SJVE    Page 3 of 8 
 
8. Click on Project Æ Configure and go to 
the C Compiler tab 
9. First, if you wish to display floating 
point numbers using printf(“%f”, 
myfloat); then you need to enable this 
feature.  Do not enable this feature 
unless required because it takes a lot of 
code memory space.  Under the 
(s)printf features, select float,width, 
precision if needed, otherwise leave it 
alone. 

 
Normally, you do not need to Verify the 
code so deslect Verify option.  Program 
Fuse Bit(s) is also not needed because 
this can change the way Atmega starts 
up using various clock options.  
Atmega’s come programmed to use 
external clock, which is exactly what the 
PCB kit and the STK500 uses so do not 
change this unless you know what you 
are doing. 
11. Click OK  
12. Scroll down towards the bottom of the 
 
code and observe that your entire code 
10. Now browse to After Make tab.  Check 
needs to be within the while(1) loop.  
the “Program the Chip” option and 
This is an infinite loop and the 
choose the options described by the 
microcontrollers must have one infinite 
next figure: 
loop otherwise they it execute code 
once and stop working.  Add the 
following code inside the while loop: 
“printf(“test\n”); 
delay_ms(100); 
Adding the delay is necessary otherwise 
the microcontroller outputs data so fast 
that the PC buffer will overflow for the 
incoming serial data and your computer 
may freeze while trying to read the data 
from the serial buffer chip. 

SJVE    Page 4 of 8 
 
13. Go to the top of the code and add this  PD0/PD1 to RX/TX and connect serial 
statement: “#include <delay.h>  cable to the 2nd DB9 port. 
14. Go to Settings Æ Programmer and  21. You should now see the microcontroller 
ensure that the COM port you chose is  outputting data. 
indeed the COM port connected to 
STK500 programming port (next to the     
DC power jack) 
15. Press Shift+F9 and then select Program 
the Chip option 
16. The code is ready and microcontroller is 
now printing printf statements, now 
you need to connect the serial cable to 
see the output. 
17. Download “Hercules Setup” by 
searching for it in google. 
18. Open up Hercules Setup and go to the 
Serial Tab.  Choose the COM port that is 
available.  This could be the same as the 
Programming COM port if you do not 
have a 2nd COM port in your computer.  
Choose the speed in bps, which must be 
the same as the one in step 4.  Click 
OPEN to open the COM port. 

 
19. Nothing should print at this point 
because you need to connect the serial 
cable to which the PCB kit of STk500 is 
outputting data to. 
20. For the PCB kit, read the manual and 
connect RX/TX and serial cable to the 
DB9 connector.  For STK500, connect 

SJVE    Page 5 of 8 
 
Include .H and .C files in CodeVision 
 

Many times, for organization purposes, the header file (.h) and the accompanying definition file (.C) 
need to be included in your project.  This can be accomplished in the following ways: 

1. Once CodeVision project has been opened, click on Project Æ Configure 
2. Click Add and browse to the C file you wish to add and click OPEN 
3. Click OK 
4. You do not have to open .H file the same way, but you should open it in case you need to edit 
the header file easily.  On the left side, you can right click on “Other Files” and click OPEN to 
open the header file. 
5. It is recommended to put the .H and .C files in the same folder as the CodeVision project files. 

   

SJVE    Page 6 of 8 
 
Sample .H File 
 
The next figure shows what can be included in the header file.  Important things to note is that functions 
can be declared in this file.  If you are declaring a variable, you must include “extern” prior to the 
variable definition.  This tells the compiler that the actual definition of the variable is somewhere else.  
The sample header file was used to develop a software‐driven UART, so it was once working, but code is 
omitted in the .C file on purpose. 

SJVE    Page 7 of 8 
 
Sample .C File 
 

Note that in the .C file, the variables are defined by the initial value along with the definition of the 
actual functions.  In this file, the only thing required is to include the .H file as included by the statement 
“#include <USART.H>”  In this file, remove the extern keyword from the variables and give them initial 
values.  For arrays, give the array size. 

You can reference the previous two screenshots to develop your own .H and .C files. 

SJVE    Page 8 of 8 
 

You might also like