Professional Documents
Culture Documents
ENGINEERING
C PROGRAMMING IN AVR STUDIO
Overview ................................................................................................................................................. 1
Install Required Programs........................................................................................................................ 1
Install the BootLoader ............................................................................................................................. 1
Create Project in AVR Studio.................................................................................................................... 2
Configure delay function calls .................................................................................................................. 3
Debug and Simulate AVR Studio Project .................................................................................................. 4
Add Header and Source files to AVR Studio Project .................................................................................. 5
Flash Programming using MegaLoad........................................................................................................ 5
EEPROM in AVR Studio ............................................................................................................................ 6
FAQ ......................................................................................................................................................... 7
Overview Install the BootLoader
To program in C using AVR Studio, first you need to All SJValley microcontroller kits come with a Bootloader
install the necessary programs that will help you create preinstalled. Follow this section if you are having
the programming file: trouble programming or your microcontroller doesn’t
contain a Bootloader program. Installing the Bootloader
is a one-time process that is performed such that your
chip can program by itself. There is a one-time need of
using a real-programmer (ISP programmer) and later the
programs can be “flashed” to the microcontroller
without this. Follow the
• AVR Studio:
http://www.atmel.com/dyn/products/tools_car
d.asp?tool_id=2725
• WINAVR:
http://winavr.sourceforge.net/download.html
1
Create Project in AVR Studio
Follow the following Instructions:
2
Configure delay function calls 5. Note that delay function parameter must be
known at compile time to be accurate otherwise
the delay time is not guaranteed. In other
The delay function in AVR Studio requires a couple of words, the delay functions are not dynamic
setup options. Follow the following steps to configure delay functions.
your delay functions: 6. Another important note is that delay_us() works
up to 768uS/CPU_MHZ and delay_ms() works up
1. Include <uitl/delay.h> in your program.
to 262ms/CPU_MHZ so If you want to make
2. Go to Project Configuration
your own delay that takes longer, try something
like this:
void myDelay10ms(char count) {
while(--count != 0)_delay_ms(10);
}
3
Debug and Simulate AVR Studio Project
5. On the right side, you see your Atmega Similarly, you can manipulate other registers or
resources that you can modify: interrupt flags to simulate an interrupt trigger.
4
Add Header and Source files to AVR
Studio Project
1. Put the header and source file in the same
directory as your project files.
2. #include your desired file:
5
EEPROM in AVR Studio given correctly to the eeprom functions. For example,
Atmega chips contain three types of memory. The when you use eeprom_write_block, you can pass along
FLASH memory is ROM (Read-only memory) and this is the address of the variable or structure to the function
where your program code resides. The contents can and the address will be unique and it will not overlap
only be changed by BootLoader or a programmer. with other variables declared as EEMEM.
Contents in this memory remain on the chip across
More functions can be located from WINAVR’s online
power-cycles which is why when your power-off and
reference:
power-on your microcontroller, you do not need to
http://www.nongnu.org/avr-libc/user-
reload the code.
manual/index.html
The second memory type is SRAM. Simply-put, the
Here are some examples of reading and writing
SRAM contents are erased when the chip is power-
eeprom. Follow the syntax carefully otherwise your
cycled. This memory is used your program to execute
chip might keep resetting itself.
your code. This finally gives way to EEPROM which
inhibits characteristics from FLASH and SRAM. Contents typedef struct TIME{
written to this memory are preserved across power- unsigned char hour, min, sec;
cycles and the user can manipulate this memory easily }TIME;
6
EEPROM should be read and written only occasionally.
If you continue to read and write to EEPROM, the
EEPROM memory may die because it has a limit of 100k
R/w cycles. Usually, it should be read at the beginning
of your program and written-back during your CPU
shutdown or upon an occasional event.
FAQ