ATMEGA8515内部EEPROM的读写程序
扫描二维码
随时随地手机看文章
/**************************************************************
;eeprom.c
;读/写atmega8515内部EEPROM的例子
;将数据0....9写入eeprom中,再读出用数码管显示出来
***************************************************************/
#include"avr/io.h"
#include"avr/eeprom.h"
#defineucharunsignedchar
#defineHidden16
ucharDispTab[17]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0x88,0x83,0xC6,0xA1,0x86,0x8E,0xFF};
ucharBitTab[6]={0x7F,0xBF,0xDF,0xEF,0xF7,0xFB};
ucharDispBuf[6]; //显示缓冲区
unsignedcharCounter=0;
statICvoidio_init(void)
{
//PortA
PORTA=0xff;
DDRA=0xff;
//PortB
PORTB=0x0;
DDRB=0x0;
//PortC
PORTC=0xfc;
DDRC=0xfc;
//PortD
PORTD=0x0;
DDRD=0x0;
}
voidmDelay(uint16_tDelayTim)
{ uint16_ti;
for(;DelayTim>0;DelayTim--)
{ for(i=0;i<600;i++)
{;}
}
}
voiddisp()
{ staticuchari;
uchartmp;
if(++i>=6)
i=0;
PORTC=0xff; //关闭显示
tmp=DispBuf[i];
PORTA=DispTab[tmp];
tmp=BitTab[i];
PORTC=tmp;
mDelay(1); //延时1ms
}
intmain(void)
{ unsignedintCount=0;
unsignedcharAddr=1; //向地址1里面写数字
uint8_ttemp=0;
io_init();
DispBuf[0]=Hidden;
DispBuf[1]=Hidden;
DispBuf[2]=Hidden;
DispBuf[3]=Hidden;
DispBuf[4]=0; //次低位显示0
while(1)
{
temp=eeprom_read_byte(Addr);
DispBuf[5]=temp;
if(++Count==1000)
{ Count=0;
Counter++;
eeprom_write_byte(Addr,Counter);
}
if(Counter>=10)
Counter=0; //计数值在0~9之间循环
disp(); //调用显示程序
}
}