ATMEGA32 basic programming tutorial: In the following ATMEGA32 tutorial i used books to learn fundamental knowledge about Embedded system. After that i install Atmel Studio compiler and Proteus simulator tool to implement following basic ATMEGA32 programming tutorial:
Interfacing of External RTC with ATMEGA32 : In this tutorial External RTC is interfaced with AVR(ATMEGA32 ) controller. You need to enter password "1234#", you can use this tutorial for security checking projects. If you want to learn more kindly see tutorial on youtube as well.
>> External RTC interface with ATMEGA32 C Code: /*******************RTC.c*******************/ #define LCD_DIR DDRD #define CTRL_DIR DDRB #define LCD PORTD #define RS PORTB.F0 #define E PORTB.F1 sbit Soft_I2C_Scl_Output at PORTC1_bit; sbit Soft_I2C_Sda_Output at PORTC0_bit; sbit Soft_I2C_Scl_Input at PINC1_bit; sbit Soft_I2C_Sda_Input at PINC0_bit; sbit Soft_I2C_Scl_Direction at DDC1_bit; sbit Soft_I2C_Sda_Direction at DDC0_bit; ///////////////////////////////////////////////////// #include"lcd.h" #include"rtc.h"
void write_rtc(char dat,char add) // eeprom write function at24c256 eeprom is used and is having 16 bits of address { Soft_I2C_Start(); // Issue start signal Soft_I2C_Write(0xD0); // Address PCF8583, see PCF8583 datasheet Soft_I2C_Write(add); // Start from address 2 Soft_I2C_Write(dat); // Start from address 2 Soft_I2C_Stop(); // Issue stop signal }
unsigned char read_rtc(char add) { char read; Soft_I2C_Start(); // Issue start signal Soft_I2C_Write(0xD0); // Address PCF8583, see PCF8583 datasheet Soft_I2C_Write(add); // Start from address 2
Soft_I2C_Start(); // Issue repeated start signal Soft_I2C_Write(0xD1); // Address PCF8583 for reading R/W=1 read = Soft_I2C_Read(0); // Read seconds byte Soft_I2C_Stop(); // Issue stop signal return read; } void set_rtc(unsigned char sec,unsigned char min,unsigned char hrs,unsigned char day,unsigned char date,unsigned char month,unsigned char year) { sec = sec & 0x7f; min = min & 0x7f; hrs = hrs & 0x3f; day = day & 0x07; date = date & 0x3f; month = month & 0x1f; year = year & 0x7f;
write_rtc(sec,0); write_rtc(min,1); write_rtc(hrs,2); write_rtc(day,3); write_rtc(date,4); write_rtc(month,5); write_rtc(year,6); } void init_rtc() { Soft_I2C_Init(); // initialize I2C communication delay_ms(100); } /********************** lcd.h **************************/ void cmd_lcd (unsigned char dat) // function to write command at lcd port { LCD=dat; RS=0; //clear RS (ie. RS=0) to write command E=1; // send H-L pulse at E pin delay_us(100); E=0; delay_us(100); }
void data_lcd (unsigned char dat) // function to write data at lcd port { LCD=dat; RS=1; // set RS=1 to write DATA E=1; // send H-L pulse at E pin delay_us(100); E=0; delay_us(100); }
void init_lcd() // function to initialize the LCD at power on time { cmd_lcd (0x38); // 2x16 display select delay_ms(50); cmd_lcd (0x0c); // display on cursor off command delay_ms(1); cmd_lcd (0x06); // automatic cursor movement to right delay_ms(1); cmd_lcd (0x01); // lcd clear command delay_ms(5); }
void string_lcd(unsigned char *str) // function to display string to lcd { while(*str!='\0') // '\0' is null char as last char of pointer is null { data_lcd(*str); str++; } } void number_lcd(unsigned int dat, unsigned int siz) // function to display any digit decimal value to lcd { unsigned int temp,temparr[10];