单片机1602液晶显示的DS1302实时时钟帮我分析以下程序运行步骤,详细解释一下,尤其是Bcd转换

单片机1602液晶显示的DS1302实时时钟帮我分析以下程序运行步骤,详细解释一下,尤其是Bcd转换,第1张

这是我写的一个成功的,你仔细对比下就知道了!

#include<reg52.h>

#include<intrins.h>

#define U8 unsigned char

#define U16 unsigned int

#define BOOL bit

U8 secondh,secondl,minuteh,minutel,hourh,hourl,second,minute,hour

U8 monthl,monthh,month,day,datel,dateh,date,year,yearl,yearh

/***********定义LCD功能管脚**************************************/

sbit LCDRS = P2^6 //控制LCD数据或命令的读写

sbit LCDRW = P2^5//控制LCD读写信号

sbit LCDEN = P2^7 //使能LCD

/***************************************************************/

/***********定义DS1302功能管脚*********************************/

sbit DSRST = P3^5 //DS1302复位管脚

sbit DSIO = P3^4 //控制DS1302数据传输

sbit DSCLK = P3^6//DS1302时钟

/***************************************************************/

/*******************延时函数***********************************/

void delay(U16 time)

{

U16 time1,time2

for(time1=timetime1>0time1--)

for(time2=110time2>0time2--)

}

/**************************************************************/

/***********检测LCD忙碌状态***********************************/

BOOL judge_lcd()

{

BOOL flag

LCDEN = 0

LCDRS = 0

LCDRW = 1

LCDEN = 1

delay(1)

flag=(BOOL)(P0&0x80) //状态位第7位为1时禁止读写,为0时可读写

LCDEN = 0

return flag

}

/**************************************************************/

/***********实现LCD写指令**************************************/

void write_lcd_command(U16 command)

{

while(judge_lcd()) //检测LCD是否为忙碌状态

P0 = command

LCDRS = 0

LCDRW = 0

LCDEN = 1

delay(1)

LCDEN = 0

}

/***************************************************************/

/***********实现LCD写数据**************************************/

void write_lcd_date(U16 date)

{

while(judge_lcd()) //检测LCD是否为忙碌状态

P0 = date

LCDRS = 1

LCDRW = 0

LCDEN = 1

delay(1)

LCDEN = 0

}

/***************************************************************/

/***********实现LCD写指令及数据********************************/

void write_lcd_com_date(U16 addr,U16 date)

{

write_lcd_command(addr)

delay(1)

write_lcd_date(date)

}

/**************************************************************/

/******************初始化LCD***********************************/

void init_lcd()

{

write_lcd_command(0x38) //设置16x2显示,5x7点阵,8位数据口

delay(1)

write_lcd_command(0x0c) //开显示,不显示光标,光标不闪烁

delay(1)

write_lcd_command(0x06)//读写一个字符后,地址自动加1,不移动屏幕

delay(1)

write_lcd_command(0x01) //清屏

delay(1)

}

/**************************************************************/

/**********************读DS1302********************************/

U8 read_ds1302(U8 addr)

{

U8 i,temp=0x00

DSRST=0

DSIO=1

DSCLK=0

DSRST=1

for (i=0i<8i++) //循环8次 写入地址数据

{

DSCLK=0

DSIO=addr&0x01 //每次传输低字节

addr>>=1 //右移一位

DSCLK=1

}

delay(1)

if(DSIO)

temp|=0x80 //每次传输低字节

DSCLK=0

temp>>=1

for (i=0i<7i++) //循环8次 读取数据

{

DSCLK=0

if(DSIO)

temp|=0x80 //每次传输低字节

DSCLK=1

temp>>=1 //右移一位

}

DSCLK=1

DSRST=0

DSIO=0

return temp //返回

}

/************************************************************/

/******************写DS1302**********************************/

void write_ds1302(U8 addr, U8 date)

{

U8 i

DSRST=0

DSCLK=0

DSRST=1

for (i=0i<8i++) //循环8次 写入地址数据

{

DSCLK=0

_nop_()

DSIO=addr&0x01 //每次传输低字节

addr>>=1 //右移一位

DSCLK=1

_nop_()

}

for (i=0i<8i++) //循环8次 写入数据

{

DSCLK=0

_nop_()

DSIO=date&0x01 //每次传输低字节

date>>=1 //右移一位 DSCLK=1

DSCLK=1

_nop_()

}

DSRST=0

delay(1)

}

/**************************************************************/

/*****************初始化DS1302********************************/

void init_ds1302()

{

//读秒

if((((second=read_ds1302(0x81))>>7)&&0x01)==1)

{

write_ds1302(0x8e,0x00) //关闭写保护

write_ds1302(0x80,0x00) //00秒

write_ds1302(0x82,0x54) //54分

write_ds1302(0x84,0x17) //17点

write_ds1302(0x86,0x14) //14日

write_ds1302(0x88,0x10) //10月

write_ds1302(0x8a,0x05) //星期五

write_ds1302(0x8c,0x11) //2011年

write_ds1302(0x8e,0x80) //允许写保护

}

}

/********************************************************/

/*********************主函数****************************/

void main()

{

init_lcd()

delay(2)

write_lcd_com_date(0x81,'2')

write_lcd_com_date(0x82,'0')

delay(1)

init_ds1302()

delay(1)

while(1)

{

second=read_ds1302(0x81)

secondl=second&0x0f

secondh=second>>4

minute=read_ds1302(0x83)

minutel=minute&0x0f

minuteh=minute>>4

hour=read_ds1302(0x85)

hourl=hour&0x0f

hourh=hour>>4

month=read_ds1302(0x89)

monthl=month&0x0f

monthh=month>>4

year=read_ds1302(0x8d)

yearl=year&0x0f

yearh=year>>4

date=read_ds1302(0x87)

datel=date&0x0f

dateh=date>>4

day=read_ds1302(0x8b)

day=day&0x0f

delay(15)

write_lcd_com_date(0xca,0x30+secondl)

write_lcd_com_date(0xc9,0x30+secondh)

write_lcd_com_date(0xc8,':')

write_lcd_com_date(0xc7,0x30+minutel)

write_lcd_com_date(0xc6,0x30+minuteh)

write_lcd_com_date(0xc5,':')

write_lcd_com_date(0xc4,0x30+hourl)

write_lcd_com_date(0xc3,0x30+hourh)

write_lcd_com_date(0x84,0x30+yearl)

write_lcd_com_date(0x83,0x30+yearh)

write_lcd_com_date(0x85,'-')

write_lcd_com_date(0x87,0x30+monthl)

write_lcd_com_date(0x86,0x30+monthh)

write_lcd_com_date(0x88,'-')

write_lcd_com_date(0x8a,0x30+datel)

write_lcd_com_date(0x89,0x30+dateh)

write_lcd_com_date(0x8c,0x30+day)

}

}

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

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

}


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

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

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

发表评论

登录后才能评论

评论列表(0条)

保存