Developed By:
Akshay Daga
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.
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.
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;
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 LCDall 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.
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.
Fig. 5: Burning BLS code into BLS of AVR circuit on breadboard
Fig. 6: LCD Display initialized by BLS code in AVR setup on breadboard
Fig. 7: USART initialized at 9600 baud rate by BLS of AVR setup on breadboard
Fig. 8: Initialize 4 bit LCD by BLS connected to AVR controller circuit on breadboard
Circuit:
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
Code 2:
#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 );
}
}
Không có nhận xét nào:
Đăng nhận xét