// 8MHZ晶体 在ATMEGA16上运行
// 显示效果:显示按键设定数字
#include <iom16v.h>
#include <macros.h>
#define uchar unsigned char
#define uint unsigned int
#pragma data: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<ms;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);
}
*/
}
}
//本程序通过验证,运行稳定!!