利用AT89C51单片机结合字符型LCD显示器设计一个简易的倒数计数器

如题所述

参考: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();

}
温馨提示:内容为网友见解,仅供参考
无其他回答

采用AT89C51单片机设计一只电子秒表,倒计时(60~1每间隔1秒倒数)?
硬件电路 有2位数码管可以显示秒表的时间 0~10秒 有3个按键 K1为开始计时 K2为停止计时 K3为清零 软件设计:K1按下则启动定时器定时 每到1秒 显示器缓冲单元内容加1 K2按下则停止定时器定时 K3按下则将显示器缓冲单元内容清零。

用C语言编写AT89C51单片机程序,设计一个智能数字钟。
num[1]=0;num[0]++;} if(num[0]>1) \/\/6

以AT89C51单片机为核心,制作一个LCD显示的智能电子钟
bit)(io|=(1<<bit))\/\/1#define cbi(io,bit)(io&=~(1<<bit))\/\/0#define gbi(pin,bit)(pin&(1<<bit))\/\/读#define enableInterrupts() {_asm("rim\\n");

以AT89C51单片机为核心,设计一个二进制计数器
比如:uchar dat = 3;P0 = dat; \/\/LED灯二进制方式显示数据dat.

利用AT89C51单片机、8位共阳极蓝色数码管和计算器键盘构成一个计算器...
主要注意乘除法,单片机没有乘除法指令,而且直接用乘除会占用大量的存储空间,4K的ROM估计不能完成你的任务,建议使用移位来代替乘除。

用AT89C51单片机编写一个C语言程序实现2个数码管秒计数器,用4个按键...
void t0isr() interrupt 1 { TH0=(65536-50000)\/256;TL0=(65536-50000)%256;num++;if(num>=20){ num=0;sec++;} } main(){ TMOD=0x01;TH0=(65536-50000)\/256;TL0=(65536-50000)%256;TR0=1;ET0=1;EA=1;LED1=0;LED2=0;while(1){ key=P1;if((key&0x0f)!=0x0f){ delay...

求用单片机设计一个秒表AT89C51
SETB EA ;定时器初始化结束,下面循环显示即可 MOV R1, #99H ;0~99计数.MOV R7, #1 ;50ms计数.MOV P0, #0C0H MOV P2, #0C0H LOOP:SJMP LOOP ;--- DELAY: ;延时子程序.AA4: MOV R4, #0 DJNZ R4,DJNZ R4,RET ;---...

1、 基于AT89C51单片机,利用定时器设计一个两位数码管显示电路。 设计...
void time1(void) interrupt 3 { TH1=(65536-5000)\/256; TL1=(65536-5000)%256; display(); } void init() { TMOD=0x11; EA=1; ET0=1; ET1=1; TR1=1; TR0=1; } main() { init(); while(1) { conv(shu); } } 本回答由提问者推荐 举报| 答案纠错 | 评论(4) 1 0 yying0907 ...

利用AT89C51单片机,LM016L液晶显示、时钟芯片DS1302芯片制作电子钟,需 ...
Delay5Ms(); WriteCommandLCM(0x38,1); \/\/显示模式设置,开始要求每次检测忙信号WriteCommandLCM(0x08,1); \/\/关闭显示WriteCommandLCM(0x01,1); \/\/显示清屏WriteCommandLCM(0x06,1); \/\/ 显示光标移动设置WriteCommandLCM(0x0C,1); \/\/ 显示开及光标设置}\/\/按指定位置显示一个字符void Display...

用C语言编写AT89C51单片机程序,设计一个智能数字钟。
void key_change(); \/\/切换显示按键函数 void key_set(); \/\/设置时间按键函数 void disp(uchar,uchar,uchar,uchar,uchar,uchar); \/\/显示函数 void zd_clock(); \/\/整点报时函数 void nz_clock(); \/\/闹钟函数 uchar incone(uchar); \/\/加1函数 uchar decone(uchar); \/\/减1函数 void set_...

相似回答