Introduction

Overview:

This manual is designed to help embedded programmers and students, rapidly exploit the Avr(Atmega)-Controller for embedded applications. This manual has been targeted at embedded systems programmers and Students who have basic knowledge of Avr(Atmega32/Avr) architecture and C-Language.

This manual provides the reference to all the library functions which are grouped under respective .c file.

  1. The .c files convention is as per the peripherals. The peripherals (lcd, keypad..) are connected to default PORTs which can be connect to required PORTs by changing the #defines .

Reference:

  1. It is recommended to go through the below reference documents and datasheets before interfacing any peripherals.

1. The Avr Microcontroller and Embedded Systems by Muhammad Ali Mazidi. 2. Atmega32 DataSheet. 3. Embedded C by Michael J Pont . 4. Any of the 16x2 lcd datasheet. 5. RTC-DS1307 from Dallas Semiconductors.


Feedback:

Suggestions for additions and improvements in code and documentation are always welcome. Please send your feedback via e-mail to feedback@xplorelabz.com

Disclaimer:

The libraries have been tested for Atmega16 on different development boards. We strongly believe that the library works on any Atmega boards. However, Xplore Labz disclaims any kind of hardware failure resulting out of usage of libraries, directly or indirectly. Documentation may be subject to change without prior notice.

The usage of tools and software demonstrated in the document are for educational purpose only, all rights pertaining to these belong to the respective owners. Users must ensure license terms are adhered to, for any use of the demonstrated software.

GNU GENERAL PUBLIC LICENSE: The library code in this document is licensed under GNU General Public License (GPL) Copyright (C) 2012.

  1. Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed.

Since the library is licensed free of charge, there is no warranty for the libraries and the entire risk of the quality and performance is with the user.

Errors and omissions should be reported to feedback@xplorelabz.com




ADC

  1. Avr ADC library
  • Filename: adc.c
  • Controller: Atmega8/16/32/128
  • Oscillator: 11.0592 MHz
  • Author: XploreLabz
  • website: www.xplorelabz.com
  • Reference:Atmega32 dataSheet
  1. #include<avr/io.h>
  2. #include <util/delay.h>
  3. #include "adc.h"


ADC_Init()

  • Description :This function initializes the ADC control registers
  • I/P Arguments: none
  • Return value: none
  1. void ADC_Init()
  2. {
  3. ADCSRA=0x81; //Enable ADC , sampling freq=osc_freq/2
  4. ADMUX=0x00; //Result right justified, select channel zero
  5. }


