利用AT89C51单片机结合字符型LCD显示器设计一个简易的倒数计数器
1个回答
2013-09-12
展开全部
参考:ks1208+12864lcd屏幕控制电机,#include<avr/io.h>
#include<avr/delay.h>
#include<string.h>
#define INT8U unsigned char#define lcd_starthang 0b11000000 // 起始行
#define lcd_ye 0XB8 // 页指令
#define lcd_lie 0X40 // 列指令
#define RW PD0
#define DI PD1
#define E PD2
#define CS1 PD3
#define CS2 PD4
#define RST PD5#define lcd_port PORTC //液晶排线
#define lcd_ctrl PORTD //控制端口
#define lcd_ddr DDRC //输入输出
#define lcd_pin PINC //控制状态的数据
#define RW1() lcd_ctrl |=_BV(RW);
#define RW0() lcd_ctrl &=~_BV(RW);//数据总线#define DI1() lcd_ctrl |=_BV(DI);
#define DI0() lcd_ctrl &=~_BV(DI);#define CS11() lcd_ctrl |=_BV(CS1);
#define CS10() lcd_ctrl &=~_BV(CS1);//片选信号
#define CS21() lcd_ctrl |=_BV(CS2);
#define CS20() lcd_ctrl &=~_BV(CS2);#define E1() lcd_ctrl |=_BV(E);
#define E0() lcd_ctrl &=~_BV(E);//使能#define RST1() lcd_ctrl |=_BV(RST);
#define RST0() lcd_ctrl &=~_BV(RST);/复位char fxshow=0;
void wait_lcd_ready()
{ check_busy:
lcd_ddr =0X00;
lcd_port=0Xff;
RW1();asm ("nop"); DI0();
E1(); asm ("nop"); E0();
if (lcd_pin & 0x80) goto check_busy;
}
//lcd控制指令函数
void write_lcd_command(INT8U ABC)
{wait_lcd_ready();<br> lcd_ddr =0XFF; <br> lcd_port=0XFF;<br> RW0();asm ("nop");DI0();<br> lcd_port=ABC;<br> E1();asm ("nop");E0();<br>} void writedata(INT8U dat)
{ wait_lcd_ready();
lcd_ddr =0XFF;
lcd_port=0XFF;
RW0();asm ("nop");DI1();
if(!fxshow) lcd_port=dat;else lcd_port=~dat;
E1();asm ("nop");E0();
}
//-----------------------------------------------------初始化
void lcd_chushihua(){ write_lcd_command(0x3f);_delay_ms(15);}//void lcd_show(INT8U y,INT8U l,INT8U j,INT8U *r)//几页 y 、几列 l、几个字符 j、
{INT8U i;<br> if(l<64)<br> {CS11();CS20(); <br> write_lcd_command(lcd_ye+y);<br> write_lcd_command(lcd_lie+l);</p><p> if(l+j<64)<br> {<br> for (i=0;i<j;i++) writedata(r[i]);<br> }
else
{for (i=0;i<64-l;i++)writedata(r[i]);//zuo半瓶显示<br> CS10();CS21();<br> write_lcd_command(lcd_ye+y);<br> write_lcd_command(lcd_lie);<br> for (i=64-l;i<j;i++) writedata(r[i]);//有半瓶显示<br> <br> }
}
else{ CS10();CS21();
write_lcd_command(lcd_ye+y);
write_lcd_command(lcd_lie+l-64);
for (i=0;i<j;i++) writedata(r[i]);
}
}
void qingping()
{ INT8U i,j;
CS11();CS21();
write_lcd_command(lcd_ye);
write_lcd_command(lcd_lie);
for (i=0;i<8;i++)
{ write_lcd_command(lcd_ye+i);
for(j=0;j<128;j++)
{write_lcd_command(lcd_lie+j);<br> writedata(0x00);}
}
}
//-----------------------------8*16void display816(INT8U y,INT8U l,INT8U *r){ lcd_show(y, l,8, r );//几页 y 、几列 l、几个字符 j、
lcd_show(y+1,l,8,r+8 );} //--------------------------------一串8*16void display8161(INT8U y,INT8U l,INT8U D,INT8U *r){INT8U i;<br> for(i=0;i<D;i++)<br> { display816(y,l+i*8,r+16*i);}}//------------------------------8*16void display1616(INT8U y,INT8U l,INT8U *r){ lcd_show(y, l,16, r );//几页 y 、几列 l、几个字符 j、
lcd_show(y+1,l,16,r+16);
}void display16161(INT8U y,INT8U l,INT8U D,INT8U *r){INT8U i;<br> for(i=0;i<D;i++)<br> { display1616(y,l+i*16,r+32*i);}
}
//----------------------------------------------------
void guang(INT8U i,INT8U j)
{
CS11();CS21();
write_lcd_command(lcd_ye);
write_lcd_command(lcd_lie);
write_lcd_command(i);
write_lcd_command(j);
writedata(0x00);
}//------------------------------------------------
//---------------------------------------------------
//----------------------------------------------------
#include<avr/io.h>
#include<avr/delay.h>
#include<string.h>
#include<avr/interrupt.h>
#define INT8U unsigned char
#define INT16U unsigned int
//-------------------------------------按键定义
#define k1() ((PINA & (1<<PA0))==0x00)
#define k2() ((PINA & (1<<PA1))==0x00)
#define k3() ((PINA & (1<<PA2))==0x00)
#define k4() ((PINA & (1<<PA3))==0x00)
#define k5() ((PINA & (1<<PA4))==0x00)
#define k6() ((PINA & (1<<PA5))==0x00)
#define k7() ((PINA & (1<<PA6))==0x00)
#define k8() ((PINA & (1<<PA7))==0x00)
//-----------------------------------------------------
#define RST PB2
#define SLCK PB1
#define IO PB0 #define DDR_ DDRB
#define PORT_ PORTB
#define io() (PINB & (1<<IO))
#define DDR_1() DDR_|=(1<<IO)
#define DDR_0() DDR_&= ~(1<<IO)
#define SLCK1() PORT_ |=(1<<SLCK)
#define SLCK0() PORT_ &=~(1<<SLCK)#define IO1() PORT_ |=(1<<IO)
#define IO0() PORT_ &=~(1<<IO)
#define RST1() PORT_ |=(1<<RST)
#define RST0() PORT_ &=~(1<<RST)
INT8U time[];//----------------------------------------------------------------------
//-------------------------------------------------------------------
//-------------------------------------------------------------------
const INT8U timeword[]=
};
const INT8U number[]={
/};
const INT8U number1[]={
//--------------------------写入dat
void write(char dat)
{ DDR_1();
char i;
for (i=0;i<8;i++)
{
if(dat & 0x01) IO1();else IO0() ;
dat=dat>>1;
SLCK0();
SLCK1();
}
}
//--------------------------------------------------------
//----------------------------输出dat
char read11()
{ char dat=0x00;
DDR_0();
char i;
for (i=0;i<8;i++)
{SLCK1(); <br> SLCK0();<br> if(io()) dat |=(1<<i);<br> }
return dat/16*10+dat%16;//--------转换为十进制
}//--------------------------------------------读取指定地址adress中的dat--------
char readadress(INT8U adress)
{ INT8U dat;
RST1();
write(adress);
dat=read11();
RST0();
return dat;
}
//-----------------------------------------------------------
//------------------------------------------在指定地址adress写入dat-----------
void wadress(char adress ,char dat)
{ RST1();
write(adress) ;
write(dat);
RST0();
}//------------------------------------------------读取时间
//-------------------------------------------------
void readtime()
{
INT8U i ,adress=0x81;
for (i=0;i<7;i++)
{
time[i]=readadress(adress);
adress += 2;}
}
//------------------------------------------------设置时间
//-----------------------------------------------------
void set()
{
INT8U i;
wadress(0x8E ,0x00);
for (i=0;i<3;i++)
{
wadress(0X80+2*i ,(time[i]/10<<4 )| (time[i]%10));
} wadress(0x8E ,0x80);
}
//--------------------------------------------四相步进八拍方式
//-------------正转励磁序列为A->AB->B->BC->C->CD->D->DA
const INT8U zheng[]={0x01,0x03,0x02,0x06,0x04,0x0C,0x08,0x09};//------------------反转励磁序列为A->AD->D->CD->C->BC->B->AB
//--------------------------------------
const INT8U fan[]={0x01,0x09,0x08,0x0C,0x04,0x06,0x02,0x03};
//-----------------------------------------------------------------
// 步进电机正转或反转n圈
//-----------------------------------------------------------------
void STEP_MOTOR_RUN(INT8U Direction,INT8U n)
{
INT8U i,j;
for (i=0;i<n;i++)
{
for(j=0;j<8;j++)
{
if(k3()) return;
if(Direction==0) PORTB=zheng[j];
if(Direction==1) PORTB= fan[j];
_delay_ms(200);
}
PORTB=0x01;
}
}
//--------------------------------------------
extern void lcd_chushihua(); //定义在别的文件中
extern void qingping();
extern void display16161(INT8U y,INT8U l,INT8U C,INT8U *m);
extern void display8161(INT8U y,INT8U l,INT8U D,INT8U *r);
extern void display1616(INT8U y,INT8U l,INT8U *r);
extern void display816(INT8U y,INT8U l,INT8U *r);
//-----------------------------------------------------------------------void reshtime()
{ INT8U s,h,m,d,m1,w,y;
s=time[0]; m=time[1]; h=time[2];d=time[3];m1=time[4];w=time[5];y=time[6];
display816(1,1,(INT8U*) (number+h/10*16)); //------时
display816(1,9,(INT8U*) (number+h%10*16)); //
display816(1,17,(INT8U*) (number+m/10*16)); //------分
display816(1,25,(INT8U*) (number+m%10*16)); //
display816(1,33,(INT8U*) (number+s/10*16)); //------秒
display816(1,41,(INT8U*) (number+s%10*16)); //
display816(3,17,(INT8U*) (number+d/10*16)); //------日
display816(3,25,(INT8U*) (number+d%10*16)); //
display816(3,1,(INT8U*) (number+m1/10*16)); //------月
display816(3,9,(INT8U*) (number+m1%10*16)); //
display816(3,33,(INT8U*) (number+y/10*16)); //------年
display816(3,41,(INT8U*) (number+y%10*16)); //
display816(3,49,(INT8U*) (number+2*16)); //------20
display816(3,57,(INT8U*) (number)); //------20
display816(5,1,(INT8U*) (number+w*16)); //------周
}int main(){INT8U r=5,i=0,j=0,k=0;</p><p><br> DDRA=0X00;PORTA=0XFF;<br> DDRB=0XFF;<br> DDRD=0XFF;PORTD=0XFF; <br> TCCR0=0x05;<br> TCNT0=256-1000000/1024*0.05;<br> <br> TIMSK =0x01;<br> sei();<br> //--------------------------------------------------------------<br> //----------------------------------固定年、月、日、时、分、秒、<br> //-----------------------------------------------------------------------<br> display1616(1,26,(INT8U*) timeword); //几页 y 、几列 l、几个字符 j、<br> display1616(1,58,(INT8U*) (timeword+32));<br> display1616(1,90,(INT8U*) (timeword+64));<br> display1616(4,26,(INT8U*) (timeword+96)); //几页 y 、几列 l、几个字符 j、<br> display1616(4,58,(INT8U*) (timeword+128));<br> display1616(4,90,(INT8U*) (timeword+160));<br> while(1)<br> { readtime();<br> <br> if(k1())<br> { <br> <br> while(k1());<br> j=(j+1)%7;<br> display816(1+i*3,42+j*8,(INT8U*) number1);<br> // while(k1());STEP_MOTOR_RUN(0,r);//STEP_MOTOR_RUN(INT8U Direction,INT8U n)<br> <br> }
if(k2())
{while(k2());<br> display816((1+i*3),(26+j*32),(INT8U*) (number+k*16));<br> k=(k+1)%10;<br> //;STEP_MOTOR_RUN(1,r);<br> } if (k3())
{
display816((1+i*3),(26+j*32),(INT8U*) (number+k*16));
j=(j+1)%10;
}
}} ISR(TIMER0_OVF_vect) {
TCNT0=256-1000000/1024*0.05;
reshtime();
}
#include<avr/delay.h>
#include<string.h>
#define INT8U unsigned char#define lcd_starthang 0b11000000 // 起始行
#define lcd_ye 0XB8 // 页指令
#define lcd_lie 0X40 // 列指令
#define RW PD0
#define DI PD1
#define E PD2
#define CS1 PD3
#define CS2 PD4
#define RST PD5#define lcd_port PORTC //液晶排线
#define lcd_ctrl PORTD //控制端口
#define lcd_ddr DDRC //输入输出
#define lcd_pin PINC //控制状态的数据
#define RW1() lcd_ctrl |=_BV(RW);
#define RW0() lcd_ctrl &=~_BV(RW);//数据总线#define DI1() lcd_ctrl |=_BV(DI);
#define DI0() lcd_ctrl &=~_BV(DI);#define CS11() lcd_ctrl |=_BV(CS1);
#define CS10() lcd_ctrl &=~_BV(CS1);//片选信号
#define CS21() lcd_ctrl |=_BV(CS2);
#define CS20() lcd_ctrl &=~_BV(CS2);#define E1() lcd_ctrl |=_BV(E);
#define E0() lcd_ctrl &=~_BV(E);//使能#define RST1() lcd_ctrl |=_BV(RST);
#define RST0() lcd_ctrl &=~_BV(RST);/复位char fxshow=0;
void wait_lcd_ready()
{ check_busy:
lcd_ddr =0X00;
lcd_port=0Xff;
RW1();asm ("nop"); DI0();
E1(); asm ("nop"); E0();
if (lcd_pin & 0x80) goto check_busy;
}
//lcd控制指令函数
void write_lcd_command(INT8U ABC)
{wait_lcd_ready();<br> lcd_ddr =0XFF; <br> lcd_port=0XFF;<br> RW0();asm ("nop");DI0();<br> lcd_port=ABC;<br> E1();asm ("nop");E0();<br>} void writedata(INT8U dat)
{ wait_lcd_ready();
lcd_ddr =0XFF;
lcd_port=0XFF;
RW0();asm ("nop");DI1();
if(!fxshow) lcd_port=dat;else lcd_port=~dat;
E1();asm ("nop");E0();
}
//-----------------------------------------------------初始化
void lcd_chushihua(){ write_lcd_command(0x3f);_delay_ms(15);}//void lcd_show(INT8U y,INT8U l,INT8U j,INT8U *r)//几页 y 、几列 l、几个字符 j、
{INT8U i;<br> if(l<64)<br> {CS11();CS20(); <br> write_lcd_command(lcd_ye+y);<br> write_lcd_command(lcd_lie+l);</p><p> if(l+j<64)<br> {<br> for (i=0;i<j;i++) writedata(r[i]);<br> }
else
{for (i=0;i<64-l;i++)writedata(r[i]);//zuo半瓶显示<br> CS10();CS21();<br> write_lcd_command(lcd_ye+y);<br> write_lcd_command(lcd_lie);<br> for (i=64-l;i<j;i++) writedata(r[i]);//有半瓶显示<br> <br> }
}
else{ CS10();CS21();
write_lcd_command(lcd_ye+y);
write_lcd_command(lcd_lie+l-64);
for (i=0;i<j;i++) writedata(r[i]);
}
}
void qingping()
{ INT8U i,j;
CS11();CS21();
write_lcd_command(lcd_ye);
write_lcd_command(lcd_lie);
for (i=0;i<8;i++)
{ write_lcd_command(lcd_ye+i);
for(j=0;j<128;j++)
{write_lcd_command(lcd_lie+j);<br> writedata(0x00);}
}
}
//-----------------------------8*16void display816(INT8U y,INT8U l,INT8U *r){ lcd_show(y, l,8, r );//几页 y 、几列 l、几个字符 j、
lcd_show(y+1,l,8,r+8 );} //--------------------------------一串8*16void display8161(INT8U y,INT8U l,INT8U D,INT8U *r){INT8U i;<br> for(i=0;i<D;i++)<br> { display816(y,l+i*8,r+16*i);}}//------------------------------8*16void display1616(INT8U y,INT8U l,INT8U *r){ lcd_show(y, l,16, r );//几页 y 、几列 l、几个字符 j、
lcd_show(y+1,l,16,r+16);
}void display16161(INT8U y,INT8U l,INT8U D,INT8U *r){INT8U i;<br> for(i=0;i<D;i++)<br> { display1616(y,l+i*16,r+32*i);}
}
//----------------------------------------------------
void guang(INT8U i,INT8U j)
{
CS11();CS21();
write_lcd_command(lcd_ye);
write_lcd_command(lcd_lie);
write_lcd_command(i);
write_lcd_command(j);
writedata(0x00);
}//------------------------------------------------
//---------------------------------------------------
//----------------------------------------------------
#include<avr/io.h>
#include<avr/delay.h>
#include<string.h>
#include<avr/interrupt.h>
#define INT8U unsigned char
#define INT16U unsigned int
//-------------------------------------按键定义
#define k1() ((PINA & (1<<PA0))==0x00)
#define k2() ((PINA & (1<<PA1))==0x00)
#define k3() ((PINA & (1<<PA2))==0x00)
#define k4() ((PINA & (1<<PA3))==0x00)
#define k5() ((PINA & (1<<PA4))==0x00)
#define k6() ((PINA & (1<<PA5))==0x00)
#define k7() ((PINA & (1<<PA6))==0x00)
#define k8() ((PINA & (1<<PA7))==0x00)
//-----------------------------------------------------
#define RST PB2
#define SLCK PB1
#define IO PB0 #define DDR_ DDRB
#define PORT_ PORTB
#define io() (PINB & (1<<IO))
#define DDR_1() DDR_|=(1<<IO)
#define DDR_0() DDR_&= ~(1<<IO)
#define SLCK1() PORT_ |=(1<<SLCK)
#define SLCK0() PORT_ &=~(1<<SLCK)#define IO1() PORT_ |=(1<<IO)
#define IO0() PORT_ &=~(1<<IO)
#define RST1() PORT_ |=(1<<RST)
#define RST0() PORT_ &=~(1<<RST)
INT8U time[];//----------------------------------------------------------------------
//-------------------------------------------------------------------
//-------------------------------------------------------------------
const INT8U timeword[]=
};
const INT8U number[]={
/};
const INT8U number1[]={
//--------------------------写入dat
void write(char dat)
{ DDR_1();
char i;
for (i=0;i<8;i++)
{
if(dat & 0x01) IO1();else IO0() ;
dat=dat>>1;
SLCK0();
SLCK1();
}
}
//--------------------------------------------------------
//----------------------------输出dat
char read11()
{ char dat=0x00;
DDR_0();
char i;
for (i=0;i<8;i++)
{SLCK1(); <br> SLCK0();<br> if(io()) dat |=(1<<i);<br> }
return dat/16*10+dat%16;//--------转换为十进制
}//--------------------------------------------读取指定地址adress中的dat--------
char readadress(INT8U adress)
{ INT8U dat;
RST1();
write(adress);
dat=read11();
RST0();
return dat;
}
//-----------------------------------------------------------
//------------------------------------------在指定地址adress写入dat-----------
void wadress(char adress ,char dat)
{ RST1();
write(adress) ;
write(dat);
RST0();
}//------------------------------------------------读取时间
//-------------------------------------------------
void readtime()
{
INT8U i ,adress=0x81;
for (i=0;i<7;i++)
{
time[i]=readadress(adress);
adress += 2;}
}
//------------------------------------------------设置时间
//-----------------------------------------------------
void set()
{
INT8U i;
wadress(0x8E ,0x00);
for (i=0;i<3;i++)
{
wadress(0X80+2*i ,(time[i]/10<<4 )| (time[i]%10));
} wadress(0x8E ,0x80);
}
//--------------------------------------------四相步进八拍方式
//-------------正转励磁序列为A->AB->B->BC->C->CD->D->DA
const INT8U zheng[]={0x01,0x03,0x02,0x06,0x04,0x0C,0x08,0x09};//------------------反转励磁序列为A->AD->D->CD->C->BC->B->AB
//--------------------------------------
const INT8U fan[]={0x01,0x09,0x08,0x0C,0x04,0x06,0x02,0x03};
//-----------------------------------------------------------------
// 步进电机正转或反转n圈
//-----------------------------------------------------------------
void STEP_MOTOR_RUN(INT8U Direction,INT8U n)
{
INT8U i,j;
for (i=0;i<n;i++)
{
for(j=0;j<8;j++)
{
if(k3()) return;
if(Direction==0) PORTB=zheng[j];
if(Direction==1) PORTB= fan[j];
_delay_ms(200);
}
PORTB=0x01;
}
}
//--------------------------------------------
extern void lcd_chushihua(); //定义在别的文件中
extern void qingping();
extern void display16161(INT8U y,INT8U l,INT8U C,INT8U *m);
extern void display8161(INT8U y,INT8U l,INT8U D,INT8U *r);
extern void display1616(INT8U y,INT8U l,INT8U *r);
extern void display816(INT8U y,INT8U l,INT8U *r);
//-----------------------------------------------------------------------void reshtime()
{ INT8U s,h,m,d,m1,w,y;
s=time[0]; m=time[1]; h=time[2];d=time[3];m1=time[4];w=time[5];y=time[6];
display816(1,1,(INT8U*) (number+h/10*16)); //------时
display816(1,9,(INT8U*) (number+h%10*16)); //
display816(1,17,(INT8U*) (number+m/10*16)); //------分
display816(1,25,(INT8U*) (number+m%10*16)); //
display816(1,33,(INT8U*) (number+s/10*16)); //------秒
display816(1,41,(INT8U*) (number+s%10*16)); //
display816(3,17,(INT8U*) (number+d/10*16)); //------日
display816(3,25,(INT8U*) (number+d%10*16)); //
display816(3,1,(INT8U*) (number+m1/10*16)); //------月
display816(3,9,(INT8U*) (number+m1%10*16)); //
display816(3,33,(INT8U*) (number+y/10*16)); //------年
display816(3,41,(INT8U*) (number+y%10*16)); //
display816(3,49,(INT8U*) (number+2*16)); //------20
display816(3,57,(INT8U*) (number)); //------20
display816(5,1,(INT8U*) (number+w*16)); //------周
}int main(){INT8U r=5,i=0,j=0,k=0;</p><p><br> DDRA=0X00;PORTA=0XFF;<br> DDRB=0XFF;<br> DDRD=0XFF;PORTD=0XFF; <br> TCCR0=0x05;<br> TCNT0=256-1000000/1024*0.05;<br> <br> TIMSK =0x01;<br> sei();<br> //--------------------------------------------------------------<br> //----------------------------------固定年、月、日、时、分、秒、<br> //-----------------------------------------------------------------------<br> display1616(1,26,(INT8U*) timeword); //几页 y 、几列 l、几个字符 j、<br> display1616(1,58,(INT8U*) (timeword+32));<br> display1616(1,90,(INT8U*) (timeword+64));<br> display1616(4,26,(INT8U*) (timeword+96)); //几页 y 、几列 l、几个字符 j、<br> display1616(4,58,(INT8U*) (timeword+128));<br> display1616(4,90,(INT8U*) (timeword+160));<br> while(1)<br> { readtime();<br> <br> if(k1())<br> { <br> <br> while(k1());<br> j=(j+1)%7;<br> display816(1+i*3,42+j*8,(INT8U*) number1);<br> // while(k1());STEP_MOTOR_RUN(0,r);//STEP_MOTOR_RUN(INT8U Direction,INT8U n)<br> <br> }
if(k2())
{while(k2());<br> display816((1+i*3),(26+j*32),(INT8U*) (number+k*16));<br> k=(k+1)%10;<br> //;STEP_MOTOR_RUN(1,r);<br> } if (k3())
{
display816((1+i*3),(26+j*32),(INT8U*) (number+k*16));
j=(j+1)%10;
}
}} ISR(TIMER0_OVF_vect) {
TCNT0=256-1000000/1024*0.05;
reshtime();
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询