Engineers Garage

  • Electronic Projects & Tutorials
    • Electronic Projects
      • Arduino Projects
      • AVR
      • Raspberry pi
      • ESP8266
      • BeagleBone
      • 8051 Microcontroller
      • ARM
      • PIC Microcontroller
      • STM32
    • Tutorials
      • Audio Electronics
      • Battery Management
      • Brainwave
      • Electric Vehicles
      • EMI/EMC/RFI
      • Hardware Filters
      • IoT tutorials
      • Power Tutorials
      • Python
      • Sensors
      • USB
      • VHDL
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Product News
    • Business News
    • Company/Start-up News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • DigiKey Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Learn
    • eBooks/Tech Tips
    • Design Guides
    • Learning Center
    • Tech Toolboxes
    • Webinars & Digital Events
  • Resources
    • Digital Issues
    • EE Training Days
    • LEAP Awards
    • Podcasts
    • Webinars / Digital Events
    • White Papers
    • Engineering Diversity & Inclusion
    • DesignFast
  • Guest Post Guidelines
  • Advertise
  • Subscribe

How to Initialize Peripherals from Boot Loader Section- (Part 32/46)

By Ashutosh Bhatt June 14, 2013

In almost all the microcontroller codes the peripheral initialization functions like uart initialization, spi initialization are written along with the different application codes. These initialization functions are actually repetitions of the original initialization functions. The same is the case with the external hardware initialization like LCD initialization, GSM modem initialization etc. Suppose the case in which the application codes required are stored in a memory chip or SD memory card so that there is an option to select between the applications. If all the application codes have the same functions for peripheral and external hardware initialization that will simply increase the size of the code only and the size of the memory required to store the codes. It will take too much time for the Boot-Loader to load such a large size application and there will be flash memory shortage issues due to the large code size. These issues can be solved by doing the initialization of the peripherals and the external hardware in the code running from the BLS itself.

The code running from the BLS is executed first and then only the application code runs. Hence before the application runs the peripherals and the external hardware will be already initialized. Thus the programmer don’t have to write all the initialization function sand hence he/she can concentrate on coding the particular application with that peripheral device only. This saves the time required for the code development. The code size also reduces in this method and the portability of the code increases. The Booting time of the microcontroller also improves due to the small code size. This project demonstrates how one can initialize the peripherals of the ATMEGA16 microcontroller irrespective of the application code using the code running from the BLS. The AVR studio4 is used as IDE in this project, USBASP is used as the programmer hardware and the AVR-BURNO-MAT is used as the burning software.


 

 

In the AVR microcontroller the flash memory is divided into two parts, namely Application Section and Boot Loader Section.  A code can be programmed into either the Application Section or the Boot loader Section (BLS). The code programmed into the Application section runs normally and is used for common applications, whereas the code running in the BLS is provided with some special features like Self Programing Mode (SPM) instructions, it can change the fuse bits etc. or in other words the code running in the BLS has complete access to the hardware. Also the microcontroller can be made to start executing from the BLS on a reset. Hence the one time initialization codes for the peripherals irrespective of the application codes are always written in the 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;

Flash Memory Architecture of ATMEGA16

Fig. 2: Flash Memory Architecture of ATMEGA16

A previous project on simple LED blinking from the BLS explains in detail how to code a program into the BLS of AVR microcontroller. In this project the UART initialization is demonstrated along with a simple LED pin initialization and also an external hardware initialization with the LCD all coded in the BLS itself. The application codes thus not require these much codes in them still they works because before executing the application codes the initialization have already done by the BLS itself. The BLS code has the initialization functions should be not be there in the application codes. The initialization functions used in the BLS code for this project are given below;

Function

Description

void lcd_init ( void )

Initialize the LCD in 4 bit mode

void usart_init ( void )

Initialize the usart in 9600 baud rate with transmission and reception enabled

DDRD |= 0x80;

Initialization of the LED pin as output

Fig. 3: Initialization functions for BLS code in AVR

//#####################################################################//

#define F_CPU 8000000

#include <avr/io.h>

#include <util/delay.h>

#include “usart.h”

#include “lcd.h”

 

int main ( void )

