代码:
#include <mega16.h>
#include <stdio.h>
#include <i2c.h>
#include <delay.h>
// Declare your global variables here
#define EEPROM_BUS_ADDRESS 0xa0
#asm
.equ __i2c_port=0x15
.equ __sda_bit=1
.equ __scl_bit=0
#endasm
unsigned char eeprom_read(unsigned int address)
{
unsigned char data;
i2c_start();
i2c_write(EEPROM_BUS_ADDRESS);
i2c_write(address>>8);
i2c_write(address);
i2c_start();
i2c_write(EEPROM_BUS_ADDRESS|1);
data=i2c_read(0);
i2c_stop();
return data;
}
void eeprom_write(unsigned int address,unsigned char data)
{
i2c_start();
i2c_write(EEPROM_BUS_ADDRESS);
i2c_write(address>>8);
i2c_write(address);
i2c_write(data);
i2c_stop();
delay_ms(10);
}
void main(void)
{
// Declare your local variables here
unsigned char i;
PORTA=0XFF;
DDRA=0XFF;
UCSRA=0;
UCSRB=0X18;
UCSRC=0X86;
UBRRH=0X00;
UBRRL=0X19;
i2c_init();
eeprom_write(0x00aa,0x5a);
i=eeprom_read(0x00aa);
if(i==0x5a)
PORTA.7=0;
else
PORTA.6=0;
while (1)
{
// Place your code here
}
}
仿真效果图: