1、单片机制作的12864液晶万年历C语言程序#include#include#include/DS18B20#define uchar unsigned char#define uint unsigned intsbit DQ = P1 0; / 定义DS18B20的单总线 #define Lcd_Bus P0sbit LCD_RS=P27; /数据/命令选择sbit LCD_RW=P26; /读写控制sbit LCD_EN=P25; /片选信号#define LCD_DATA P0 /8 bit数据线#define LOW 0#define HIGH 1/初始化指令#define CLEAR_
2、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,范围为
3、:00H3FH#define SET_DD_AC 0x80#define FUN_MODEK 0x36 /扩展指令集,8位指令#define X1address 0x80 /上半屏X轴起始地址#define X2address 0x88 /下半屏X轴起始地址#define Yaddress 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 h
4、anzishu=一二三四五六日;unsigned char TempData = 0; double DS18B20Data;uint jiemian=0;/DS1302sbit DS1302_RST = P14;sbit DS1302_IO = P13;sbit DS1302_SCK = P12;unsigned char table=0X00,0X18,0X0c,0X15,0X06,0X01,0X0a;/0s 24min 12hour 14ri 5yue 5zhou 2010nian unsigned char data table17;/uchar code table2=0x30,0x
5、31,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,0x2
6、e,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 = P37;sbit key_jia = P20;sbit key_jian = P21;sbit key_e
7、nter= P22;uchar key_number=0;/int nian,yue,ri,shi,fen;/红外sbit HW_IRQ = P3 2; / 定义38KHz一体化红外接收头与CPU的连接 unsigned char DT;static unsigned char data IRCode3,IRCON,IRCON2; /蜂鸣器sbit BEEP =P11; /* 函数名称:DS18B20_Delay()* 功 能:DS18B20软件延时专用* 入口参数:count 延时参数,值越大,延时越长* 出口参数:无*/void DS18B20_Delay(uint count) /延时函
8、数 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); / 精确延时,大于480us DQ = 1; / 拉高总线 DS18B20_Delay(14); / 稍做延时后 x = DQ; / 若x=0则初始化成功,若x=1则初始化失败
9、DS18B20_Delay(20); return (x);/* 函数名称:DS18B20_Read1Byte()* 功 能:从DS18B20读一个字节* 入口参数:无* 出口参数:从DS18B20读出的一个字节*/uchar DS18B20_Read1Byte(void) uchar i = 0; uchar dat = 0; for(i=8;i0;i-) DQ = 0; / 给脉冲信号 dat = 1; DQ = 1; / 给脉冲信号 if(DQ) dat |= 0x80; DS18B20_Delay(4); return (dat);/* 函数名称:DS18B20_Write1Byte(
10、)* 功 能:向DS18B20写一个字节* 入口参数:dat 要写入DS18B20的一字节数据* 出口参数:无*/void DS18B20_Write1Byte(uchar dat) uchar i = 0; for(i=8;i0;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
11、= 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
12、.0625; return t; /*/函数名称:delay(uint t)延时函数。对于12M晶振大约延时1ms/入口参数:t/出口参数:无/*void delay(uint t) uint i; while(t-) for(i=0;i125;i+) ; /蜂鸣器鸣叫void FM() BEEP=0; delay(100); BEEP=1;/*/函数名称:write_cmd(uint lcd_cmd)写指令/入口参数:lcd_cmd/返回参数:无/*void WriteCmd12864(uchar lcd_cmd) LCD_RS=LOW; LCD_RW=LOW; LCD_EN=HIGH; L
13、CD_DATA=lcd_cmd; LCD_EN=LOW; /*/函数名称:write_data(uint lcd_data)写数据/入口参数:current_data/返回参数:无/*void WriteData12864(uchar lcd_data) LCD_RS=HIGH; LCD_RW=LOW; LCD_EN=HIGH; LCD_DATA=lcd_data; LCD_EN=LOW;/*函数名称:Write12864(uint)写数据入口参数:cmd,data返回参数:无*/void Write12864(uchar lcd_cmd,uchar lcd_data) WriteCmd128
14、64(lcd_cmd); WriteData12864(lcd_data);/*/函数名称:ReadBusy()判忙/入口参数:无/返回参数:1忙;0不忙/*void ReadBusy(void) uchar i=5; while(i-);/*函数名称:LcdInit()初始化入口参数:无返回参数:无*/void LcdInit() delay(50); WriteCmd12864(FUN_MODE); delay(1); /WriteCmd12864(FUN_MODE); /delay(1); WriteCmd12864(DISPLAY_ON); delay(1); /WriteCmd128
15、64(AC_INIT); /delay(1); WriteCmd12864(CLEAR_SCREEN); delay(20); WriteCmd12864(CURSE_ADD); delay(1);/*函数名称:LcdClear()清屏入口参数:无返回参数:无*/void LcdClear(void) ReadBusy(); WriteCmd12864(CLEAR_SCREEN); delay(3);void write_date(uchar date) LCD_RS=1; LCD_RW=0; LCD_EN=0; P0=date; delay(5); LCD_EN=1; delay(5); L
16、CD_EN=0;void input1byte(unsigned char date) unsigned char i; for(i=0;i=1; unsigned char read1byte() unsigned char i,date=0; for(i=0;i=1; DS1302_SCK=1; DS1302_SCK=0; DS1302_IO=1; if(DS1302_IO)date=date|0x80; return date;void write1302(unsigned char address,unsigned char date) DS1302_RST=0; DS1302_SCK
17、=0; DS1302_RST=1; input1byte(address); input1byte(date); DS1302_RST=0; DS1302_SCK=1;unsigned char read1302(unsigned char address) unsigned char ucdat; DS1302_RST=0; DS1302_SCK=0; DS1302_RST=1; input1byte(address); ucdat=read1byte(); DS1302_RST=0; DS1302_SCK=1; return ucdat;void set1302(unsigned char
18、 address,unsigned char table,unsigned char n) unsigned char i; write1302(0x8e,0x00); for(i=0;in;i+) write1302(address,tablei); address+=2; write1302(0x8e,0x80);void read1302xulie(unsigned char address,unsigned char n) unsigned char i; for(i=0;in;i+) table1i=read1302(address); address+=2; void init13
19、02() DS1302_RST=0; DS1302_SCK=0; write1302(0x8e,0x00); /取消写保护 write1302(0x90,0x00); /清零涓流充电寄存器void chuli() unsigned char aa,bb,i; for(i=0;i7;i+) aa=tablei/10; bb=tablei%10; tablei=(aa4; table1z=bb*10+aa;void jiashijian() switch(key_number) case 0: break; case 1: table16+; /if(table16=10)table16=5; w
20、rite1302(0x8e,0x00); write1302(0x8c,table6); write1302(0x8e,0x80); _nop_(); _nop_();break; case 2: table14+; if(table14=13)table14=1; write1302(0x8e,0x00); write1302(0x88,table14); write1302(0x8e,0x80); _nop_(); _nop_();break; case 3: table13+; if(table13=31)table13=1; write1302(0x8e,0x00); write130
21、2(0x86,table13); write1302(0x8e,0x80); _nop_(); _nop_();break; case 4: table15+; if(table15=8)table15=1; write1302(0x8e,0x00); write1302(0x8a,table15); write1302(0x8e,0x80); _nop_(); _nop_();break; case 5: table12+; if(table12=24)table12=0; write1302(0x8e,0x00); write1302(0x84,table12); write1302(0x8e,0x80); _nop_(); _nop_();break; case 6: table11+; if(table11=61)table11=0; write1302(0x8e,0x00); write1302(0x82,table11); table10=0; write1302(0x80,0); write1302(0x8e,0x80); _nop_(); _nop_();break; void jianshijian() switch(key_number) case 0: b
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1