#include <reg51.h>
sbit LCD_CLK = P1^0; /*LCD时钟线引脚 */
sbit LCD_SID = P1^1; /*LCD数据线引脚 */
sbit LCD_CS = P1^2; /*LCD片选线引脚 */
sbit ctrout = P1^3; /*控制输出*/
sbit T_RST = P1^4; /*实时时钟复位线引脚 */
sbit T_IO = P1^5; /*实时时钟数据线引脚 */
sbit T_CLK = P1^6; /*实时时钟时钟线引脚 */
sbit sw_1 = P3^0; /*开关1*/
sbit sw_2 = P3^1; /*开关2*/
sbit sw_3 = P3^2; /*开关3*/
sbit sw_4 = P3^3; /*开关4*/
sbit ACC0 = ACC^0;
sbit ACC7 = ACC^7;
sbit key_flag = PSW^5;
/*调用函数声明*/
void delay(); /*延时*/
void keydis(); /* 键盘处理*/
void write_GLCD(unsigned char dat); /*写数据到lcd*/
void chkbusy_GLCD(); /*检查lcd状态*/
void command_GLCD(unsigned char ord); /*往lcd写命令*/
void initial_GLCD(); /*初始化lcd*/
void RTInputByte(unsigned char); /*往DS1302写入1Byte数据*/
unsigned char RTOutputByte(void); /*从DS1302读取1Byte数据 */
void W1302(unsigned char, unsigned char); /*写数据到DS1302*/
unsigned char R1302(unsigned char); /*读DS1302数据*/
void Set1302(unsigned char *); /*设置日期、时间和汽车尾号*/
void Get1302(unsigned char curvalue[]); /*读取DS1302当前时间*/
void bcd_ds(unsigned char *dclock); /*数据格式转换*/
typedef struct
{
unsigned char hour;
unsigned char min;
unsigned char sec;
unsigned char date;
unsigned char month;
unsigned char today;
unsigned char year;
unsigned char number;
}x;
x value;
/*定义12232液晶屏所显示的日期和时间码*/
char code bcdmap[60][2]={
"00","01","02","03", "04","05","06","07","08","09"
,"10","11","12","13","14","15","16","17","18","19"
,"20","21","22","23","24","25","26","27","28","29"
,"30","31","32","33","34","35","36","37","38","39"
,"40","41","42","43","44","45","46","47","48","49"
,"50","51","52","53","54","55","56","57","58","59",
};
static char xdata DHZTAB1[15]="日期20XX:XX:XX";
static char xdata DHZTAB2[15]="时间 XX:XX:XX ";
static char xdata DHZTAB3[12]="今日不能开车";
unsigned char RClock[7];
void RTInputByte(unsigned char d)
{
unsigned char i;
ACC = d; /*d 写入的数据*/
for(i=8; i>0; i--)
{
T_IO = ACC0;
T_CLK = 1;
T_CLK = 0;
ACC = ACC >> 1;
}
}
unsigned char RTOutputByte(void)
{
unsigned char i;
for(i=8; i>0; i--)
{
ACC = ACC >>1;
ACC7 = T_IO;
T_CLK = 1;
T_CLK = 0;
}
return(ACC);
}
void W1302(unsigned char ucAddr, unsigned char ucDa)
{
T_RST = 0;
T_CLK = 0;
T_RST = 1;
RTInputByte(ucAddr); /* 地址,命令 */
RTInputByte(ucDa); /* 写1Byte数据*/
T_CLK = 1;
T_RST = 0;
}
uchar R1302(unsigned char ucAddr)
{
unsigned char ucData;
T_RST = 0;
T_CLK = 0;
T_RST = 1;
RTInputByte(ucAddr); /* 地址,命令 */
ucData = RTOutputByte(); /* 读1Byte数据 */
T_CLK = 1;
T_RST = 0;
return(ucData);
}
void Set1302(unsigned char *pClock)
{
unsigned char i;
unsigned char ucAddr = 0x80;
W1302(0x8e,0x00); /* 清除写保护位 */
for(i =7; i>0; i--)
{
W1302(ucAddr,*pClock);
pClock++;
ucAddr +=2;
}
W1302(0x8e,0x80); /* 控制命令,WP=1,写保护*/
}
void Get1302(unsigned char ucCurvalue[])
{
unsigned char i;
unsigned char ucAddr = 0x81;
for (i=0; i<7; i++)
{
ucCurvalue = R1302(ucAddr); /*格式为: 秒 分 时 日 月 星期 年 */
ucAddr += 2;
}
}
void bcd_ds(unsigned char *dclock)
{
unsigned char temp;
unsigned char m;
unsigned char n;
temp = *dclock;
n = temp&0x0f; /*获取秒值的低四位*/
m = temp&0xf0; /*获取秒值的高四位*/
m>>=4;
value.sec = m*10+n; /*把秒值转化成十进制的形式*/
dclock++;
temp = *dclock;
n = temp&0x0f; /*获取分钟值的低四位*/
m = temp&0xf0; /*获取分钟值的高四位*/
m>>=4;
value.min = m*10+n; /*把分钟值转化成十进制的形式*/
dclock++;
temp = *dclock;
n = temp&0x0f; /*获取小时值的低四位*/
m = temp&0xf0; /*获取小时值的高四位*/
m >>=4;
value.hour = m*10+n; /*把小时值转化成十进制的形式*/
dclock++;
temp = *dclock;
n = temp&0x0f; /*获取日的低四位*/
m = temp&0xf0; /*获取日的高四位*/
m>>=4;
value.date = m*10+n; /*把日值转化成十进制的形式*/
dclock++;
temp = *dclock;
n = temp&0x0f; /*获取月的低四位值*/
m = temp&0xf0; /*获取月的高四位值*/
m>>=4;
value.month= m*10+n; /*把月值转化成十进制的形式*/
dclock++;
temp = *dclock;
n = temp&0x0f;
value.today= n; /*获取星期的值*/
dclock++;
temp = *dclock;
n = temp&0x0f; /*获取年的低四位*/
m = temp&0xf0; /*获取年的高四位*/
m>>=4;
value.year = m*10+n; /*把年转化成十进制的形式*/
}
/****************主程序*******************/
main()
{
unsigned char i=0;
unsigned char j,temp;
key_flag = 0;
sw_2value=0;
initial_GLCD();
while(1)
{
Get1302(&RClock); /*获取当前日期时间值*/
keydis();
i=0;
limitweek=(value.number+value.month+3)mod 5 +1
if(limitweek==value.today) /*判断是否限行*/
{
if(sw_1==0)
ctrout=1; /*断开机动车起动开关线*/
else
ctrout=0;
command_GLCD(0x90); /*显示"今日不能开车"*/
for(j=0;j<7;j++)
{
write_GLCD(DHZTAB3);
i++;
write_GLCD(DHZTAB3);
i++;
}
i=0;
}
else
{
temp=0x10;
command_GLCD(temp|0x80);
bcd_ds(RClock); /*对得到的时间值进行转化*/
DHZTAB2[5]=bcdmap[value.hour][0]; /*小时的高位*/
DHZTAB2[6]=bcdmap[value.hour][1]; /*小时的低位*/
DHZTAB2[8]=bcdmap[value.min ][0]; /*分钟的高位*/
DHZTAB2[9]=bcdmap[value.min ][1]; /*分钟的低位*/
DHZTAB2[11]=bcdmap[value.sec][0]; /*秒的高位*/
DHZTAB2[12]=bcdmap[value.sec][1]; /*秒的低位*/
for(j=0;j<7;j++)
{
write_GLCD(DHZTAB1);
i++;
write_GLCD(DHZTAB1);
i++;
}
i=0;
temp=0x00;
command_GLCD(temp|0x80);
DHZTAB1[6]=bcdmap[value.year ][0]; /*年的高位*/
DHZTAB1[7]=bcdmap[value.year ][1]; /*年的低位*/
DHZTAB1[9]=bcdmap[value.month ][0]; /*月的高位*/
DHZTAB1[10]=bcdmap[value.month][1]; /*月的地位*/
DHZTAB1[12]=bcdmap[value.date ][0]; /*日的高位*/
DHZTAB1[13]=bcdmap[value.date ][1]; /*日的低位*/
for(j=0;j<7;j++)
{
write_GLCD(DHZTAB2);
i++;
write_GLCD(DHZTAB2);
i++;
}
}
}
}
用户377235 2014-3-17 12:36
按键输入 还是 dip开关输入啊?
用户377235 2014-3-17 12:35
按键输入 还是 dip开关输入啊?