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 EEPROM with ATMEGA32 : In this tutorial External EEPROMis 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 EEPROM interface with ATMEGA32 C Code: /*******************EEPROM.c*******************/ ///////////////////// pin defined /////////////////// 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;
#define LCD_DIR DDRA #define CTRL_DIR DDRB #define LCD PORTA #define RS PORTB.F0 #define E PORTB.F1 ///////////////////////////////////////////////////// #include"lcd.h" #include"eeprom.h"
void main(void) { char disp; int value = 20, add = 10; CTRL_DIR=0XFF; LCD_DIR=0XFF; init_lcd(); cmd_lcd(0x80); string_lcd("EEPROM Test"); write_eeprom(value,add); while(1) { disp = read_eeprom(add); cmd_lcd(0xC0); number_lcd(disp,3); } } /*******************eeprom.h*******************/ void write_eeprom(unsigned char dat,unsigned 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(0xa0); // 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_eeprom(unsigned char add) { char read; Soft_I2C_Start(); // Issue start signal Soft_I2C_Write(0xa0); // Address PCF8583, see PCF8583 datasheet Soft_I2C_Write(add); // Start from address 2 Soft_I2C_Start(); // Issue repeated start signal Soft_I2C_Write(0xa1); // Address PCF8583 for reading R/W=1 read = Soft_I2C_Read(0); // Read seconds byte Soft_I2C_Stop(); // Issue stop signal return read; }
void init_eeprom() { 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];