(急!!!)用DS1302与1602LCD设计的可调式电子日历与时钟 我要C程序啊。

(急!!!)用DS1302与1602LCD设计的可调式电子日历与时钟 我要C程序啊。,第1张

哈哈,你有福了 我也想了好久这程序 这不 刚写好 研究研究

#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()

}

#include <reg51.h>

typedef unsigned int uint

typedef unsigned char uchar

sbit DS1302_SDA=P1^0

sbit DS1302_SCK=P1^1

sbit DS1302_RST=P1^2

sbit KEY1=P3^0

sbit KEY2=P3^1

sbit KEY3=P3^2

sbit KEY4=P3^3

sbit BEEP=P3^7

uchar code DSY_CODE[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90}

uchar Display_Buffer[]={0x00,0x00,0xbf,0x00,0x00,0xbf,0x00,0x00}

uchar Display_Buffer1[]={0x00,0x00,0xbf,0x00,0x00,0xbf,0x00,0x00}

uchar Bit_CODE[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80}

uchar Current_Time[7]

uchar alarm_clock[7]

char Flag=0

char T_OR_A=0

uchar w=0,u=0,v

void delay_ms(uint x)

{

uchar i

while(x--)for(i=0i<120i++)

}

void WriteAByteToDS1302(uchar x)

{

uchar i

for(i=0i<8i++)

{

DS1302_SDA=x&0x01

DS1302_SCK=1

DS1302_SCK=0

x>>=1

}

}

uchar GetAByteFromDS1302(void)

{

uchar i,b=0X00,t

for(i=0i<8i++)

{

b>>=1

t=DS1302_SDA

b|=t<<7

DS1302_SCK=1

DS1302_SCK=0

}

return (b/16*10+b%16)

}

void ResetDS1302(void)

{

DS1302_RST=0

DS1302_SCK=0

DS1302_RST=1

}

uchar ReadData(uchar addr)

{

uchar dat

ResetDS1302()

WriteAByteToDS1302(addr)

dat=GetAByteFromDS1302()

DS1302_SCK=0

DS1302_RST=0

return dat

}

void WriteDS1302(uchar addr,uchar dat)

{

ResetDS1302()

WriteAByteToDS1302(addr)

WriteAByteToDS1302(dat)

DS1302_SCK=0

DS1302_RST=0

}

void SetDS1302(void)

{

uchar i

WriteDS1302(0x8e,0x00)

for(i=0i<7i++)

{

WriteDS1302(0x80+i*2,(Current_Time[i]/10<<4)|(Current_Time[i]%10))

}

WriteDS1302(0x8e,0x80)

}

void GetTime(void)

{

uchar i,addr=0x81

for(i=0i<7i++)

{

Current_Time[i]=ReadData(addr)

addr+=2

}

}

void time0Init(void)

{

TMOD=0x01

TH0=(65536-50000)/256

TL0=(65536-50000)%256

EA=1

ET0=1

TR0=1

}

void ScanKey(void)

{

if(KEY1==0)

{

delay_ms(10)

if(KEY1==0)

{

while(!KEY1)

w=w++

if(w==5)w=1

switch(w)

{

case 1:Flag=1break

case 2:Flag=1 break

case 3:Flag=1 break

case 4:SetDS1302()

Flag=0

break

}

}

}

if(w!=0)

{

if(KEY2==0)

{

delay_ms(10)

if(KEY2==0)

{

while(!KEY2)

switch(w)

{

case 1:Current_Time[0]++break

case 2:Current_Time[1]++break

case 3:Current_Time[2]++break

}

}

}

if(KEY3==0)

{

delay_ms(10)

if(KEY3==0)

{

while(!KEY3)

switch(w)

{

case 1:Current_Time[0]--break

case 2:Current_Time[1]--break

case 3:Current_Time[2]--break

}

}

}

}

if(KEY4==0)

{

delay_ms(10)

if(KEY4==0)

{

while(!KEY4)

u++

if(u==5)u=1

switch(u)

{

case 1:T_OR_A=1

break

case 2:break

case 3:break

case 4:T_OR_A=0break

}

}

}

if(u!=0)

{

if(KEY2==0)

{

delay_ms(10)

if(KEY2==0)

{

while(!KEY2)

switch(u)

{

case 1:alarm_clock[0]++break

case 2:alarm_clock[1]++break

case 3:alarm_clock[2]++break

}

}

}

if(KEY3==0)

{

delay_ms(10)

if(KEY3==0)

{

while(!KEY3)

switch(u)

{

case 1:alarm_clock[0]--break

case 2:alarm_clock[1]--break

case 3:alarm_clock[2]--break

}

}

}

}

}

void Play(uchar t)

{

uchar i

for(i=0i<100i++)

{

BEEP = ~BEEP

delay_ms(t)

}

BEEP = 0

}

void main(void)

{

//uchar i

SetDS1302()

time0Init()

//GetTime()

while(1)

{

if(Flag==0)

GetTime()

Display_Buffer[0]=DSY_CODE[Current_Time[2]/10]

Display_Buffer[1]=DSY_CODE[Current_Time[2]%10]

Display_Buffer[3]=DSY_CODE[Current_Time[1]/10]

Display_Buffer[4]=DSY_CODE[Current_Time[1]%10]

Display_Buffer[6]=DSY_CODE[Current_Time[0]/10]

Display_Buffer[7]=DSY_CODE[Current_Time[0]%10]

//Display_Buffer[0]=DSY_CODE[Current_Time[2]/10]

ScanKey()

Display_Buffer1[0]=DSY_CODE[alarm_clock[2]/10]

Display_Buffer1[1]=DSY_CODE[alarm_clock[2]%10]

Display_Buffer1[3]=DSY_CODE[alarm_clock[1]/10]

Display_Buffer1[4]=DSY_CODE[alarm_clock[1]%10]

Display_Buffer1[6]=DSY_CODE[alarm_clock[0]/10]

Display_Buffer1[7]=DSY_CODE[alarm_clock[0]%10]

if((Current_Time[2]==alarm_clock[2])&&(Current_Time[1]==alarm_clock[1])&&(Current_Time[0]==alarm_clock[0]))//闹钟

Play(1)

}

}

void tiemr0() interrupt 1

{

//if(flag1) j++

uchar i,j

TH0=(65536-50000)/256

TL0=(65536-50000)%256

if(T_OR_A==0)

{

for(i=0i<8i++)

{

P2=Bit_CODE[i]

P0=Display_Buffer[i]

delay_ms(2)

}

}

else

{

for(j=0j<8j++)

{

P2=Bit_CODE[j]

P0=Display_Buffer1[j]

delay_ms(2)

}

}

}

上面是基于DS1302和LED数码管的可调闹钟C语言程序!


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/12142060.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-21
下一篇 2023-05-21

发表评论

登录后才能评论

评论列表(0条)

保存