{

            int i;

//==============================================================

            DDRD |= 0x80; //initiaization of LED

// ———- initialization of LCD ————– //

            lcd_init();       

 

            lcd_clear();

            lcd_1st_line();

            lcd_string(”    ENGINEERS   ”  );

            lcd_2nd_line();

            lcd_string(”     GARAGE     ”  );

            _delay_ms(3000);

 

            lcd_clear();

            lcd_1st_line();

            lcd_string(“Booting ATMEGA16”  );

            _delay_ms(2000);

 

            lcd_clear();

            lcd_1st_line();

            lcd_string(”  LCD      [OK] ”  );

            lcd_2nd_line();

            lcd_string(”   16*2, 4 bit  ”  );

            _delay_ms(2000);

// ———- initialization of LCD ————– //

 

// ——— initialization of USART ————- //

            usart_init();

 

            lcd_clear();

            lcd_1st_line();

            lcd_string(” USART     [OK] ”  );

            lcd_2nd_line();

            lcd_string(“9600bps, Tx & Rx”  );

            _delay_ms(2000);

// ——— initialization of USART ————- //

 

// ———— intro display on LCD ————- //

            lcd_clear();

            lcd_1st_line();

            lcd_string(“loading App…  ”  );

            lcd_2nd_line();

            for(i = 0; i < 16; i ++)

            {

                        _delay_ms(350);

                        dis_data(0xFF);

            }

 

            lcd_clear();

            lcd_1st_line();

// ———— intro display on LCD ————- //

//=============================================================

 

            asm ( “jmp 0x0000” );//jump to application section

 

}

//#####################################################################//

 

In this project the application code actually displays some text in the LCD which has already been initialized by the BLS code and turn an LED on or off whose pin has already been set as output by the BLS. The application code also sends some text to the serial port which is already initialized at 9600 baud rate by the BLS.

The application code use the following function calls to access the USART, LCD and LED without their initializing functions anywhere in the code.

Function

Description

lcd_clear ()

Clear the LCD

lcd_string ()

Display a string in the LCD

usart_send_string ()

Send a string via usart

PORTD &= 0x7F;

Turn ON the LED

PORTD |= 0x80;

Turn OFF the LED

 Fig. 4: Function calls of Application Code to access USART, LCD and LED in AVR

 //#####################################################################//

#define F_CPU 8000000

 

#include <avr/io.h>

#include <util/delay.h>

#include “usart.h”

#include “lcd.h”

 

int main ( void )

{

//writing into LCD and USART without calling their initialization functions//

            lcd_clear ();

            lcd_string( “led app running” );

            usart_send_string( “led app running” );

 

//blinking LED without initializing the corresponding port//

            while(1)

            {

                        PORTD &= 0x7F;

                        _delay_ms ( 2000 );

                        PORTD |= 0x80;

                        _delay_ms ( 2000 );

            }

 

}

//#####################################################################//

 

First burn the BLS code into the BLS of the AVR and then burn the application code as explained in the project on simple LED blinking from BLS.

Burning BLS code into BLS of AVR circuit on breadboard

Fig. 5: Burning BLS code into BLS of AVR circuit on breadboard

 

LCD Display initialized by BLS code in AVR setup on breadboard

Fig. 6: LCD Display initialized by BLS code in AVR setup on breadboard

 

 USART initialized at 9600 baud rate by BLS of AVR setup on breadboard

Fig. 7: USART initialized at 9600 baud rate by BLS of AVR setup on breadboard

 

Initialize 4 bit LCD by BLS connected to AVR controller circuit on breadboard

Fig. 8: Initialize 4 bit LCD by BLS connected to AVR controller circuit on breadboard

 

Project Source Code

###


#define F_CPU 8000000
#include <avr/io.h>
#include <util/delay.h>
#include "usart.h"
#include "lcd.h"
 
int main ( void )
{
int i;
 
//====================================================================================
DDRD |= 0x80; //initiaization of LED
 
// ---------- initialization of LCD -------------- //
lcd_init();
 
lcd_clear();
lcd_1st_line();
lcd_string("    ENGINEERS   "  );
lcd_2nd_line();
lcd_string("     GARAGE     "  );
_delay_ms(3000);
 
lcd_clear();
lcd_1st_line();
lcd_string("Booting ATMEGA16"  );
_delay_ms(2000);
 
lcd_clear();
lcd_1st_line();
lcd_string("  LCD      [OK] "  );
lcd_2nd_line();
lcd_string("   16*2, 4 bit  "  );
_delay_ms(2000);
// ---------- initialization of LCD -------------- //
 
// --------- initialization of USART ------------- //
usart_init(); 
 
lcd_clear();
lcd_1st_line();
lcd_string(" USART     [OK] "  );
lcd_2nd_line();
lcd_string("9600bps, Tx & Rx"  );
_delay_ms(2000);
// --------- initialization of USART ------------- //
 
// ------------ intro display on LCD ------------- //
lcd_clear();
lcd_1st_line();
lcd_string("loading App...  "  );
lcd_2nd_line();
for(i = 0; i < 16; i ++)
{
_delay_ms(350);
dis_data(0xFF);
}
 
lcd_clear();
lcd_1st_line();
// ------------ intro display on LCD ------------- //
//====================================================================================
 
 
asm ( "jmp 0x0000" );//jump to application section
 
}

//############# LCD ##############//
 

#define _LCD_H #ifndef F_CPU #define F_CPU 8000000 #endif #include<avr/io.h> #include<util/delay.h> #include<inttypes.h> #include <stdio.h> #include <string.h> #define rs PA0 #define rw PA1 #define en PA2 void lcd_init(); void dis_cmd(char); void dis_data(char); void lcdcmd(char); void lcddata(char); void lcd_clear(void); void lcd_2nd_line(void); void lcd_1st_line(void); void lcd_string(const char *data); int lcd_print(char c, FILE *stream); int lcd_scroll(const char *data); FILE lcd_out = FDEV_SETUP_STREAM(lcd_print, NULL, _FDEV_SETUP_WRITE); char disp_beg [] = " "; int lcd_print(char c, FILE *stream) { if('n' == c) lcd_2nd_line(); else dis_data(c); return 0; } int lcd_scroll(const char *data) { int i; int j = 0; strcat(disp_beg, data); for(i = 0; i < 14; i ++) strcat(disp_beg, " "); while(1) { for(i = 0;i < 16;i ++) { if(!disp_beg[i + j]) return 0; else; dis_data(disp_beg [i + j]); } j ++; _delay_ms(500); lcd_clear (); } return 0; } void lcd_string(const char *data) { for(;*data;data++) dis_data (*data); } void lcd_clear(void) { dis_cmd(0x01); _delay_ms(10); } void lcd_2nd_line(void) { dis_cmd(0xC0); _delay_ms(1); } void lcd_1st_line(void) { dis_cmd(0x80); _delay_ms(1); } void lcd_init() // fuction for intialize { DDRA=0xFF; dis_cmd(0x02); // to initialize LCD in 4-bit mode. dis_cmd(0x28); //to initialize LCD in 2 lines, 5X7 dots and 4bit mode. dis_cmd(0x0C); dis_cmd(0x06); dis_cmd(0x80); dis_cmd(0x01); _delay_ms(500); stdout = &lcd_out; } void dis_cmd(char cmd_value) { char cmd_value1; cmd_value1 = cmd_value & 0xF0; //mask lower nibble because PA4-PA7 pins are used. lcdcmd(cmd_value1); // send to LCD cmd_value1 = ((cmd_value<<4) & 0xF0); //shift 4-bit and mask lcdcmd(cmd_value1); // send to LCD } void dis_data(char data_value) { char data_value1; data_value1=data_value&0xF0; lcddata(data_value1); data_value1=((data_value<<4)&0xF0); lcddata(data_value1); } void lcdcmd(char cmdout) { PORTA=cmdout; PORTA&=~(1<<rs); PORTA&=~(1<<rw); PORTA|=(1<<en); _delay_ms(1); PORTA&=~(1<<en); } void lcddata(char dataout) { PORTA=dataout; PORTA|=(1<<rs); PORTA&=~(1<<rw); PORTA|=(1<<en); _delay_ms(1); PORTA&=~(1<<en); } #endif


//########## USART ##########//
 

#define _USART_H #ifndef F_CPU #define F_CPU 8000000 #endif #define USART_BAUDRATE 9600 #define BAUD_PRESCALE (((F_CPU / (USART_BAUDRATE * 16UL))) - 1) #include<avr/io.h> #include<util/delay.h> #include <stdio.h> void usart_init(); void usart_putch(unsigned char send); unsigned int usart_getch(); void usart_send_string(const char* data); int uart_print(char c, FILE *stream); FILE uart_out = FDEV_SETUP_STREAM(uart_print, NULL, _FDEV_SETUP_WRITE); int uart_print(char c, FILE *stream) { if (c == 'n') uart_print('r', stream); loop_until_bit_is_set(UCSRA, UDRE); UDR = c; return 0; } void usart_init() { UCSRB |= (1 << RXEN) | (1 << TXEN); // Turn on the transmission and reception circuitry UCSRC |= (1 << URSEL) | (1<<USBS) | (1 << UCSZ0) | (1 << UCSZ1); // Use 8-bit character sizes UBRRL = BAUD_PRESCALE; // Load lower 8-bits of the baud rate value.. // into the low byte of the UBRR register UBRRH = (BAUD_PRESCALE >> 8); // Load upper 8-bits of the baud rate value.. // into the high byte of the UBRR register stdout = &uart_out; } void usart_putch(unsigned char send) { while ((UCSRA & (1 << UDRE)) == 0); // Do nothing until UDR is ready.. // for more data to be written to it UDR = send; // Send the byte } unsigned int usart_getch() { while ((UCSRA & (1 << RXC)) == 0); // Do nothing until data have been received and is ready to be read from UDR return(UDR); // return the byte } void usart_send_string(const char* data) { for(; *data; data ++) usart_putch(*data); } #endif

 

###

 


Project Source Code

###


#define F_CPU 8000000
#include <avr/io.h>
#include <util/delay.h>
#include "usart.h"
#include "lcd.h"
 
