AVR Eeprom的c語言編程
//***************ICCAVR 編譯*****************//
本文引用地址:http://www.ex-cimer.com/article/201611/316776.htm#include
#define uchar unsigned char
#define uint unsigned int
//數(shù)碼管字型表,對應(yīng)0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F//
uchar Table[16]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
uchar Read_Da
void DelayMs(uint i) //Ms級延時,參數(shù)i為延時時間
{uint j;
for(;i!=0;i--)
{for(j=8000;j!=0;j--) {;}}
}
void Display(uchar *p) //動態(tài)顯示函數(shù),參數(shù)p為待顯示的數(shù)組名
{uchar i,sel=0x01;
for(i=0;i<4;i++)
{PORTA=sel; //選通最右邊的數(shù)碼管
PORTC=0xff-Table[p[i]]; //送字型碼
DelayMs(1); //顯示延時
sel=sel<<1; //移位以顯示前一位
}
}
void Init_IO(void) //初始化I/O口
{DDRA=0xff; //設(shè)置A口為推挽1輸出
PORTA=0xff;
DDRC=0xff; //設(shè)置C口為推挽1輸出
PORTC=0xff;
}
void Write_EEPROM(uchar Da
{if(EECR&0x20) DelayMs(4);
EEARH=Address>>8;
EEARL=Address&0x00ff;
EEDR=Da
EECR=EECR|0x04;
EECR=EECR|0x02;
DelayMs(4);
}
uchar Read_EEPROM(uint Address)
{uchar i;
if(EECR&0x01) DelayMs(4);
EEARH=Address>>8;
EEARL=Address&0x00ff;
EECR=EECR|0x01;
DelayMs(5);
i=EEDR;
return(i);
}
void main(void)
{uchar i;
Init_IO(); //初始化I/O口
PORTA=0xff; //點亮以測試所有的數(shù)碼管
PORTC=0x00;
DelayMs(300); //延時
PORTC=0xff; //熄滅所有的數(shù)碼管
for(i=0;i<4;i++)
Write_EEPROM(4*i,i);
for(i=0;i<4;i++)
Read_Da
while(1)
Display(Read_Da
}
評論