单片机万年历C语言程序完整

合集下载

单片机制作的12864液晶万年历_C语言程序

单片机制作的12864液晶万年历_C语言程序

#include<reg52.H>#include<intrins.h>#include<math.h>////////////////////////////////////////DS18B20#define uchar unsigned char#define uint unsigned intsbit DQ = P1 ^ 0; // 定义DS18B20的单总线#define Lcd_Bus P0sbit LCD_RS=P2^7; //数据/命令选择sbit LCD_RW=P2^6; //读写控制sbit LCD_EN=P2^5; //片选信号#define LCD_DA TA P0 //8 bit数据线#define LOW 0#define HIGH 1//初始化指令#define CLEAR_SCREEN 0x01 //清屏指令:清屏且AC值为00H#define AC_INIT 0x02 //将AC设置为00H。

且游标移到原点位置#define CURSE_ADD 0x06 //设定游标移到方向及图像整体移动方向(默认游标右移,图像整体不动)#define FUN_MODE 0x30 //工作模式:8位基本指令集#define DISPLAY_ON 0x0c //显示开,显示游标,且游标位置反白#define DISPLAY_OFF 0x08 //显示关#define CURSE_DIR 0x14 //游标向右移动:AC=AC+1#define SET_CG_AC 0x40 //设置AC,范围为:00H~3FH#define SET_DD_AC 0x80#define FUN_MODEK 0x36 //扩展指令集,8位指令#define X1address 0x80 //上半屏X轴起始地址#define X2address 0x88 //下半屏X轴起始地址#define Y address 0x80 //Y轴起始地址#define Set_Column 0x40#define Set_Page 0xB8uchar code shu[]={0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39};uchar code xingqi[]="星期";uchar code hanzishu[]="一二三四五六日";unsigned char TempData = 0;double DS18B20Data;uint jiemian=0;//////////////////////////////////////////DS1302sbit DS1302_RST = P1^4;sbit DS1302_IO = P1^3;sbit DS1302_SCK = P1^2;unsigned char table[]={0X00,0X18,0X0c,0X15,0X06,0X01,0X0a};//0s 24min 12hour 14ri 5yue 5zhou 2010nianunsigned char data table1[7];//uchar code table2[]={0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39};uchar code shuzi[]={0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,0x1a,0x1b,0x1c,0x1d,0x1e,0x1f,0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29,0x2a,0x2b,0x2c,0x2d,0x2e,0x2f,0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x3a,0x3b,0x3c,};unsigned char aa,bb;uchar miaoge,miaoshi,fenge,fenshi,shige,shishi,rige,rishi,yuege,yueshi,zhou,niange,nianshi;uchar shezhinum=0;//unsigned char shi,ge;////////////////////////////////////////////////键盘扫描sbit key_mnue = P3^7;sbit key_jia = P2^0;sbit key_jian = P2^1;sbit key_enter= P2^2;uchar key_number=0;//int nian,yue,ri,shi,fen;/////////////////////////////////////////////////红外sbit HW_IRQ = P3 ^ 2; // 定义38KHz一体化红外接收头与CPU的连接unsigned char DT;static unsigned char data IRCode[3],IRCON,IRCON2;//////////////////////////////////////////////////蜂鸣器sbit BEEP =P1^1;/****************************************************************************** *************** 函数名称:DS18B20_Delay()* 功能:DS18B20软件延时专用* 入口参数:count 延时参数,值越大,延时越长* 出口参数:无******************************************************************************* **************/void DS18B20_Delay(uint count) //延时函数{while(count--);}/****************************************************************************** *************** 函数名称:DS18B20_Init()* 功能:DS18B20初始化* 入口参数:无* 出口参数:返回值为1则初始化成功* 返回值为0则初始化失败******************************************************************************* **************/uchar DS18B20_Init(void){uchar x = 0;DQ = 1; // DQ复位DS18B20_Delay(8); // 稍做延时DQ = 0; // 单片机将DQ拉低DS18B20_Delay(80); // 精确延时,大于480usDQ = 1; // 拉高总线DS18B20_Delay(14); // 稍做延时后x = DQ; // 若x=0则初始化成功,若x=1则初始化失败DS18B20_Delay(20);return (~x);}/****************************************************************************** *************** 函数名称:DS18B20_Read1Byte()* 功能:从DS18B20读一个字节* 入口参数:无* 出口参数:从DS18B20读出的一个字节******************************************************************************* **************/uchar DS18B20_Read1Byte(void){uchar i = 0;uchar dat = 0;for(i=8;i>0;i--){DQ = 0; // 给脉冲信号dat >>= 1;DQ = 1; // 给脉冲信号if(DQ) dat |= 0x80;DS18B20_Delay(4);}return (dat);}/****************************************************************************** *************** 函数名称:DS18B20_Write1Byte()* 功能:向DS18B20写一个字节* 入口参数:dat 要写入DS18B20的一字节数据* 出口参数:无******************************************************************************* **************/void DS18B20_Write1Byte(uchar dat){uchar i = 0;for(i=8;i>0;i--){DQ = 0;DQ = dat&0x01;DS18B20_Delay(5);DQ = 1;dat>>=1;}}/****************************************************************************** *************** 函数名称:Read_Disp_Temperature()* 功能:读取并显示温度* 入口参数:无* 出口参数:无******************************************************************************* **************/double Read_Disp_Temperature(void){uint a = 0, b = 0, c = 0,d = 0,e = 0, t = 0;double tt = 0;DS18B20_Init();DS18B20_Write1Byte(0xCC); // 跳过读序号列号的操作DS18B20_Write1Byte(0x44); // 启动温度转换DS18B20_Init();DS18B20_Write1Byte(0xCC); // 跳过读序号列号的操作DS18B20_Write1Byte(0xBE); // 读取温度寄存器a = DS18B20_Read1Byte();b = DS18B20_Read1Byte();t = b;t <<= 8;t = t | a;//tt = t * 0.0625;return t;}//***************************************************************** //函数名称:delay(uint t)延时函数。

51单片机12864万年历(图片+主要程序C语言)

51单片机12864万年历(图片+主要程序C语言)

51单片机做的12864万年历这是我花了一个星期时间做的万年历;一开始是一些图片,后面是这个制作的C语言源程序;希望大家稀饭:Main.c#include "reg52.h"#include "macroconst.h"#include "keyscan.h"#include "screen.h"#include "18b20.h"#include "ds1302.h"#include "atao_12864.h"#include "24c02.h"idata unsigned char state1[1];unsigned char screennum=1;//屏幕号#define time_task_sec 200 //节拍200Hz#define clock 24000000 //晶振#define max_task 4 //最大任务数idata unsigned char task_delay[4];#define task_delay0 time_task_sec/1 //2赫兹18b20 #define task_delay1 time_task_sec/5 //10赫兹屏幕#define task_delay2 time_task_sec/3 //4赫兹1302 #define task_delay3 time_task_sec/20 //20赫兹键盘void init(){unsigned char i;init18b20();ds1302_init();initina1(); //---------------------------LCD字库初始化程序RdFromROM(state1,8,1);state[0]=state1[0]; //-------界面RdFromROM(state1,15,1);state[1]=state1[0];//------- 闹钟RdFromROM(state1,11,1);key_sound=state1[0];//-------按键音// state[2]=state1[2];// state[3]=state1[3];screennum=1;//------默认一号屏for(i=0;i<max_task;i++)//清除任务延时{task_delay[i]=0;}TMOD=0x11;EA=1;TH0=255-clock/time_task_sec/12/526;TL0=255-clock/time_task_sec/12%526;ET0=1;TR0=1;}void main(){init();while(1){ if(task_delay[0]==0){readtemp(); task_delay[0]=task_delay0;} if(task_delay[3]==0){MenuCheckKey();task_delay[3]=task_delay1;}if(task_delay[1]==0){screen_select(screennum);task_delay[1]=task_delay1;} if(task_delay[2]==0){ds1302_read_time(); task_delay[2]=task_delay0;} }}void timer0() interrupt 1{uchar i=0;TH0=255-clock/time_task_sec/12/526;TH1=255-clock/time_task_sec/12%526;for(i=0;i<max_task;i++)if(task_delay[i]) task_delay[i]--; // miao=21,fen=33,xiaomiao=94;if(ma_start==1){tt++;if(tt==8){tt=0;xiaomiao++;if(xiaomiao==99){xiaomiao=0;miao++;if(miao==60){miao=0;fen++;if(fen==99){fen=0;}}}}}}Screen.c:#include "reg52.h"#include "macroconst.h"#include "delay.h"#include "18b20.h"#include "atao_12864.h"#include "ds1302.h"#include "24c02.h"#include "festival.h"#include "keyscan.h"idata unsigned char miao=0,fen=78,xiaomiao=0,tt=0;idata unsigned char bai,shi,ge,shinian,genian,shiyue,geyue,shiri,geri,shishi,geshi,shifen,gefen,shimi ao,gemiao;idata unsigned char Number[4];unsigned char code mainmenu[]={" 主菜单""3.查询 4.温度""1.调时 2.秒表""5.设置 6.返回"};unsigned char code setmenu[]={" 设置""3.查温 4.按键""1.界面 2.闹铃""5.报时 6.返回"};void gettime(){shinian=(time_buf1[1]%100)/10;genian=(time_buf1[1]%100)%10;shiyue=(time_buf1[2])/10;geyue=(time_buf1[2])%10;shiri=(time_buf1[3])/10;geri=(time_buf1[3])%10;shishi=(time_buf1[4])/10;geshi=(time_buf1[4])%10;shifen=(time_buf1[5])/10;gefen=(time_buf1[5])%10;shimiao=(time_buf1[6])/10;gemiao=(time_buf1[6])%10;}void screen_select(uchar num){// initina1();gettime() ;if(num==1) //----------主界面{Lcd_Set_Post(1,0);TransferData(shinian+48,1);//----------------------年TransferData(genian+48,1);TransferData('/',1);TransferData(shiyue+48,1);//----------------------月TransferData(geyue+48,1);TransferData('/',1);TransferData(shiri+48,1);//----------------------日TransferData(geri+48,1);Lcd_Set_Post(1,4);if(state[1]==1)//----------------闹铃的状态图标{TransferData(' ',1);TransferData(14,1);}Lcd_Set_Post(1,5);if(key_sound==1){TransferData(13,1);}if(state[0]==0){Lcd_Set_Post(1,7); //----------------农历年nian(0);Lcd_Set_Post(2,0);//----------------------- 农历日月LCD_WRITE_CHAR("农历");Conversion(0,time_buf[1],time_buf[2],time_buf[3]);TransferData(month_moon/16+'0',1);TransferData(month_moon%16+'0',1);TransferData('/',1);TransferData(day_moon/16+'0',1);TransferData(day_moon%16+'0',1);}if(state[0]==1){Lcd_Set_Post(3,5);}else {Lcd_Set_Post(2,5);}LCD_WRITE_CHAR("星期");//----------------------星期if(time_buf1[7]==7) {LCD_WRITE_CHAR("日");}if(time_buf1[7]==6) {LCD_WRITE_CHAR("六");}if(time_buf1[7]==5) {LCD_WRITE_CHAR("五");}if(time_buf1[7]==4) {LCD_WRITE_CHAR("四");}if(time_buf1[7]==3) {LCD_WRITE_CHAR("三");}if(time_buf1[7]==2) {LCD_WRITE_CHAR("二");}if(time_buf1[7]==1) {LCD_WRITE_CHAR("一");}Lcd_Set_Post(3,0);TransferData(shishi+48,1);//----------------------时TransferData(geshi+48,1);TransferData(58,1);TransferData(shifen+48,1);//----------------------分TransferData(gefen+48,1);TransferData(58,1);TransferData(shimiao+48,1);//----------------------秒TransferData(gemiao+48,1);if(state[0]==0){bai=(temph/100); //---------------------------------温度shi=(temph%100)/10;ge=(temph%100)%10;Lcd_Set_Post(3,4);if(zerotemp==0){TransferData(' ',1);TransferData(' ',1);} Lcd_Set_Post(3,5);TransferData(shi+48,1);TransferData(ge+48,1);TransferData('.',1);TransferData(templ+48,1);TransferData(0xa1,1);TransferData(0xe6,1);Lcd_Set_Post(4,0);festival(time_buf[2],time_buf[3],time_buf[4]);//农历}}if(num==3)//----------主菜单{Lcd_Set_Post(1,0);//--------设置显示位置LCD_WRITE_CHAR(mainmenu);}if(num==4)//----------设置菜单{Lcd_Set_Post(1,0);//--------设置显示位置LCD_WRITE_CHAR(setmenu);}if(num==5)//----------时间设置{Lcd_Set_Post(1,2);//--------设置显示位置LCD_WRITE_CHAR("时间设置");Lcd_Set_Post(2,1);TransferData(shinian+48,1);//----------------------年TransferData(genian+48,1);LCD_WRITE_CHAR("年");TransferData(shiyue+48,1);//----------------------月TransferData(geyue+48,1);LCD_WRITE_CHAR("月");TransferData(shiri+48,1);//----------------------日TransferData(geri+48,1);LCD_WRITE_CHAR("日");TransferData(time_buf1[7]+48,1);//---------------星期Lcd_Set_Post(3,2);TransferData(shishi+48,1);//--------------------时TransferData(geshi+48,1);LCD_WRITE_CHAR("时");TransferData(shifen+48,1);//---------------------分TransferData(gefen+48,1);LCD_WRITE_CHAR("分");TransferData(shimiao+48,1);//--------------------秒TransferData(gemiao+48,1);}if(num==6)//----------秒表{uchar shi1,shi2,shi3,ge1,ge2,ge3;Lcd_Set_Post(1,3);//--------设置显示位置LCD_WRITE_CHAR("秒表");Lcd_Set_Post(3,2);shi1=fen/10;ge1=fen%10;TransferData(shi1+48,1);TransferData(ge1+48,1);TransferData(':',1);shi2=miao/10;ge2=miao%10;TransferData(shi2+48,1);TransferData(ge2+48,1);TransferData(':',1);shi3=xiaomiao/10;ge3=xiaomiao%10;TransferData(shi3+48,1);TransferData(ge3+48,1);}if(num==8){Lcd_Set_Post(1,2);//--------农历查询LCD_WRITE_CHAR("温度查询");Lcd_Set_Post(3,0);//--------农历查询LCD_WRITE_CHAR("最高温度"); TransferData(':',1);RdFromROM(Number,0,4);if(Number[3]==0){TransferData('-',1);}Lcd_Set_Post(3,5);TransferData(Number[0]+48,1); TransferData(Number[1]+48,1); TransferData('.',1);TransferData(Number[2]+48,1); TransferData(0xa1,1);TransferData(0xe6,1); Lcd_Set_Post(4,0);//--------农历查询LCD_WRITE_CHAR("最低温度"); TransferData(':',1);RdFromROM(Number,4,4);if(Number[3]==0){TransferData('-',1);}Lcd_Set_Post(4,5);TransferData(Number[0]+48,1); TransferData(Number[1]+48,1); TransferData('.',1);TransferData(Number[2]+48,1); TransferData(0xa1,1);TransferData(0xe6,1); }if(num==9)//----------界面菜单{Lcd_Set_Post(1,2);//--------设置显示位置LCD_WRITE_CHAR("界面选择");Lcd_Set_Post(2,0);TransferData(1+48,1);TransferData('.',1);LCD_WRITE_CHAR("多功能界面");Lcd_Set_Post(3,0);TransferData(2+48,1);TransferData('.',1);LCD_WRITE_CHAR("简洁界面");}。

单片机万年历C语言程序完整

