/**************************************************************
;eeprom.c 在AVR单片机中能够用在ATMEGA16和ATMEGA8中都能够用,在GCC下
;编译经过。09年11月1号! 陈永飞已测试过!
;读/写atmega8515内部EEPROM的比如
;将数据0….9写入eeprom中,再读出用数码管显现出来
***************************************************************/
#include <avr/io.h>
#include
#define uchar unsigned char
#define uint unsigned int
#define Hidden 16
uchar DispTab[17]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0x88,0x83,0xC6,0xA1,0x86,0x8E,0xFF};
uchar BitTab[6]={0x7F,0xBF,0xDF,0xEF,0xF7,0xFB};
uchar DispBuf[6]; //显现缓冲区
unsigned char Counter=0;
void io_init(void)
{
// PortA
PORTA = 0xff;
DDRA = 0xff;
// PortB
PORTB = 0x0;
DDRB = 0x0;
// PortC
PORTC = 0xfc;
DDRC = 0xfc;
// PortD
PORTD = 0x0;
DDRD = 0x0;
}
void mDelay(uint DelayTim)
{
uint i;
for(;DelayTim>0;DelayTim–)
{
for(i=0;i<600;i++)
{;}
}
}
void disp()
{
uchar i;
uchar tmp;
if(++i>=6)
i=0;
PORTC=0xff; //封闭显现
tmp=DispBuf[i];
PORTA=DispTab[tmp];
tmp=BitTab[i];
PORTC=tmp;
mDelay(1); //延时1ms
}
int main(void)
{
unsigned int Count=0;
unsigned char Addr=1; //向地址1里边写数字
uint8_t temp=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(); //调用显现程序
}
}