ADC_StartConversion()

  • Description  :This function does the ADC conversioin for the Selected Channel and returns the converted 10bit result
  • I/P Arguments: char(channel number)
  • Return value : int(10 bit ADC result)


  1.  
  2. unsigned int ADC_StartConversion(unsigned char channel)
  3. {
  4. ADMUX=channel;
  5. _delay_ms(5);
  6. ADCSRA=0xc1;
  7. while((ADCSRA & (1<<ADIF)==0);
  8. return(ADCW);
  9. }

LCD_8_bit Mode

  1. AVR LCD library for 8-bit mode
  • Filename: lcd_8_bit.c
  • Controller: Atmega Family(8,16,32,64,128)
  • Oscillator: 11.0592 MHz
  • Author: XploreLabz
  • website: www.xplorelabz.com

Note:

  • Pin connection for LCD display in 8-bit mode is as shown below.By default the LCD is connected to PORTB(databus) and PORTD(controlbus).
  • The code can be modified to connect the LCD to any of the PORTs by changing the "#define".
  • io.h contains the defnition of all ports and SFRs delay.h contains the in built delay routines(us and ms routines).
  1. #include <avr\io.h>
  2. #include <util\delay.h>
  3.  
  4. #include "lcd.h"
  5.  
  6. #define databus_direction DDRC // LCD databus Direction Configuration
  7. #define controlbus_direction DDRD // LCD Control bus Direction Configuration
  8.  
  9. #define databus PORTC // LCD databus connected to PORTB
  10. #define control_bus PORTD // LCD Control bus connected to PORTD
  11.  
  12. #define rs 5 // Register select pin connected 6th bit(D5) Control bus
  13. #define rw 6 // Read Write pin connected to 7th bit(D6) Control bus
  14. #define en 7 // Enable pin connected to 8th bit(D7) Control bus
  15.  
  16.  
  17. /* 16x2 LCD Specification */
  18. #define LCDMaxLines 2
  19. #define LCDMaxChars 16
  20. #define LineOne 0x80
  21. #define LineTwo 0xc0
  22.  
  23. #define BlankSpace ' '


LCD_Init()

  • Description  :This function is used to initialize the lcd in 8-bit mode
  • Function name: LCD_Init()
  • I/P Arguments: none.
  • Return value : none


  1. void LCD_Init()
  2. {
  3. _delay_ms(50);
  4. databus_direction = 0xff; // Configure both databus and controlbus as output
  5. controlbus_direction = 0xff;
  6. LCD_CmdWrite(0x38); // LCD 2lines, 5*7 matrix
  7. LCD_CmdWrite(0x0E); // Display ON cursor ON
  8. LCD_CmdWrite(0x01); // Clear the LCD
  9. LCD_CmdWrite(0x80); // Move the Cursor to First line First Position
  10. }

LCD_Clear()

  • Description  :This function clears the LCD and moves the cursor to first Position
  • I/P Arguments: none.
  • Return value : none


  1. void LCD_Clear()
  2. {
  3. LCD_CmdWrite(0x01); // Clear the LCD and go to First line First Position
  4. LCD_CmdWrite(LineOne);
  5. }


LCD_GoToLineOne()

  • Description  :This function moves the Cursor to First line First Position
  • I/P Arguments: none
  • Return value : none


  1. void LCD_GoToLineOne()
  2. {
  3. LCD_CmdWrite(LineOne); // Move the Cursor to First line First Position
  4. }

LCD_GoToLineTwo()

  • Description  :This function moves the Cursor to Second line First Position
  • I/P Arguments: none
  • Return value : none


  1. void LCD_GoToLineTwo()
  2. {
  3. LCD_CmdWrite(LineTwo); // Move the Cursor to Second line First Position
  4. }

LCD_GoToXY()

  • Description  :This function moves the Cursor to specified position
  • I/P Arguments: char row,char col
    • row -> line number(line1=0, line2=1),For 2line LCD the I/P argument should be either 0 or 1.
    • col -> char number.For 16-char LCD the I/P argument should be betwen 0-15.
  • Return value : none


  1. void LCD_GoToXY(char row, char col)
  2. {
  3. char pos;
  4.  
  5. if(row<LCDMaxLines)
  6. {
  7. pos= LineOne | (row << 6); // take the line number
  8. //row0->pos=0x80 row1->pos=0xc0
  9.  
  10. if(col<LCDMaxChars)
  11. pos= pos+col; //take the char number
  12. //now pos points to the given XY pos
  13.  
  14. LCD_CmdWrite(pos); // Move the Cursor to specified Position
  15. }
  16. }

LCD_CmdWrite()

  • Description :This function sends a command to LCD in the following steps.
    • step1: Send the I/P command to LCD.
    • step2: Select the Control Register by making RS low.
    • step3: Select Write operation making RW low.
    • step4: Send a High-to-Low pulse on Enable PIN with some delay_us.
  • I/P Arguments: 8-bit command supported by LCD.
  • Return value : none


  1. void LCD_CmdWrite( char cmd)
  2. {
  3.  
  4. databus=cmd; // Send the command to LCD
  5. control_bus &=~(1<<rs); // Select the Command Register by pulling RS LOW
  6. control_bus &=~(1<<rw); // Select the Write Operation by pulling RW LOW
  7. control_bus |=1<<en; // Send a High-to-Low Pusle at Enable Pin
  8. _delay_us(1);
  9. control_bus &=~(1<<en);
  10. _delay_ms(1);
  11. }

LCD_DataWrite()

  • Description:This function sends a character to be displayed on LCD in the following steps.
    • step1: Send the character to LCD.
    • step2: Select the Data Register by making RS high.
    • step3: Select Write operation making RW low.
    • step4: Send a High-to-Low pulse on Enable PIN with some delay_us.
  • I/P Arguments: ASCII value of the char to be displayed.
  • Return value : none


  1. void LCD_DataWrite( char dat)
  2. {
  3.  
  4. databus=dat; // Send the data to LCD
  5. control_bus |=1<<rs; // Select the Data Register by pulling RS HIGH
  6. control_bus &=~(1<<rw); // Select the Write Operation by pulling RW LOW
  7. control_bus |=1<<en; // Send a High-to-Low Pusle at Enable Pin
  8. _delay_us(1);
  9. control_bus &=~(1<<en);
  10. _delay_ms(1);
  11.  
  12. }

LCD_DisplayString()

  • Description  :This function is used to display the ASCII string on the lcd.
  1. The string_ptr points to the first char of the string and traverses till the end(NULL CHAR).
  2. Each time a char is sent to LCD_DataWrite funtion to display.
  • I/P Arguments: String(Address of the string) to be displayed.
  • Return value : None
  1. void LCD_DisplayString(char *string_ptr)
  2. {
  3. while(*string_ptr)
  4. LCD_DataWrite(*string_ptr++);
  5. }

LCD_DisplayNumber()

  • Description  :This function is used to display a 5-digit integer(0-65535).
    • ex:
  1. if the number is 12345 then 12345 is displayed.
  2. if the number is 123 then 00123 is displayed.
  • Function name: LCD_DisplayNumber()
  • I/P Arguments: unsigned int.
  • Return value : none


Lcd displaynumber.png

  1. void LCD_DisplayNumber(unsigned int num)
  2. {
  3. LCD_DataWrite((num/10000)+0x30);
  4. num=num%10000;
  5.  
  6. LCD_DataWrite((num/1000)+0x30);
  7. num=num%1000;
  8.  
  9. LCD_DataWrite((num/100)+0x30);
  10. num=num%100;
  11.  
  12. LCD_DataWrite((num/10)+0x30);
  13.  
  14. LCD_DataWrite((num%10)+0x30);
  15.  
  16. }

LCD_ScrollMessage()

  • Description  :This function scrolls the given message on the first line.""
  1. 16 chars are displayed at atime.
  2. Pointer is incremented to skip a char each time to give the illusion of moving chars.
  3. If the chars are less than 16, then the BlankSpaces are displayed.
  • I/P Arguments: char *msg_ptr
  1. (msg_ptr -> pointer to the string to be scrolled)
  • Return value : none


  1. void LCD_ScrollMessage(char *msg_ptr)
  2. {
  3. unsigned char i,j;
  4. LCD_CmdWrite(0x0c); //Disable the Cursor
  5. for(i=0;msg_ptr[i];i++) //Loop to display the complete string
  6. { //each time 16 chars are displayed and
  7. //pointer is incremented to point to next char
  8.  
  9. LCD_GoToLineOne(); //Move the Cursor to first line
  10.  
  11. for(j=0;j<LCDMaxChars && msg_ptr[i+j];j++) //loop to Display first 16 Chars
  12. LCD_DataWrite(msg_ptr[i+j]); //or till Null char
  13.  
  14. for(j=j; j<LCDMaxChars; j++) //If the chars are below 16
  15. LCD_DataWrite(BlankSpace); //then display blank spaces
  16.  
  17. _delay_ms(500);
  18. }
  19. LCD_CmdWrite(0x0E); //Enable the Cursor
  20. }

LCD_DisplayRtcTime()

  • Description  :This function display hour,min,sec read from DS1307.
  • I/P Arguments: char hour,char min,char sec(hour,min,sec should be packed BCD format,as read from DS1307)
  • Return value : none

Lcd rtctime.png

  1. void LCD_DisplayRtcTime(char hour,char min,char sec)
  2. {
  3. LCD_DataWrite(((hour>>4) & 0x0f) + 0x30);
  4. LCD_DataWrite((hour & 0x0f) + 0x30);
  5. LCD_DataWrite(':');
  6.  
  7. LCD_DataWrite(((min>>4) & 0x0f) + 0x30);
  8. LCD_DataWrite((min & 0x0f) + 0x30);
  9. LCD_DataWrite(':');
  10.  
  11. LCD_DataWrite(((sec>>4) & 0x0f) + 0x30);
  12. LCD_DataWrite((sec & 0x0f) + 0x30);
  13.  
  14. }

LCD_DisplayRtcDate()

  • Description  :This function display day,month,year read from DS1307.
  • I/P Arguments: char day,char month,char year(day,month,year should be packed BCD format,as read from DS1307)
  • Return value : none

Lcd rtcdate.png

  1. void LCD_DisplayRtcDate(char day,char month,char year)
  2. {
  3. LCD_DataWrite(((day>>4) & 0x0f) + 0x30);
  4. LCD_DataWrite((day & 0x0f) + 0x30);
  5. LCD_DataWrite('/');
  6.  
  7. LCD_DataWrite(((month>>4) & 0x0f) + 0x30);
  8. LCD_DataWrite((month & 0x0f) + 0x30);
  9. LCD_DataWrite('/');
  10.  
  11. LCD_DataWrite(((year>>4) & 0x0f) + 0x30);
  12. LCD_DataWrite((year & 0x0f) + 0x30);
  13.  
  14. }

LCD_4_bit Mode

  1. AVR LCD library for 4-bit mode
  • Filename: lcd_4_bit.c
  • Controller: Atmega Family(8,16,32,64,128)
  • Oscillator: 11.0592 MHz
  • Author: XploreLabz
  • website: www.xplorelabz.com

Note:

  1. Pin connection for LCD display in 4-bit mode.
  2. By default the LCD is connected to PORTB.
  3. The code can be modified to connect the LCD to any of the PORTs by changing the "#define databus PORTB".


  • io.h contains the defnition of all ports and SFRs delay.h contains the in built delay routines(us and ms routines)
  1. #include <avr\io.h>
  2. #include <util\delay.h>
  3.  
  4. #include "lcd.h"
  5.  
  6. #define databus_direction DDRB // LCD data and Control bus Direction Configuration
  7.  
  8. #define databus PORTB // LCD databus connected to PORTB
  9. #define control_bus PORTB // LCD Control bus connected to PORTB
  10.  
  11. #define rs 0 // Register select pin connected 1st bit(D0) Control bus
  12. #define rw 1 // Read Write pin connected to 2nd bit(D1) Control bus
  13. #define en 2 // Enable pin connected to 3rd bit(D2) Control bus
  14.  
  15.  
  16. /* 16x2 LCD Specification */
  17. #define LCDMaxLines 2
  18. #define LCDMaxChars 16
  19. #define LineOne 0x80
  20. #define LineTwo 0xc0
  21.  
  22. #define BlankSpace ' '


LCD_Init()

  • Description  :This function is used to initialize the lcd in 4-bit mode
  • Function name: LCD_Init()
  • I/P Arguments: none
  • Return value : none


  1. void LCD_Init()
  2. {
  3. _delay_ms(50);
  4. databus_direction = 0xff; // Configure both databus and controlbus as output
  5. LCD_CmdWrite(0x02); //Initilize the LCD in 4bit Mode
  6. LCD_CmdWrite(0x28);
  7. LCD_CmdWrite(0x0E); // Display ON cursor ON
  8. LCD_CmdWrite(0x01); // Clear the LCD
  9. LCD_CmdWrite(0x80); // Move the Cursor to First line First Position
  10.  
  11. }

LCD_CmdWrite()

  • Description  :This function sends a command to LCD in the following steps.
    • step1: Send the Higher Nibble of the I/P command to LCD.
    • step2: Select the Control Register by making RS low.
    • step3: Select Write operation making RW low.
    • step4: Send a High-to-Low pulse on Enable PIN with some delay_us.
    • step5: Send the Lower Nibble of the I/P command to LCD.
    • step6: Select the Control Register by making RS low.
    • step7: Select Write operation making RW low.
    • step8: Send a High-to-Low pulse on Enable PIN with some delay_us.
  • I/P Arguments: 8-bit command supported by LCD.
  • Return value : none


  1. void LCD_CmdWrite( char cmd)
  2. {
  3.  
  4. databus=(cmd & 0xf0); // Send the Higher Nibble of the command to LCD
  5. control_bus &=~(1<<rs); // Select the Command Register by pulling RS LOW
  6. control_bus &=~(1<<rw); // Select the Write Operation by pulling RW LOW
  7. control_bus |=1<<en; // Send a High-to-Low Pusle at Enable Pin
  8. _delay_us(1);
  9. control_bus &=~(1<<en);
  10.  
  11. _delay_us(10); // wait for some time
  12.  
  13. databus=((cmd<<4) & 0xf0); // Send the Lower Nibble of the command to LCD
  14. control_bus &=~(1<<rs); // Select the Command Register by pulling RS LOW
  15. control_bus &=~(1<<rw); // Select the Write Operation by pulling RW LOW
  16. control_bus |=1<<en; // Send a High-to-Low Pusle at Enable Pin
  17. _delay_us(1);
  18. control_bus &=~(1<<en);
  19.  
  20. _delay_ms(1);
  21. }


LCD_DataWrite()

  • Description:This function sends a character to be displayed on LCD in the following steps.
    • step1: Send the higher nibble of the character to LCD.
    • step2: Select the Data Register by making RS high.
    • step3: Select Write operation making RW low.
    • step4: Send a High-to-Low pulse on Enable PIN with some delay_us.
    • step5: wait for some time
    • step6: Send the lower nibble of the character to LCD.
    • step7: Select the Data Register by making RS high.
    • step8: Select Write operation making RW low.
    • step9: Send a High-to-Low pulse on Enable PIN with some delay_us.
  • Function name: LCD_DataWrite()
  • I/P Arguments: ASCII value of the char to be displayed.
  • Return value : none


  1. void LCD_DataWrite( char dat)
  2. {
  3. databus=(dat & 0xf0); // Send the Higher Nibble of the Data to LCD
  4. control_bus |=1<<rs; // Select the Data Register by pulling RS HIGH
  5. control_bus &=~(1<<rw); // Select the Write Operation by pulling RW LOW
  6. control_bus |=1<<en; // Send a High-to-Low Pusle at Enable Pin
  7. _delay_us(1);
  8. control_bus &=~(1<<en);
  9. _delay_us(10);
  10.  
  11. databus=((dat <<4) & 0xf0); // Send the Lower Nibble of the Data to LCD
  12. control_bus |=1<<rs; // Select the Data Register by pulling RS HIGH
  13. control_bus &=~(1<<rw); // Select the Write Operation by pulling RW LOW
  14. control_bus |=1<<en; // Send a High-to-Low Pusle at Enable Pin
  15. _delay_us(1);
  16. control_bus &=~(1<<en);
  17. _delay_ms(1);
  18.  
  19. }

Keypad

  1. Avr 4x4 Keypad Library
  • Filename: keypad.c
  • Controller:Atmega8/16/32/128
  • Oscillator: 11.0592 MHz
  • Author: XploreLabz
  • website: www.xplorelabz.com
  1. Note:
  1. Rows are connected to lower 4-bits of PORTC
  2. Cols are connected to higher 4-bits of PORTC
  1. #include <avr\io.h>
  2. #include <util\delay.h>
  3. #include "keypad.h"
  4. #include "lcd.h"
  5. #define RowColDirection DDRB //Data Direction Configuration for keypad
  6. #define ROW PORTB //Lower four bits of PORTC are used as ROWs
  7. #define COL PINB //Higher four bits of PORTC are used as COLs


KEYPAD_Init()

  • Description  : This function the rows and colums for keypad scan
  1. ROW lines are configured as Output.
  2. Column Lines are configured as Input.
  • I/P Arguments:none
  • Return value : none


  1. void KEYPAD_Init()
  2. {
  3. RowColDirection=0xf0; // Configure Row lines as O/P and Column lines as I/P
  4. }


KEYPAD_WaitForKeyRelease()

  • Description  : This function waits till the previous key is released.
  1. All the ROW lines are pulled low.
  2. Column Lines are read to check the key press.
  3. If all the Keys are released then Column lines will remain high(0x0f)
  • I/P Arguments:none
  • Return value : none
  1. void KEYPAD_WaitForKeyRelease()
  2. {
  3. unsigned char key;
  4. do
  5. {
  6. ROW=0x0f; // Pull the ROW lines to low and Column high low.
  7. key=COL & 0x0f; // Read the Columns, to check the key press
  8. }while(key!=0x0f); // Wait till the Key is released,
  9. // If no Key is pressed, Column lines will remain high (0x0f)
  10. }


KEYPAD_WaitForKeyPress()

  • Description  : This function waits till a new key is pressed.
  1. All the ROW lines are pulled low.
  2. Column Lines are read to check the key press.
  3. If any Key is pressed then corresponding Column Line goes low.
  4. Wait for Some time and perform the above operation to ensure the True Key Press before decoding the KEY.
  • I/P Arguments:none
  • Return value : none


  1. void KEYPAD_WaitForKeyPress()
  2. {
  3. unsigned char key;
  4. do
  5. {
  6. do
  7. {
  8. ROW=0x0f; // Pull the ROW lines to low and Column lines high.
  9. key=COL & 0x0F; // Read the Columns, to check the key press
  10. }while(key==0x0f); // Wait till the Key is pressed,
  11. // if a Key is pressed the corresponding Column line go low
  12.  
  13. _delay_ms(1); // Wait for some time(debounce Time);
  14.  
  15. ROW=0x0f; // After debounce time, perform the above operation
  16. key=COL & 0x0F; // to ensure the Key press.
  17.  
  18. }while(key==0x0f);
  19.  
  20. }


KEYPAD_ScanKey()

  • Description  : This function scans all the rows to decode the key pressed.
  1. Each time a ROW line is pulled low to detect the KEY.
  2. Column Lines are read to check the key press.
  3. If any Key is pressed then corresponding Column Line goes low.
  4. Return the ScanCode(Combination of ROW & COL) for decoding the key.
  • I/P Arguments:none
  • Return value : char--> Scancode of the Key Pressed


  1. unsigned char KEYPAD_ScanKey()
  2. {
  3.  
  4. unsigned char ScanKey = 0xe0,i, key;
  5.  
  6. for(i=0;i<0x04;i++) // Scan All the 4-Rows for key press
  7. {
  8. ROW=ScanKey + 0x0F; // Select 1-Row at a time for Scanning the Key
  9. key=COL & 0x0F; // Read the Column, for key press
  10.  
  11. if(key!= 0x0F) // If the KEY press is detected for the selected
  12. break; // ROW then stop Scanning,
  13.  
  14. ScanKey=(ScanKey<<1)+ 0x10; // Rotate the ScanKey to SCAN the remaining Rows
  15. }
  16.  
  17. key = key + (ScanKey & 0xf0); // Return the row and COL status to decode the key
  18.  
  19. return(key);
  20. }


KEYPAD_GetKey()

  • Description: This function waits till a key is pressed and returns its ASCII Value
  1. Wait till the previous key is released..
  2. Wait for the new key press.
  3. Scan all the rows one at a time for the pressed key.
  4. Decode the key pressed depending on ROW-COL combination and returns its ASCII value.
  • I/P Arguments: none
  • Return value : char--> ASCII value of the Key Pressed


  1. unsigned char KEYPAD_GetKey()
  2. {
  3. unsigned char key;
  4.  
  5. KEYPAD_WaitForKeyRelease(); // Wait for the previous key release
  6. _delay_ms(1);
  7.  
  8. KEYPAD_WaitForKeyPress(); // Wait for the new key press
  9. key = KEYPAD_ScanKey(); // Scan for the key pressed.
  10.  
  11. switch(key) // Decode the key
  12. {
  13. case 0xe7: key='0'; break;
  14. case 0xeb: key='1'; break;
  15. case 0xed: key='2'; break;
  16. case 0xee: key='3'; break;
  17. case 0xd7: key='4'; break;
  18. case 0xdb: key='5'; break;
  19. case 0xdd: key='6'; break;
  20. case 0xde: key='7'; break;
  21. case 0xb7: key='8'; break;
  22. case 0xbb: key='9'; break;
  23. case 0xbd: key='A'; break;
  24. case 0xbe: key='B'; break;
  25. case 0x77: key='C'; break;
  26. case 0x7b: key='D'; break;
  27. case 0x7d: key='E'; break;
  28. case 0x7e: key='F'; break;
  29. default: key='z';
  30. }
  31. return(key); // Return the key
  32. }

UART

  1. AVR UART library for Serial Communication for 9600 baud rate at 11.0592Mhz
  • Filename: uart.c
  • Controller: Atmega8/16/32/128
  • Oscillator: 11.0592 MHz
  • Author: XploreLabz
  • website: www.xplorelabz.com
  1. #include<avr/io.h>

UART_Init()

  • Description  :This function is used to initialize the UART at 9600 baud rate by below configuration.
  • I/P Arguments: none
  • Return value : none
  1. void UART_Init()
  2. {
  3. UCSRB= 0x18; // Enable Receiver and Transmitter
  4. UCSRC= 0x86; // Asynchronous mode 8-bit data and 1-stop bit
  5. UCSRA= 0x00; // Normal Baud rate(no doubling), Single processor commn
  6. UBRRH= 0;
  7. UBRRL= 71; // 9600 Baud rate at 11.0592MHz
  8. }

UART_RxChar()

  • Description :This function is used to receive a char from UART module.
  1. It waits till a char is received ie.till RXC is set,
  2. RXC will be set once a CHAR is received.
  3. Finally returns the received char.
  • I/P Arguments: none
  • Return value : char
  1. char UART_RxChar()
  2. {
  3. while((UCSRA & (1<<RXC))==0); // Wait till the data is received
  4. return(UDR); // return the received char
  5. }


UART_TxChar()

  • Description  :This function is used to transmit a char through UART module.
  1. It waits till previous char is transmitted ie.till UDRE is set.
  2. UDRE will be set once a CHAR is transmitted ie UDR becomes empty.
  3. Finally the new Char to be transmitted is loaded into UDR.
  • I/P Arguments: char--> data to be transmitted
  • Return value : none
  1.  
  2. void UART_TxChar(char ch)
  3. {
  4. while((UCSRA & (1<<UDRE))==0); // Wait till Transmitter(UDR) register becomes Empty
  5. UDR =ch; // Load the data to be transmitted
  6. }



UART_TxString()

  • Description :This function is used to transmit the ASCII string through UART.
  1. The string_ptr points to the first char of the string.
  2. And it is incremented each time to traverse till the end(NULL CHAR).
  3. Each time a char is sent to UART_TxChar() fun to transmit it through UART
  • I/P Arguments: String(Address of the string) to be transmitted.
  • Return value : none
  1.  
  2.  
  3. void UART_TxString(char *string_ptr)
  4. {
  5. while(*string_ptr)
  6. UART_TxChar(*string_ptr++);
  7. }


UART_RxString()

  • Description  :
  1. This function is used to receive a ASCII string through UART till the carriage_return/New_line.
  2. The string_ptr points to the begining of the string and each time UART_RxChar() function is called to receive a char and copy it into the buffer(STRING) and incrment string_ptr.
  3. Once the carriage_return/New_line is encountered the loop is breaked and the String is NULL terminated.
  • I/P Arguments: *string_ptr(Address of the string where the received data needs to be stored)
  • Return value : none


  1. NOTE:
  1. The received char is ECHOED back,if not required then comment UART_TxChar(ch) in the code.
  2. BackSlash is not taken care.
  1. void UART_RxString(char *string_ptr)
  2. {
  3. char ch;
  4. while(1)
  5. {
  6. ch=UART_RxChar(); //Reaceive a char
  7. UART_TxChar(ch); //Echo back the received char
  8.  
  9. if((ch=='\r') || (ch=='\n')) //read till enter key is pressed
  10. { //once enter key is pressed
  11. *string_ptr=0; //null terminate the string
  12. break; //and break the loop
  13. }
  14. *string_ptr=ch; //copy the char into string.
  15. string_ptr++; //and increment the pointer
  16. }
  17. }


UART_TxNumber()

  • Description  :This function is used to transmit a 5-digit integer(0-65535).
    • ex:
  1. if the number is 12345 then 12345 is transmitted.
  2. if the number is 123 then 00123 is transmitted.
  • I/P Arguments: unsigned int
  • Return value : none

UART txnumber.png


  1. void UART_TxNumber(unsigned int num)
  2. {
  3.  
  4. UART_TxChar((num/10000)+0x30);
  5. num=num%10000;
  6.  
  7. UART_TxChar((num/1000)+0x30);
  8. num=num%1000;
  9.  
  10. UART_TxChar((num/100)+0x30);
  11. num=num%100;
  12.  
  13. UART_TxChar((num/10)+0x30);
  14.  
  15. UART_TxChar((num%10)+0x30);
  16. }


I2C

  1. Avr I2C library
  • Filename: I2C.c
  • Controller: Atmega8/16/32/128
  • Oscillator: 11.0592 MHz
  • Author: XploreLabz
  • website: www.xplorelabz.com

Refer Atmega32 for register description.

  1. #include<avr\io.h>
  2. #include<util\delay.h>
  3. #include "i2c.h"

I2C_Init()

  • Description :This function is used to initialize the I2c Module.
  • I/P Arguments: none
  • Return value : none
  1. void I2C_Init()
  2. {
  3. TWSR=0x00; //set presca1er bits to zero
  4. TWBR=0x46; //SCL frequency is 100K for XTAL = 7.3728M
  5. TWCR=0x04; //enab1e TWI module
  6. }


I2C_Start()

  • Description  :This function is used to generate I2C Start Condition.
    • Start Condition: SDA goes low when SCL is High.
  • I/P Arguments: none
  • Return value : none

I2C start.png

  1. void I2C_Start()
  2. {
  3. TWCR = ((1<<TWINT) | (1<<TWSTA) | (1<<TWEN));
  4. while (!(TWCR & (1<<TWINT)));
  5. }

I2C_Stop()

  • Description  :This function is used to generate I2C Stop Condition.
    • Stop Condition: SDA goes High when SCL is High.
  • I/P Arguments: none
  • Return value : none

I2C stop.png

  1. void I2C_Stop(void)
  2. {
  3. TWCR = ((1<< TWINT) | (1<<TWEN) | (1<<TWSTO));
  4. _delay_us(10) ; //wait for a short time
  5. }

I2C_Write()

  • Description  :This function is used to send a byte on SDA line using I2C protocol
    • 8bit data is sent bit-by-bit on each clock cycle.
    • MSB(bit) is sent first and LSB(bit) is sent at last.
    • Data is sent when SCL is low.
  • I/P Arguments: unsigned char-->8bit data to be sent.
    • Return value: none

I2C write.png

  1. void I2C_Write(unsigned char dat)
  2. {
  3. TWDR = dat ;
  4. TWCR = ((1<< TWINT) | (1<<TWEN));
  5. while (!(TWCR & (1 <<TWINT)));
  6. }

I2C_Read()

  • Description :This fun is used to receive a byte on SDA line using I2C protocol.
    • 8bit data is received bit-by-bit each clock and finally packed into Byte.
    • MSB(bit) is received first and LSB(bit) is received at last.
  • I/P Arguments: char: Acknowledgement for the Ninth clock cycle.
  • Return value : Unsigned char(received byte)

I2C read.png

  1. unsigned char I2C_Read(unsigned char ack)
  2. {
  3. TWCR = ((1<< TWINT) | (1<<TWEN) | (ack<<TWEA));
  4. while ( !(TWCR & (1 <<TWINT)));
  5. return TWDR;
  6. }

RTC_DS1307

  1. DS1307 library
  • Filename: DS1307.c
  • Controller: Atmega8/16/32/128
  • Oscillator: 11.0592 MHz
  • Author: XploreLabz
  • website: www.xplorelabz.com


  1. #include <avr\io.h>
  2. #include <util\delay.h>
  3.  
  4. #include "ds1307.h"
  5. #include "i2c.h"


  • Below values are fixed and should not be changed.
  • Refer Ds1307 DataSheet for more info
  1. #define DS1307_ID 0xD0 // DS1307 ID
  2.  
  3. #define SEC_ADDRESS 0x00 // Address to access Ds1307 SEC register
  4. #define DATE_ADDRESS 0x04 // Address to access Ds1307 DATE register
  5. #define CONTROL 0x07 // Address to access Ds1307 CONTROL register


DS1307_Init()

  • Description :This function is used to initialize the Ds1307 RTC.
    • Ds1307 ic is enabled by sending the DS1307 id on the I2C bus.
    • After selecting DS1307, write 0x00 into Control register of Ds1307
  • I/P Arguments: none
  • Return value : none
  1. void DS1307_Init()
  2. {
  3. I2C_Init(); // Initilize the I2c module.
  4. I2C_Start(); // Start I2C communication
  5.  
  6. I2C_Write(DS1307_ID); // Connect to DS1307 by sending its ID on I2c Bus
  7. I2C_Write(CONTROL); // Select the Ds1307 ControlRegister to configure Ds1307
  8.  
  9. I2C_Write(0x00); // Write 0x00 to Control register to disable SQW-Out
  10.  
  11. I2C_Stop(); // Stop I2C communication after initilizing DS1307
  12. }


DS1307_SetTime()

  • Description  :This function is used to set Time(hh,mm,ss) into the Ds1307 RTC.
    • Ds1307 ic is enabled by sending the DS1307 id on the I2C bus.
    • After selecting DS1307, select the RAM address 0x00 to point to sec.
    • Initilze Sec, MIN, Hour one after the other.
    • Stop the I2c communication.
  • I/P Arguments: char,char,char-->hh,mm,ss to initilize the time into DS1307.
  • Return value : none
  1.  
  2.  
  3. void DS1307_SetTime(unsigned char hh, unsigned char mm, unsigned char ss)
  4. {
  5. I2C_Start(); // Start I2C communication
  6.  
  7. I2C_Write(DS1307_ID); // connect to DS1307 by sending its ID on I2c Bus
  8. I2C_Write(SEC_ADDRESS); // Select the SEC RAM address
  9.  
  10. I2C_Write(ss); // Write sec on RAM address 00H
  11. I2C_Write(mm); // Write min on RAM address 01H
  12. I2C_Write(hh); // Write hour on RAM address 02H
  13.  
  14. I2C_Stop(); // Stop I2C communication after Setting the Time
  15. }


DS1307_SetDate()

  • Description  :This function is used to set Date(dd,mm,yy) into the Ds1307 RTC.
    • Ds1307 ic is enabled by sending the DS1307 id on the I2C bus.
    • After selecting DS1307, select the RAM address 0x04 to point to day.
    • Initilze Day,Month and Year one after the other.
    • Stop the I2c communication.
  • I/P Arguments: char,char,char-->day,month,year to initilize the Date into DS1307.
  • Return value : none
  1. void DS1307_SetDate(unsigned char dd, unsigned char mm, unsigned char yy)
  2. {
  3. I2C_Start(); // Start I2C communication
  4.  
  5. I2C_Write(DS1307_ID); // connect to DS1307 by sending its ID on I2c Bus
  6. I2C_Write(DATE_ADDRESS); // Request DAY RAM address at 04H
  7.  
  8. I2C_Write(dd); // Write date on RAM address 04H
  9. I2C_Write(mm); // Write month on RAM address 05H
  10. I2C_Write(yy); // Write year on RAM address 06h
  11.  
  12. I2C_Stop(); // Stop I2C communication after Setting the Date
  13. }



DS1307_GetTime()

  • Description  :This function is used to get the Time(hh,mm,ss) from Ds1307 RTC.
    • Ds1307 ic is enabled by sending the DS1307 id on the I2C bus.
    • After selecting DS1307, select the RAM address 0x00 to point to sec.
    • Get Sec, MIN, Hour one after the other.
    • Stop the I2c communication.
  • I/P Arguments: char *,char *,char *-->pointers to get the hh,mm,ss.
  • Return value : none
  1. void DS1307_GetTime(unsigned char *h_ptr,unsigned char *m_ptr,unsigned char *s_ptr)
  2. {
  3. I2C_Start(); // Start I2C communication
  4.  
  5. I2C_Write(DS1307_ID); // connect to DS1307 by sending its ID on I2c Bus
  6. I2C_Write(SEC_ADDRESS); // Request Sec RAM address at 00H
  7.  
  8. I2C_Stop(); // Stop I2C communication after selecting Sec Register
  9.  
  10. I2C_Start(); // Start I2C communication
  11. I2C_Write(0xD1); // connect to DS1307( under Read mode)
  12. //by sending its ID on I2c Bus
  13.  
  14. *s_ptr = I2C_Read(1); // read second and return Positive ACK
  15. *m_ptr = I2C_Read(1); // read minute and return Positive ACK
  16. *h_ptr = I2C_Read(0); // read hour and return Negative/No ACK
  17.  
  18. I2C_Stop(); // Stop I2C communication after reading the Time
  19. }



DS1307_GetDate()

  • Description  :This function is used to get the Date(y,m,d) from Ds1307 RTC.
    • Ds1307 ic is enabled by sending the DS1307 id on the I2C bus.
    • After selecting DS1307, select the RAM address 0x00 to point to DAY.
    • Get Day, Month, Year one after the other.
    • Stop the I2c communication.
  • I/P Arguments: char *,char *,char *-->pointers to get the y,m,d.
  • Return value : none


  1. void DS1307_GetDate(unsigned char *d_ptr,unsigned char *m_ptr,unsigned char *y_ptr)
  2. {
  3. I2C_Start(); // Start I2C communication
  4.  
  5. I2C_Write(DS1307_ID); // connect to DS1307 by sending its ID on I2c Bus
  6. I2C_Write(DATE_ADDRESS); // Request DAY RAM address at 04H
  7.  
  8. I2C_Stop(); // Stop I2C communication after selecting DAY Register
  9.  
  10.  
  11. I2C_Start(); // Start I2C communication
  12. I2C_Write(0xD1); // connect to DS1307( under Read mode)
  13. // by sending its ID on I2c Bus
  14.  
  15. *d_ptr = I2C_Read(1); // read Day and return Positive ACK
  16. *m_ptr = I2C_Read(1); // read Month and return Positive ACK
  17. *y_ptr = I2C_Read(0); // read Year and return Negative/No ACK
  18.  
  19. I2C_Stop(); // Stop I2C communication after reading the Time
  20. }