单片机万年历C语言程序完整
sbit out = P2^3; //立刻跳出调整模式按钮
sbit DQ = P3^0;
char done,count,temp,flag,up_flag,down_flag,t_value;
uchar TempBuffer[5],week_value[2];
void show_time(); //液晶显示程序
unsigned char Day;
unsigned char Month;
unsigned char Year;
unsigned char DateString[11];
unsigned char TimeString[9];
}SYSTEMTIME;//定义的时间类型
#define AM(X)X
#define PM(X)(X+12)//转成24小时制
{
unsigned char ucData;
DS1302_RST = 0;
DS1302_CLK = 0;
DS1302_RST = 1;
DS1302InputByte(ucAddr|0x01); //地址,命令
ucData = DS1302OutputByte(); //读1Byte数据
DS1302_CLK = 1;
#define DS1302_SECOND0x80 //时钟芯片的寄存器位置,存放时间
#define DS1302_MINUTE0x82
#define DS1302_HOUR0x84
#define DS1302_WEEK0x8A
#define DS1302_DAY0x86
#define DS1302_MONTH0x88
#define uchar unsigned char

c语言万年历程序

c语言万年历程序

#include <stdio.h>#include <stdlib.h>#include <ctype.h>int leap (int year) ;//判断是否为闰年int days_month (int month,int year) ;//判断这个月的天数int menu_select();/*void f(){int day,month,year,sum,leap,S;printf("\n请输入年月日\n");scanf("%d%d%d",&year,&month,&day);switch(month){case 1:sum=0;break;case 2:sum=31;break;case 3:sum=59;break;case 4:sum=90;break;case 5:sum=120;break;case 6:sum=151;break;case 7:sum=181;break;case 8:sum=212;break;case 9:sum=243;break;case 10:sum=273;break;case 11:sum=304;break;case 12:sum=334;break;default:printf("data error");break;}sum=sum+day;if(year%400==0||(year%4==0&&year%100!=0))leap=1;else leap=0;if(leap==1&&month>2)sum++;S=(year-1+(year-1)/4-(year-1)/100+(year-1)/400+sum)%7; //X表示年份,C是该年份元旦开始到这一日的天数//S/7的系数就是星期数//01printf("%d",S);switch(S){case 1:printf("星期一\n");break;case 2:printf("星期二\n");break;case 3:printf("星期三\n");break;case 4:printf("星期四\n");break;case 5:printf("星期五\n");break;case 6:printf("星期六\n");break;case 0:printf("星期日\n");break;}}*/int Leap(int year){ int leap;if(year%400==0||(year%4==0&&year%100!=0))leap=1;else leap=0;return leap;}int SU(int sum,int year){int S;S=(year-1+(year-1)/4-(year-1)/100+(year-1)/400+sum)%7;return S;}int Sum(int year,int month,int day=1){int sum,leap;switch(month){case 1:sum=0;break;case 2:sum=31;break;case 3:sum=59;break;case 4:sum=90;break;case 5:sum=120;break;case 6:sum=151;break;case 7:sum=181;break;case 8:sum=212;break;case 9:sum=243;break;case 10:sum=273;break;case 11:sum=304;break;case 12:sum=334;break;default:printf("data error");break;}sum=sum+day;leap=Leap(year);if(leap==1&&month>2)sum++;return sum;}void Print(int S){switch(S){case 1:printf("星期一\n");break;case 2:printf("星期二\n");break;case 3:printf("星期三\n");break;case 4:printf("星期四\n");break;case 5:printf("星期五\n");break;case 6:printf("星期六\n");break;case 0:printf("星期日\n");break;}}void Fun1(){int day,month,year,sum,S;printf("\n请输入年月日\n");scanf("%d%d%d",&year,&month,&day);sum=Sum(year,month,day);S=SU(sum,year);Print(S);}void Fun2(){int i,j=1,k=1,a,b,month,year,sum;printf("\n 输入年月:\n");scanf("%d%d",&year,&month);b=days_month(month,year);sum=Sum(year,month);a=SU(sum,year);printf("*************************************\n");printf(" Sun Mon Tue Wed Thu Fri Sat \n");if(a==7){for(i=1;i<=b;i++){printf("%4d",i);if(i%7==0){printf("\n");}}}if(a!=7){while (j<=4*a){printf(" ");j++;}for(i=1;i<=b;i++){printf("%4d",i);if(i==7*k-a){printf("\n");k++;}}}printf("\n*************************************\n");printf("\n");}int days_month (int month,int year){if(month==1||month==3||month==5||month==7||month==8||month==10||month==12) return 31;if(month==4||month==6||month==9||month==11)return 30;if(month==2&&Leap(year)==1) return 29;else return 28;}void main(){for(;;){switch(menu_select()){case 1:printf("输入年月日计算该月第一天是星期几\n");Fun1();break;case 2:printf("打印任意日历\n");Fun2();break;case 3:printf("谢谢使用!\n");exit(0);}}}int menu_select(){ system("cls");char s;int cn;printf("1. 输入年月日计算该月第一天是星期几\n");printf("2. 打印任意日历\n");printf("3.谢谢使用! \n");printf("input 1-3:");do {s=getchar();cn=(int)s-48;}while(cn<0||cn>6);return cn;}。

单片机实训--万年历程序

单片机实训--万年历程序

#include<reg52.h>typedef unsigned char uint8; #define clrdp 0x01#define enmode 0x06#define curback 0x02#define ondp 0x0f#define offdp 0x08#define movd 0x14#define funset 0x38#define bus P1#define contw 0x8e#define secw 0x80#define secr 0x81#define minw 0x82#define minr 0x83#define hourw 0x84#define hourr 0x85#define datw 0x86#define datr 0x87#define monthw 0x88#define monthr 0x89#define dayw 0x8a#define dayr 0x8b#define yearw 0x8c#define yearr 0x8dsbit K1=P2^7;sbit K2=P2^5;sbit K3=P2^4;sbit RS=P3^7;sbit RW=P3^6;sbit EN=P3^5;sbit DIO=P2^1;sbit RST=P2^2;sbit SCK=P2^0;bit flag=0;uint8 sel;uint8 time[7]={0};uint8 time_disp[32]={0}; void init_1602(void);void write_com(uint8 comd); void write_dat(uint8 wdata); void delay(void);void long_dleay(void);void init_1302(void);uint8 read_1302(uint8 addr);void write_1302(uint8 addr,uint8 wdata); void disp(void);void read_time(void);void sepr_time(void);void chk_key(void);uint8 bcd_dec(uint8 wdata);uint8 dec_bcd(uint8 wdata);void main(void){init_1302();init_1602();while(1){chk_key();read_time();sepr_time();disp();}}//1302初始化void init_1302(void){write_1302(contw,0);write_1302(secw,0);}//写1302寄存器void write_1302(uint8 addr,uint8 wdata) {uint8 i;SCK=0;RST=1;for(i=0;i<8;i++){DIO=addr&0x01;SCK=1;SCK=0;addr>>=1;}for(i=0;i<8;i++){DIO=wdata&0x01;SCK=1;SCK=0;wdata>>=1;}RST=0;}//读1302寄存器uint8 read_1302(uint8 addr) {uint8 i;uint8 temp;SCK=0;RST=1;for(i=0;i<8;i++){DIO=addr&0x01;SCK=1;SCK=0;addr>>=1;}for(i=0;i<8;i++){temp>>=1;if(! DIO){temp&=0x7f;}else{temp|=0x80;}SCK=1;SCK=0;}RST=0;return temp;}//读1302时间寄存器void read_time(void){uint8 i;uint8 temp;for(i=0;i<7;i++){temp=read_1302(0x81+i*2);time[i]=temp;}time[0]&=0x7f;}//显示时间void disp(void){uint8 i;write_com(0x80);for(i=0;i<16;i++){write_dat(time_disp[i]);}write_com(0xc0);for(i=16;i<32;i++){write_dat(time_disp[i]);}}//分离时间void sepr_time(void){time_disp[0]='-';time_disp[1]='-';time_disp[2]='2';time_disp[3]='0';time_disp[4]=(time[6]>>4)+0x30;time_disp[5]=(time[6]&0x0f)+0x30;time_disp[6]='-';time_disp[7]=(time[4]>>4)+0x30;time_disp[8]=(time[4]&0x0f)+0x30;time_disp[9]='-';time_disp[10]=(time[3]>>4)+0x30;time_disp[11]=(time[3]&0x0f)+0x30;time_disp[12]='-';time_disp[13]='-';time_disp[14]=(time[5]>>4)+0x30;time_disp[15]=(time[5]&0x0f)+0x30;time_disp[16]='-';time_disp[17]='-';time_disp[18]='-';time_disp[19]='-';time_disp[20]=(time[2]>>4)+0x30;time_disp[21]=(time[2]&0x0f)+0x30;time_disp[22]='-';time_disp[23]=(time[1]>>4)+0x30;time_disp[24]=(time[1]&0x0f)+0x30;time_disp[25]='-';time_disp[26]=(time[0]>>4)+0x30;time_disp[27]=(time[0]&0x0f)+0x30;time_disp[28]='-';time_disp[29]='-';time_disp[30]='-';time_disp[31]='-';}void init_1602(void){write_com(offdp);write_com(funset);write_com(enmode);write_com(movd);write_com(ondp);write_com(clrdp);}void write_com(uint8 comd){RS=0;RW=0;bus=comd;EN=1;EN=0;delay();}void write_dat(uint8 wdata){RS=1;RW=0;bus=wdata;EN=1;EN=0;delay();}void delay(void){uint8 t;for(t=0;t<250;t++);}void long_delay(void){uint8 t;for(t=0;t<200;t++){delay();}}void chk_key(void){uint8 buf;uint8 temp;if(!K1){long_delay();if(!K1){if(flag){if((sel>=0)&&(sel<8)){sel++;}else if(sel==8){temp=read_1302(secr);temp&=0x7f;write_1302(secw,temp);sel=0;flag=0;}else{sel=8;}}else{temp=read_1302(secr);temp|=0x80;write_1302(secw,temp);flag=1;}}}else if(!K2){long_delay();if(!K2){switch(sel){case 1:temp=read_1302(yearr);buf=bcd_dec(temp);if(buf<99){buf++;temp=dec_bcd(buf);write_1302(yearw,temp);}break;case 2:temp=read_1302(dayr);buf=bcd_dec(temp);if(buf<7){buf++;temp=dec_bcd(buf);write_1302(dayw,temp);}break;case 3:temp=read_1302(monthr);buf=bcd_dec(temp);if(buf<12){buf++;temp=dec_bcd(buf);write_1302(monthw,temp);}break;case 4:temp=read_1302(datr);buf=bcd_dec(temp);if(buf<31){buf++;temp=dec_bcd(buf);write_1302(datw,temp);}break;case 5:temp=read_1302(hourr);buf=bcd_dec(temp);if(buf<23){buf++;temp=dec_bcd(buf);write_1302(hourw,temp);}break;case 6:temp=read_1302(minr);buf=bcd_dec(temp);if(buf<59){buf++;temp=dec_bcd(buf);write_1302(minw,temp);}break;case 7:temp=read_1302(secr);temp&=0x7f;buf=bcd_dec(temp);if(buf<59){buf++;temp=dec_bcd(buf);temp|=0x80;write_1302(secw,temp);}break;default:break;}}}else if(!K3){long_delay();if(!K3){switch(sel){case 1:temp=read_1302(yearr);buf=bcd_dec(temp);if(buf>0){buf--;temp=dec_bcd(buf);write_1302(yearw,temp);}break;case 2:temp=read_1302(dayr);buf=bcd_dec(temp);if(buf>1){buf--;temp=dec_bcd(buf);write_1302(dayw,temp);}break;case 3:temp=read_1302(monthr);buf=bcd_dec(temp);if(buf>1){buf--;temp=dec_bcd(buf);write_1302(monthw,temp);}break;case 4:temp=read_1302(datr);buf=bcd_dec(temp);if(buf>1){buf--;temp=dec_bcd(buf);write_1302(datw,temp);}break;case 5:temp=read_1302(hourr);buf=bcd_dec(temp);if(buf>0){buf--;temp=dec_bcd(buf);write_1302(hourw,temp);}break;case 6:temp=read_1302(minr);buf=bcd_dec(temp);if(buf>0){buf--;temp=dec_bcd(buf);write_1302(minw,temp);}break;case 7:temp=read_1302(secr);temp&=0x7f;buf=bcd_dec(temp);if(buf>0){buf--;temp=dec_bcd(buf);temp|=0x80;write_1302(secw,temp);}break;default:break;}}}}uint8 dec_bcd(uint8 wdata){uint8 temp;uint8 i;uint8 j;i=wdata/10;j=wdata%10;temp=i;temp=temp<<4;temp=temp|j;return temp;}uint8 bcd_dec(uint8 wdata){uint8 temp;uint8 i;uint8 j;i=wdata>>4;j=wdata&0x0f;temp=i*10+j;return temp;。

#51单片机12864万年历C语言

#51单片机12864万年历C语言

51单片机做的12864万年历Main.c#include "reg52.h"#include "macroconst.h"#include "keyscan.h"#include "screen.h"#include "18b20.h"#include "ds1302.h"#include "atao_12864.h"#include "24c02.h"idata unsigned char state1[1]。

unsigned char screennum=1。

//屏幕号#define time_task_sec 200 //节拍200Hz#define clock 24000000 //晶振#define max_task 4 //最大任务数idata unsigned char task_delay[4]。

#define task_delay0 time_task_sec/1 //2赫兹18b20 #define task_delay1 time_task_sec/5 //10赫兹屏幕#define task_delay2 time_task_sec/3 //4赫兹1302 #define task_delay3 time_task_sec/20 //20赫兹键盘void init(>{unsigned char i。

init18b20(>。

ds1302_init(>。

initina1(>。

//---------------------------LCD字库初始化程序 RdFromROM(state1,8,1>。

state[0]=state1[0]。

//-------界面RdFromROM(state1,15,1>。

c语言——万年历程序

c语言——万年历程序

#include<stdio.h>#include<stdlib.h>char*month_str[]={"January","February","March","April","May","June","July","August ","September","October","November","December"};char*week[]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Sat urday"};int IsLeapYear(int year) /*find out the year is leap year or not*/{if((year%4==0&&year%100!=0)||(year%400==0)) //这里是判断是否是闰年的return 1; //如果是闰年就返回值1elsereturn 0;//不是的话返回0}int month_day(int year,int month) //这个函数用来判断这年的月分有多少天的{int mon_day[]={31,28,31,30,31,30,31,31,30,31,30,31};if(IsLeapYear(year)&&month==2) /*判断是判断是否是闰年,如果是闰年而且这个月是2月那这个月有29天*/return 29;elsereturn(mon_day[month-1]);}int DaySearch(int year,int month,int day) /*这个函数是计算输入的日期对应的星期*/{int c=0;float s;int m;for(m=1;m<month;m++)c=c+month_day(year,m); //这是计算输入的月分的累计天数c=c+day; //计算日期在这一年中是第几天s=year-1+(int)(year-1)/4+(int )(year-1)/100+(int)(year-1)/400-40+c; /*这是计算日期对应的星期公式,这个公式可在网上查到*/return ((int)s%7); //与上语句同属计算日期对应的星期}int PrintAllYear(int year)/*这个函数是用来输出全年的日历*/{int temp;int i,j;printf("\n\n%d Calander\n",year);for(i=1;i<=12;i++){printf("\n\n%s(%d)\n",month_str[i-1],i); //输出月分名称printf("0 1 2 3 4 5 6 \n");printf("S M T W T F S \n\n");temp=DaySearch(year,i,1);for(j=1;j<=month_day(year,i)+temp;j++){if(j-temp<=0)printf(" ");else if(j-temp<10)printf("%d ",j-temp);elseprintf("%d ",j-temp);if(j%7==0)printf("\n");}}return 0;}int main(){int option,da;char ch;int year,month,day;printf("Copyright @ 2005 TianQian All rights reserved!:):):)");printf("\n\nWelcome to use the WanNianLi system!\n");while(1){printf("\nPlease select the service you need:\n"); //用来提示选择执行功能printf("\n1 Search what day the day is"); //选择1时,用来计算这一天是星期几printf("\n2 Search whether the year is leap year or not"); //计算是否这年是闰年printf("\n3 Print the calander of the whole year"); //输入全年的日历printf("\n4 Exit\n"); //选择退出程序scanf("%d",&option);switch(option) //用来选择执行{case 1:while(1){printf("\nPlease input the year,month and day(XXXX,XX,XX):"); //提示输入scanf("%d,%d,%d,%c",&year,&month,&day); //读入数据da=DaySearch(year,month,day); //调用DaySearch()函数来计算是星期几printf("\n%d-%d-%d is %s,do you want tocontinue?(Y/N)",year,month,day,week[da]);fflush(stdin); //刷新输入缓冲区scanf("%c",&ch);if(ch=='N'||ch=='n')break;}break;case 2: /*当为2时,进行相应运算*/while(1){printf("\nPlease input the year which needs searched?(XXXX)");scanf("%d",&year);if(IsLeapYear(year))printf("\n%d is Leap year,do you want to continue?(Y/N)",year);elseprintf("\n%d is not Leap year,do you want to continue(Y/N)?",year); fflush(stdin);scanf("%c",&ch);if(ch=='N'||ch=='n')break;}break;case 3: /*当为3时运行相应的运算*/while(1){printf("\nPlease input the year which needs printed(XXXX)");scanf("%d",&year);PrintAllYear(year);printf("\nDo you want to continue to print(Y/N)?");fflush(stdin);scanf("%c",&ch);if(ch=='N'||ch=='n')break;}break;case 4:fflush(stdin);printf("Are you sure?(Y/N)");scanf("%c",&ch);if(ch=='Y'||ch=='y')exit(1);break;default:printf("\nError:Sorry,there is no this service now!\n"); break;}}return 0;}。

自己制作的单片机万年历程序+原理图

自己制作的单片机万年历程序+原理图

自己制作的单片机万年历程序+原理图单片机万年历仿真原理图如下仿真Altium Designer画的万年历原理图和PCB图如下:PCB原理图基于51单片机,可以完成时钟显示、公历显示、农历显示、温度显示、闹钟报警定时的LCD时钟PPT内容预览:本设计使用AT89C51来做主控芯片,其强大的功能足够实现我们设计的所有功能。

使用LCD1602的液晶显示器来进行显示。

使用Keil uVision5进行编程。

通过Proteus8.6来进行仿真。

点击一次K1进入时钟设置页面,通过点击K2切换时、秒、分、星期、年、月、日,通过K3与K4实现加减来完成时钟的设置点击两次K1进入闹钟设置页面,通过点击K2切换开关、时、秒、分,通过K3与K4实现加减完成闹钟的设置。

单片机源程序如下注释是很全的#include //调用单片机头文件#define uchar unsigned char //无符号字符型宏定义变量范围0~255#define uint unsigned int //无符号整型宏定义变量范围0~65535#include "eeprom52.h"#include "nongli.h"#include "intrins.h"bit flag_200ms ;bit flag_100ms ;sbit beep = P3^7; //蜂鸣器定义bit flag_beep_en;uint clock_value; //用作闹钟用的sbit dq = P3^1; //18b20 IO口的定义uint temperature ; //温度变量uchar flag_nl; //农历阳历显示标志位uchar menu_1,menu_2;uchar key_time,flag_value; //用做连加的中间变量bit key_500ms ;uchar n_nian,n_yue,n_ri; //农历显示的函数#include "ds1302.h"#include "lcd1602.h"/******************把数据保存到单片机内部eeprom中******************/void write_eeprom(){SectorErase(0x2000);byte_write(0x2000, fen1);byte_write(0x2001, shi1);byte_write(0x2002, open1);byte_write(0x2058, a_a);}/******************把数据从单片机内部eeprom中读出来*****************/void read_eeprom(){fen1 = byte_read(0x2000);shi1 = byte_read(0x2001);open1 = byte_read(0x2002);a_a = byte_read(0x2058);}/**************开机自检eeprom初始化*****************/void init_eeprom(){read_eeprom(); //先读if(a_a != 1) //新的单片机初始单片机内问eeprom{fen1 = 3;shi1 = 8;a_a = 1;write_eeprom(); //保存数据}}/***********************18b20初始化函数*****************************/void init_18b20(){bit q;dq = 1; //把总线拿高delay_uint(1); //15usdq = 0; //给复位脉冲delay_uint(80); //750usdq = 1; //把总线拿高等待delay_uint(10); //110usq = dq; //读取18b20初始化信号delay_uint(20); //200usdq = 1; //把总线拿高释放总线}/*************写18b20内的数据***************/void write_18b20(uchar dat){uchar i;for(i=0;i<8;i++){ //写数据是低位开始dq = 0; //把总线拿低写时间隙开始dq = dat & 0x01; //向18b20总线写数据了delay_uint(5); // 60usdq = 1; //释放总线}}/*************读取18b20内的数据***************/uchar read_18b20(){uchar i,value;for(i=0;i<8;i++){dq = 0; //把总线拿低读时间隙开始value >>= 1; //读数据是低位开始dq = 1; //释放总线if(dq == 1) //开始读写数据value |= 0x80;delay_uint(5); //60us 读一个时间隙最少要保持60us 的时间}return value; //返回数据}/*************读取温度的值读出来的是小数***************/uint read_temp(){uint value;uchar low; //在读取温度的时候如果中断的太频繁了,就应该把中断给关了,否则会影响到18b20的时序init_18b20(); //初始化18b20write_18b20(0xcc); //跳过64位ROMwrite_18b20(0x44); //启动一次温度转换命令delay_uint(50); //500usinit_18b20(); //初始化18b20write_18b20(0xcc); //跳过64位ROMwrite_18b20(0xbe); //发出读取暂存器命令EA = 0;low = read_18b20(); //读温度低字节value = read_18b20(); //读温度高字节EA = 1;value <<= 8; //把温度的高位左移8位value |= low; //把读出的温度低位放到value的低八位中value *= 0.625; //转换到温度值小数return value; //返回读出的温度带小数}/******************1ms 延时函数*******************/void delay_1ms(uint q){uint i,j;for(i=0;i<>< p=""><>for(j=0;j<120;j++);}/******************写星期函数*******************/void write_week(uchar hang,uchar add,uchar week)//写星期函数{if(hang==1)write_com(0x80+add);elsewrite_com(0x80+0x40+add);。

万年历系统——C语言完整版

万年历系统——C语言完整版

实验十九:编写一个万年历系统1.设计目的:从实验的角度来看,这次课程设计一方面可以让自己巩固并加深对C语言程序设计知识的理解,掌握和提高C语言编程和程序的基本调试的基本技能,进一步理解和运用结构化程序的思想和方法;另一方面,可以让自己在面对一个全新的问题时,学会如何思考,如何寻找问题的关键,从而提升自己的能力。

2.总体设计:1.、当前页以系统当前日期的月份为准,显示当前月的每一天(显示出日及对应的星期几);2、当系统日期变到下一个月时,系统自动翻页到下一月。

3.调试与测试:4.源程序清单和执行结果:#include <stdio.h>#include <conio.h>#include <stdlib.h>#include <time.h>int leap(int year ){if ((year %4 == 0) && (year % 100 != 0)|| (year % 400 == 0)){return 1;}return 0;}void show(int year,int month,int date,int sec,int mini,int hour) {const char month_str[][4]={"","Jan","Feb","Mar","Apl", "May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};const int month_day[]={0,31,28,31,30,31,30,31,31,30,31,30,31}; int i,j,wdays,mdays,days;for(i=1,days=0;i<year;i++){if(leap(i)){days += 366;}else{days += 365;}}for(i=1;i<month;i++){if(i==2 && leap(year)){days+=29;}else{days+=month_day[i];}}printf(" %s.%d.%d %d:%d:%d \n",month_str[month],date,year,hour,mini,sec);printf(" 一二三四五六日\n");wdays = days % 7;for( j = 0; j < wdays; j++){printf(" ");}if(month == 2 && leap(year)){mdays=29;}else{mdays= month_day[month];}for(i=1;i<=mdays;i++){if( i > 1 && days % 7 == 0 ){Printf(“\n”);printf("\n");}printf("%4d",i);days=days+1;}printf("\n---------------------------\n\n\n");}void main(){time_t rawtime;struct tm *info;int year,month,date,sec,mini,hour; char ch;time ( &rawtime );info = localtime ( &rawtime );year =info->tm_year + 1900; month =info->tm_mon + 1;date =info->tm_mday;hour =info->tm_hour;mini =info->tm_min;sec =info->tm_sec;while(1){show(year,month,date,sec,mini,hour); printf("↑......上一年\n\n");printf("↓......下一年\n\n");printf("←....前一个月\n\n");printf("→....后一个月\n\n");printf("Esc.......退出\n\n");ch=getch();switch(ch){case 27://Ecsexit(0);case -32://Navigatorch=getch();if(ch==77){//Rightyear+=(month==12)?1:0;month=month%12+1;}else if(ch==75){//Leftyear-=(month==1)?1:0;month=(month-2+12)%12+1;}else if(ch==72) {//Upyear--;}else if(ch==80) {//Downyear++;}system("cls"); }}}。

单片机数字钟万年历(c语言)

单片机数字钟万年历(c语言)

#include <REGX51.H>#include <ABSACC.h>#define Font_code XBYTE[0xefff] //字符码写地址#define reg_code XBYTE[0xdfff] //字位码写地址#define s_1_con_key P3_2#define m_1_con_key P3_3#define h_1_con_key P3_4#define w_1_con_key P3_5#define p1 P1sbit BELL=P1^0;//sbit p10=P1^0;//sbit p20=P1^1;//sbit p30=P1^2;//sbit p40=P1^3;//sbit p50=P1^4;//sbit p60=P1^5;//sbit p70=P1^6;//sbit p80=P1^7;//字型码const unsigned char seg_design[11]={0x3F,/*0*/0x06,/*1*/0x5B,/*2*/0x4F,/*3*/0x66,/*4*/0x6D,/*5*/0x7D,/*6*/0x07,/*7*/0x7F,/*8*/0x6F,/*9*/0x40,/*-*/};//字位码const unsigned char bit_design[8]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};code unsigned char sszymmh[]={ 6,2,3, 5,2,1, 3,2,2, 5,2,2, 1,3,2, 6,2,1, 5,2,1,6,2,4, 3,2,2, 5,2,1, 6,2,1, 5,2,2, 3,2,2, 1,2,1,6,1,1, 5,2,1, 3,2,1, 2,2,4, 2,2,3, 3,2,1, 5,2,2,5,2,1, 6,2,1, 3,2,2, 2,2,2, 1,2,4, 5,2,3,3,2,1,2,2,1, 1,2,1, 6,1,1, 1,2,1, 5,1,6, 0,0,0} ;// 音阶频率表高八位code unsigned char FREQH[]={0xF2,0xF3,0xF5,0xF5,0xF6,0xF7,0xF8,0xF9,0xF9,0xFA,0xFA,0xFB,0xFB,0xFC,0xFC,//1,2,3,4,5,6,7,8,i0xFC,0xFD,0xFD,0xFD,0xFD,0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFF,} ;// 音阶频率表低八位code unsigned char FREQL[]={0x42,0xC1,0x17,0xB6,0xD0,0xD1,0xB6,0x21,0xE1,0x8C,0xD8,0x68,0xE9,0x5B,0x8F,//1,2,3,4,5,6,7,8,i0xEE,0x44, 0x6B,0xB4,0xF4,0x2D,0x47,0x77,0xA2,0xB6,0xDA,0xFA,0x16,};unsigned char bit_con;unsigned int ms_1_con; //毫秒计数器unsigned char s_1_con; //秒计数器unsigned char m_1_con; //分计数器unsigned char h_1_con=12;//时计数器unsigned char w_1_con=6; //周计时器unsigned char d_1_con=1;unsigned char mon_1_con=1;unsigned int y_1_con=2011;unsigned char s1; //秒计数器unsigned char m1; //分计数器unsigned char h1; //时计数器unsigned char Display_Buffer[8]; //显示缓冲区unsigned int k1=0;unsigned int k2=0;unsigned char timer0h,timer0l,time,led=1,j=0;unsigned char flagd=0;void delay_nms(unsigned int n);void Calculation_display(void);void time_set(void);void time_set1(void);void time_set2(void);void InitTimer0(void){TMOD = 0x01; // 方式1,16位计数器方式TH0 = 0xFC; //定时初值=65536-n n=1000,定时周期是1ms TL0 = 0x18;EA = 1; //开总中断使能ET0 = 1; //允许定时器T0溢出中断TR0 = 1; //T0 运行}void delay(unsigned char t){unsigned char t1;unsigned long t2;for(t1=0;t1<t;t1++){for(t2=0;t2<4000;t2++){;}}TR0=0;}void song(){TH0=timer0h;TL0=timer0l;TR0=1;delay(time);}void sing(){unsigned char k,i=0;time=1;while(time){if(k1==0) break;InitTimer0();Calculation_display();if(k2==0)time_set();else if(k2==1)time_set1();elsetime_set2();p1=~seg_design[8];k=sszymmh[i]+7*sszymmh[i+1]-1;timer0h=FREQH[k];timer0l=FREQL[k];time=sszymmh[i+2];i=i+3;song();}}void main(void){InitTimer0();while(1){Calculation_display();delay_nms(100);if(k2==0)time_set();else if(k2==1)time_set1();elsetime_set2();sing();k1=0;InitTimer0();}}void Timer0Interrupt(void) interrupt 1 using 1 {TH0 = 0xFC;TL0 = 0x18;ms_1_con++;//采用定时扫描方式bit_con++;if(bit_con > 7) bit_con = 0;reg_code = 0x00; //先消隐显示Font_code = Display_Buffer[bit_con];reg_code = bit_design[bit_con];if(k1==1){TR0=0;BELL=!BELL;TH0=timer0h;TL0=timer0l;TR0=1;}}void delay_nms(unsigned int n) //延时N ms{unsigned char a,b;unsigned int c;for(c=n;c>0;c--)for(b=142;b>0;b--)for(a=2;a>0;a--);}void Calculation_display(void){unsigned char day;if(ms_1_con > 999) //1000ms定时到{ms_1_con = 0;s_1_con++;if(s_1_con > 59){s_1_con = 0;m_1_con++;if(m_1_con > 59){m_1_con = 0;h_1_con++;k1=1;if(h_1_con > 23){h_1_con = 0;w_1_con++;if(w_1_con>7)w_1_con=1;switch(mon_1_con){case 1:case 3:case 5:case 7:case 8:case 10:case 12:day=31;break;case 2:day=28;break;case 4:case 6:case 9:case 11:day=30;break;}d_1_con++;if(d_1_con>day){mon_1_con++;d_1_con=1;if(mon_1_con>12){y_1_con++;mon_1_con=1;}}}}}}p1=~seg_design[w_1_con];if(k2==0){if(ms_1_con<500){Display_Buffer[2] = seg_design[10];Display_Buffer[5] = seg_design[10];}else{Display_Buffer[2] = !seg_design[10];Display_Buffer[5] = !seg_design[10];}Display_Buffer[6] = seg_design[s_1_con/10];//秒十位Display_Buffer[7] = seg_design[s_1_con%10]; //秒个位Display_Buffer[3] = seg_design[m_1_con/10]; //分十位Display_Buffer[4] = seg_design[m_1_con%10]; //分个位Display_Buffer[0] = seg_design[h_1_con/10]; //时十位Display_Buffer[1] = seg_design[h_1_con%10]; //时个位}if(k2==1){Display_Buffer[3] = seg_design[y_1_con%10]; //年个位Display_Buffer[2] = seg_design[(y_1_con/10)%10]; //年十位Display_Buffer[1] = seg_design[((y_1_con/100)%10)]; //年百位Display_Buffer[0] = seg_design[y_1_con/1000]; //年千位Display_Buffer[6] = seg_design[d_1_con/10]; //日十位Display_Buffer[7] = seg_design[d_1_con%10]; //日个位Display_Buffer[4] = seg_design[mon_1_con/10]; //月十位Display_Buffer[5] = seg_design[mon_1_con%10]; //月个位}if(k2==2){Display_Buffer[2] = seg_design[10];Display_Buffer[5] = seg_design[10];Display_Buffer[6] = seg_design[s1/10]; //秒十位Display_Buffer[7] = seg_design[s1%10]; //秒个位Display_Buffer[3] = seg_design[m1/10]; //分十位Display_Buffer[4] = seg_design[m1%10]; //分个位Display_Buffer[0] = seg_design[h1/10]; //时十位Display_Buffer[1] = seg_design[h1%10]; //时个位}if(s1==s_1_con&&m1==m_1_con&&h1==h_1_con)k1=1;}void time_set(void){unsigned char day;if(!s_1_con_key){s_1_con++;if(s_1_con > 59){s_1_con = 0;m_1_con++;if(m_1_con > 59){m_1_con = 0;h_1_con++;if(h_1_con > 23){h_1_con = 0;w_1_con++;if(w_1_con>7)w_1_con=1;d_1_con++;switch(mon_1_con){case 1:case 3:case 5:case 7:case 8:case 10:case 12:day=31;break;case 2:day=28;break;case 4:case 6:case 9:case 11:day=30;break;}if(d_1_con>day){mon_1_con++;d_1_con=1;if(mon_1_con>12){y_1_con++;mon_1_con=1;}}}}}}if(!m_1_con_key){m_1_con++;if(m_1_con > 59){m_1_con = 0;h_1_con++;if(h_1_con > 23){h_1_con = 0;w_1_con++;if(w_1_con>7)w_1_con=1;d_1_con++;switch(mon_1_con){case 1:case 3:case 5:case 7:case 8:case 10:case 12:day=31;break;case 2:day=28;break;case 4:case 6:case 9:case 11:day=30;break;}if(d_1_con>day){mon_1_con++;d_1_con=1;if(mon_1_con>12){y_1_con++;mon_1_con=1;}}}}}if(!h_1_con_key){h_1_con++;if(h_1_con > 23){h_1_con = 0;w_1_con++;if(w_1_con>7)w_1_con=1;d_1_con++;switch(mon_1_con){case 1:case 3:case 5:case 7:case 8:case 10:case 12:day=31;break;case 2:day=28;break;case 4:case 6:case 9:case 11:day=30;break;}if(d_1_con>day){mon_1_con++;d_1_con=1;if(mon_1_con>12){y_1_con++;mon_1_con=1;}}}}if(!w_1_con_key){k2=1;}}void time_set1(void){unsigned char day;switch(mon_1_con){case 1:case 3:case 5:case 7:case 8:case 10:case 12:day=31;break;case 2:day=28;break;case 4:case 6:case 9:case 11:day=30;break;}if(!s_1_con_key){d_1_con++;w_1_con++;if(w_1_con>7)w_1_con=1;if(d_1_con>day){mon_1_con++;d_1_con=1;if(mon_1_con>12){y_1_con++;mon_1_con=1;}}}if(!m_1_con_key){mon_1_con++;w_1_con=(w_1_con+day%7)%7;if(mon_1_con>12){y_1_con++;mon_1_con=1;}}if(!h_1_con_key){y_1_con++;if(y_1_con>9999){w_1_con=(w_1_con+365%7)%7;y_1_con=0;}}if(!w_1_con_key){k2=2;}}void time_set2(void){if(!s_1_con_key){s1++;if(s1>59)s1=0;}if(!m_1_con_key){m1++;if(m1>59)m1=0;}if(!h_1_con_key){h1++;if(h1>23)h1=0;}if(!w_1_con_key){k2=0;}}。

万年历c语言编程代码

万年历c语言编程代码

#include <stdio.h>// 函数声明int is_leap_year(int year);int get_first_day_of_year(int year);// 主函数int main() {int year, month, first_day_of_year, first_day_of_month, days_in_month;int i, j, k;printf("请输入年份(例如:2022):");scanf("%d", &year);first_day_of_year = get_first_day_of_year(year);for (month = 1; month <= 12; month++) {printf("%d 年%d 月\n", year, month);printf("日一二三四五六\n");if (month == 2) {if (is_leap_year(year)) {days_in_month = 29;} else {days_in_month = 28;}} else if (month == 4 || month == 6 || month == 9 || month == 11) { days_in_month = 30;} else {days_in_month = 31;}first_day_of_month = (first_day_of_year + (month-1)*28)%7;for (i = 1; i <= first_day_of_month; i++) {printf(" ");}for (j = 1; j <= days_in_month; j++) {printf("%2d ", j);k = (i + j - 1) % 7;if (k == 0) {printf("\n");}}printf("\n");}}// 判断是否闰年int is_leap_year(int year) {return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);}// 计算年份第一天是星期几int get_first_day_of_year(int year) {int first_day = (year - 1) * 365 + (year - 1) / 4 - (year - 1) / 100 + (year - 1) / 400;first_day = (first_day + 1) % 7;return first_day;}这是一个简单的万年历示例代码,但它不是完整的代码。

c语言四位数码管万年历程序

c语言四位数码管万年历程序

&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& 万年历&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& #include<reg51.h>#define uchar unsigned char#define uint unsigned intuchar code taba[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};uchar code tabb[]={0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10};uint a,b,c,i,j,n=0,nian=2010,yue=1,ri=1,shi=0,fen=0,miao=0;sbit K1=P3^2;sbit K2=P3^3;sbit K3=P3^4;sbit K4=P3^5;sbit F=P3^0;sbit D=P0^7;void int0()interrupt 1 //T0定时器中断{TH0=(65536-50000)/256;TL0=(65536-50000)%256;i++;if(i==20) i=0,miao++,P1=~P1; //定时满1秒,秒进1,流水灯明灭if(miao==60) miao=0,fen++; //秒钟数满59,秒归0,分进1if(fen==60) fen=0,shi++; //分钟数满59,分归0}void delay(uint j) //延时函数j*1ms{for(a=1;a<=j;a++) //延时j*1msfor(b=1;b<=121;b++);}void del_nian() //实现数码管动态显示年{while(miao<=5&&K1) //显示5秒,--年{P0=taba[nian/1000],P2=0xf7,delay(3),P0=0xff,P2=0xff; //数码管第1位显示P0=taba[nian/100%10],P2=0xfb,delay(3),P0=0xff,P2=0xff; //数码管第2位显示P0=taba[nian%100/10],P2=0xfd,delay(3),P0=0xff,P2=0xff; //数码管第3位显示P0=taba[nian%10],P2=0Xfe,delay(3),P0=0xff,P2=0xff;} //数码管第4位显示}void del_yue_ri() //实现数码管动态显示月,日{while(miao<=10&&K1) //显示5秒,--月--日{P0=taba[yue/10],P2=0xf7,delay(3),P0=0xff,P2=0xff; //数码管第1位显示P0=tabb[yue%10],P2=0xfb,delay(3),P0=0xff,P2=0xff; //数码管第2位显示P0=taba[ri/10],P2=0xfd,delay(3),P0=0xff,P2=0xff; //数码管第3位显示P0=taba[ri%10],P2=0Xfe,delay(3),P0=0xff,P2=0xff;} //数码管第4位显示}void del_shi_fen() //实现数码管动态显示时,分{while(miao>10&&miao<=60&&K1) //显示50秒,--分--钟{P0=taba[shi/10],P2=0xf7,delay(3),P0=0xff,P2=0xff; //数码管第1位显示P0=tabb[shi%10],P2=0xfb,delay(3),P0=0xff,P2=0xff; //数码管第2位显示P0=taba[fen/10],P2=0xfd,delay(3),P0=0xff,P2=0xff; //数码管第3位显示P0=taba[fen%10],P2=0Xfe,delay(3),P0=0xff,P2=0xff;} //数码管第4位显示}void del_tiao_nian(){while(K1) //按下K1返回{switch(n){case 2:{if(K3==0) delay(200),K3=1,nian++; //第2次按K1,按K3加,来调W2 if(K4==0) delay(200),K4=1,nian--;};break;}P0=taba[nian/1000],P2=0xf7,delay(3),P0=0xff,P2=0xff;P0=taba[nian/100%10],P2=0xfb,delay(3),P0=0xff,P2=0xff;P0=taba[nian%100/10],P2=0xfd,delay(3),P0=0xff,P2=0xff;P0=tabb[nian%10],P2=0Xfe,delay(3),P0=0xff,P2=0xff;} //用带点数组,表示调整的是哪个值---第四个点---调-年}void del_tiao_yue_ri(){ while(K1) //按下K1返回{switch(n){case 3:{if(K3==0) delay(200),K3=1,yue++; //第3次按K1,按K3加,来调W4 if(K4==0) delay(200),K4=1,yue--;P0=tabb[yue%10],P2=0xfb,delay(3),P0=0xff,P2=0xff;};break; //用带点数组,表示调整的是哪个值---第二个点---调-月case 4:{if(K3==0) delay(200),K3=1,ri++; //第4次按K1,按K3加,来调W4 if(K4==0) delay(200),K4=1,ri--;P0=tabb[ri%10],P2=0Xfe,delay(3),P0=0xff,P2=0xff;};break;} //用带点数组,表示调整的是哪个值---第四个点---调-日P0=taba[yue/10],P2=0xf7,delay(3),P0=0xff,P2=0xff;P0=taba[yue%10],P2=0xfb,delay(3),P0=0xff,P2=0xff;P0=taba[ri/10],P2=0xfd,delay(3),P0=0xff,P2=0xff;P0=taba[ri%10],P2=0Xfe,delay(3),P0=0xff,P2=0xff;}}void del_tiao_shi_fen(){while(K1) //按下K1返回{switch(n){case 5:{if(K3==0) delay(200),K3=1,shi++; //第5次按K1,按K3加,来调W4if(K4==0) delay(200),K4=1,shi--;P0=tabb[shi/10],P2=0xf7,delay(3),P0=0xff,P2=0xff; //P0=tabb[shi%10],P2=0xfb,delay(3),P0=0xff,P2=0xff;};break; //用带点数组,表示调整的是哪个值---前两个点---调-年case 6:{if(K3==0) delay(200),K3=1,fen++; //第6次按K1,按K3加,来调W4if(K4==0) delay(200),K4=1,fen--;P0=tabb[fen/10],P2=0Xfd,delay(3),P0=0xff,P2=0xff; //P0=tabb[fen%10],P2=0Xfe,delay(3),P0=0xff,P2=0xff;};break;} //用带点数组,表示调整的是哪个值---后两个点---调-年P0=taba[shi/10],P2=0xf7,delay(3),P0=0xff,P2=0xff;P0=taba[shi%10],P2=0xfb,delay(3),P0=0xff,P2=0xff;P0=taba[fen/10],P2=0xfd,delay(3),P0=0xff,P2=0xff;P0=taba[fen%10],P2=0Xfe,delay(3),P0=0xff,P2=0xff;}}void del_tiao() //暂停调整程序{while(n!=8) //第8次按下,调整循环了一圈,跳回主程序{if(K1==0)delay(300),K1=1,n++; //通过K1改变n值while(K1&&n!=8) //按下K2返回主程序{switch(n){case 1:P0=0x00,P1=0xff,P2=0x00;break; //第1次按下,显示管全亮case 2:del_tiao_nian();break; //第2次按下,进到调--年--状态case 3:del_tiao_yue_ri();break; //第3次按下,进到调--月--状态case 4:del_tiao_yue_ri();break; //第4次按下,进到调--日--状态case 5:del_tiao_shi_fen();break; //第5次按下,进到调--时--状态case 6:del_tiao_shi_fen();break; //第6次按下,进到调--分--状态case 7:P0=0x00,P1=0xff,P2=0x00;break;} //第7次按下,显示管全亮}}}void main() //主函数{TMOD=0X01; //TH0=(65536-50000)/256; //TL0=(65536-50000)%256; ////////定时初始化EA=1; //ET0=1; //while(1){TR0=1; //开启定时器n=0;if(yue==1^yue==3^yue==5^yue==7^yue==8^yue==10) ////实现1,3,5,7,8,10月,有31天{if(ri==32) ri=0,yue++;};if(yue==4^yue==6^yue==9^yue==11) //实现4,6,9,11月,有30天{if(ri==31) ri=0,yue++;};if(!nian%4) //实现闰年有28天{if(yue==2){if(ri==29) ri=0,yue++;}};if(nian%4) //实现平年有29天{if(yue==2){if(ri==30) ri=0,yue++;}};if(yue==12) //在12月份,满31天,年加1,月-日归0 {if(ri==32) ri=0,nian++;};if(shi==24) shi=0,ri++; //实现满24小时,时归0,日加1del_nian(); //调用-年----显示if(!K1) del_tiao(); //有按键按下,进入调整程序del_yue_ri(); //调用-月-日-显示if(!K1) del_tiao(); //有按键按下,进入调整程序del_shi_fen(); //调用-时-分-显示if(!K1) del_tiao(); //有按键按下,进入调整程序}}&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& 抢答器&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& #include<reg52.h> //头文件#define uchar unsigned char //宏定义#define uint unsigned int //宏定义uchar code table[5]={0xC0,0xF9,0xA4,0xB0,0x99};// 0 , 1 , 2 , 3 , 4 ;uchar code tab[5]={0xF7, 0xFB, 0xFD, 0xFE, 0xFF};//数码管:一亮,二亮,三亮,四亮,全灭uint i,j=0,z=5,x=5,c=5,v=5,a,b;sbit K4=P3^5;sbit L4=P1^0;sbit K3=P3^4;sbit L3=P1^1;sbit K2=P3^3;sbit L2=P1^2;sbit K1=P3^2;sbit L1=P1^3;sbit M=P3^0;void delay(uint i) //延时子函数---延时:i*1ms{for(a=0;a<+i;a++)for(b=0;b<=121;b++);}void del(void) //子函数----倒计时{ for(i=3;i>0;i--) //倒计时3,2,1{P3=0xFF,P2=0xF0;for(j=0;j<=30000;j++) //循环扫描按键{P0=table[i];if(K1==0) { while(1) //数码管二,三闪烁锁定提前抢答者1,蜂鸣器报警{P2=0xF9,P0=table[1],M=0,delay(100),P2=0xFF,M=1,delay(100);}}if(K2==0) { while(1) //数码管二,三闪烁锁定提前抢答者2,蜂鸣器报警{P2=0xF9,P0=table[2],M=0,delay(100),P2=0xFF,M=1,delay(100);;}}if(K3==0) { while(1) //数码管二,三闪烁锁定提前抢答者3,蜂鸣器报警{P2=0xF9,P0=table[3],M=0,delay(100),P2=0xFF,M=1,delay(100);;}}if(K4==0) { while(1) //数码管二,三闪烁锁定提前抢答者4,蜂鸣器报警{P2=0xF9,P0=table[4],M=0,delay(100),P2=0xFF,M=1,delay(100);;}}}};}void main(){ del();if(i==0) M=0,delay(50),M=1; //蜂鸣器响后开始抢答P3=0xFF,j=0;while(1) //实现四人强答,动态显示{ if(K1==0){M=0,L1=0; //蜂鸣器响对应第一流水灯亮if(z==5) z=j++;}P2=tab[z],P0=table[1],delay(4),M=1,P0=0xFF,P2=0xFF;if(K2==0){M=0,L2=0; //蜂鸣器响对应第二流水灯亮if(x==5) x=j++;}P2=tab[x],P0=table[2],delay(4),M=1,P0=0xFF,P2=0xFF;if(K3==0){M=0,L3=0; //蜂鸣器响对应第三流水灯亮if(c==5) c=j++;}P2=tab[c],P0=table[3],delay(4),M=1,P0=0xFF,P2=0xFF;if(K4==0){M=0,L4=0; //蜂鸣器响对应第四流水灯亮if(v==5) v=j++;}P2=tab[v],P0=table[4],delay(4),M=1,P0=0xFF,P2=0xFF;}}。

基于单片机的万年历源代码程序

基于单片机的万年历源代码程序

基于单片机的万年历源代码程序源程序代码如下:#include<reg52.h>#include<intrins.h>unsigned char code displaywelcome[]={" Welcome To My Lcd Timer"};//欢迎界面unsigned char code displaywish[]={" Happy Every Day ^_^"}; //欢迎界面unsigned char code overtemperature[]={"OVERTEMPERATURE!"};unsigned char code digit[]={"0123456789"}; //数字代码unsigned char mode,TH,TL,TN,TD,length,tempswitch,Maxtemp=40,amode,alarmmode,minutes,hours, minutea,seconds,houra=12;sbit SCLK=P1^0; //DS1302时钟输入sbit DATE=P1^1; //DS1302数据输入sbit REST=P1^2; //DS1302复位端口sbit SET=P1^4; //DS1302设置模式选择位sbit ADD=P1^5; //增加sbit RED=P1^6; //减小sbit CANL=P1^7;void delay1ms(int i)//1毫秒延时{int j,k;while(i--)for(j=76;j>1;j--);for(k=29;k>1;k--);}void delaynus(unsigned char n) //延时若干微秒{unsigned char i;for(i=0;i<n;i++);}/***********蜂鸣器模块************/sbit beep=P3^6; //位定义,定义P.6位fmpvoid dely500(void){unsigned char i;for(i=250;i>0;i--){_nop_();}}void baojing(unsigned char n){unsigned char x,i;while(n--){for(i=0;i<5;i++){for(x=0;x<200;x++){beep=~beep;dely500();}}for(i=0;i<3;i++){for(x=0;x<200;x++){beep=~beep;dely500();dely500();}}}}/*******DS1302模块*************/void Write1302(unsigned char date)//向1302写数据{unsigned char i;SCLK=0;delaynus(2);for(i=0;i<8;i++){DATE=date&0x01;SCLK=1;delaynus(2);SCLK=0;delaynus(2);date>>=1;}}void WriteSet1302(unsigned char cmd,unsigned char date) //根据相应的命令输入相应的数据{REST=0;SCLK=0;REST=1;Write1302(cmd);delaynus(5);Write1302(date);SCLK=1;REST=0;}unsigned char Read1302(void) //读取1302数据{unsigned char i,date;delaynus(2);for(i=0;i<8;i++){date>>=1;if(DATE==1)date|=0x80;SCLK=1;delaynus(2);SCLK=0;delaynus(2);}return date;}unsigned char ReadSet1302(unsigned char cmd)//根据命令读取1302相应的值{unsigned char date;REST=0;SCLK=0;REST=1;Write1302(cmd);delaynus(2);date=Read1302();SCLK=1;REST=0;return date;}void IntDS1302(void) //DS1302初始化{unsigned char flag;flag= ReadSet1302(0x81);if(flag&0x80) { //判断时钟芯片是否关闭WriteSet1302(0x8E,0x00); //根据写状态寄存器命令字,写入不保护指令WriteSet1302(0x80,((0/10)<<4|(0%10))); //根据写秒寄存器命令字,写入秒的初始值WriteSet1302(0x82,((0/10)<<4|(0%10))); //根据写分寄存器命令字,写入分的初始值WriteSet1302(0x84,((0/10)<<4|(0%10))); //根据写小时寄存器命令字,写入小时的初始值WriteSet1302(0x86,((0/10)<<4|(0%10))); //根据写日寄存器命令字,写入日的初始值WriteSet1302(0x88,((0/10)<<4|(0%10))); //根据写月寄存器命令字,写入月的初始值WriteSet1302(0x8c,((10/10)<<4|(10%10))); //根据写年寄存器命令字,写入年的初始值WriteSet1302(0x90,0xa5); //打开充电功能选择2K电阻充电方式WriteSet1302(0x8E,0x80); //根据写状态寄存器命令字,写入保护指令}}/***************液晶显示模块*****************/sbit RS=P2^5;sbit RW=P2^6;sbit E=P2^7;sbit BF=P0^7;/*液晶忙检测*/bit BusyTest(void){bit result;RS=0;RW=1;E=1;_nop_();_nop_();_nop_();result=BF;_nop_();_nop_();_nop_();E=0;return result;/*写指令*/void Write_com(unsigned char command) {while(BusyTest()!=0);RS=0;RW=0;E=0;_nop_();_nop_();_nop_();P0=command;_nop_();_nop_();_nop_();E=1;_nop_();_nop_();_nop_();E=0;}/*写地址*/void Write_Address(unsigned char address) {Write_com(address|0x80);delay1ms(1);}/*写数据*/void Write_Date(unsigned char date){RS=1;RW=0;E=0;_nop_();_nop_();_nop_();P0=date;_nop_();_nop_();_nop_();E=1;_nop_();_nop_();_nop_();E=0;delay1ms(1);}/*初始化*/void Lcd_Int(void){Write_com(0x38);delay1ms(1);Write_com(0x38);delay1ms(1);Write_com(0x06);delay1ms(1);Write_com(0x0c);delay1ms(1);Write_com(0x01);}void displaymainpart(void)//显示液晶主要部分(不变化部分){Write_Address(0x01);delay1ms(1);Write_Date('D');Write_Date('A');Write_Date('T');Write_Date('E');Write_Date(':');delay1ms(1);Write_Address(0x0A);delay1ms(1);Write_Date('-');Write_Address(0x0D);Write_Date('-');Write_Address(0x42);Write_Date('T');Write_Date('I');Write_Date('M');Write_Date('E');Write_Date(':');Write_Address(0x4A);Write_Date('-');Write_Address(0x4D);Write_Date('-');Write_Address(0x06);Write_Date('2');Write_Date('0');}void display_Second(unsigned char second) //在液晶上显示秒{unsigned char i,j;i=second/10;j=second%10;Write_Address(0x4E);Write_Date(digit[i]);Write_Date(digit[j]);delay1ms(1);}void display_Minute(unsigned char minute) //在液晶上显示分{unsigned char i,j;i=minute/10;j=minute%10;Write_Address(0x4B);Write_Date(digit[i]);Write_Date(digit[j]);delay1ms(1);}void display_Hour(unsigned char hour) //在液晶上显示时{unsigned char i,j;i=hour/10;j=hour%10;Write_Address(0x48);Write_Date(digit[i]);Write_Date(digit[j]);delay1ms(1);}void display_Day(unsigned char day) //在液晶上显示日{unsigned char i,j;i=day/10;j=day%10;Write_Address(0x0E);Write_Date(digit[i]);Write_Date(digit[j]);delay1ms(1);}void display_Month(unsigned char month) //在液晶上显示月{unsigned char i,j;i=month/10;j=month%10;Write_Address(0x0B);Write_Date(digit[i]);Write_Date(digit[j]);delay1ms(1);}void display_Year(unsigned char year) //在液晶上显示年{unsigned char i,j;i=year/10;j=year%10;Write_Address(0x08);Write_Date(digit[i]);Write_Date(digit[j]);delay1ms(1);}void display_houra(unsigned char x) //闹钟小时部分显示{unsigned char i,j;i=x/10;j=x%10;Write_Address(0x44);Write_Date(digit[i]);Write_Date(digit[j]);}void display_minutea(unsigned char x)//闹钟分钟部分显示{unsigned char i,j;i=x/10;j=x%10;Write_Address(0x47);Write_Date(digit[i]);Write_Date(digit[j]);}void display_Time(void) //显示实时时间{unsigned char value,day,month,year;Write_com(0x0c);value=ReadSet1302(0x81);seconds=(((value&0x70)>>4)*10+(value&0x0f));display_Second(seconds);value=ReadSet1302(0x83);minutes=(((value&0x70)>>4)*10+(value&0x0f));display_Minute(minutes);value=ReadSet1302(0x85);hours=(((value&0x70)>>4)*10+(value&0x0f));display_Hour(hours);value=ReadSet1302(0x87);day=(((value&0x70)>>4)*10+(value&0x0f));display_Day(day);value=ReadSet1302(0x89);month=(((value&0x70)>>4)*10+(value&0x0f));display_Month(month);value=ReadSet1302(0x8D);year=(((value&0xf0)>>4)*10+(value&0x0f));display_Year(year);}void displaystar(void) //显示欢迎界面{unsigned char i,j;Write_Address(0x0f);while(displaywelcome[i]!='\0'){Write_Date(displaywelcome[i]);i++;delay1ms(1);}i=0;Write_Address(0x4f);while(displaywish[i]!='\0'){Write_Date(displaywish[i]);i++;delay1ms(1);}j=40;while(j--){Write_com(0x18);//循环左移delay1ms(700);}Write_com(0x01);delay1ms(10);}void gbdisplay(unsigned char address) //时间调整时光标闪烁{Write_Address(address);delay1ms(5);Write_com(0x0f);delay1ms(5);}void displaymaxt(unsigned char x) //显示最大温度{unsigned char i,j,k;Write_com(0x0c);delay1ms(2);Write_Address(0x44);i=x/100;j=x/10;k=x%10;Write_Date(digit[i]);Write_Date(digit[j]);Write_Date(digit[k]);}/*****************时间调整部分*********************/void hourset(void) //调时{unsigned char timevalue,hour;delay1ms(500); //防止多次触发WriteSet1302(0x8e,0x00);//将写保护去掉,确保能正常将调整后的数值写入DS1302timevalue=ReadSet1302(0x85); //读取此时的数值hour=(((timevalue&0x70)>>4)*10+(timevalue&0x0f));while(1){if(ADD==0){delay1ms(50);if(ADD==0){hour++;delay1ms(300);while(ADD==0);}}if(RED==0){delay1ms(50);if(RED==0){hour--;delay1ms(300);if(hour==0) hour=23;while(RED==0);}}timevalue=(((hour)/10)<<4|(hour%10));WriteSet1302(0x84,timevalue);delay1ms(1);display_Hour(hour);Write_Address(0x49);delay1ms(5);if(hour>=24) hour=0;delay1ms(5);if(CANL==0){mode=0;Write_com(0x0c);break;}if(SET==0) break;}WriteSet1302(0x8e,0x80);}void minuteset(void) //调分{unsigned char timevalue,minute;delay1ms(500);WriteSet1302(0x8e,0x00);timevalue=ReadSet1302(0x83);minute=(((timevalue&0x70)>>4)*10+(timevalue&0x0f));while(1){if(ADD==0){delay1ms(10);if(ADD==0){minute++;if(minute>=60) timevalue=0;while(ADD==0);}}if(RED==0)delay1ms(10);if(RED==0){minute--;delay1ms(300);if(minute==0) minute=59;while(RED==0);}}timevalue=((minute/10)<<4|(minute%10));WriteSet1302(0x82,timevalue);delay1ms(1);display_Minute(minute);Write_Address(0x4c);delay1ms(5);if(minute>=60) minute=0;delay1ms(5);if(CANL==0){mode=0;Write_com(0x0c);break;}if(SET==0) break;}WriteSet1302(0x8e,0x80);}void secondset(void) //秒归零{unsigned char second;delay1ms(500);WriteSet1302(0x8e,0x00);while(1){if(ADD==0){delay1ms(10);if(ADD==0){second=0;WriteSet1302(0x80,0x00);while(ADD==0);}delay1ms(1);display_Second(second);Write_Address(0x4f);delay1ms(5);if(CANL==0){mode=0;Write_com(0x0c);break;}if(SET==0) break;}WriteSet1302(0x8e,0x80);}void yearset(void) //调年{unsigned char datevalue,year;delay1ms(500);WriteSet1302(0x8e,0x00);datevalue=ReadSet1302(0x8d);year=(((datevalue&0x70)>>4)*10+(datevalue&0x0f));while(1){if(ADD==0){delay1ms(10);if(ADD==0){year++;while(ADD==0);}}if(RED==0){delay1ms(10);if(RED==0){year--;delay1ms(300);while(RED==0);}}datevalue=((year/10)<<4|(year%10));WriteSet1302(0x8c,datevalue);delay1ms(5);display_Year(year);delay1ms(5);Write_Address(0x09);delay1ms(5);if(CANL==0){mode=0;Write_com(0x0c);break;}if(SET==0) break;}WriteSet1302(0x8e,0x80);}void monthset(void) //调月{unsigned char datevalue,month;delay1ms(500);WriteSet1302(0x8e,0x00);datevalue=ReadSet1302(0x89);month=(((datevalue&0x70)>>4)*10+(datevalue&0x0f));while(1){if(ADD==0){delay1ms(10);if(ADD==0){month++;if(month>12) month=1;while(ADD==0);}}if(RED==0){delay1ms(10);if(RED==0){month--;delay1ms(300);if(month==0) month=12;while(RED==0);}}datevalue=((month/10)<<4|(month%10));WriteSet1302(0x88,datevalue);delay1ms(1);display_Month(month);Write_Address(0x0c);delay1ms(5);if(CANL==0){mode=0;Write_com(0x0c);break;}if(SET==0) break;}WriteSet1302(0x8e,0x80);}void dayset(void) //调日{unsigned char datevalue,day;delay1ms(500);WriteSet1302(0x8e,0x00);datevalue=ReadSet1302(0x87);day=(((datevalue&0x70)>>4)*10+(datevalue&0x0f));while(1){if(ADD==0){delay1ms(10);if(ADD==0){day++;if(day>31) day=1;while(ADD==0);}}if(RED==0){delay1ms(10);if(RED==0){day--;delay1ms(300);if(day==0) day=31;while(RED==0);}}datevalue=((day/10)<<4|(day%10));WriteSet1302(0x86,datevalue);delay1ms(1);display_Day(day);Write_Address(0x0f);delay1ms(5);if(CANL==0){mode=0;Write_com(0x0c);break;}if(SET==0) break;}WriteSet1302(0x8e,0x80);}void alarmhourset(void) //闹钟小时调整{unsigned char value;value=houra;while(1){if(ADD==0){delay1ms(20);if(ADD==0){Write_com(0x0c);delay1ms(5);value++;if(value>23) value=0;while(ADD==0);}}if(RED==0){delay1ms(20);if(RED==0){value--;if(value==0) value=23;while(RED==0);}}houra=value;display_houra(houra);gbdisplay(0x45);if(CANL==0){delay1ms(500);break;}}}void alarmminuteset(void) //闹钟分钟调整{while(1){if(ADD==0){delay1ms(20);if(ADD==0){minutea++;if(minutea>59) minutea=0;while(ADD==0) ;}}if(RED==0){delay1ms(20);if(RED==0){minutea--;if(minutea==0) minutea=59;while(RED==0) ;}}display_minutea(minutea);gbdisplay(0x48);if(CANL==0){delay1ms(500);break;}}}void TimeSet(void) //时间调整函数{display_Time();if(SET==0){delay1ms(10);if(SET==0){mode++;delay1ms(20);switch(mode){case(1):{gbdisplay(0x48);hourset(); delay1ms(500);} break;case(2):{gbdisplay(0x4c);minuteset();delay1ms(500);} break;case(3):{gbdisplay(0x4f);secondset();delay1ms(500);} break;case(4):{gbdisplay(0x09);yearset();delay1ms(500);} break;case(5):{gbdisplay(0x0c);monthset();delay1ms(500);} break;case(6):{gbdisplay(0x0f);dayset();delay1ms(500);} break;}if(mode==7) mode=0;}}}/***************显示温度模块********************/unsigned char code displayexplain[]={"Digit thermometer"};unsigned char code displayerror[]={"DS18B20 ERROR"};unsigned char code displayerror1[]={"PLEASE CHECK "};unsigned char code displaypart1[]={"WenDu:"};unsigned char code displaypart2[]={"Cent"};unsigned char flag;/**********************操作DS18B20模块***************************/ sbit DQ=P3^3;unsigned char time;/**********************DS18B20初始化****************************/bit IntDS18B20(void){bit temp;DQ=1;for(time=0;time<2;time++);DQ=0;for(time=0;time<200;time++);DQ=1;for(time=0;time<10;time++);temp=DQ;for(time=0;time<200;time++);return temp;}/**************************读DS18B20**********************/unsigned char ReadDS18B20(void){unsigned char i;unsigned char dat;for(i=0;i<8;i++){DQ=1;_nop_();DQ=0;_nop_();DQ=1;for(time=0;time<2;time++);dat>>=1;if(DQ==1)dat=dat|0x80;elsedat=dat|0x00;for(time=0;time<10;time++);}return dat;}/*********************向DS18B20写数据**************************/ void WriteDS18B20(unsigned char date){unsigned char i;for(i=0;i<8;i++){DQ=1;_nop_();DQ=0;DQ=date&0x01;for(time=0;time<10;time++);DQ=1;for(time=0;time<1;time++);date>>=1;}for(time=0;time<4;time++);}/*******************为读取温度做好准备************************/ void ReadyreadDS18B20(void){IntDS18B20();WriteDS18B20(0XCC);WriteDS18B20(0X44);delay1ms(200);IntDS18B20();WriteDS18B20(0XCC);WriteDS18B20(0XBE);}/*************在1602液晶上显示相关信息模块**********************/ /****************当未检测到DS18B20时显示信息模块****************/ void Display_Error(void){unsigned char i=0;Write_Address(0x00);while(displayerror[i]!='\0'){Write_Date(displayerror[i]);i++;delay1ms(100);}delay1ms(5);i=0;Write_Address(0x40);while(displayerror1[i]!='\0'){Write_Date(displayerror1[i]);i++;delay1ms(100);}delay1ms(3000) ;}/*************温度显示模块****************/void Display_Explain(void){unsigned char i=0;Write_Address(0x00);delay1ms(5);while(displayexplain[i]!='\0'){Write_Date(displayexplain[i]);i++;delay1ms(5);}i=0;Write_Address(0x40);while(displaypart1[i]!='\0'){Write_Date(displaypart1[i]);i++;delay1ms(5);}i=0;Write_Address(0x4C);while(displaypart2[i]!='\0'){Write_Date(displaypart2[i]);i++;delay1ms(5);}Write_Address(0x49);Write_Date('.');delay1ms(5);}/*********************显示温度整数部分*************************/ void Display_Integer(unsigned char x){unsigned char i,j,k;i=x/100;j=x%100/10;k=x%10;Write_Address(0x46);if(flag==1) Write_Date('-');Write_Date(digit[i]);Write_Date(digit[j]);Write_Date(digit[k]);delay1ms(50) ;}/************显示温度小数部分*********************/ void Display_Decimal(unsigned char x){Write_Address(0x4A);delay1ms(5);Write_Date(digit[x]);delay1ms(50);}void displayovert(void){unsigned char i;Write_com(0x01);delay1ms(10);Write_Address(0x03);delay1ms(10);Write_Date('W');Write_Date('A');Write_Date('R');Write_Date('N');Write_Date('I');Write_Date('N');Write_Date('G');Write_Date('!');Write_Address(0x40);while(overtemperature[i]!='\0'){Write_Date(overtemperature[i]);i++;delay1ms(10);}}/*******************温度主函数********************/ void Temperature(void){unsigned char i;delay1ms(5);if(IntDS18B20()!=0) Display_Error();else{delay1ms(1);Write_com(0x01);delay1ms(5);Display_Explain();for(length=50;length>0;length--){for(i=0;i<6;i++){flag=0;ReadyreadDS18B20();TL=ReadDS18B20();TH=ReadDS18B20();if((TH&0XF8)!=0X00){flag=1;TL=~TL;TH=~TH;TL+=1;if(TL>255) TH+=1;TN=TH*16+TL/16;TD=(TL%16)*10/16;}TN=TH*16+TL/16;TD=(TL%16)*10/16;Display_Integer(TN);Display_Decimal(TD);delay1ms(10);}if(tempswitch==1){if(TN>=Maxtemp){Write_Address(0x03);delay1ms(5);displayovert();while(1){baojing(1);if(CANL==0){Write_com(0x01);delay1ms(5);break;}}}}if(CANL==0) break;}}}void tempset(void){if(ADD==0){delay1ms(10);if(ADD==0){Maxtemp++;while(ADD==0);}}if(RED==0){delay1ms(10);if(RED==0){Maxtemp--;while(RED==0);}}}void Maxtempset(void) //高温报警设置{Write_com(0x01);delay1ms(10);Write_Address(0x03);delay1ms(10);Write_Date('T');Write_Date('E');Write_Date('M');Write_Date('P');Write_Address(0x09);Write_Date('S');Write_Date('E');Write_Date('T');Write_Address(0x40);Write_Date('M');Write_Date('A');Write_Date('X');Write_Date(':');while(1){if(SET==0){delay1ms(20);if(SET==0){Write_Address(0x4e);Write_com(0x0f);while(1){if(ADD==0){delay1ms(20);if(ADD==0){Write_Address(0x4b);delay1ms(5);Write_Date('O');Write_Date('N');Write_Date(' ');tempswitch=1;}}if(RED==0){delay1ms(20);if(RED==0){Write_Address(0x4b);Write_Date('O');Write_Date('F');Write_Date('F');tempswitch=0;}}if(CANL==0){delay1ms(500);break;}}}}if(tempswitch==1){Write_Address(0x4b);Write_Date('O');Write_Date('N');Write_Date(' ');tempset();displaymaxt(Maxtemp);if(CANL==0){Write_com(0x01);displaymainpart();break;}}if(tempswitch==0){Write_com(0x0c);Write_Address(0x4b);Write_Date('O');Write_Date('F');Write_Date('F');}if(CANL==0){Write_com(0x01); displaymainpart();break;}}}void alarmswitchset(void){while(1){if(ADD==0){delay1ms(100);if(ADD==0){alarmmode=1;Write_Address(0x4c);Write_Date('O');Write_Date('N');Write_Date(' ');}}if(RED==0){alarmmode=0;Write_Address(0x4c);Write_Date('O');Write_Date('F');Write_Date('F');}if(CANL==0){delay1ms(500);break;}}}/**************** 设置模块 ******************/ void Set(void){if(SET==0){delay1ms(10);if(SET==0){Write_Address(0x40);Write_Date('T');Write_com(0x0f);delay1ms(1000);Write_com(0x0c);while(1){TimeSet();if(ADD==0){delay1ms(20);if(ADD==0){Write_com(0x01);Write_Address(0x00);delay1ms(5);Write_Date('S');delay1ms(10);Maxtempset();}}if(RED==0){delay1ms(20);if(RED==0){Write_com(0x01);delay1ms(5);Write_Address(0x02);Write_Date('A');Write_Date('L');Write_Date('A');Write_Date('R');Write_Date('M');Write_Address(0x09);Write_Date('S');Write_Date('E');Write_Date('T');Write_Address(0x46);Write_Date('-');display_houra(houra);display_minutea(minutea);if(alarmmode==1){Write_Address(0x4c);Write_Date('O');Write_Date('N');Write_Date(' ');}if(alarmmode==0){Write_Address(0x4c);Write_Date('O');Write_Date('F');Write_Date('F');}while(1){if(SET==0){delay1ms(100);if(SET==0){amode++;switch(amode){case(1):{gbdisplay(0x45);alarmhourset();Write_com(0x0c);delay1ms( 500);} break;case(2):{gbdisplay(0x48);alarmminuteset();Write_com(0x0c);delay1m s(500);} break;case(3):{gbdisplay(0x4c);alarmswitchset();Write_com(0x0c);delay1m s(500);} break;}if(amode==4) amode=0;}}if(CANL==0){Write_com(0x01);delay1ms(5);displaymainpart();break;}}}}if(CANL==0){Write_Address(0x40);Write_Date(' ');Write_com(0x0c);break;}}}}}/***************主函数**********************/void main(){IE=0X82; //打开定时中断TMOD=0X01; //选择定时器0工作在方式1TR0=0; //启动定时器0IntDS1302(); //初始化DS1302delay1ms(1);Lcd_Int(); //1602液晶初始化delay1ms(2);displaystar(); //显示欢迎界面displaymainpart(); //显示主要部分(不变化)while(1){display_Time( );Set( );if(ADD==0){Write_com(0x01);delay1ms(5);Temperature();Write_com(0x01);displaymainpart();}if((hours==houra)&&(minutes==minutea)&&(seconds==0)) {if(alarmmode==1){Write_com(0x01);delay1ms(5);Write_Address(0x03);Write_Date('T');Write_Date('I');Write_Date('M');Write_Date('E');Write_Address(0x08);Write_Date('U');Write_Date('P');Write_Date('!');delay1ms(5);while(1){baojing(1);if(CANL==0){Write_com(0x01);delay1ms(5);displaymainpart();break;}}}}}}。

单片机实训--万年历程序

单片机实训--万年历程序

#include<reg52.h>typedef unsigned char uint8; #define clrdp 0x01#define enmode 0x06#define curback 0x02#define ondp 0x0f#define offdp 0x08#define movd 0x14#define funset 0x38#define bus P1#define contw 0x8e#define secw 0x80#define secr 0x81#define minw 0x82#define minr 0x83#define hourw 0x84#define hourr 0x85#define datw 0x86#define datr 0x87#define monthw 0x88#define monthr 0x89#define dayw 0x8a#define dayr 0x8b#define yearw 0x8c#define yearr 0x8dsbit K1=P2^7;sbit K2=P2^5;sbit K3=P2^4;sbit RS=P3^7;sbit RW=P3^6;sbit EN=P3^5;sbit DIO=P2^1;sbit RST=P2^2;sbit SCK=P2^0;bit flag=0;uint8 sel;uint8 time[7]={0};uint8 time_disp[32]={0}; void init_1602(void);void write_com(uint8 comd); void write_dat(uint8 wdata); void delay(void);void long_dleay(void);void init_1302(void);uint8 read_1302(uint8 addr);void write_1302(uint8 addr,uint8 wdata); void disp(void);void read_time(void);void sepr_time(void);void chk_key(void);uint8 bcd_dec(uint8 wdata);uint8 dec_bcd(uint8 wdata);void main(void){init_1302();init_1602();while(1){chk_key();read_time();sepr_time();disp();}}//1302初始化void init_1302(void){write_1302(contw,0);write_1302(secw,0);}//写1302寄存器void write_1302(uint8 addr,uint8 wdata) {uint8 i;SCK=0;RST=1;for(i=0;i<8;i++){DIO=addr&0x01;SCK=1;SCK=0;addr>>=1;}for(i=0;i<8;i++){DIO=wdata&0x01;SCK=1;SCK=0;wdata>>=1;}RST=0;}//读1302寄存器uint8 read_1302(uint8 addr) {uint8 i;uint8 temp;SCK=0;RST=1;for(i=0;i<8;i++){DIO=addr&0x01;SCK=1;SCK=0;addr>>=1;}for(i=0;i<8;i++){temp>>=1;if(! DIO){temp&=0x7f;}else{temp|=0x80;}SCK=1;SCK=0;}RST=0;return temp;}//读1302时间寄存器void read_time(void){uint8 i;uint8 temp;for(i=0;i<7;i++){temp=read_1302(0x81+i*2);time[i]=temp;}time[0]&=0x7f;}//显示时间void disp(void){uint8 i;write_com(0x80);for(i=0;i<16;i++){write_dat(time_disp[i]);}write_com(0xc0);for(i=16;i<32;i++){write_dat(time_disp[i]);}}//分离时间void sepr_time(void){time_disp[0]='-';time_disp[1]='-';time_disp[2]='2';time_disp[3]='0';time_disp[4]=(time[6]>>4)+0x30;time_disp[5]=(time[6]&0x0f)+0x30;time_disp[6]='-';time_disp[7]=(time[4]>>4)+0x30;time_disp[8]=(time[4]&0x0f)+0x30;time_disp[9]='-';time_disp[10]=(time[3]>>4)+0x30;time_disp[11]=(time[3]&0x0f)+0x30;time_disp[12]='-';time_disp[13]='-';time_disp[14]=(time[5]>>4)+0x30;time_disp[15]=(time[5]&0x0f)+0x30;time_disp[16]='-';time_disp[17]='-';time_disp[18]='-';time_disp[19]='-';time_disp[20]=(time[2]>>4)+0x30;time_disp[21]=(time[2]&0x0f)+0x30;time_disp[22]='-';time_disp[23]=(time[1]>>4)+0x30;time_disp[24]=(time[1]&0x0f)+0x30;time_disp[25]='-';time_disp[26]=(time[0]>>4)+0x30;time_disp[27]=(time[0]&0x0f)+0x30;time_disp[28]='-';time_disp[29]='-';time_disp[30]='-';time_disp[31]='-';}void init_1602(void){write_com(offdp);write_com(funset);write_com(enmode);write_com(movd);write_com(ondp);write_com(clrdp);}void write_com(uint8 comd){RS=0;RW=0;bus=comd;EN=1;EN=0;delay();}void write_dat(uint8 wdata){RS=1;RW=0;bus=wdata;EN=1;EN=0;delay();}void delay(void){uint8 t;for(t=0;t<250;t++);}void long_delay(void){uint8 t;for(t=0;t<200;t++){delay();}}void chk_key(void){uint8 buf;uint8 temp;if(!K1){long_delay();if(!K1){if(flag){if((sel>=0)&&(sel<8)){sel++;}else if(sel==8){temp=read_1302(secr);temp&=0x7f;write_1302(secw,temp);sel=0;flag=0;}else{sel=8;}}else{temp=read_1302(secr);temp|=0x80;write_1302(secw,temp);flag=1;}}}else if(!K2){long_delay();if(!K2){switch(sel){case 1:temp=read_1302(yearr);buf=bcd_dec(temp);if(buf<99){buf++;temp=dec_bcd(buf);write_1302(yearw,temp);}break;case 2:temp=read_1302(dayr);buf=bcd_dec(temp);if(buf<7){buf++;temp=dec_bcd(buf);write_1302(dayw,temp);}break;case 3:temp=read_1302(monthr);buf=bcd_dec(temp);if(buf<12){buf++;temp=dec_bcd(buf);write_1302(monthw,temp);}break;case 4:temp=read_1302(datr);buf=bcd_dec(temp);if(buf<31){buf++;temp=dec_bcd(buf);write_1302(datw,temp);}break;case 5:temp=read_1302(hourr);buf=bcd_dec(temp);if(buf<23){buf++;temp=dec_bcd(buf);write_1302(hourw,temp);}break;case 6:temp=read_1302(minr);buf=bcd_dec(temp);if(buf<59){buf++;temp=dec_bcd(buf);write_1302(minw,temp);}break;case 7:temp=read_1302(secr);temp&=0x7f;buf=bcd_dec(temp);if(buf<59){buf++;temp=dec_bcd(buf);temp|=0x80;write_1302(secw,temp);}break;default:break;}}}else if(!K3){long_delay();if(!K3){switch(sel){case 1:temp=read_1302(yearr);buf=bcd_dec(temp);if(buf>0){buf--;temp=dec_bcd(buf);write_1302(yearw,temp);}break;case 2:temp=read_1302(dayr);buf=bcd_dec(temp);if(buf>1){buf--;temp=dec_bcd(buf);write_1302(dayw,temp);}break;case 3:temp=read_1302(monthr);buf=bcd_dec(temp);if(buf>1){buf--;temp=dec_bcd(buf);write_1302(monthw,temp);}break;case 4:temp=read_1302(datr);buf=bcd_dec(temp);if(buf>1){buf--;temp=dec_bcd(buf);write_1302(datw,temp);}break;case 5:temp=read_1302(hourr);buf=bcd_dec(temp);if(buf>0){buf--;temp=dec_bcd(buf);write_1302(hourw,temp);}break;case 6:temp=read_1302(minr);buf=bcd_dec(temp);if(buf>0){buf--;temp=dec_bcd(buf);write_1302(minw,temp);}break;case 7:temp=read_1302(secr);temp&=0x7f;buf=bcd_dec(temp);if(buf>0){buf--;temp=dec_bcd(buf);temp|=0x80;write_1302(secw,temp);}break;default:break;}}}}uint8 dec_bcd(uint8 wdata){uint8 temp;uint8 i;uint8 j;i=wdata/10;j=wdata%10;temp=i;temp=temp<<4;temp=temp|j;return temp;}uint8 bcd_dec(uint8 wdata){uint8 temp;uint8 i;uint8 j;i=wdata>>4;j=wdata&0x0f;temp=i*10+j;return temp;。

c51单片机万年历的c程序

c51单片机万年历的c程序

c51单片机万年历的c程序c51单片机万年历的c程序求一个单片机可以显示阳历的年、月、日、星期、小时.分.秒。

阴历也能显示,标明是否为闰年。

用LCD液晶显示,可以调整时间。

这有个数码管显示的程序以前做的,。

你可以在我这个程序上修改修改包括键盘扫描,还有动态显示。

【。

】#include <REG51.H>//常量参数#define TMODW 0x01;#define SCONW 0x00;#define xplay 0x04;//显示分频系数//显示位选unsigned char data stb;//键值缓存,0xFF无键命令unsigned char data keynum;//显示字型变量unsigned char data play[8];//工作参数unsigned char data l,m;//字型码unsigned char code BCDPC[10]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};//字位码unsigned char code STBCODE[8]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80 };//400Hz xplay分频计数unsigned char data cttime;//时钟参数unsigned char data hr,min,sec,sec100;//调整时钟参数(时钟“走”)void ct1(){sec100++;if (sec100==100){sec100=0;sec++;if (sec==60){sec=0;min++;if (min==60){min=0;hr++;if (hr==24) hr=0;}}}}//时钟参数→LED 显示缓存7段参数转换函数;void xcplay(){play[0]=BCDPC[hr/10];play[1]=BCDPC[hr%10];play[2]=BCDPC[min/10];play[3]=BCDPC[min%10];play[4]=BCDPC[sec/10];play[5]=BCDPC[sec%10];play[6]=BCDPC[sec100/10]; play[7]=BCDPC[sec100%10];}//显示扫描void cplay(){T0=1;//T0-高电平消隐T1=0;//T1-低电平准备发脉冲前沿TI=0;//?P1=0;//?SBUF=STBCODE[stb];while (TI==0){};TI=0;SBUF=play[stb];while (TI==0){};T1=1;T0=0;stb=++stb&0x07;}extern void cthl0();//定时器0中断处理程序void ct0(void) interrupt 1 using 1 {cthl0();cttime--;if (cttime==0){cttime=xplay;ct1();//调用时钟“走”函数xcplay();//调用时钟参数→Led显示缓存转换函数};cplay();}void w20ms(){for (l=0;l<41;l++){for (m=0;m<81;m++){}}}void tkey(){P1=0xF0;keynum=0xFF;if (P1!=0xF0){w20ms();P1=0xF0;if (P1!=0xF0){P1=0xFE;switch (P1){case 0xEE:keynum=0;break;case 0xDE:keynum=1;break;case 0xBE:keynum=2;break;case 0x7E:keynum=3;break;}P1=0xFD;switch (P1){case 0xED:keynum=4;break;case 0xDD:keynum=5;break;case 0xBD:keynum=6;break; case 0x7B:keynum=7;break; }P1=0xFB;switch (P1){case 0xEB:keynum=8;break; }};};P1=0x00;}void command(){switch (keynum){case 0:{hr=hr+1;if (hr==24)hr=0;}break;case 1:{min=min+1;if (min==60)min=0;}break;case 2:{sec=sec+1;if (sec==60)sec=0;}break;case 3:{sec100=0; }break;case 4:{while(!(P1=0xED)) {hr=0;min=0;sec=0;}}break;case 5:{hr=hr-1;if (hr==00)hr=24;}break;case 6:{min=min-1; if (min==00)min=59;}break;case 7:{sec=sec-1; if (sec==00)sec=0;}break;case 0xFF:break; }keynum=0xFF; }main (){ hr=8;min=5;sec=8;sec100=0; TMOD=TMODW; SCON=SCONW; ET0=1;TR0=1;EA=1;cttime=xplay; while (1){w20ms();tkey(); command();};}。

单片机万年历程序

单片机万年历程序

ATmega8单片机万年历C程序(内部RTC)作者:jdzj868来源:机电之家下载站录入:jdzj868更新时间:2009-11-30 16:17:01点击数:0【字体:】正文:) 调整时间和日期,只设置了四个按键a, 复位按键;b, 调整模态选择按键,切换调整不同参数;c, 上调按键,调整模式下按此键被调整值上调;d, 下调按键,调整模式下按此键被调整值下调.这shi调整年份时de显示状态,LCD第二行末尾显示"yar".依此类推,调整月份时显示"mth",调整日期时显示"day",小时时显示"hor",分钟时显示"min".3) 软件部分.程序中包含一个头文件"LCD1620.h",此文件中包含有关LCDde一些显示函数.下面shi部分程序原代码---不知道怎么回事,今天网络速度比较慢,无法上传文件.如有需要可与我e-mail联系lhy_radio@./*******************************************************//* Project :Calendar *//* Compiler :ICCAVR 6.31A *//* Date :2008,5,31 *//* Author :lhy *//* Version :v1.3 *//* Rework :lhy *//* Rework Date :2008.6.26 *//* *//* Comments : *//* 1,使用Atmega8内部RTC(实时时钟,32.768KHz) *//* 2,显示使用LCD1620 *//* 3,在Timer2中断中刷新显示,精度提高1S@5H *//* 4,加入星期显示*//* 5,加入农历换算*//* 6,秒末位大于等于5时,显示公历:Solar 年月日*//* 7,SolarYear>2099或SolarYear<1900, *//* 显示"Out of range!" *//* 8,加入手动调整日期和时间*//*******************************************************/#i nclude <iom8v.h>#i nclude <macros.h>#define DataPortPullup PORTD#define DataPortDirection DDRD#define ReadLCDPin PIND#define CtrlPortPullup PORTC#define CtrlPortDirection DDRC#define RS_Bit PC0#define RW_Bit PC1#define E_Bit PC2#i nclude "LCD1620.h"#define ClkTcnt2 0x80/****************变量声明********************/unsigned int Year = 2008;//年变量unsigned char Month = 6;//月变量unsigned char Day = 1;unsigned char Hour = 12;//小时unsigned char Min = 0;//分钟unsigned char Sec = 0;//秒unsigned char YearBit1,YearBit2,YearBit3,YearBit4;unsigned char MonthBit1,MonthBit2;unsigned char DayBit1,DayBit2;unsigned char HourBit1,HourBit2;unsigned char MinBit1,MinBit2;unsigned char SecBit1,SecBit2;unsigned char Week[][4] = {"Mon","Tue","Wed","Thu","Fri","Sat","Sun"};unsigned char ChangeSymbol[][5] = {"-Yar","-Mth","-Day","-Hor","-Min"};unsigned char LeapYearWeekTable[] = {3,6,0,3,5,1,3,6,2,4,0,2};//闰年星期偏差表unsigned char UnLeapWeekTable[] = {4,0,0,3,5,1,3,6,2,4,0,2};unsigned char TodayWeek = 0;unsigned char LeapYearBit = 0;//为0时,平年unsigned char LunarData[] = {0,0,0,0};unsigned charLunarYear1,LunarYear2,LunarYear3,LunarYear4,LunarMonth1,LunARMonth2,LunarDay1,LunarDay2; unsigned char LunarConvertBit;//0,农历转换成功unsigned char CalendarStopBit;//1,时钟暂停unsigned char OldKeyNum = 0;unsigned char NewKeyNum = 0;unsigned char ModeSelect = 0;unsigned char ChangeModeBit = 0;//ModeSelect != 0时,为1,标志现在处于更改时间状态unsigned char SecOddBit = 0;//秒为偶数时,此为0,否则为1static unsigned char const LunarDayCode1[9]={0x0,0x1f,0x3b,0x5a,0x78,0x97,0xb5,0xd4,0xf3};static unsigned short const LunarDayCode2[3]={0x111,0x130,0x14e};/*************************************子函数***********************************/void PortInit(void) //端口初始化{DDRC &= ~((1<<PC3)|(1<<PC4)|(1<<PC5));PORTC |= (1<<PC3)|(1<<PC4)|(1<<PC5);}void Timer2Init(void) //timer2初始化{TCCR2 = 0x00; //stopASSR = 0x08; //set async modeTCNT2 = ClkTcnt2; //setupTCCR2 = 0x06; //start}void Timer2Ovf(void)//timer2中断函数{TCNT2 = ClkTcnt2;//reload counter valueSec += 1;AskLeapYear();SolarCount();AskLeapYear();CountWeek();LunarCount();//先判断shi否转换成功,然后再计算各位显示数值if(ModeSelect == 0){LCDispClear();DispUpdate();}}void AskLeapYear(void){if ( ( (Year%4 == 0) && (Year % 100!=0) ) || Year%400 == 0)//判断shi否为闰年{LeapYearBit = 1;}else{LeapYearBit = 0;}}void SolarCount(void)//计算各位显示值{if (Sec>59){Sec = 0;Min ++;}if (Min>59){Min = 0;Hour ++;}if (Hour>23){Hour = 0;Day ++;}if ( ( (Month==1) || (Month==3) || (Month==5) || (Month==7) ||(Month==8) || (Month==10) || (Month==12) ) && (Day>31) ){Month ++;Day = 1;}else if ( ( (Month==4) || (Month==6) || (Month==9) || (Month==11) ) && (Day>30) ){Month ++;Day = 1;}else if (Month==2){if (LeapYearBit == 1)//如果shi闰年{if (Day>29){Month ++;Day = 1;}}else if (Day > 28)//平年{Month ++;Day = 1;}}if (Month > 12){Month = 1;Year ++;}//计算各位de值YearBit1 = Year /1000;YearBit2 = Year %1000/100; YearBit3 = Year %100/10; YearBit4 = Year %10;MonthBit1 = Month /10;MonthBit2 = Month %10;DayBit1 = Day /10;DayBit2 = Day %10;HourBit1 = Hour /10;HourBit2 = Hour %10;MinBit1 = Min /10;MinBit2 = Min %10;SecBit1 = Sec /10;SecBit2 = Sec %10;}void DeviceInit(void)//MCU初始化{CLI();PortInit();Timer2Init();MCUCR = 0x00;GICR = 0x00;TIMSK = 0x40;LCD_Init();SEI();}//更新显示void DispUpdate(void){if((SecBit2 >= 5) || (ModeSelect != 0))//秒末位大于5时,显示公历{PutStringLCD(0,0,"Solar");PutNumberLCD(0,6,YearBit1);PutNumberLCD(0,7,YearBit2);PutNumberLCD(0,8,YearBit3);PutNumberLCD(0,9,YearBit4);PutNumberLCD(0,11,MonthBit1);PutNumberLCD(0,12,MonthBit2);PutNumberLCD(0,14,DayBit1);PutNumberLCD(0,15,DayBit2);}else if(LunarConvertBit == 1)//秒末位小于5并且农历转换成功时,显示农历{PutStringLCD(0,0,"Lunar");PutNumberLCD(0,6,LunarYear1);PutNumberLCD(0,7,LunarYear2);PutNumberLCD(0,8,LunarYear3);PutNumberLCD(0,9,LunarYear4);PutNumberLCD(0,11,LunARMonth1);PutNumberLCD(0,12,LunARMonth2);PutNumberLCD(0,14,LunarDay1);PutNumberLCD(0,15,LunarDay2);}else{PutStringLCD(0,0,"Out of range!");}PutNumberLCD(1,0,HourBit1);PutNumberLCD(1,1,HourBit2);if(SecOddBit){PutOneCharLCD(1,2,":");}PutNumberLCD(1,3,MinBit1);PutNumberLCD(1,4,MinBit2);if(SecOddBit){PutOneCharLCD(1,5,":");}PutNumberLCD(1,6,SecBit1);PutNumberLCD(1,7,SecBit2);PutStringLCD(1,9,Week[TodayWeek]);if(ChangeModeBit && SecOddBit){PutStringLCD(1,12,ChangeSymbol[ModeSelect - 1]);}}void CountWeek(void){if(LeapYearBit == 1){TodayWeek = ((Year/100%4*5) + (Year%100/4*5) + (Year%4) + 1 + Day + LeapYearWeekTable[Month - 1])%7;}else{TodayWeek = ((Year/100%4*5) + (Year%100/4*5) + (Year%4) + 1 + Day + UnLeapWeekTable[Month - 1])%7;}}void KeyScan(void){if((PINC&(1<<PC5)) == 0){OldKeyNum = 1;}else if((PINC&(1<<PC4)) == 0){OldKeyNum = 2;}else if((PINC&(1<<PC3)) == 0){OldKeyNum = 3;}if((PINC & (1<<PC5)) && (PINC & (1<<PC4)) && (PINC & (1<<PC3)))//判断shi否有按键按下{NewKeyNum = OldKeyNum;OldKeyNum = 0;}}void TimeChange(void){switch(ModeSelect){case 0:{if(NewKeyNum == 2){ModeSelect += 1;}}break;case 1://更改年变量{switch (NewKeyNum){case 1: Year --; break;case 2: ModeSelect ++; break;case 3: Year ++; break;default:break;}}break;case 2://更改月变量{switch (NewKeyNum){case 1: Month --; break;case 2: ModeSelect ++; break;case 3: Month ++; break;default:break;}}break;case 3://更改日变量{switch (NewKeyNum){case 1: Day --; break;case 2: ModeSelect ++; break;case 3: Day ++; break;default:break;}}break;case 4://更改小时变量{switch (NewKeyNum){case 1: Hour --; break;case 2: ModeSelect ++; break;case 3: Hour ++; break;default:break;}}break;case 5:{switch (NewKeyNum){case 1: Min --; break;case 2: ModeSelect = 0; break;case 3: Min ++; break;default:break;}}break;default : break;}}/*********************************主函数*******************************/void main(){DeviceInit();SEI();for (;;){if(ModeSelect != 0)//判断现在shi否处于更改时间日期状态{ChangeModeBit = 1;}else{ChangeModeBit = 0;}if(SecBit2 % 2 == 0)//如果秒末位shi偶数,SecSymbol=0 {SecOddBit = 0;}else{SecOddBit = 1;}KeyScan();TimeChange();if(ChangeModeBit == 1){CLI();DispUpdate();SEI();}}}此万年历程序不足,未尽完善之处:1) 未设置闹钟,只能显示阴历阳历和星期等信息.2) 后续应加入温度显示,以测定日气温3) 没有与计算机或其他设备通讯接口4) 液晶显示处于常亮状态,影响寿命.应当设置如果一定时间未操作,显示屏应关闭.待有按键操作时再打开.这样才能使LCD寿命更为长久.5) 程序和硬件中未加入对于LCD背光de控制模块,无光亮时无法看时间.。

单片机万年历程序代码

单片机万年历程序代码

单片机万年历程序代码以下是一个示例单片机万年历的程序代码:```c#include <reg51.h>typedef unsigned char uchar;typedef unsigned int uint;sbit K1 = P2^0; // 显示年份sbit K2 = P2^1; // 显示月份sbit K3 = P2^2; // 显示日期uchar code year_tab[] = {31,28,31,30,31,30,31,31,30,31,30,31}; uchar code week_tab[] = {0x06,0x07,0x01,0x02,0x03,0x04,0x05}; uchar year, month, day, week;void delay(uint ms){uint i, j;for(i=ms;i>0;i--)for(j=110;j>0;j--);}uchar getKey(){if(K1==0) {delay(5);if(K1==0)return 1;while(!K1);}if(K2==0) {delay(5);if(K2==0)return 2;while(!K2);}if(K3==0) {delay(5);if(K3==0)return 3;while(!K3);}return 0;}void display(uchar num) {P0 = num;delay(1);P0 = 0x00;}void init(){TMOD=0x01;TH0=0xFC;TL0=0x67;EA=1;ET0=1;TR0=1;}void main(){init();while(1) {uchar key = getKey();if(key == 1) {year++;if(year == 100)year = 0;}else if(key == 2) {month++;if(month == 13)month = 1;}else if(key == 3) {day++;if(day > year_tab[month-1]) { day = 1;}}display(year / 10);display(year % 10);display(month / 10);display(month % 10);display(day / 10);display(day % 10);display(week);}}void timer0() interrupt 1{TH0=0xFC;TL0=0x67;week++;if(week == 7)week = 0;}```该代码的主要思路是通过外部三个按键模拟年、月和日的调节,通过一个定时器不断更新星期的计数,然后将年、月、日和星期分别在数码管上显示出来。

单片机c语言电子万年历完整程序

单片机c语言电子万年历完整程序

该程序为51单片机c语言电子万年历#include"reg52.h"//#include<stdio.h>#define uchar unsigned char#define uint unsigned intsbit lcden=P3^4;sbit lcdrs=P3^5;sbit DATA=P0^7;sbit RST=P0^5;sbit SCLK=P0^6;sbit menu=P3^0; //菜单sbit add=P3^1; //加一sbit dec=P3^7; //减一sbit led0=P1^0;sbit led1=P1^1;sbit led2=P1^2;sbit led3=P1^3;sbit ds=P3^2;//sbit beep=P3^3;uint temp;float f_temp;uint warn_l1=270;uint warn_l2=250;uint warn_h1=300;uint warn_h2=320;uint get_temp();void delayms(uint x);void write_com(uchar com);void write_data(uchar date);void init();void dis_temp(uint t);void Write1302(uchar dat);void WriteSet1302(uchar Cmd,uchar dat);uchar Read1302(void);uchar ReadSet1302(uchar Cmd);void Init_DS1302(void);void DisplaySecond(uchar x);void DisplayMinute(uchar x);void DisplayHour(uchar x);void DisplayDay(uchar x);void DisplayMonth(uchar x);void DisplayYear(uchar x);void DisplayWeek(uchar x);void dis_temp(uint t);void read_date(void);void turn_val(char newval,uchar flag,uchar newaddr,uchar s1num);void key_scan(void);char code table[]="0123456789" ;uchar code table2[]= "TUEWESTHUFRISATSUNMON";uchar second,minute,hour,day,month,year,week,count=0; uchar ReadValue,num,time;void delayms(uint x){uint i,j;for(i=x;i>0;i--)for(j=110;j>0;j--);}////////////////////////////////////////////////////////////void write_com(uchar com){lcdrs=0;P2=com;delayms(5);lcden=1;delayms(5);lcden=0;}void write_data(uchar date){lcdrs=1;P2=date;delayms(5);lcden=1;delayms(5);lcden=0;}void init(){lcden=0;write_com(0x38);write_com(0x0c);write_com(0x06);write_com(0x01);}///////////////////////////////////////////////////////////////// void Write1302(uchar dat){uchar i;SCLK=0; //拉低SCLK,为脉冲上升沿写入数据做好准备delayms(2); //稍微等待,使硬件做好准备for(i=0;i<8;i++) //连续写8个二进制位数据{DATA=dat&0x01; //取出dat的第0位数据写入1302delayms(2); //稍微等待,使硬件做好准备SCLK=1; //上升沿写入数据delayms(2); //稍微等待,使硬件做好准备SCLK=0; //重新拉低SCLK,形成脉冲dat>>=1; //将dat的各数据位右移1位,准备写入下一个数据位 }}void WriteSet1302(uchar Cmd,uchar dat){RST=0; //禁止数据传递SCLK=0; //确保写数居前SCLK被拉低RST=1; //启动数据传输delayms(2); //稍微等待,使硬件做好准备Write1302(Cmd); //写入命令字Write1302(dat); //写数据SCLK=1; //将时钟电平置于已知状态RST=0; //禁止数据传递}uchar Read1302(void){uchar i,dat;delayms(2); //稍微等待,使硬件做好准备for(i=0;i<8;i++) //连续读8个二进制位数据{dat>>=1; //将dat的各数据位右移1位,因为先读出的是字节的最低位 if(DATA==1) //如果读出的数据是1dat|=0x80; //将1取出,写在dat的最高位SCLK=1; //将SCLK置于高电平,为下降沿读出delayms(2); //稍微等待SCLK=0; //拉低SCLK,形成脉冲下降沿delayms(2); //稍微等待}return dat; //将读出的数据返回}uchar ReadSet1302(uchar Cmd){uchar dat;RST=0; //拉低RSTSCLK=0; //确保写数居前SCLK被拉低RST=1; //启动数据传输Write1302(Cmd); //写入命令字dat=Read1302(); //读出数据SCLK=1; //将时钟电平置于已知状态RST=0; //禁止数据传递return dat; //将读出的数据返回}void Init_DS1302(void){WriteSet1302(0x8E,0x00); //根据写状态寄存器命令字,写入不保护指令WriteSet1302(0x80,((0/10)<<4|(0%10))); //根据写秒寄存器命令字,写入秒的初始值WriteSet1302(0x82,((59/10)<<4|(59%10))); //根据写分寄存器命令字,写入分的初始值WriteSet1302(0x84,((23/10)<<4|(23%10))); //根据写小时寄存器命令字,写入小时的初始值WriteSet1302(0x86,((28/10)<<4|(28%10))); //根据写日寄存器命令字,写入日的初始值WriteSet1302(0x88,((2/10)<<4|(2%10))); //根据写月寄存器命令字,写入月的初始值WriteSet1302(0x8c,((14/10)<<4|(14%10))); //nian//WriteSet1302(0x8a,((4/10)<<4|(4%10)));}/////////////////////////////////////////////////////////////////void DisplaySecond(uchar x){uchar i,j;i=x/10;j=x%10;write_com(0x80+0x46);write_data(i+0x30);write_com(0x80+0x47);write_data(j+0x30);write_com(0x80+0x48);write_data(' ');dis_temp(get_temp());}void DisSecond(uchar x){uchar i,j;ReadValue = ReadSet1302(0x81);second=((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F); i=x/10;j=x%10;write_com(0x80+0x46);write_data(i+0x30);write_com(0x80+0x47);write_data(j+0x30);}void DisplayMinute(uchar x){uchar i,j;i=x/10;j=x%10;write_com(0x80+0x43);write_data(i+0x30);write_com(0x80+0x44);write_data(j+0x30);write_com(0x80+0x45);write_data(':');}void DisplayHour(uchar x){uchar i,j;i=x/10;j=x%10;write_com(0x80+0x40);write_data(i+0x30);write_com(0x80+0x41);write_data(j+0x30);write_com(0x80+0x42);write_data(':');}void DisplayDay(uchar x){uchar i,j;i=x/10;j=x%10;write_com(0x89);write_data(i+0x30);write_com(0x8a);write_data(j+0x30); }void DisplayMonth(uchar x) {uchar i,j;i=x/10;j=x%10;write_com(0x86);write_data(i+0x30);write_com(0x87);write_data(j+0x30);write_com(0x88);write_data('/');}void DisplayYear(uchar x){uchar i,j;i=x/10;j=x%10;write_com(0x81);write_data(2+0x30);write_com(0x82);write_data(0+0x30);write_com(0x83);write_data(i+0x30);write_com(0x84);write_data(j+0x30);write_com(0x85);write_data('/');}void DisplayWeek(uchar x){ uchar i;x=x*3;// write_com(0x8c);write_data(table2[x]); // write_com(0x8d);write_data(table2[x+1]); // write_com(0x8e);write_data(table2[x+2]); write_com(0x8c);for(i=0;i<3;i++){write_data(table2[x]);x++;}}void read_date(void){ReadValue = ReadSet1302(0x81);second=((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F);ReadValue = ReadSet1302(0x83);minute=((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F);ReadValue = ReadSet1302(0x85);hour=((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F);ReadValue = ReadSet1302(0x87);day=((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F);ReadValue = ReadSet1302(0x89);month=((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F);ReadValue = ReadSet1302(0x8d);year=((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F);ReadValue=ReadSet1302(0x8b); //读星期 week=ReadValue&0x07;DisplayYear(year);DisplayMonth(month);DisplayDay(day);DisplayWeek(week);DisplayHour(hour);DisplayMinute(minute);DisplaySecond(second);dis_temp(get_temp()); ///温度显示key_scan(); ///按键检测}void turn_val(char newval,uchar flag,uchar newaddr,uchar s1num){newval=ReadSet1302(newaddr); //读取当前时间newval=((newval&0x70)>>4)*10+(newval&0x0f); //将bcd码转换成十进制if(flag) //判断是加一还是减一{newval++;switch(s1num){ case 1: if(newval>99) newval=0;DisplayYear(newval);break;case 2: if(newval>12) newval=1;DisplayMonth(newval);break;case 3: if(newval>31) newval=1;DisplayDay(newval);break;case 4: if(newval>6) newval=0;DisplayWeek(newval);break;case 5: if(newval>23) newval=0;DisplayHour(newval);break;case 6: if(newval>59) newval=0;DisplayMinute(newval);break;case 7: if(newval>59) newval=0;DisplaySecond(newval);break;default:break;}}else{newval--;switch(s1num){ case 1: if(newval==0) newval=99;DisplayYear(newval);break;case 2: if(newval==0) newval=12;DisplayMonth(newval);break;case 3: if(newval==0) newval=31;DisplayDay(newval);break;case 4: if(newval<0) newval=6;DisplayWeek(newval);break;case 5: if(newval<0) newval=23;DisplayHour(newval);break;case 6: if(newval<0) newval=59;DisplayMinute(newval);break;case 7: if(newval<0) newval=59;DisplaySecond(newval);break;default:break;}}WriteSet1302((newaddr-1),((newval/10)<<4)|(newval%10)); //将新数据写入寄存器}//////////////////////////////////////void dsreset(void){uint i;ds=0;i=103;while(i>0)i--;ds=1;i=4;while(i>0)i--;}bit tempreadbit(void){uint i;bit dat;ds=0;i++;ds=1;i++;i++;dat=ds;i=8;while(i>0)i--;return(dat);}uchar tempread(void){uchar i,j,dat;dat=0;for(i=1;i<=8;i++){j=tempreadbit();dat=(j<<7)|(dat>>1); }return(dat);}void tempwritebyte(uchar dat) {uint i;uchar j;bit testb;for(j=1;j<=8;j++){testb=dat&0x01;dat=dat>>1;if(testb){ds=0;i++;i++;ds=1;i=8;while(i>0)i--;}else{ds=0;i=8;while(i>0)i--;ds=1;i++;i++;}}}void tempchange(void){dsreset();delayms(1);tempwritebyte(0xcc);tempwritebyte(0x44);key_scan(); //////按键函数}uint get_temp(){uchar a,b;tempchange(); //////温度函数dsreset();delayms(1);tempwritebyte(0xcc);tempwritebyte(0xbe);a=tempread();b=tempread();temp=b;temp<<=8;temp=temp|a;f_temp=temp*0.0625;temp=f_temp*10+0.5;f_temp=f_temp+0.05;return temp;}//////void dis_temp(uint t){uchar n1,n2,n3;n1=t/100;n2=t%100/10;n3=t%100%10;DisSecond(second); ///秒显示 write_com(0x80+0x49);write_data(table[n1]);//delayms(5);write_com(0x80+0x4a);write_data(table[n2]);//delayms(5);write_com(0x80+0x4b);write_data('.');// delayms(5);write_com(0x80+0x4c);write_data(table[n3]);//delayms(5);write_com(0x80+0x4d);write_data('^');//delayms(5);write_com(0x80+0x4e);write_data('C');//delayms(5);DisSecond(second); ////秒显示}/*********************液晶显示*****************/void warn(uint s,uchar led){uchar i;i=s;// beep=0;P1=~(led);while(i--){dis_temp(get_temp());}// beep=1;P1=0xff;i=s;while(i--){dis_temp(get_temp());}}void deal(uint t){uchar i;if((t>warn_l2)&&(t<=warn_l1)){warn(40,0x01);}else if(t<=warn_l2){warn(10,0x03);}else if((t<warn_h2)&&(t>=warn_h1)) {warn(40,0x04);}else if(t>=warn_h2){warn(10,0x0c);}else{i=40;while(i--){dis_temp(get_temp());DisSecond(second);} }}///////////////////////////////////////void main(){init();Init_DS1302();while(1){tempchange();read_date();deal(temp);key_scan();}}////******************************************* void key_scan(void){// uchar miao,s1num=0;uchar s1num=0;if(menu==0){delayms(5);if(menu==0){while(!menu);s1num++;while(1){if(menu==0){delayms(5);if(menu==0){while(!menu);s1num++;}}// miao=ReadSet1302(0x81);// second=miao;// WriteSet1302(0x80,miao|0x80);write_com(0x0f);//光标闪射if(s1num==1){ //year=ReadSet1302(0x8d);write_com(0x80+4); //年光标if(add==0){delayms(3);if(add==0){ while(!add);turn_val(year,1,0x8d,1);}}if(dec==0){delayms(3);if(dec==0){ while(!dec);turn_val(year,0,0x8d,1);}}}if(s1num==2){//month=ReadSet1302(0x89);write_com(0x80+7); //月光标if(add==0){delayms(3);if(add==0){ while(!add);turn_val(month,1,0x89,2);}}if(dec==0){delayms(3);if(dec==0){ while(!dec);turn_val(month,0,0x89,2);}}}if(s1num==3){ //day=ReadSet1302(0x87);write_com(0x80+10);//日光标if(add==0){delayms(3);if(add==0){ while(!add);turn_val(day,1,0x87,3);}}if(dec==0){delayms(3);if(dec==0){ while(!dec);turn_val(day,0,0x87,3); //写入日寄存器}}}if(s1num==4){ //week=ReadSet1302(0x8b);write_com(0x80+14); //星期光标if(add==0){delayms(3);if(add==0){ while(!add);turn_val(week,1,0x8b,4);}if(dec==0){delayms(3);if(dec==0){ while(!dec);turn_val(week,0,0x8b,4);}}}if(s1num==5){// hour=ReadSet1302(0x85)write_com(0x80+0x40+1); //时光标if(add==0){delayms(3);if(add==0){ while(!add);turn_val(hour,1,0x85,5);}}if(dec==0){delayms(3);if(dec==0){ while(!dec);turn_val(hour,0,0x85,5);}}if(s1num==6)//调时间分{ // minute=ReadSet1302(0x83);write_com(0x80+0x40+4);if(add==0){delayms(5);if(add==0){ while(!add);turn_val(minute,1,0x83,6); //写入分寄存器}}if(dec==0){delayms(3);if(dec==0){ while(!dec);turn_val(minute,0,0x83,6); //写入分寄存器}}}if(s1num==7)//调时间秒{// second=ReadSet1302(0x81);write_com(0x80+0x40+7);//秒光标if(add==0){delayms(3);if(add==0){ while(!add);if(second==0x60)second=0x00;turn_val(second,1,0x81,7);}}if(dec==0){delayms(3);if(dec==0){ while(!dec);turn_val(second,0,0x81,7);}}}if(s1num==8){// miao=ReadSet1302(0x81);// second=miao;// WriteSet1302(0x80,second&0x7f);s1num=0;//s1num清零//write_com(0x0c);//光标不闪烁//break;}}} } }。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
{
LCD_Write(LCD_COMMAND, 0x04|InputMode);
}
//初始化LCD************************************************************
void LCD_Initial()
{
LcdEn=0;
LCD_Write(LCD_COMMAND,0x38); //8位数据端口,2行显示,5*7点阵
#define LCD_AC_UP0x02
#define LCD_AC_DOWN0x00 // default
#define LCD_MOVE0x01 //画面可平移
#define LCD_NO_MOVE0x00 //default
void LCD_SetInput(unsigned char InputMode)
sbit Set = P2^0; //模式切换键
sbit Up = P2^1; //加法按钮
sbit Down = P2^2; //减法按钮
sbit out = P2^3; //立刻跳出调整模式按钮
sbit DQ = P3^0;
char done,count,temp,flag,up_flag,down_flag,t_value;
}
Time->DateString[4] = '/';
if(hide_month<2)
{
Time->DateString[5] = Time->Month/10 + '0';
{ if(hide_year<2) //这里的if,else语句都是判断位闪烁,<2显示数据,>2就不显示,输出字符串为2007/07/22
{
Time->DateString[0] = '2';
Time->DateString[1] = '0';
Time->DateString[2] = Time->Year/10 + '0';
Time->DateString[3] = Time->Year%10 + '0';
}
else
{
Time->DateString[0] = ' ';
Time->DateString[1] = ' ';
Time->DateString[2] = ' ';
Time->DateString[3] = ' ';
sbit LcdRs= P2^5;
sbit LcdRw= P2^6;
sbit LcdEn= P2^7;
sfr DBPort= 0x80;//P0=0x80,P1=0x90,P2=0xA0,P3=0xB0.数据端口
//内部等待函数**************************************************************
#include <REG51.H>
#include <intrins.h>
//#include "LCD1602.h"
//#include "DS1302.h"
#define uint unsigned int
#define uchar unsigned char
sbit DS1302_CLK = P1^7; //实时时钟时钟线引脚
}
//液晶字符输入的位置************************
void GotoXY(unsigned char x, unsigned char y)
{
if(y==0)
LCD_Write(LCD_COMMAND,0x80|x);
if(y==1)
LCD_Write(LCD_COMMAND,0x80|(x-0x40));
unsigned char LCD_Wait(void)
{
LcdRs=0;
LcdRw=1;_nop_();
LcdEn=1;_nop_();
LcdEn=0;
return DBPort;
}
//向LCD写入命令或数据**************************************
#define LCD_COMMAND0 // Command
#define LCD_DATA1 // Data
#define LCD_CLEAR_SCREEN0x01 //清屏
#define LCD_HOMING0x02 //光标返回原点
void LCD_Write(bit style, unsigned char input)
{
LcdEn=0;
LcdRs=style;
LCD_Write(LCD_COMMAND,0x38);
LCD_SetDisplay(LCD_SHOW|LCD_NO_CURSOR); //开启显示,无光标
LCD_Write(LCD_COMMAND,LCD_CLEAR_SCREEN); //清屏
LCD_SetInput(LCD_AC_UP|LCD_NO_MOVE); //AC递增,画面不动
void LCD_SetDisplay(unsigned char DisplayMode)
{
LCD_Write(LCD_COMMAND, 0x08|DisplayMode);
}
//设置输入模式************************************************************
{
unsigned char ucData;
DS1302_RST = 0;
DS1302_CLK = 0;
DS1302_RST = 1;
DS1302InputByte(ucAddr|0x01); //地址,命令
ucData = DS1302OutputByte(); //读1Byte数据
DS1302_CLK = 1;
ReadValue = Read1302(DS1302_HOUR);
Time->Hour = ((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F);
ReadValue = Read1302(DS1302_DAY);
Time->Day = ((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F);
{
unsigned char Second;
unsigned char Minute;
unsigned char Hour;
unsigned char Week;
unsigned char Day;
unsigned char Month;
unsigned char Year;
unsigned char DateString[11];
{
unsigned char i;
ACC = d;
for(i=8; i>0; i--)
{
DS1302_IO = ACC0;//相当于汇编中的RRC
DS1302_CLK = 1;
DS1302_CLK = 0;
ACC = ACC >> 1;
}
}
unsigned char DS1302OutputByte(void)//实时时钟读取一字节(内部函数)
unsigned char TimeString[9];
}SYSTEMTIME;//定义的时间类型
#define AM(X)X
#define PM(X)(X+12)//转成24小时制
#define DS1302_SECOND0x80 //时钟芯片的寄存器位置,存放时间
#define DS1302_MINUTE0x82
ReadValue = Read1302(DS1302_WEEK);
Time->Week = ((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F);
ReadValue = Read1302(DS1302_MONTH);
Time->Month = ((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F);
#define DS1302_HOUR0x84
#define DS1302_WEEK0x8A
#define DS1302_DAY0x86
#define DS1302_MONTH0x88
#define DS1302_YEAR0x8C
void DS1302InputByte(unsigned char d)//实时时钟写入一字节(内部函数)
{
DS1302_RST = 0;
DS1302_CLK = 0;
DS1302_RST = 1;
DS1302InputByte(ucAddr);//地址,命令
DS1302InputByte(ucDa);//写1Byte数据
DS1302_CLK = 1;
DS1302_RST = 0;
}
unsigned char Read1302(unsigned char ucAddr)//读取DS1302某地址的数据
}
//将字符输出到液晶显示
void Print(unsigned char *str)
{
while(*str!='\0')
相关文档
最新文档