|
仔细看了一下,你的思路完全正确,但是我的程序改了之后还是老样子,真的不知道哪里出了问题,你帮我看一看,我是新手,刚开始学习AVR单片机,谢谢了!
#include
#include
#define uchar unsigned char
#define uint unsigned int
#include"lcd1602.c"
#include"anjian.c"
uchar x,y,z,key;
uchar shigao,shidi,fengao,fendi,miaogao,miaodi;
#pragma data:code
const uchar string1[]=" current time: ";
const uchar string2[]="set new time....";
#pragma data:data
uchar string3[]="0123456789:";
#pragma interrupt_handler miao:9 //定时器T1中断入口
uchar settime;
uchar change_h_m_s;
void miao()
{
CLI();
MCUCR=0X00; //空闲模式
GICR=0X00;//禁止外部中断请求
z++;
if(z==60)
{
z=0;
y+=1;
if(y==60)
{
y=0;
x+=1;
if(x==24)
{
x=0;y=0;z=0;
}
}
}
TCNT1H=0x85;
TCNT1L=0Xed;
SEI();
}
void set_lcd_pos(uchar p)
{
lcd_write_com(p|0x80);
delay(1);
}
void display_string(uchar p, const uchar *s)
{
uchar i;
set_lcd_pos(p);
for(i=0;i<16;i++)
{
lcd_write_date(s);
delay(1);
}
}
void beep()
{
uint i; uchar j=200;
for(i=0;i<1145;i++)
{
while(--j)
PORTA=~BIT(6);
}
}
void fenli()
{
shigao=x/10; delay(1);
shidi=x%10; delay(1);
fengao=y/10; delay(1);
fendi=y%10; delay(1);
miaogao=z/10;delay(1);
miaodi=z%10; delay(1);
}
void display_shifenmiao()
{
fenli();
lcd_write_com(0xc4);
lcd_write_date(string3[shigao]);
lcd_write_com(0xc5);
lcd_write_date(string3[shidi]);
lcd_write_com(0xc7);
lcd_write_date(string3[fengao]);
lcd_write_com(0xc8);
lcd_write_date(string3[fendi]);
lcd_write_com(0xca);
lcd_write_date(string3[miaogao]);
lcd_write_com(0xcb);
lcd_write_date(string3[miaodi]);
}
/* change_time()
{
settime=0;
if(key==1||key==2||key==3)
{
display_string(0x00,string2);
settime=1;
}
while(settime)
{
if(key==1)
{
beep();
while(key==1);
change_h_m_s++;
if(change_h_m_s==4)
{change_h_m_s=0;}
}
else if(key==2)
{
beep();
while(key==2);
if(change_h_m_s==1)
{if(++x==24) x=0;}
else if(change_h_m_s==2)
{if(++y==60) y=0;}
else if(change_h_m_s==3)
{if(++z==60) z=0;}
}
else if(key==3)
{
beep();
while(key==3);
if(change_h_m_s==1)
{if(--x==0xff) x=23;}
else if(change_h_m_s==2)
{if(--y==0xff) y=59;}
else if(change_h_m_s==3)
{if(--z==0xff) z=59;}
}
else if(key==4)
{
beep();
while(key==4);
display_string(0x00,string1);
settime=0;
}
display_shifenmiao();
delay(5);
}
} */
void main()
{
uchar i;
x=22;
y=24;
z=0;
settime=0x00;
change_h_m_s=0x01;
DDRA=0X40;//设置接蜂鸣器的端口PA6为输出
PORTA=BIT(6);
CLI();
TCCR1B=0X04;//256分频
TCNT1H=0x85;
TCNT1L=0XED;//设置定时器初值
TIMSK|=BIT(2);//开启定时器中断
SEI();//开启总中断TCCR1B=0X04;
DDRD=0X70;//设置PD4,PD5,PD6都为输出
DDRB=0XFF;//pb口设置为输出
lcd_init();//lcd1602初始化
display_string(0x00,string1); // 第一行显示字符串1
lcd_write_com(0xc6);
lcd_write_date(string3[10]);
delay(1);
lcd_write_com(0xc9);
lcd_write_date(string3[10]);
delay(1); //显示“ : : ”
while(1)
{
display_shifenmiao();
//delay(10); //显示时间"22 44 00"
}
}
lcd1602的设置
void delay(uint ms)
{
uint a,b;
for(a=0;a
for(b=0;b<1141;b++);
}
void lcd_write_com(uchar com)
{
DDRD=0X70;//设置PD4,PD5,PD6都为输出
PORTD&=~BIT(4);
PORTD&=~BIT(5);//设置rs和rw为0
PORTD&=~BIT(6);//en为0
PORTB=com;
PORTD|=BIT(6);//en为1
delay(1);
PORTD&=~BIT(6); //en为0
}
void lcd_write_date(uchar dat)
{
DDRD=0X70;//设置PD4,PD5,PD6都为输出
PORTD|=BIT(4);//设置rs为1
PORTD&=~BIT(5);//设置rw为0
PORTD&=~BIT(6);//en为0
PORTB=dat;
PORTD|=BIT(6);//en为1
delay(1);
PORTD&=~BIT(6); //en为0
}
void lcd_init()
{
lcd_write_com(0x38);//设置lcd1602为5*7点阵,2行显示,8位总线
delay(1);
lcd_write_com(0x01);//清屏幕
delay(1);
lcd_write_com(0x0c);//开显示
delay(1);
lcd_write_com(0x06);//地址加1
delay(1);
}
按键部分先暂时不调用,先解决这个问题在说! 在线的各位高手,都来帮忙解决一下这个问题.
|
|