int main ( void )
{
//writing into LCD and USART without calling their initialization functions//
lcd_clear ();
lcd_string( "led app running" );
usart_send_string( "led app running" );
 
//blinking LED without initializing the corresponding port//
while(1)
{
PORTD &= 0x7F;
_delay_ms ( 2000 );
PORTD |= 0x80;
_delay_ms ( 2000 );
}
}

###

 


Circuit Diagrams

Circuit-Diagram-of-How-to-Initialize-Peripherals-from-Boot-Loader-Section

Project Components

  • ATmega16
  • LCD
  • LED
  • Resistor

Project Video


Filed Under: AVR
Tagged With: avr microcontroller, bls, bootloader section, circuit, project
 

Next Article

← Previous Article
Next Article →

Questions related to this article?
👉Ask and discuss on Electro-Tech-Online.com and EDAboard.com forums.



Tell Us What You Think!! Cancel reply

You must be logged in to post a comment.

EE TECH TOOLBOX

“ee
Tech Toolbox: 5G Technology
This Tech Toolbox covers the basics of 5G technology plus a story about how engineers designed and built a prototype DSL router mostly from old cellphone parts. Download this first 5G/wired/wireless communications Tech Toolbox to learn more!

EE Learning Center

EE Learning Center
“engineers
EXPAND YOUR KNOWLEDGE AND STAY CONNECTED
Get the latest info on technologies, tools and strategies for EE professionals.

HAVE A QUESTION?

Have a technical question about an article or other engineering questions? Check out our engineering forums EDABoard.com and Electro-Tech-Online.com where you can get those questions asked and answered by your peers!


RSS EDABOARD.com Discussions

  • BF999 Input and output impedance
  • Industrial Relay Board Design for Motorcycle Use
  • Sendust vs Ferrite for SMPS
  • On/Off Slide Switch Reassembly Help
  • sim7090g

RSS Electro-Tech-Online.com Discussions

  • I Wanna build a robot
  • Wierd makita battery
  • using a RTC in SF basic
  • Is AI making embedded software developers more productive?
  • ac current limiting

Featured – LoRa/LoRaWan Series

  • What is the LoRaWAN network and how does it work?
  • Understanding LoRa architecture: nodes, gateways, and servers
  • Revolutionizing RF: LoRa applications and advantages
  • How to build a LoRa gateway using Raspberry Pi
  • How LoRa enables long-range communication
  • How communication works between two LoRa end-node devices

Recent Articles

  • STMicroelectronics unveils SoC based on secure MCU
  • Nexperia’s 48 V ESD diodes support higher data rates with ultra-low capacitance design
  • Taoglas releases Patriot antenna with 18 integrated elements covering 600 to 6000 MHz
  • Amphenol RF introduces SMPM to SMPM assemblies on RG-178 cable
  • Infineon launches 3D magnetic sensors with ±50 mT to ±160 mT measurement ranges

EE ENGINEERING TRAINING DAYS

engineering

Submit a Guest Post

submit a guest post
Engineers Garage
  • Analog IC TIps
  • Connector Tips
  • Battery Power Tips
  • DesignFast
  • EDABoard Forums
  • EE World Online
  • Electro-Tech-Online Forums
  • EV Engineering
  • Microcontroller Tips
  • Power Electronic Tips
  • Sensor Tips
  • Test and Measurement Tips
  • 5G Technology World
  • Subscribe to our newsletter
  • About Us
  • Contact Us
  • Advertise

Copyright © 2025 WTWH Media LLC. All Rights Reserved. The material on this site may not be reproduced, distributed, transmitted, cached or otherwise used, except with the prior written permission of WTWH Media
Privacy Policy

Search Engineers Garage

  • Electronic Projects & Tutorials
    • Electronic Projects
      • Arduino Projects
      • AVR
      • Raspberry pi
      • ESP8266
      • BeagleBone
      • 8051 Microcontroller
      • ARM
      • PIC Microcontroller
      • STM32
    • Tutorials
      • Audio Electronics
      • Battery Management
      • Brainwave
      • Electric Vehicles
      • EMI/EMC/RFI
      • Hardware Filters
      • IoT tutorials
      • Power Tutorials
      • Python
      • Sensors
      • USB
      • VHDL
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Product News
    • Business News
    • Company/Start-up News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • DigiKey Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Learn
    • eBooks/Tech Tips
    • Design Guides
    • Learning Center
    • Tech Toolboxes
    • Webinars & Digital Events
  • Resources
    • Digital Issues
    • EE Training Days
    • LEAP Awards
    • Podcasts
    • Webinars / Digital Events
    • White Papers
    • Engineering Diversity & Inclusion
    • DesignFast
  • Guest Post Guidelines
  • Advertise
  • Subscribe