jiacaijt 对这个问题的回答完全搞错了方向。程序不是专业做液晶屏的工厂用的,更不是伍大液晶屏工厂返携写的。液晶屏工厂是生产液晶显示器件的。在电子钟这个产品中只做屏甚至驱动部分都不做,更别说主控部分了,我就是专业液晶屏生产厂腔世竖的。顶层文件 万年历.C #include<reg51.h>#include "LCD1602.h" #include "DS1302.h" #define uchar unsigned char #define uint unsigned int sbit speaker=P2^4bit key_flag1=0,key_flag2=0SYSTEMTIME adjusteduchar sec_add=0,min_add=0,hou_add=0,day_add=0,mon_add=0,yea_add=0uchar data_alarm[7]={0}/************键盘控明辩制******************************/ int key_scan() //扫描是否有键按下 { int i=0uint tempP1=0xf0temp=P1if(temp!=0xf0) i=1else i=0return i} uchar key_value() //激晌缺确定按键的值 { uint m=0,n=0,tempuchar valueuchar v[4][3]={'2','1','0','5','4','3','8','7','6','b'谨银,'a','9'} P1=0xfetemp=P1if(temp!=0xfe)m=0P1=0xfdtemp=P1 if(temp!=0xfd)m=1P1=0xfbtemp=P1 if(temp!=0xfb)m=2P1=0xf7temp=P1 if(temp!=0xf7)m=3P1=0xeftemp=P1 if(temp!=0xef)n=0P1=0xdftemp=P1 if(temp!=0xdf)n=1P1=0xbftemp=P1 if(temp!=0xbf)n=2value=v[m][n]return value} /***************************设置闹铃
函数*******************************/ void naoling(void) { uchar i=0,l=0,jinit1602()while(key_flag2&&i<12) if(key_scan()){j=key_value()write_data(j)if(i%2==0)data_alarm[l]=(j-'0')*10else {data_alarm[l]+=(j-'0')l++}i++delay(600)} write_com(0x01)} uchar according(void) { uchar kif(data_alarm[0]==adjusted.Year&&data_alarm[1]==adjusted.Month&&data_alarm[2]==adjusted.Day&&data_alarm[3]==adjusted.Hour&&data_alarm[4]==adjusted.Minute&&data_alarm[5]==adjusted.Second) k=1else k=0return k} void speak(void) {uint i=50while(i) {speaker=0delay(1)speaker=1delay(1)i--} } void alarm(void) {uint i=10while(i) { speak()delay(10)i--} } /**************************修改时间 *** 作********************************/ void reset(void) { sec_add=0min_add=0hou_add=0day_add=0mon_add=0yea_add=0 } void adjust(void) { if(key_scan()&&key_flag1) switch(key_value()) {case '0':sec_add++breakcase '1':min_add++breakcase '2':hou_add++breakcase '3':day_add++breakcase '4':mon_add++breakcase '5':yea_add++breakcase 'b':reset()breakdefault: break} adjusted.Second+=sec_addadjusted.Minute+=min_addadjusted.Hour+=hou_addadjusted.Day+=day_addadjusted.Month+=mon_addadjusted.Year+=yea_addif(adjusted.Second>59) adjusted.Second=adjusted.Second%60if(adjusted.Minute>59) adjusted.Minute=adjusted.Minute%60if(adjusted.Hour>23) adjusted.Hour=adjusted.Hour%24if(adjusted.Day>31) adjusted.Day=adjusted.Day%31if(adjusted.Month>12) adjusted.Month=adjusted.Month%12if(adjusted.Year>100) adjusted.Year=adjusted.Year%100} /**************************中断处理函数*********************************/ void changing(void) interrupt 0 using 0 //需要修改时间和日期,或者停止修改 { if(key_flag1)key_flag1=0else key_flag1=1} void alarming(void) interrupt 3 using 0 //需要设置闹铃或者停止设置 { if(key_flag2)key_flag2=0else key_flag2=1} /********************************主函数***********************************/ main() {uint iuchar *luchar p1[]="D:",p2[]="T:"SYSTEMTIME TEA=1EX0=1IT0=1EA=1EX1=1IT1=1init1602()Initial_DS1302() while(1) { write_com(0x80)write_string(p1,2)write_com(0xc0)write_string(p2,2)DS1302_GetTime(&T) adjusted.Second=T.Secondadjusted.Minute=T.Minuteadjusted.Hour=T.Houradjusted.Week=T.Weekadjusted.Day=T.Dayadjusted.Month=T.Monthadjusted.Year=T.Yearfor(i=0i<9i++) { adjusted.DateString[i]=T.DateString[i]adjusted.TimeString[i]=T.TimeString[i]} adjust()if(key_flag2)naoling()if(according())alarm()DateToStr(&adjusted)TimeToStr(&adjusted)write_com(0x82)write_string(adjusted.DateString,8)write_com(0xc2)write_string(adjusted.TimeString,8)delay(10)} (二)头文件1 显示模块 LCD1602.H #ifndef LCD_CHAR_1602_2009_5_9 #define LCD_CHAR_1602_2009_5_9 #define uchar unsigned char #define uint unsigned int sbit lcdrs = P2^0sbit lcdrw = P2^1sbit lcden = P2^2void delay(uint z) // 延时 { uint x,yfor(x=zx>0x--) for(y=110y>0y--)} void write_com(uchar com) // 写入指令数据到 lcd { lcdrw=0lcdrs=0P0=comdelay(5)lcden=1delay(5)lcden=0} void write_data(uchar date) // 写入字符显示数据到 lcd { lcdrw=0lcdrs=1P0=datedelay(5)lcden=1delay(5)lcden=0} void init1602() // 初始化设定 { lcdrw=0lcden=0write_com(0x3C)write_com(0x0c)write_com(0x06)write_com(0x01)write_com(0x80)} void write_string(uchar *pp,uint n) { int ifor(i=0i<ni++) write_data(pp[i])} #endif (三)头文件2
时钟模块 DS1302.H #ifndef _REAL_TIMER_DS1302_2009_5_20_ #define _REAL_TIMER_DS1302_2003_5_20_ sbit DS1302_CLK = P2^6//实时时钟时钟线引脚 sbit DS1302_IO = P2^7//实时时钟数据线引脚 sbit DS1302_RST = P2^5//实时时钟复位线引脚 sbit ACC0 = ACC^0sbit ACC7 = ACC^7typedef struct SYSTEM_TIME { unsigned char Secondunsigned char Minuteunsigned char Hourunsigned char Weekunsigned char Dayunsigned char Monthunsigned char Yearunsigned char DateString[9]//用这两个字符串来放置读取的时间 unsigned char TimeString[9]}SYSTEMTIME//定义的时间类型 #define AM(X) X #define PM(X) (X+12) // 转成24小时制 #define DS1302_SECOND 0x80 #define DS1302_MINUTE 0x82 #define DS1302_HOUR 0x84 #define DS1302_WEEK 0x8A #define DS1302_DAY 0x86 #define DS1302_MONTH 0x88 #define DS1302_YEAR 0x8C #define DS1302_RAM(X) (0xC0+(X)*2) //用于计算 DS1302_RAM 地址的宏 /******内部指令**********/ void DS1302InputByte(unsigned char d) //实时时钟写入一字节(内部函数) { unsigned char iACC = dfor(i=8i>0i--) { DS1302_IO = ACC0DS1302_CLK = 1DS1302_CLK = 0ACC = ACC >>1//因为在前面已经定义了ACC0 = ACC^0以便再次利用DS1302_IO = ACC0} } unsigned char DS1302OutputByte(void) //实时时钟读取一字节(内部函数) { unsigned char ifor(i=8i>0i--) { ACC = ACC >>1ACC7 = DS1302_IODS1302_CLK = 1DS1302_CLK = 0} return(ACC)} /********************************/ void Write1302(unsigned char ucAddr, unsigned char ucDa) //ucAddr: DS1302地址, ucData: 要写的数据 { DS1302_RST = 0DS1302_CLK = 0DS1302_RST = 1DS1302InputByte(ucAddr)// 地址,命令 DS1302InputByte(ucDa)// 写1Byte数据 DS1302_CLK = 1DS1302_RST = 0} unsigned char Read1302(unsigned char ucAddr) //读取DS1302某地址的数据 { unsigned char ucDataDS1302_RST = 0DS1302_CLK = 0DS1302_RST = 1DS1302InputByte(ucAddr|0x01)// 地址,命令 ucData = DS1302OutputByte()// 读1Byte数据 DS1302_CLK = 1DS1302_RST = 0return(ucData)} void DS1302_SetProtect(bit flag) //是否写保护 { if(flag) Write1302(0x8E,0x10)else Write1302(0x8E,0x00)} void DS1302_SetTime(unsigned char Address, unsigned char Value) // 设置时间函数 { DS1302_SetProtect(0)Write1302(Address, ((Value/10)<<4 | (Value%10)))//将十进制数转换为BCD码 } //在DS1302中的与日历、时钟相关的寄存器存放的数据必须为BCD码形式 void DS1302_GetTime(SYSTEMTIME *Time) { unsigned char ReadValueReadValue = Read1302(DS1302_SECOND)Time->Second = ((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F)//将BCD码转换为十进制数 ReadValue = Read1302(DS1302_MINUTE)Time->Minute = ((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F)ReadValue = Read1302(DS1302_HOUR)Time->Hour = ((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F)ReadValue = Read1302(DS1302_DAY)Time->Day = ((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F)ReadValue = Read1302(DS1302_WEEK)Time->Week = ((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F)ReadValue = Read1302(DS1302_MONTH)Time->Month = ((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F)ReadValue = Read1302(DS1302_YEAR)Time->Year = ((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F)} unsigned char *DataToBCD(SYSTEMTIME *Time) { unsigned char D[8]D[0]=Time->Second/10<<4+Time->Second%10D[1]=Time->Minute/10<<4+Time->Minute%10D[2]=Time->Hour/10<<4+Time->Hour%10D[3]=Time->Day/10<<4+Time->Day%10D[4]=Time->Month/10<<4+Time->Month%10D[5]=Time->Week/10<<4+Time->Week%10D[6]=Time->Year/10<<4+Time->Year%10return D} void DateToStr(SYSTEMTIME *Time) { //将十进制数转换为液晶显示的ASCII值 Time->DateString[0] = Time->Year/10 + '0'Time->DateString[1] = Time->Year%10 + '0'Time->DateString[2] = '-'Time->DateString[3] = Time->Month/10 + '0'Time->DateString[4] = Time->Month%10 + '0'Time->DateString[5] = '-'Time->DateString[6] = Time->Day/10 + '0'Time->DateString[7] = Time->Day%10 + '0'Time->DateString[8] = '\0'} void TimeToStr(SYSTEMTIME *Time) { //将十进制数转换为液晶显示的ASCII值 Time->TimeString[0] = Time->Hour/10 + '0'Time->TimeString[1] = Time->Hour%10 + '0'Time->TimeString[2] = ':'Time->TimeString[3] = Time->Minute/10 + '0'Time->TimeString[4] = Time->Minute%10 + '0'Time->TimeString[5] = ':'Time->TimeString[6] = Time->Second/10 + '0'Time->TimeString[7] = Time->Second%10 + '0'Time->DateString[8] = '\0'} void Initial_DS1302(void) { unsigned char SecondSecond=Read1302(DS1302_SECOND)if(Second&0x80) //初始化时间 DS1302_SetTime(DS1302_SECOND,0)} void DS1302_TimeStop(bit flag) // 是否将时钟停止 { unsigned char DataData=Read1302(DS1302_SECOND)DS1302_SetProtect(0)if(flag) Write1302(DS1302_SECOND, Data|0x80)else Write1302(DS1302_SECOND, Data&0x7F)} #endifzftzbpujjj1134358763,jaiezwwysu6517560870,2012-2-15 11:08:28哈哈,你有福了 我也想了好久这程序 这不 刚写好 研究研究
#include <reg52.h>
#include <intrins.h>
#include <string.h>
#define uint unsigned int
#define uchar unsigned char
sbit IO = P1^0
sbit SCLK = P1^1
sbit RST = P1^2
sbit RS = P2^0
sbit RW = P2^1
sbit EN = P2^2
sbit KEY1=P3^4
sbit KEY2=P3^5
sbit KEY3=P3^6
uchar *WEEK[]={"SUN","***","MON","TUS","WEN","THU","FRI","SAT"}
uchar LCD_DSY_BUFFER1[]={"DATE 00-00-00"}
uchar LCD_DSY_BUFFER2[]={"TIME 00:00:00"}
uchar DateTime[7]//秒,分,时,日,月,周,年源档
uchar flag,flag_1,i,miao,fen,shi,ri,yue,zhou,nian
void DelayMS(uint ms)
{
uchar i
while(ms--)
{
for(i=0i<120i++)
}
}
/*****************************************************/
//函数名称: Write_A_Byte_TO_DS1302(uchar x)
//函数功能:向1302写入一个字节
//入口参数: x
//出口参数:无
//调用子程序:无
/*****************************************************/
void Write_A_Byte_TO_DS1302(uchar x)
{
uchar i
for(i=0i<8i++)
{
IO=x&0x01
SCLK=1
SCLK=0
x>>=1
}
}
void Write_DS1302(uchar add,uchar num)
{
SCLK=0
RST=0
RST=1
Write_A_Byte_TO_DS1302(add)
Write_A_Byte_TO_DS1302(num)
RST=0
SCLK=1
}
/*****************************************************/
//函数名称:Get_A_Byte_FROM_DS1302()
//函数功能:从1302读做态一个字节
//入口参数: 无
//出口参数:b/16*10+b%16
//调用子程序:无
/*****************************************************/
uchar Get_A_Byte_FROM_DS1302()
{
uchar i,b=0x00
for(i=0i<8i++)
{
b |= _crol_((uchar)IO,i)
SCLK=1SCLK=0
}
return b/16*10+b%16
}
/*****************************************************/
//函数名称:Read_Data(uchar addr)
//雹胡乱函数功能:指定位置读数据
//入口参数: addr
//出口参数:dat
//调用子程序:Write_Abyte_1302(addr)
/*****************************************************/
uchar Read_Data(uchar addr)
{
uchar dat
RST = 0SCLK=0RST=1
Write_A_Byte_TO_DS1302(addr)
dat = Get_A_Byte_FROM_DS1302()
SCLK=1RST=0
return dat
}
/*****************************************************/
//函数名称:GetTime()
//函数功能:读取时间
//入口参数: 无
//出口参数:无
//调用子程序:无
/*****************************************************/
void GetTime()
{
uchar i,addr = 0x81
for(i=0i<7i++)
{
DateTime[i]=Read_Data(addr)addr+=2
}
}
uchar Read_LCD_State()
{
uchar state
RS=0RW=1EN=1DelayMS(1)
state=P0
EN = 0DelayMS(1)
return state
}
void LCD_Busy_Wait()
{
while((Read_LCD_State()&0x80)==0x80)
DelayMS(5)
}
void Write_LCD_Data(uchar dat) //写数据到1602
{
LCD_Busy_Wait()
RS=1RW=0EN=0P0=datEN=1DelayMS(1)EN=0
}
void Write_LCD_Command(uchar cmd)//写命令
{
LCD_Busy_Wait()
RS=0RW=0EN=0P0=cmdEN=1DelayMS(1)EN=0
}
void Init_LCD() //1602 初始化
{
Write_LCD_Command(0x38)
DelayMS(1)
Write_LCD_Command(0x01)
DelayMS(1)
Write_LCD_Command(0x06)
DelayMS(1)
Write_LCD_Command(0x0c)
DelayMS(1)
}
void Set_LCD_POS(uchar p)
{
Write_LCD_Command(p|0x80)
}
void Display_LCD_String(uchar p,uchar *s) //1602显示
{
uchar i
Set_LCD_POS(p)
for(i=0i<16i++)
{
Write_LCD_Data(s[i])
DelayMS(1)
}
}
void write_com(uchar com)
{
RS=0
P0=com
DelayMS(5)
EN=1
DelayMS(5)
EN=0
}
void write_date(uchar date)
{
RS=1
P0=date
DelayMS(5)
EN=1
DelayMS(5)
EN=0
}
void display(uchar add,uchar date)
{
uchar shi,ge
shi=date/10
ge=date%10
write_com(0x80+0x40+add)
write_date(0x30+shi)
write_date(0x30+ge)
}
void display1(uchar add,uchar date)
{
uchar shi,ge
shi=date/10
ge=date%10
write_com(0x80+add)
write_date(0x30+shi)
write_date(0x30+ge)
}
void Format_DateTime(uchar d,uchar *a)
{
a[0]=d/10+'0'
a[1]=d%10+'0'
}
uchar ZH(uchar dat)
{
uchar tmp
tmp=dat/10
dat=dat%10
dat=dat+tmp*16
return dat
}
void Keyscan()
{
flag_1=1
while(flag_1)
{
if(KEY1==0)
{
DelayMS(5)
while(!KEY1)
flag=(flag+1)%8
switch(flag)
{
case(1): Write_LCD_Command(0x0f)
Write_LCD_Command(0x80+0x40+11)
break
case(2): Write_LCD_Command(0x80+0x40+8)
break
case(3): Write_LCD_Command(0x80+0x40+5)
break
case(4): Write_LCD_Command(0x80+13)
break
case(5): Write_LCD_Command(0x80+11)
break
case(6): Write_LCD_Command(0x80+8)
break
case(7): Write_LCD_Command(0x80+5)
break
case(0): flag_1=0
Write_LCD_Command(0x0c)
//miao
Write_DS1302(0x8e,0x00)
Write_DS1302(0x80,ZH(DateTime[0]))
Write_DS1302(0x8e,0x80)
//fen
Write_DS1302(0x8e,0x00)
Write_DS1302(0x82,ZH(DateTime[1]))
Write_DS1302(0x8e,0x80)
//shi
Write_DS1302(0x8e,0x00)
Write_DS1302(0x84,ZH(DateTime[2]))
Write_DS1302(0x8e,0x80)
//ri
Write_DS1302(0x8e,0x00)
Write_DS1302(0x86,ZH(DateTime[3]))
Write_DS1302(0x8e,0x80)
// yue
Write_DS1302(0x8e,0x00)
Write_DS1302(0x88,ZH(DateTime[4]))
Write_DS1302(0x8e,0x80)
//nian
Write_DS1302(0x8e,0x00)
Write_DS1302(0x8c,ZH(DateTime[6]))
Write_DS1302(0x8e,0x80)
break
}
}
if(flag!=0)
{
if(KEY2==0)
{
DelayMS(5)
if(KEY2==0)
while(!KEY2)
if(flag==1) //miao
{
DateTime[0]++
if(DateTime[0]==60)
DateTime[0]=0
write_com(0x80+0x40+11)
display(11,DateTime[0])
}
if(flag==2) //fen
{
DateTime[1]++
if(DateTime[1]==60)
DateTime[1]=0
write_com(0x80+0x40+8)
display(8,DateTime[1])
}
if(flag==3) //shi
{
DateTime[2]++
if(DateTime[2]==24)
DateTime[2]=0
write_com(0x80+0x40+5)
display(5,DateTime[2])
}
/* if(flag==4)//zhou
{
DateTime[0]++
if(DateTime[0]==60)
DateTime[0]=0
write_com(0x80+0x40+11)
display(11,DateTime[0])
} */
if(flag==5) // ri
{
DateTime[3]++
if(DateTime[3]==30)
DateTime[3]=0
write_com(0x80+11)
display1(11,DateTime[3])
}
if(flag==6) //yue
{
DateTime[4]++
if(DateTime[4]==13)
DateTime[4]=0
write_com(0x80+8)
display1(8,DateTime[4])
}
if(flag==7)//nian
{
DateTime[6]++
if(DateTime[6]==100)
DateTime[6]=0
write_com(0x80+5)
display1(5,DateTime[6])
}
}
}
if(flag!=0)
{
if(KEY3==0)
{
DelayMS(5)
if(KEY3==0)
while(!KEY3)
if(flag==1) //miao
{
DateTime[0]--
if(DateTime[0]==-1)
DateTime[0]=0
write_com(0x80+0x40+11)
display(11,DateTime[0])
}
if(flag==2) //fen
{
DateTime[1]--
if(DateTime[1]==-1)
DateTime[1]=0
write_com(0x80+0x40+8)
display(8,DateTime[1])
}
if(flag==3) //shi
{
DateTime[2]--
if(DateTime[2]==-1)
DateTime[2]=0
write_com(0x80+0x40+5)
display(5,DateTime[2])
}
/* if(flag==4)//zhou
{
DateTime[0]++
if(DateTime[0]==60)
DateTime[0]=0
write_com(0x80+0x40+11)
display(11,DateTime[0])
} */
if(flag==5) // ri
{
DateTime[3]--
if(DateTime[3]==-1)
DateTime[3]=0
write_com(0x80+11)
display1(11,DateTime[3])
}
if(flag==6) //yue
{
DateTime[4]--
if(DateTime[4]==-1)
DateTime[4]=0
write_com(0x80+8)
display1(8,DateTime[4])
}
if(flag==7)//nian
{
DateTime[6]--
if(DateTime[6]==-1)
DateTime[6]=0
write_com(0x80+5)
display1(5,DateTime[6])
}
}
}
}
}
void main()
{
Init_LCD()
while(1)
{
EA=1
EX0=1
GetTime()
Format_DateTime(DateTime[6],LCD_DSY_BUFFER1+5) //年
Format_DateTime(DateTime[4],LCD_DSY_BUFFER1+8) //月
Format_DateTime(DateTime[3],LCD_DSY_BUFFER1+11)//日
strcpy(LCD_DSY_BUFFER1+13,WEEK[DateTime[5]]) //周 串拷贝 包含于STRING.H
Format_DateTime(DateTime[2],LCD_DSY_BUFFER2+5) //时
Format_DateTime(DateTime[1],LCD_DSY_BUFFER2+8) //分
Format_DateTime(DateTime[0],LCD_DSY_BUFFER2+11) //秒
Display_LCD_String(0x00,LCD_DSY_BUFFER1)
Display_LCD_String(0x40,LCD_DSY_BUFFER2)
}
}
void int0() interrupt 0
{
Keyscan()
}
评论列表(0条)