1、intrins.h/#include LCD1602.hDS1302.h#define uint unsigned int#define uchar unsigned charsbit DS1302_CLK = P17; /实时时钟时钟线引脚 sbit DS1302_IO = P16; /实时时钟数据线引脚 sbit DS1302_RST = P15; /实时时钟复位线引脚sbit wireless_1 = P30;sbit wireless_2 = P31;sbit wireless_3 = P32;sbit wireless_4 = P33;sbit ACC0 = ACC0;sbit AC
2、C7 = ACC7;char hide_sec,hide_min,hide_hour,hide_day,hide_week,hide_month,hide_year; /秒,分,时到日,月,年位闪的计数sbit Set = P20; /模式切换键sbit Up = P21; /加法按钮sbit Down = P22; /减法按钮sbit out = P23; /立刻跳出调整模式按钮sbit DQ = P10; /温度传送数据IO口char done,count,temp,flag,up_flag,down_flag;uchar temp_value; /温度值uchar TempBuffer5
3、,week_value2;void show_time(); /液晶显示程序/*1602液晶显示部分子程序*/Port Definitions*sbit LcdRs = P25;sbit LcdRw = P26;sbit LcdEn = P27;sfr DBPort = 0x80; /P0=0x80,P1=0x90,P2=0xA0,P3=0xB0.数据端口/内部等待函数*unsigned char LCD_Wait(void) LcdRs=0; LcdRw=1; _nop_(); LcdEn=1; _nop_(); LcdEn=0; return DBPort; /向LCD写入命令或数据*#d
4、efine LCD_COMMAND 0 / Command#define LCD_DATA 1 / Data#define LCD_CLEAR_SCREEN 0x01 / 清屏#define LCD_HOMING 0x02 / 光标返回原点void LCD_Write(bit style, unsigned char input) LcdRs=style; LcdRw=0; DBPort=input;/注意顺序 LCD_Wait(); /设置显示模式*#define LCD_SHOW 0x04 /显示开#define LCD_HIDE 0x00 /显示关 #define LCD_CURSOR
5、0x02 /显示光标#define LCD_NO_CURSOR 0x00 /无光标 #define LCD_FLASH 0x01 /光标闪动#define LCD_NO_FLASH 0x00 /光标不闪动void LCD_SetDisplay(unsigned char DisplayMode) LCD_Write(LCD_COMMAND, 0x08|DisplayMode);/设置输入模式*#define LCD_AC_UP 0x02#define LCD_AC_DOWN 0x00 / default#define LCD_MOVE 0x01 / 画面可平移#define LCD_NO_MO
6、VE 0x00 /defaultvoid LCD_SetInput(unsigned char InputMode) LCD_Write(LCD_COMMAND, 0x04|InputMode);/初始化LCD*void LCD_Initial() LCD_Write(LCD_COMMAND,0x38); /8位数据端口,2行显示,5*7点阵 LCD_SetDisplay(LCD_SHOW|LCD_NO_CURSOR); /开启显示, 无光标 LCD_Write(LCD_COMMAND,LCD_CLEAR_SCREEN); /清屏 LCD_SetInput(LCD_AC_UP|LCD_NO_M
7、OVE); /AC递增, 画面不动/液晶字符输入的位置*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);/将字符输出到液晶显示void Print(unsigned char *str) while(*str!=0) LCD_Write(LCD_DATA,*str); str+;/*DS1302时钟部分子程序*/typedef struct _SYSTEMTIME_ unsigned c
8、har Second; unsigned char Minute; unsigned char Hour; unsigned char Week; unsigned char Day; unsigned char Month; unsigned char Year; unsigned char DateString11; unsigned char TimeString9;SYSTEMTIME; /定义的时间类型SYSTEMTIME CurrentTime;#define AM(X) X#define PM(X) (X+12) / 转成24小时制#define DS1302_SECOND 0x
9、80 /时钟芯片的寄存器位置,存放时间#define DS1302_MINUTE 0x82#define DS1302_HOUR 0x84 #define DS1302_WEEK 0x8A#define DS1302_DAY 0x86#define DS1302_MONTH 0x88#define DS1302_YEAR 0x8C void DS1302InputByte(unsigned char d) /实时时钟写入一字节(内部函数) unsigned char i; ACC = d; for(i=8; i0; i-) DS1302_IO = ACC0; /相当于汇编中的 RRC DS13
10、02_CLK = 1; DS1302_CLK = 0; ACC = ACC 1;unsigned char DS1302OutputByte(void) /实时时钟读取一字节(内部函数)1; /相当于汇编中的 RRC ACC7 = DS1302_IO; return(ACC);void Write1302(unsigned char ucAddr, unsigned char ucDa) /ucAddr: DS1302地址, ucData: 要写的数据 DS1302_RST = 0; DS1302_RST = 1; DS1302InputByte(ucAddr); / 地址,命令 DS1302
11、InputByte(ucDa); / 写1Byte数据unsigned char Read1302(unsigned char ucAddr) /读取DS1302某地址的数据 unsigned char ucData; DS1302InputByte(ucAddr|0x01); / 地址,命令 ucData = DS1302OutputByte(); / 读1Byte数据 return(ucData);void DS1302_GetTime(SYSTEMTIME *Time) /获取时钟芯片的时钟数据到自定义的结构型数组 unsigned char ReadValue; ReadValue =
12、 Read1302(DS1302_SECOND); Time-Second = (ReadValue&0x70)4)*10 + (ReadValue&0x0F); ReadValue = Read1302(DS1302_MINUTE);Minute = (ReadValue& ReadValue = Read1302(DS1302_HOUR);Hour = (ReadValue& ReadValue = Read1302(DS1302_DAY);Day = (ReadValue& ReadValue = Read1302(DS1302_WEEK);Week = (ReadValue& Read
13、Value = Read1302(DS1302_MONTH);Month = (ReadValue& ReadValue = Read1302(DS1302_YEAR);Year = (ReadValue&void DateToStr(SYSTEMTIME *Time) /将时间年,月,日,星期数据转换成液晶显示字符串,放到数组里DateString if(hide_year2) /这里的if,else语句都是判断位闪烁,2就不显示,输出字符串为 2007/07/22DateString0 = 2;DateString1 = 0DateString2 = Time-Year/10 + Date
14、String3 = Time-Year%10 + else DateString2 = DateString3 = DateString4 = / if(hide_monthMonth/10 + DateString6 = Time-Month%10 + DateString5 = DateString6 = DateString7 = if(hide_dayDay/10 + DateString9 = Time-Day%10 + DateString8 = DateString9 = if(hide_weekWeek%10 + /星期的数据另外放到 week_value数组里,跟年,月,日的
15、分开存放,因为等一下要在最后显示 else week_value0 = week_value1 = DateString10 = /字符串末尾加 ,判断结束字符void TimeToStr(SYSTEMTIME *Time) /将时,分,秒数据转换成液晶显示字符放到数组 TimeString; if(hide_hourHour/10 + TimeString1 = Time-Hour%10 + TimeString0 = TimeString1 = TimeString2 = : if(hide_minMinute/10 + TimeString4 = Time-Minute%10 + Tim
16、eString3 = TimeString4 = TimeString5 = if(hide_secSecond/10 + TimeString7 = Time-Second%10 + TimeString6 = TimeString7 = void Initial_DS1302(void) /时钟芯片初始化 unsigned char Second=Read1302(DS1302_SECOND); if(Second&0x80) /判断时钟芯片是否关闭 Write1302(0x8e,0x00); /写入允许 Write1302(0x8c,0x07); /以下写入初始化时间 日期:07/07/
17、25.星期: 3. 时间: 23:59:55 Write1302(0x88,0x07); Write1302(0x86,0x25); Write1302(0x8a,0x07); Write1302(0x84,0x23); Write1302(0x82,0x59); Write1302(0x80,0x55); Write1302(0x8e,0x80); /禁止写入/*ds18b20子程序*/*ds18b20延迟子函数(晶振12MHz )*/ void delay_18B20(unsigned int i) while(i-);/*ds18b20初始化函数*/void Init_DS18B20(v
18、oid) unsigned char x=0; DQ = 1; /DQ复位 delay_18B20(8); /稍做延时 DQ = 0; /单片机将DQ拉低 delay_18B20(80); /精确延时 大于 480us /拉高总线 delay_18B20(14); x=DQ; /稍做延时后 如果x=0则初始化成功 x=1则初始化失败 delay_18B20(20);/*ds18b20读一个字节*/ unsigned char ReadOneChar(void) uchar i=0; uchar dat = 0; for (i=8;ii-) DQ = 0; / 给脉冲信号 dat=1; DQ =
19、 1; if(DQ) dat|=0x80; delay_18B20(4); return(dat);/*ds18b20写一个字节*/ void WriteOneChar(uchar dat) unsigned char i=0; for (i=8; DQ = 0; DQ = dat&0x01; delay_18B20(5); DQ = 1;/*读取ds18b20当前温度*/void ReadTemp(void) unsigned char a=0; unsigned char b=0; unsigned char t=0; Init_DS18B20(); WriteOneChar(0xCC);
20、 / 跳过读序号列号的操作 WriteOneChar(0x44); / 启动温度转换 delay_18B20(100); / this message is wery important /跳过读序号列号的操作 WriteOneChar(0xBE); /读取温度寄存器等(共可读9个寄存器) 前两个就是温度 a=ReadOneChar(); /读取温度值低位 b=ReadOneChar(); /读取温度值高位 temp_value=bvoid temp_to_str() /温度数据转换成液晶字符显示 TempBuffer0=temp_value/10+ /十位 TempBuffer1=temp_value%10+ /个位 TempBuffer2=0xdf; /温度符号 TempBuffer3=C TempBuffer4=void Delay1ms(unsigned int count) unsigned int i,j; for(i=0;icount;i+) for(j=0;jdelay-) for(i=0;62;i+) /1ms延时.void outkey() /跳出调整模式,返回默认显示 uchar Second; if(out=0|wireless_1=1) mdelay(8); count=0; hide_sec=0,hide_min=
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1