ATmega8535L Eeprom示例程序
ATmega8535L Eeprom示例程序
//***************ICCAVRV6.30編譯*****************//
#include
#defineucharunsignedchar
#defineuintunsignedint
//數(shù)碼管字型表,對應(yīng)0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F//
ucharTable[16]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
ucharRead_Data[4];//定義讀出數(shù)據(jù)
voidDelayMs(uinti)//Ms級延時,參數(shù)i為延時時間
{uintj;
for(;i!=0;i--)
{for(j=8000;j!=0;j--){;}}
}
voidDisplay(uchar*p)//動態(tài)顯示函數(shù),參數(shù)p為待顯示的數(shù)組名
{uchari,sel=0x01;
for(i=0;i<4;i++)
{PORTA=sel;//選通最右邊的數(shù)碼管
PORTC=0xff-Table[p[i]];//送字型碼
DelayMs(1);//顯示延時
sel=sel<<1;//移位以顯示前一位
}
}
voidInit_IO(void)//初始化I/O口
{DDRA=0xff;//設(shè)置A口為推挽1輸出
PORTA=0xff;
DDRC=0xff;//設(shè)置C口為推挽1輸出
PORTC=0xff;
}
voidWrite_EEPROM(ucharData,uintAddress)
{if(EECR&0x20)DelayMs(4);
EEARH=Address>>8;
EEARL=Address&0x00ff;
EEDR=Data;
EECR=EECR|0x04;
EECR=EECR|0x02;
DelayMs(4);
}
ucharRead_EEPROM(uintAddress)
{uchari;
if(EECR&0x01)DelayMs(4);
EEARH=Address>>8;
EEARL=Address&0x00ff;
EECR=EECR|0x01;
DelayMs(5);
i=EEDR;
return(i);
}
voidmain(void)
{uchari;
Init_IO();//初始化I/O口
PORTA=0xff;//點亮以測試所有的數(shù)碼管
PORTC=0x00;
DelayMs(300);//延時
PORTC=0xff;//熄滅所有的數(shù)碼管
for(i=0;i<4;i++)
Write_EEPROM(5*i,i);
for(i=0;i<4;i++)
Read_Data[i]=Read_EEPROM(i);
while(1)
Display(Read_Data);
}