The AVR flash memory is divided into two sections namely the application section and the Boot-Loader section (BLS). In case of the ATMEGA16 it has 16 KB of flash memory of which the 15KB is application section and the rest 1KB is BLS. The memory architecture of the ATMEGA16 is shown in the following figure;
Fig. 2: Architechure of AVR Flash Memory
The code for the BLS or application section is written as normally does and there is no much difference. The only thing to be careful about is the size of the code binary. It should not be more than 1KB, otherwise it won’t be able to code programmed into the BLS. The following section discusses how to program a code into the BLS of ATMEGA16 microcontroller with the help of AVR studio as IDE, USBasp as the programmer and AVR-Burnomat as the burner software.
Open the AVR studio, copy and paste a simple led blinking code which is given below;
//#######################################################################//
#define F_CPU 8000000
#include <avr/io.h>
#include <util/delay.h>
int main ( void )
{
DDRD |= 0x80;
while(1)
{
PORTD &= 0x7F; //led on
_delay_ms ( 500 );
PORTD |= 0x80; //led off
_delay_ms ( 500 );
}
}
//#######################################################################//
Before compiling the few settings need to be done. Select the target as ATMEGA 16 as shown in the figure.
Fig. 3: AVR Studio Window
The code should be compiled in such a way that it will get flashed at the BLS only. For that one need to do the memory settings as shown below. Select the memory as “flash” name the memory as “.text “give the memory address as “0x1C00”.
Fig. 4: Memory Settings to flash code in BLS of AVR
Now build the code. Once the compilation has been completed a hex file will be generated. The USBASP programmer is currently not supported by the AVR studio 4. Hence software called AVR-BURNO-MAT can be used for flashing the hex file.
One must write the fuse bits before flashing the code so as to enable the features such as to enable the internal oscillator and to select the reset vector as 0x1C00.
FUSE BITS CONFIGURATION
|
FUSE
|
VALUE
|
OCDEN
|
1
|
OCD1
|
0
|
JTAGEN
|
0
|
SPIEN
|
1
|
CKPOT
|
1
|
EESAVE
|
0
|
BOOTSZ1
|
0
|
BOOTSZ0
|
0
|
BOOTRST
|
0
|
BODLEVEL
|
1
|
BODEN
|
1
|
SUT1
|
0
|
SUT0
|
0
|
CKSEL3
|
0
|
CKSEL2
|
1
|
CKSEL1
|
0
|
CKSEL0
|
0
|
Fig. 5: Fuse Bit Configuration
The AVR-BURNO-MAT is the best application available for writing the fuse bits into the AVR microcontroller. The fuse bit selection in the AVR-BURNO-MAT is shown in the following figure.
Fig. 6: Fuse bit selection in AVR-BURNO-MAT
This is the fuse bit setting which are about to written into ATMEGA16. Now click the “write button” and the fuse bits will get written into it.
Now one can flash the code to the boot loader section using the AVR-BURNO-MAT itself. Select the required hex file which has been generated by the AVR-STUDIO. Select the file format as Intel Hex. Now click the write button and the code will get written into the microcontroller.
Fig. 7: Code Flashed in boot loader using the AVR-BURNO-MAT
Once the flashing has been completed the LED starts Blinking, which means the code has been programmed successfully into the BLS and the microcontroller starts executing from the BLS section on reset. Now one can try to write a code for the BLS section which when done executing the code can make a jump to the application section using the jump instruction. The following section discuss about how to do that. For the BLS a simple LED blinking code which can blink the led 5 times with a delay of 500ms can be written. At the end of the code one can write a statement asm ( “jmp 0x0000” )
for making a jump to the address 0x0000 which is the starting address of the application section. The code is as shown below.
//#####################################################################//
#define F_CPU 8000000
#include <avr/io.h>
#include <util/delay.h>
int main ( void )
{
DDRD |= 0x80;
PORTD &= 0x7F;
_delay_ms ( 500 );
PORTD |= 0x80;
_delay_ms ( 500 );
PORTD &= 0x7F;
_delay_ms ( 500 );
PORTD |= 0x80;
_delay_ms ( 500 );
PORTD &= 0x7F;
_delay_ms ( 500 );
PORTD |= 0x80;
_delay_ms ( 500 );
PORTD &= 0x7F;
_delay_ms ( 500 );
PORTD |= 0x80;
_delay_ms ( 500 );
PORTD &= 0x7F;
_delay_ms ( 500 );
PORTD |= 0x80;
_delay_ms ( 500 );
asm ( "jmp 0x0000" );
}
//#####################################################################//
For the application section one can write another code which blinks the led with a delay of 2 seconds so that whenever the code jumps from the BLS to the application section it can be easily identified by the difference in the delay with which the LED blinks. The code is as shown below.
//#####################################################################//
#define F_CPU 8000000
#include <avr/io.h>
#include <util/delay.h>
int main ( void )
{
DDRD |= 0x80;
while(1)
{
PORTD &= 0x7F;
_delay_ms ( 2000 );
PORTD |= 0x80;
_delay_ms ( 2000 );
}
}
//#####################################################################//
Flash the led blinking with small delay to the BLS before flashing the led blinking with large delay to the application section. One should disabled the flash memory erasing option at the AVR-BURNO-MAT before flashing the code to the application section.
Fig. 8: Disabled flash memory Erasing option in AVR-BURNO-MAT
Không có nhận xét nào:
Đăng nhận xét