51单片机程序计时器.docx
《51单片机程序计时器.docx》由会员分享,可在线阅读,更多相关《51单片机程序计时器.docx(11页珍藏版)》请在冰豆网上搜索。
![51单片机程序计时器.docx](https://file1.bdocx.com/fileroot1/2023-1/9/99121203-fa80-41be-95fa-644e753675b5/99121203-fa80-41be-95fa-644e753675b51.gif)
51单片机程序计时器
计时器
1、LCD.c
#include
#include
unsignedcharcodenumber_X[]=
{//宽x高=8x16,纵向字节倒序
0x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00,//0
0x00,0x0F,0x10,0x20,0x20,0x10,0x0F,0x00,
0x00,0x10,0x10,0xF8,0x00,0x00,0x00,0x00,//1
0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,
0x00,0x70,0x08,0x08,0x08,0x88,0x70,0x00,//2
0x00,0x30,0x28,0x24,0x22,0x21,0x30,0x00,
0x00,0x30,0x08,0x88,0x88,0x48,0x30,0x00,//3
0x00,0x18,0x20,0x20,0x20,0x11,0x0E,0x00,
0x00,0x00,0xC0,0x20,0x10,0xF8,0x00,0x00,//4
0x00,0x07,0x04,0x24,0x24,0x3F,0x24,0x00,
0x00,0xF8,0x08,0x88,0x88,0x08,0x08,0x00,//5
0x00,0x19,0x21,0x20,0x20,0x11,0x0E,0x00,
0x00,0xE0,0x10,0x88,0x88,0x18,0x00,0x00,//6
0x00,0x0F,0x11,0x20,0x20,0x11,0x0E,0x00,
0x00,0x38,0x08,0x08,0xC8,0x38,0x08,0x00,//7
0x00,0x00,0x00,0x3F,0x00,0x00,0x00,0x00,
0x00,0x70,0x88,0x08,0x08,0x88,0x70,0x00,//8
0x00,0x1C,0x22,0x21,0x21,0x22,0x1C,0x00,
0x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00,//9
0x00,0x00,0x31,0x22,0x22,0x11,0x0F,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//.
0x00,0x00,0x00,0x00,0x00,0x60,0x60,0x00,
0x00,0x80,0x80,0x80,0x80,0x80,0x80,0x00,//-
0x00,0x01,0x01,0x01,0x01,0x01,0x01,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//nop
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0xC0,0xC0,0x00,0x00,0x00,//:
0x00,0x00,0x00,0x30,0x30,0x00,0x00,0x00
};
voidLCD_WriteCommandE1(unsignedcharcom)
{
while(CRADD1&0x80);
CWADD1=com;
}
voidLCD_WriteDataE1(unsignedchardat)
{
while(CRADD1&0x80);
DWADD1=dat;
}
voidLCD_WriteCommandE2(unsignedcharcom)
{
while(CRADD2&0x80);
CWADD2=com;
}
voidLCD_WriteDataE2(unsignedchardat)
{
while(CRADD2&0x80);
DWADD2=dat;
}
voidLCD_Init()
{
LCD_WriteCommandE1(0xe2);
LCD_WriteCommandE2(0xe2);
LCD_WriteCommandE1(0xa4);
LCD_WriteCommandE2(0xa4);
LCD_WriteCommandE1(0xa9);
LCD_WriteCommandE2(0xa9);
LCD_WriteCommandE1(0xa0);
LCD_WriteCommandE2(0xa0);
LCD_WriteCommandE1(0xc0);
LCD_WriteCommandE2(0xc0);
LCD_WriteCommandE1(0xaf);
LCD_WriteCommandE2(0xaf);
}
voidLCD_Clear(void)
{
unsignedchari,j;
for(i=0;i<4;i++)
{
LCD_WriteCommandE1(i+0xb8);
LCD_WriteCommandE2(i+0xb8);
LCD_WriteCommandE1(0x00);
LCD_WriteCommandE2(0x00);
for(j=0;j<0x50;j++)
{
LCD_WriteDataE1(0x00);
LCD_WriteDataE2(0x00);
}
}
}
voiddisplay_cn(unsignedcharlin,unsignedintcol,unsignedintlen,unsignedchar*p)
{
unsignedintseg,i,j;
unsignedchara,L,n;
switch(lin)
{
case0:
n=0xba;break;
case1:
n=0xb8;break;
}
for(i=0;i{
for(j=0;j<2;j++)
{
L=col;
LCD_WriteCommandE1(n+j);
LCD_WriteCommandE2(n+j);
for(seg=0;seg<16;seg++)
{
if(L<61)
{
a=L;
LCD_WriteCommandE1(a);
LCD_WriteDataE1(*p++);
}
else
{
a=L-61;
LCD_WriteCommandE2(a);
LCD_WriteDataE2(*p++);
}
L++;
}
}
col=col+16;
}
}
voiddisplay_number(unsignedcharlin,unsignedintcol,unsignedcharnum)
{
unsignedintseg,i,j;
unsignedchara,L,n,k;
switch(lin)
{
case0:
n=0xba;break;
case1:
n=0xb8;break;
}
k=num*16;
for(j=0;j<2;j++)
{
L=col;
LCD_WriteCommandE1(n+j);
LCD_WriteCommandE2(n+j);
for(seg=0;seg<8;seg++)
{
if(L<61)
{
a=L;
LCD_WriteCommandE1(a);
LCD_WriteDataE1(number_X[k++]);
}
else
{
a=L-61;
LCD_WriteCommandE2(a);
LCD_WriteDataE2(number_X[k++]);
}
L++;
}
}
}
voiddisplay_unsigned_int(unsignedcharlin,unsignedintcol,unsignedintdat)
{
unsignedintseg;
unsignedchark[4];
k[3]=dat%10;
k[2]=((dat/10)%10);
k[1]=((dat/100)%10);
k[0]=((dat/1000)%10);
if(k[0]==0){k[0]=12;}
if((k[0]==12)&&(k[1]==0)){k[0]=12;k[1]=12;}
if((k[0]==12)&&(k[1]==12)&&(k[2]==0)){k[0]=12;k[1]=12;k[2]=12;}
for(seg=0;seg<4;seg++)
{
display_number(lin,col,k[seg]);
col=col+10;
}
}
voiddisplay_signed_int(unsignedcharlin,unsignedintcol,signedintdat)
{
unsignedintseg;
unsignedchark[5],a;
k[0]=12;
if(dat<0)
{
dat=(~dat)+1;
k[0]=11;
}
k[4]=dat%10;
k[3]=((dat/10)%10);
k[2]=((dat/100)%10);
k[1]=((dat/1000)%10);
a=k[0];
if(k[1]==0){k[0]=12;k[1]=a;}
if((k[1]==a)&&(k[2]==0)){k[0]=12;k[1]=12;k[2]=a;}
if((k[1]==12)&&(k[2]==a)&&(k[3]==0)){k[0]=12;k[1]=12;k[2]=12;k[3]=a;}
for(seg=0;seg<5;seg++)
{
display_number(lin,col,k[seg]);
col=col+10;
}
}
voiddisplay_unsigned_char(unsignedcharlin,unsignedintcol,unsignedchardat)
{
unsignedintseg;
unsignedchark[3];
k[1]=dat%10;
k[0]=((dat/10)%10);
for(seg=0;seg<2;seg++)
{
display_number(lin,col,k[seg]);
col=col+10;
}
}
1、LCD.h
#include
#include
#ifndef__LCD__
#define__LCD__
#defineCWADD1XBYTE[0x8000]
#defineDWADD1XBYTE[0x8001]
#defineCRADD1XBYTE[0x8002]
#defineDRADD1XBYTE[0x8003]
#defineCWADD2XBYTE[0x8004]
#defineDWADD2XBYTE[0x8005]
#defineCRADD2XBYTE[0x8006]
#defineDRADD2XBYTE[0x8007]
externvoidLCD_Init();
externvoiddisplay_cn(unsignedcharlin,unsignedintcol,unsignedintlen,unsignedchar*p);
externvoiddisplay_signed_int(unsignedcharlin,unsignedintcol,signedintdat);
externvoiddisplay_unsigned_int(unsignedcharlin,unsignedintcol,unsignedintdat);
externvoiddisplay_unsigned_char(unsignedcharlin,unsignedintcol,unsignedchardat);
externvoidLCD_Clear(void);
#endif
1、MCU.c
#include
#include"MCU.h"
voidtimer0_init(void)
{//ok
TMOD|=0x01;//Timer0is16bitstimer,timer1isBaudrategenerator
TH0=0x97;
TL0=0xd5;
TR0=1;
ET0=1;
}
2、MCU.h
#ifndef__MCU__
#define__MCU__
externvoidtimer0_init(void);
#endif
3、MAIN.c
#include
#include
#include
#include"MCU.h"
voidwait(unsignedintx)
{
unsignedinti;
i=0;
for(i=0;i}
voidtimer0(void)interrupt1
{
TH0=0x3c;
TL0=0xb0;
ms_count++;
if(ms_count>=20)
{
ms_count=0;
second_count++;
if(second_count>=60)
{
second_count=0;
minute_count++;
if(minute_count>=60)
{
minute_count=0;
hous_count++;
if(hous_count>=24)
{
hous_count=0;
}
}
}
}
}
voidmain(void)
{
floatF;
signedinta;
LCD_Init();
LCD_Clear();
timer0_init();
display_cn(0,20,3,dzb);
display_cn(1,18,1,hous);
display_cn(1,50,1,minute);
display_cn(1,82,1,second);
EA=1;
while
(1)
{
display_unsigned_char(1,0,hous_count);
display_unsigned_char(1,34,minute_count);
display_unsigned_char(1,66,second_count);
wait(5000);
}
}
4、MAIN.h(字模通过软件自建)
#ifndefMAIN_H__
#defineMAIN_H__
//中文字模库16x16点阵
codeunsignedchardzb[]={//纵向字节倒序
.....................................//计
.....................................//时
.....................................//器
};
codeunsignedcharsecond[]={
......................................//秒
};
codeunsignedcharminute[]={
.....................................//分
};
codeunsignedcharhous[]={
......................................//时
};
unsignedcharms_count,second_count,minute_count,hous_count;
#endif