您的位置 首页 芯闻

ATMEGA16驱动内部EEPROM程序

8MHZ晶体在ATMEGA16上运行显示效果:显示按键设定数字includeiom16vh>includemacrosh>defineucharunsignedcharde

// 8MHZ晶体在ATMEGA16上运转

// 显现作用:显现按键设定数字
#include
#include
#define uchar unsigned char
#define uint unsigned int
#pragmadata:code
const uchar table[]=”I LIKE AVR !”;
const uchar table1[]=”WWW*FUMANCHE*COM”;
const uchar table_num[]=”01234567890″;

void delay(uint ms)//延时
{
uint i,j;
for(i=0;i {
for(j=0;j<1142;j++);
}
}
void write_com(uchar com) //写指令
{
PORTD&=~BIT(4);
PORTD&=~BIT(5);
PORTC=com;
PORTD|=BIT(6);
delay(1);
PORTD&=~BIT(6);
}

void write_dat(uchar dat) //写数据
{
PORTD|=BIT(4);
PORTD&=~BIT(5);
PORTC=dat;
PORTD|=BIT(6);
delay(1);
PORTD&=~BIT(6);
}

/*EEPROM读取函数*/
/*addr:地址;number:长度;p_buff:读出数据寄存指针*/

void eprom_read(unsigned int addr, unsigned char number, unsigned char *p_buff)
{

while(EECR & (1 << EEWE));

EEARH = 0x00;

while(number –)
{
EEARL = addr ++;
EECR |= (1 << EERE);
*p_buff++ = EEDR;
}
}
/*EEPROM写入函数*/
/*addr:地址;number:长度;p_buff:写入数据寄存指针*/

void eprom_write(unsigned int addr, unsigned char number, unsigned char *p_buff)
{

EEARH = 0x00;

while(number –) {
while(EECR & (1 << EEWE));
EEARL = addr ++;
EEDR = *p_buff ++;
EECR |= (1 << EEMWE);
EECR |= (1 << EEWE);
}
}

void main()
{

uchar i;
uchar bai,shi,ge,temp;
DDRB = 0xf0; //PB口低四位设为输入
PORTB = 0x0f; //PB口低四位上拉电阻使能
DDRC=0XFF; //液晶端口设定为输出
DDRD|=BIT(4)|BIT(5)|BIT(6);

delay(250); //参加延时,防止影响搅扰下一句
PORTD&=~BIT(6);

write_com(0X38); //液晶初始化
delay(5);
write_com(0X01); //显现清屏
delay(5);
write_com(0X0C); //开显现
delay(5);
// write_com(0X0F); //显现光标并闪耀
// delay(5);
delay(250); //发动延时
eprom_read(0x10, 0x01, &temp);//读出记载

while(1)
{
if(PINB==0x0e) //PB0按键检测
{
delay(250); //发动延时
eprom_read(0x10, 0x01, &temp);//读出记载
temp++; //刷新记载
eprom_write(0x10, 0x01, &temp);//写入记载
}
while(PINB==0x0e); //当按键一向按下时等候

if(PINB==0x0d) //PB1按键检测
{
delay(250); //发动延时
eprom_read(0x10, 0x01, &temp);//读出记载
temp–; //刷新记载
eprom_write(0x10, 0x01, &temp);//写入记载
}
while(PINB==0x0d);

delay(5);
ge=temp%10;
shi=temp%100/10;
bai=temp/100;
write_com(0X80+0X06);//数据开端指针设置–从头设置为这个地址开端寄存数据
write_dat(table_num[bai]);
delay(5);
write_dat(table_num[shi]);
delay(5);
write_dat(table_num[ge]);
delay(5);

write_com(0X80+0X40);//数据开端指针设置
delay(5);
for(i=0;i<16;i++) //循环13次显现内容
{
write_dat(table1[i]);
delay(5);
}
/*
for(i=0;i<16;i++) //循环16次从LCD屏幕左面一向显现到右边
{
write_com(0X1c);
delay(500);
}
*/

}
}
//本程序经过验证,运转安稳!!

声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/news/xinwen/318526.html

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部