?? 帶農歷.c
字號:
/****************************************************************************/
//頭文件
#include <REG52.h>
#include <string.h>
#include <intrins.h>
#define uint unsigned int
#define uchar unsigned char
/*****************************************************************************/
sbit LCM_cs = P2^0;
sbit LCM_std = P2^1;
sbit LCM_sclk = P2^2;
sbit LCM_psb = P2^3; //H=并口; L=串口;
sbit LCM_rst = P2^7; //Reset Signal 低電平有效
sbit beep = P2^6;
/*****************************************************************************/
//溫度傳感器定義
sbit DQ = P2^ 4;//ds18B20
/*****************************************************************************/
//鍵盤引腳定義
sbit KEY_1 = P3^4; //左上
sbit KEY_2 = P3^5; //左下
sbit KEY_3 = P3^6; //右上
sbit KEY_4 = P3^7; //右下
//定義DS1302時鐘接口
sbit DS1302_CLK = P1^0; //實時時鐘時鐘線引腳
sbit DS1302_IO = P1^2; //實時時鐘數據線引腳
sbit DS1302_RST = P1^1; //實時時鐘復位線引腳/*****************************************************************************/
//定義累加器A中的各位
sbit ACC0 = ACC^0;
sbit ACC7 = ACC^7;
/****************************************************************************/
//定義全局變量
unsigned char yy,mo,dd,xq,hh,mm,ss,e;//定義時間映射全局變量(專用寄存器)
bit w = 0; //調時標志位
static unsigned char menu = 0;//定義靜態小時更新用數據變量
static unsigned char keys = 0;//定義靜態小時更新用數據變量
static unsigned char timecount = 0;//定義靜態軟件計數器變量
uchar code tab1[]={
" 電子萬年歷 "
" 版本: V1.0 "
"2008年03月10日夜"
" 朱雷 "
};
/****************************************************************************/
void DelayM(unsigned int a) //延時函數 1MS/次
{
unsigned char i;
while( --a != 0)
{
for(i = 0; i < 125; i++); //一個 ; 表示空語句,CPU空轉。
} //i 從0加到125,CPU大概就耗時1毫秒
}
/****************************************************************************/
//函數聲明
void LCM_WriteString(unsigned char *str) ; //寫字符 例:LCM_WriteString("您好!");
void LCM_WriteDatOrCom(bit ,unsigned char ); //(0,指令) (1,數據)
void LCM_w_byte(unsigned char bbyte); //寫一個字節給LCM
void delaynms(unsigned int di); //延時
void LCM_init(void); //LCM初始化
void LCM_clr(void); //清屏
void Delay(int num);
void nl();
void days (viod) //公歷節日數據庫
{
LCM_WriteDatOrCom(0,0x80);
// LCM_WriteString("[Dis] [Set]");
LCM_WriteDatOrCom(0,0x98);
// ----|----------------|
if ( mo == 0x01 && dd == 0x01 ){ LCM_WriteString("Happy New Year!!"); }//1月
if ( mo == 0x01 && dd == 0x28 ){ LCM_WriteString(" 世界麻風日 "); }
if ( mo == 0x02 && dd == 0x02 ){ LCM_WriteString(" 世界濕地日 "); }//2月
if ( mo == 0x02 && dd == 0x13 ){ LCM_WriteString(" 明天情人節了 "); }
if ( mo == 0x02 && dd == 0x14 ){ LCM_WriteString(" 今天是情人節 "); }
if ( mo == 0x03 && dd == 0x01 ){ LCM_WriteString(" 國際海豹日 "); }//3月
if ( mo == 0x03 && dd == 0x03 ){ LCM_WriteString(" 全國愛耳日 "); }
if ( mo == 0x03 && dd == 0x08 ){ LCM_WriteString(" 3.8婦女節 "); }
if ( mo == 0x03 && dd == 0x12 ){ LCM_WriteString(" 植樹節 "); }
if ( mo == 0x03 && dd == 0x14 ){ LCM_WriteString(" 國際警察日 "); }
if ( mo == 0x03 && dd == 0x15 ){ LCM_WriteString(" 消費者權益日 "); }
if ( mo == 0x03 && dd == 0x17 ){ LCM_WriteString(" 國際航海日 "); }
if ( mo == 0x03 && dd == 0x21 ){ LCM_WriteString(" 世界森林日 "); }
if ( mo == 0x03 && dd == 0x22 ){ LCM_WriteString(" 世界水日 "); }
if ( mo == 0x03 && dd == 0x23 ){ LCM_WriteString(" 世界氣象日 "); }
if ( mo == 0x03 && dd == 0x24 ){ LCM_WriteString("世界防治結核病日"); }
if ( mo == 0x04 && dd == 0x01 ){ LCM_WriteString("愚人節 小心上當"); }//4
if ( mo == 0x04 && dd == 0x07 ){ LCM_WriteString(" 世界衛生日 "); }
if ( mo == 0x04 && dd == 0x08 ){ LCM_WriteString(" 復活節 "); }
if ( mo == 0x04 && dd == 0x13 ){ LCM_WriteString(" 黑色星期五 "); }
if ( mo == 0x05 && dd == 0x01 ){ LCM_WriteString(" 勞動節 放假 "); }//5
if ( mo == 0x05 && dd == 0x04 ){ LCM_WriteString(" 青年節 "); }
if ( mo == 0x05 && dd == 0x08 ){ LCM_WriteString(" 世界紅十字日 "); }
if ( mo == 0x05 && dd == 0x12 ){ LCM_WriteString(" 國際護士節 "); }
if ( mo == 0x05 && dd == 0x05 ){ LCM_WriteString("近日注意母親節 "); }
if ( mo == 0x05 && dd == 0x15 ){ LCM_WriteString(" 國際家庭日 "); }
if ( mo == 0x05 && dd == 0x31 ){ LCM_WriteString(" 世界無煙日 "); }
if ( mo == 0x06 && dd == 0x01 ){ LCM_WriteString(" 國際兒童節 "); }//6
if ( mo == 0x06 && dd == 0x05 ){ LCM_WriteString(" 世界環境日 "); }
if ( mo == 0x06 && dd == 0x26 ){ LCM_WriteString(" 國際禁毒日 "); }
if ( mo == 0x06 && dd == 0x06 ){ LCM_WriteString(" 全國愛眼日 "); }
if ( mo == 0x06 && dd == 0x13 ){ LCM_WriteString("近日注意父親節 "); }
if ( mo == 0x06 && dd == 0x15 ){ LCM_WriteString("近日注意父親節 "); }
if ( mo == 0x07 && dd == 0x01 ){ LCM_WriteString("香港回歸記念日 "); }//7
if ( mo == 0x07 && dd == 0x07 ){ LCM_WriteString("抗日戰爭記念日 "); }
if ( mo == 0x07 && dd == 0x11 ){ LCM_WriteString(" 世界人口日 "); }
if ( mo == 0x08 && dd == 0x01 ){ LCM_WriteString(" 八一建軍節 "); }//8
if ( mo == 0x08 && dd == 0x08 ){ LCM_WriteString(" 中國男子節 "); }
if ( mo == 0x08 && dd == 0x15 ){ LCM_WriteString("抗戰勝利記念日 "); }
if ( mo == 0x09 && dd == 0x10 ){ LCM_WriteString(" 中國教師節 "); }//9
if ( mo == 0x09 && dd == 0x18 ){ LCM_WriteString("九·一八事變記念"); }
if ( mo == 0x09 && dd == 0x20 ){ LCM_WriteString(" 國際愛牙日 "); }
if ( mo == 0x09 && dd == 0x27 ){ LCM_WriteString(" 世界旅游日 "); }
if ( mo == 0x10 && dd == 0x01 ){ LCM_WriteString(" 中國國慶節 "); }//10
if ( mo == 0x10 && dd == 0x04 ){ LCM_WriteString(" 世界動物日 "); }
if ( mo == 0x10 && dd == 0x24 ){ LCM_WriteString(" 聯合國日 "); }
if ( mo == 0x10 && dd == 0x12 ){ LCM_WriteString("明天國際教師節 "); }
if ( mo == 0x10 && dd == 0x13 ){ LCM_WriteString(" 國際教師節 "); }
if ( mo == 0x11 && dd == 0x10 ){ LCM_WriteString(" 世界青年節 "); }//11
if ( mo == 0x11 && dd == 0x17 ){ LCM_WriteString(" 世界學生節 "); }
if ( mo == 0x12 && dd == 0x01 ){ LCM_WriteString(" 世界艾滋病日 "); }//12
if ( mo == 0x12 && dd == 0x23 ){ LCM_WriteString(" 明晚平安夜 "); }
if ( mo == 0x12 && dd == 0x24 ){ LCM_WriteString(" 今晚平安夜 "); }
if ( mo == 0x12 && dd == 0x25 ){ LCM_WriteString(" 圣誕快樂 "); }
if ( mo == 0x12 && dd == 0x31 ){ LCM_WriteString(" 明日新年 "); }
else{
if ( hh >= 0x04 && hh < 0x06 ){ LCM_WriteString("★★__▲▲__凌晨"); }
if ( hh >= 0x06 && hh < 0x08 ){ LCM_WriteString("☆○__▲△__早晨"); }
if ( hh >= 0x08 && hh < 0x12 ){ LCM_WriteString("__●__▲▲__上午"); }
if ( hh == 0x12) { LCM_WriteString("____▲●▲__中午"); }
if ( hh >= 0x13 && hh < 0x18 ){ LCM_WriteString("__▲▲__●__下午"); }
if ( hh >= 0x18 && hh < 0x22 ){ LCM_WriteString("△▲__●☆__晚上"); }
if ( hh >= 0x22 && hh <= 0x23 ){ LCM_WriteString("△▲__★☆__夜里"); }
if ( hh >= 0x00 && hh < 0x04 ){ LCM_WriteString("__★▲▲★__深夜"); }
}
}
/****************************************************************************/
//初始化LCM
void LCM_init(void)
{
LCM_rst=1;
LCM_psb=0;
LCM_WriteDatOrCom (0,0x30); /*30---基本指令動作*/
LCM_WriteDatOrCom (0,0x01); /*清屏,地址指針指向00H*/
Delay (100);
LCM_WriteDatOrCom (0,0x06); /*光標的移動方向*/
LCM_WriteDatOrCom(0,0x0c); /*開顯示,關游標*/
}
/******************************************************************************/
//寫指令或數據 (0,指令) (1,數據)
void LCM_WriteDatOrCom(bit dat_comm,uchar content)
{
uchar a,i,j;
Delay(50);
a=content;
LCM_cs=1;
LCM_sclk=0;
LCM_std=1;
for(i=0;i<5;i++)
{
LCM_sclk=1;
LCM_sclk=0;
}
LCM_std=0;
LCM_sclk=1;
LCM_sclk=0;
if(dat_comm)
LCM_std=1; //data
else
LCM_std=0; //command
LCM_sclk=1;
LCM_sclk=0;
LCM_std=0;
LCM_sclk=1;
LCM_sclk=0;
for(j=0;j<2;j++)
{
for(i=0;i<4;i++)
{
a=a<<1;
LCM_std=CY;
LCM_sclk=1;
LCM_sclk=0;
}
LCM_std=0;
for(i=0;i<4;i++)
{
LCM_sclk=1;
LCM_sclk=0;
}
}
}
/*********************************************************************************/
void chn_disp (uchar code *chn)
{
uchar i,j;
LCM_WriteDatOrCom (0,0x30);
LCM_WriteDatOrCom (0,0x80);
for (j=0;j<4;j++)
{
for (i=0;i<16;i++)
LCM_WriteDatOrCom (1,chn[j*16+i]);
}
}
/*****************************************************************************/
//清屏函數
void LCM_clr(void)
{
LCM_WriteDatOrCom (0,0x30);
LCM_WriteDatOrCom (0,0x01);
Delay (180);
}
/*****************************************************************************/
//向LCM發送一個字符串,長度64字符之內。
//應用:LCM_WriteString("您好!");
void LCM_WriteString(unsigned char *str)
{
while(*str != '\0')
{
LCM_WriteDatOrCom(1,*str++);
}
*str = 0;
}
/******************************************************************************/
void Delay(int num)//延時函數
{
while(num--);
}
/******************************************************************************/
void Init_DS18B20(void)//初始化ds1820
{
unsigned char x=0;
DQ = 1; //DQ復位
Delay(8); //稍做延時
DQ = 0; //單片機將DQ拉低
Delay(80); //精確延時 大于 480us
DQ = 1; //拉高總線
Delay(14);
x=DQ; //稍做延時后 如果x=0則初始化成功 x=1則初始化失敗
Delay(20);
}
/******************************************************************************/
unsigned char ReadOneChar(void)//讀一個字節
{
unsigned char i=0;
unsigned char dat = 0;
for (i=8;i>0;i--)
{
DQ = 0; // 給脈沖信號
dat>>=1;
DQ = 1; // 給脈沖信號
if(DQ)
dat|=0x80;
Delay(4);
}
return(dat);
}
/******************************************************************************/
void WriteOneChar(unsigned char dat)//寫一個字節
{
unsigned char i=0;
for (i=8; i>0; i--)
{
DQ = 0;
DQ = dat&0x01;
Delay(5);
DQ = 1;
dat>>=1;
}
}
/******************************************************************************/
unsigned int ReadTemperature(void)//讀取溫度
{
unsigned char a=0;
unsigned char b=0;
unsigned int t=0;
float tt=0;
Init_DS18B20();
WriteOneChar(0xCC); // 跳過讀序號列號的操作
WriteOneChar(0x44); // 啟動溫度轉換
Init_DS18B20();
WriteOneChar(0xCC); //跳過讀序號列號的操作
WriteOneChar(0xBE); //讀取溫度寄存器
a=ReadOneChar(); //讀低8位
b=ReadOneChar(); //讀高8位
t=b;
t<<=8;
t=t|a;
tt=t*0.0625;
t= tt*10+0.5; //放大10倍輸出并四舍五入
return(t);
}
/*****************************************************************************/
//聲明
void LCM_w_ss(void);
void LCM_w_mm(void);
void LCM_w_hh(void);
void LCM_w_dd(void);
void LCM_w_mo(void);
void LCM_w_yy(void);
void LCM_w_xq(void);
void DS1302InputByte(unsigned char d) ;
unsigned char DS1302OutputByte(void) ;
void Init_1302(void);
unsigned char Read1302(unsigned char);
void Set_time(unsigned char sel);
void Write1302(unsigned char , unsigned char );
void updata (void);
/*****************************************************************************/
/*****************************************************************************/
//調時用加1程序
void Set_time(unsigned char sel) //根據選擇調整的相應項目加1并寫入DS1302
{
signed char address,item;
signed char max,mini;
LCM_WriteDatOrCom(0,0x9a);
LCM_WriteString("調整");
if(sel==6) {LCM_WriteString("秒鐘");address=0x80; max=0;mini=0;} //秒7
if(sel==5) {LCM_WriteString("分鐘");address=0x82; max=59;mini=0;} //分鐘6
if(sel==4) {LCM_WriteString("小時");address=0x84; max=23;mini=0;} //小時5
if(sel==3) {LCM_WriteString("星期");address=0x8a; max=7;mini=1;} //星期4
if(sel==2) {LCM_WriteString("日期");address=0x86; max=31;mini=1;} //日3
if(sel==1) {LCM_WriteString("月份");address=0x88; max=12;mini=1;} //月2
if(sel==0) {LCM_WriteString("年份");address=0x8c; max=99; mini=0;} //年1
//讀取1302某地址上的數值轉換成10進制賦給item
item=((Read1302(address+1))/16)*10 + (Read1302(address+1))%16;
if(KEY_2 == 0)
{
item++;//數加 1
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -