eeprom读写.c

来自「ICC mega8例子」· C语言 代码 · 共 74 行

C
74
字号
//ICC-AVR application builder : 2009-3-12 17:39:42
// Target : M8
// Crystal: 8.0000Mhz

#include <iom8v.h>
#include <macros.h>

void port_init(void)
{
 PORTB = 0x00;
 DDRB  = 0x00;
 PORTC = 0x00; //m103 output only
 DDRC  = 0x00;
 PORTD = 0x00;
 DDRD  = 0x00;
}

//call this routine to initialize all peripherals
void init_devices(void)
{
 //stop errant interrupts until set up
 CLI(); //disable all interrupts
 port_init();

 MCUCR = 0x00;
 GICR  = 0x00;
 TIMSK = 0x00; //timer interrupt sources
 SEI(); //re-enable interrupts
 //all peripherals are now initialized
}

/*****************************************************************************
用    途:EEPROM读写程序
Taget   :mega8
crystal :8M
介    绍:
*****************************************************************************/
//eeprom写操作
void eeprom_write(unsigned int address,unsigned char data)
{
 while(EECR&(1<<EEWE))
 {;}
 EEAR=address;
 EEDR=data;
 EECR|=(1<<EEMWE);
 EECR|=(1<<EEWE);
}
//eeprom读操作
unsigned char eeprom_read(unsigned int address)
{
 while(EECR&(1<<EEWE))
 {;}
 EEAR=address;
 EECR|=(1<<EERE);
 return EEDR;
}
//****************************************************************************
void main()
{
 unsigned char i;
 port_init();
 init_devices();
 
 eeprom_write(1,0x55);
 eeprom_write(2,0xAA);
 i=eeprom_read(1);
 PORTB=i;
 i=eeprom_read(2);
 PORTD=i;
 while(1)
 {
  ;
 }
} 

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?