单片机数字钟

来源:互联网 发布:tscttp244pro标签软件 编辑:程序博客网 时间:2024/05/16 02:00

原理图:

程序代码:

#include "reg51.h"#include "LCD1602.H"#define uchar unsigned char#define uint  unsigned int sbit k0=P1^0;sbit k1=P1^1;sbit k2=P1^2;uint second=59,minute=59,hour=23;uchar counter;void delay(uint ms){uint i,j;for(i=0;i<ms;i++)for(j=0;j<1141;j++);}void initTimer(void){ TMOD=0x1; TH0=0x3c; TL0=0xb0;}void timer0(void) interrupt 1{ TH0=0x3c; TL0=0xb0; counter++; if(counter>=20) { second++;counter=0; } if(second>59) { minute++;second=0; } if(minute>59) { hour++;minute=0; } if(hour>23) { hour=0; } //add your code here.}void main(void){initTimer();TR0=1;ET0=1;EA=1;lcd_init();write_lcd_string(0,0,"my clock!");write_lcd_string(0,1,"  :  :  ");while(1){write_lcd_char(7,1,coder[second%10]);write_lcd_char(6,1,coder[second/10]);write_lcd_char(4,1,coder[minute%10]);write_lcd_char(3,1,coder[minute/10]);write_lcd_char(1,1,coder[hour%10]);write_lcd_char(0,1,coder[hour/10]);if(k0==0){delay(15);if(k0==0){second+=1;if(second>59)second=0;}}while(k0==0);if(k1==0){delay(15);if(k1==0){minute+=1;if(minute>59)minute=0;}}while(k1==0);if(k2==0){delay(15);if(k2==0){    hour+=1;if(hour>23)hour=0;}}while(k2==0);}}
1602LCD头文件:
/***************************//lcd1602.h//data 2009***************************/unsigned char code coder[]="0123456789abcdef";#define LCD_PORT P0#define DISPLAYON 0x0c#define DISPLAYOFF 0x08#define INPUTMODE 0x06#define DISPLAYCLR 0x01#define RETURNHOME 0x02sbit LCD_RS=P2^5;sbit LCD_RW=P2^6;sbit LCD_EN=P2^7;void write_lcd_command(unsigned char);void write_lcd_data(unsigned char);void write_lcd_char(unsigned char x,bit y,unsigned char DATA);//void write_lcd_string(unsigned char x,unsigned char y,unsigned char *p);void lcd_init();bit read_busy();void gotoxy(unsigned char x,bit y);/***************************************************************/void lcd_init(){write_lcd_command (DISPLAYCLR);//清屏while(read_busy());//读忙标志write_lcd_command (0x38);//8位数据发送模式while(read_busy());//读忙标志write_lcd_command (DISPLAYON);//0x0Cwhile(read_busy());//读忙标志write_lcd_command (INPUTMODE);//input modewhile(read_busy());//读忙标志write_lcd_command (0x80);//设置LCD开始显示地址while(read_busy());//读忙标志write_lcd_command (DISPLAYCLR);//清屏}void write_lcd_command(unsigned char command){LCD_PORT = command;LCD_RS = 0;LCD_RW = 0;LCD_EN = 1;LCD_EN = 0;}void write_lcd_data(unsigned char dat){LCD_PORT = dat;LCD_RS = 1;LCD_RW = 0;LCD_EN = 1;LCD_EN = 0;}void write_lcd_char(unsigned char x,bit y,unsigned char DAT){gotoxy( x, y);while(read_busy());//读忙标志write_lcd_data(DAT);}void write_lcd_string(unsigned char x,unsigned char y,unsigned char *p){gotoxy( x, y);while (*p){while(read_busy());//读忙标志write_lcd_data(*p);p++;}}   bit read_busy(){unsigned char readdata;LCD_PORT = 0xff;LCD_RS = 0;LCD_RW = 1;LCD_EN = 1;readdata = LCD_PORT;LCD_EN = 0;if (readdata >> 7)return (1);elsereturn (0);}void gotoxy(unsigned char x,bit y){while(read_busy());//读忙标志if(y)write_lcd_command(0xc0 + x);//2 lineelsewrite_lcd_command(0x80 + x);//1 line}

                                             
0 0