谁有用51单片机控制12864显示数字时钟的程序!急求!不用ds1302;简单一点就好。有好的答案加分!

谁有用51单片机控制12864显示数字时钟的程序!急求!不用ds1302;简单一点就好。有好的答案加分!,第1张

里面有128564显示汉字的程序,自己研究下,不过是用msp430控制的,你改改端口i就可以了。

#include "msp430x26x.h"

#define uchar unsigned char

#define uint unsigned int

#define iDat 1 //数据标志

#define iCmd 0 //指令标志

#define LCDb_RS 0x20 //定义四个控制引脚

#define LCDb_RW 0x40

#define LCDb_E 0x80

#define LCDb_RST 0x04

#define LCDb_L1 0x80 //第一行的地址

#define LCDb_L2 0x90 //第二行的地址

#define LCDb_L3 0x88 //第三行的地址

#define LCDb_L4 0x98 //第四行的地址

#define LCDb_SET_RS P1OUT|=LCDb_RS //四个控制管脚的控制 *** 作

#define LCDb_SET_RW P1OUT|=LCDb_RW

#define LCDb_SET_E P1OUT|=LCDb_E

#define LCDb_SET_RST P8OUT|=LCDb_RST

#define LCDb_CLR_RS P1OUT&=~LCDb_RS

#define LCDb_CLR_RW P1OUT&=~LCDb_RW

#define LCDb_CLR_E P1OUT&=~LCDb_E

#define LCDb_CLR_RST P8OUT&=~LCDb_RST

#define LCDb_DO P4OUT //输出数据总线端口定义

#define LCDb_FUNCTION 0x38 // 液晶模式为8位,2行,5*8字符

#define LCDb_BASCMD 0x30 // 基本指令集

#define LCDb_CLS 0x01 // 清屏

#define LCDb_HOME 0x02 // 地址返回原点,不改变DDRAM内容

#define LCDb_ENTRY 0x06 // 设数春定输入模式,光标加,屏幕不移动

#define LCDb_C2L 0x10 // 光标左移

#define LCDb_C2R 0x14 // 光标右移

#define LCDb_D2L 0x18 // 屏幕左移

#define LCDb_D2R 0x1C // 屏幕又移

#define LCDb_ON 0x0C // 打开显示

#define LCDb_OFF 0x08 //告野 关闭显示

unsigned char RXData

unsigned char Seg_Data[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f} //数码管袜毕喊编码

unsigned char Result[5] //整数转化成字符串,给LCD显示

void Delayms(uint MS)

{

uint i,j

for( i=0i<MSi++)

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

}

void Delayus(uint US)

{

uint i

US=US*5/4

for( i=0i<USi++)

}

void LCD12864_portini()

{

P1DIR=0xFF

P4DIR=0xFF

P5DIR=0xFF

P8DIR=0xFF

P8OUT |=LCDb_RST

//P1OUT=0xFF

}

/*函数名称: LCD12864_sendbyte

功能: 向12864液晶写入一个字节数据或者指令

*/

void LCD12864_sendbyte(uchar DatCmd, uchar dByte)

{

if (DatCmd == iCmd) //指令 *** 作

LCDb_CLR_RS

else

LCDb_SET_RS

LCDb_CLR_RW //写 *** 作

LCDb_SET_E

LCDb_DO = dByte //写入数据

//Delayus(500)

Delayms(1)

LCDb_CLR_E

}

/*函数名称: LCD12864_sendstr

功能: 向12864液晶写入一个字符串

参数: ptString--字符串指针

返回值 : 无

*/

void LCD12864_sendstr(uchar *ptString)

{

while((*ptString)!='\0') //字符串未结束一直写

{

LCD12864_sendbyte(iDat, *ptString++)

}

}

/*函数名称: LCD12864_clear

功能: 12864液晶清屏

参数: 无

返回值 : 无

*/

void LCD12864_clear(void)

{

LCD12864_sendbyte(iCmd,LCDb_CLS)

Delayms(2)// 清屏指令写入后,2ms 的延时是很必要的!!!

}

/*

函数名称: LCD12864_gotoXY

功能: 移动到指定位置

参数: Row--指定的行

Col--指定的列

返回值 : 无

*/

void LCD12864_gotoXY(uchar Row, uchar Col)

{

switch (Row) //选择行

{

case 2:

LCD12864_sendbyte(iCmd, LCDb_L2 + Col)break //写入第2行的指定列

case 3:

LCD12864_sendbyte(iCmd, LCDb_L3 + Col)break //写入第3行的指定列

case 4:

LCD12864_sendbyte(iCmd, LCDb_L4 + Col)break //写入第4行的指定列

default:

LCD12864_sendbyte(iCmd, LCDb_L1 + Col)break //写入第1行的指定列

}

}

/*

函数名称: LCD12864_initial

功能: 12864液晶初始化

*/

void LCD12864_initial(void)

{

Delayms(100) // 等待内部复位

LCD12864_portini() //端口初始化

LCD12864_sendbyte(iCmd, LCDb_FUNCTION) //功能、模式设定

LCD12864_sendbyte(iCmd, LCDb_ON) //打开显示

LCD12864_clear() //清屏

LCD12864_sendbyte(iCmd, LCDb_ENTRY) // 输入模式设定

}

void Int_char(int data)

{

if(data/1000)

{

Result[0]=data/1000+'0'

Result[1]=data/100%10+'0'

Result[2]=data/10%10+'0'

Result[3]=data%10+'0'

Result[4]=0

}

else if(data/100)

{

Result[0]=data/100+'0'

Result[1]=data/10%10+'0'

Result[2]=data%10+'0'

Result[3]=0

}

else if(data/10)

{

Result[0]=data/10%10+'0'

Result[1]=data%10+'0'

Result[2]=0

}

else

{

Result[0]=data%10+'0'

Result[1]=0

}

}

unsigned char Key_Press(void)

{

P7OUT=0xF0

if((P7IN&0x10)&&(P7IN&0x20)&&(P7IN&0x40)&&(P7IN&0x80)) return 0x00

else return 0xFF

}

unsigned char Get_Keycode(void)

{

while(1)

{

P7OUT=0xFE //扫描第一列

if((P7IN&0x10)==0) return 0

else if((P7IN&0x20)==0) return 4

else if((P7IN&0x40)==0) return 8

else if((P7IN&0x80)==0) return 12

P7OUT=0xFD //扫描第二列

if((P7IN&0x10)==0) return 1

else if((P7IN&0x20)==0) return 5

else if((P7IN&0x40)==0) return 9

else if((P7IN&0x80)==0) return 13

P7OUT=0xFB //扫描第三列

if((P7IN&0x10)==0) return 2

else if((P7IN&0x20)==0) return 6

else if((P7IN&0x40)==0) return 10

else if((P7IN&0x80)==0) return 14

P7OUT=0xF7 //扫描第四列

if((P7IN&0x10)==0) return 3

else if((P7IN&0x20)==0) return 7

else if((P7IN&0x40)==0) return 11

else if((P7IN&0x80)==0) return 15

}

}

void Init_compa()

{

CACTL1 = CAON+CAREF_2+CARSEL// Enable Comp, ref = 0.5*Vcc = Vin-

CACTL2 = P2CA0 // Pin to CA0

P1DIR |= 0x01 // P1.0 = o/p direction(CAOUT - LED)

P1SEL |= 0x01 // P1.0 - CAOUT, option select

}

/*

** 函数名称:初始化函数

*/

void Init_IIC(void)

{

P3SEL |= 0x06 // Assign I2C pins to USCI_B0

UCB0CTL1 |= UCSWRST // Enable SW reset

UCB0CTL0 = UCMST + UCMODE_3 + UCSYNC// I2C Master, synchronous mode

UCB0CTL1 = UCSSEL_2 + UCSWRST // Use SMCLK, keep SW reset

UCB0BR0 = 12// fSCL = SMCLK/12 = ~100kHz

UCB0BR1 = 0

UCB0I2CSA = 0x50 // Slave Address is 048h

UCB0CTL1 &= ~UCSWRST// Clear SW reset, resume operation

IE2 |= UCB0RXIE // Enable RX interrupt

_BIS_SR(GIE)

// RXCompare = 0x0 // Used to check incoming data

}

/** 函数名称:字节写函数

*/

void EEPROM_Write(unsigned char high_Address,unsigned char low_Address,unsigned char Word)

{

while (UCB0CTL1 &UCTXSTP) // 确定总线空闲

UCB0CTL1 |= UCTXSTT + UCTR // 发送起始位,确定为发送模式

UCB0TXBUF = high_Address// 发送高位地址

while((IFG2 &UCB0TXIFG)==0) // 判断是否发送完毕

UCB0TXBUF = low_Address // 发送低位地址

while((IFG2 &UCB0TXIFG)==0) // 判断是否发送完毕

UCB0TXBUF = Word// 发送数据

while((IFG2 &UCB0TXIFG)==0) // 判断是否发送完毕

UCB0CTL1 |= UCTXSTP // 发送停止位

while((UCB0CTL1 &UCTXSTP)==1) // 判断停止位是否发送完毕

}

/**

** 函数名称:字节读函数

*/

void EEPROM_readmore()

{

UCB0CTL1 &= ~UCTR // 确定为读

while (UCB0CTL1 &UCTXSTP) // 总线是否空闲

UCB0CTL1 |= UCTXSTT// 发送开始位

}

/*

** 函数名称:字节写函数

**/

void EEPROM_read(unsigned char high_Address,unsigned char low_Address)

{

while (UCB0CTL1 &UCTXSTP) // Ensure stop condition got sent

UCB0CTL1 |= UCTXSTT + UCTR // 发送起始位,确定为写

UCB0TXBUF = high_Address// 发送地址位高位

while((IFG2 &UCB0TXIFG)==0) // 判断是否发送完毕

UCB0TXBUF = low_Address // 发送地址位低位

while((IFG2 &UCB0TXIFG)==0) // 判断是否发送完毕

UCB0CTL1 &= ~UCTR // 确定为接收

while (UCB0CTL1 &UCTXSTP) //

UCB0CTL1 |=UCTXSTT

while((UCB0CTL1 &UCTXSTT)==1)

for(unsigned char i=0x0i<0x2fi++) // 延时确定数据已经被发送出去

UCB0CTL1 |=UCTXSTP + UCTXNACK // 发送停止位和NACK位

}

/*

** 函数名称:接收中断函数

**/

// USCI_B0 Data ISR

#pragma vector = USCIAB0TX_VECTOR

__interrupt void USCIAB0TX_ISR(void)

{

RXData = UCB0RXBUF // Get RX data

Int_char(RXData)

LCD12864_gotoXY(2,0) //第2行,第1列显示

LCD12864_sendstr(Result)

/*

key_code[0]=RXData%10+'0'

key_code[1]=0

LCD12864_gotoXY(1,0) //第1行,第1列显示

LCD12864_sendstr(key_code)

*/

// __bic_SR_register_on_exit(CPUOFF) // Exit LPM0

}

void Init_UART()

{

P3OUT &= ~(BIT4+BIT5+BIT6+BIT7)

P3SEL = 0xF0// P3.4,5,6,7 = USCI_A0 TXD/RXD USCI_A1 TXD/RXD

UCA0CTL1 |= UCSSEL_1// CLK = ACLK

UCA0BR0 = 0x03 // 32kHz/9600 = 3.41

UCA0BR1 = 0x00 //

UCA0MCTL = UCBRS1 + UCBRS0 // Modulation UCBRSx = 3

UCA0CTL1 &= ~UCSWRST// **Initialize USCI state machine**

IE2 |= UCA0RXIE // Enable USCI_A0 RX interrupt

UCA1CTL1 |= UCSSEL_1// CLK = ACLK

UCA1BR0 = 0x03 // 32kHz/9600 = 3.41

UCA1BR1 = 0x00 //

UCA1MCTL = UCBRS1 + UCBRS0 // Modulation UCBRSx = 3

UCA1CTL1 &= ~UCSWRST// **Initialize USCI state machine**

UC1IE |= UCA1RXIE // Enable USCI_A0 RX interrupt

_BIS_SR(GIE)//使能中断

}

void Init_ADC()

{

ADC12CTL0 = SHT0_2 + ADC12ON// Set sampling time, turn on ADC12

ADC12CTL1 = SHP // Use sampling timer

ADC12IE = 0x01 // Enable interrupt

ADC12CTL0 |= ENC// Conversion enabled

P6DIR &= 0x01 // P6.0, i/p

P6SEL |= 0x01 // P6.0-ADC option select

_BIS_SR(GIE)//使能中断

}

void Start_ADC()

{

ADC12CTL0 |= ADC12SC // Start convn, software controlled

}

#pragma vector=USCIAB0RX_VECTOR

__interrupt void USCI0RX_ISR(void)

{

while (!(IFG2&UCA0TXIFG)) // USCI_A0 TX buffer ready?

UCA0TXBUF = UCA0RXBUF // TX ->RXed character

LCD12864_sendbyte(iDat,UCA0RXBUF)

}

#pragma vector=USCIAB1RX_VECTOR

__interrupt void USCI1RX_ISR(void)

{

while (!(UC1IFG&UCA1TXIFG)) // USCI_A0 TX buffer ready?

UCA1TXBUF = UCA1RXBUF // TX ->RXed character

LCD12864_sendbyte(iDat,UCA0RXBUF)

//UCA1TXBUF = 'z'

}

// ADC12 interrupt service routine

#pragma vector=ADC12_VECTOR

__interrupt void ADC12_ISR (void)

{

int i=ADC12MEM0

Int_char(i)

LCD12864_gotoXY(2,0) //第1行,第1列显示

LCD12864_sendstr(Result)

/*

key_code[0] =i/1000+'0'

key_code[1] =i/100%10+'0'

key_code[2] =i/10%10+'0'

key_code[3] =i%10+'0'

key_code[4] =0

LCD12864_gotoXY(1,0) //第1行,第1列显示

LCD12864_sendstr(key_code)

*/

}

void Init_all()

{

LCD12864_initial()//LCD初始化,包含了数码管和LED灯初始化

P7DIR=0x0F //键盘扫描初始化

P7REN=0xF0 //输入上下拉电阻使能,输出上下拉不使能

P7OUT=0xF0//输入上拉

Init_UART()//串口初始化

Init_compa()//比较器初始化

Init_ADC()//ADC初始化

Init_IIC()//IIC初始化

}

void Test_Led()

{

unsigned char i=0

LCD12864_gotoXY(1,0) //第1行,第1列显示

LCD12864_sendstr("1.Test_LED")

for(i<16i++)

{

P8OUT=0xF0|i

Delayms(50)

}

}

void Test_Seg()

{

int i

LCD12864_gotoXY(1,0) //第1行,第1列显示

LCD12864_sendstr("2.Test_SEG")

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

{

//4,3,2,1

P1OUT&=~0x02

P1OUT|=0x10|0x08|0x04

P5OUT=Seg_Data[9] //清楚数码管显示

Delayms(1)

P1OUT&=~0x04

P1OUT|=0x10|0x08|0x02

P5OUT=Seg_Data[8] //清楚数码管显示

Delayms(1)

P1OUT&=~0x08

P1OUT|=0x10|0x04|0x02

P5OUT=Seg_Data[7] //清楚数码管显示

Delayms(1)

P1OUT&=~0x10

P1OUT|=0x08|0x04|0x02

P5OUT=Seg_Data[6] //清楚数码管显示

Delayms(1)

}

P5OUT=0x00//熄灭所有数码管

}

void Test_Key()

{

unsigned char i=0

LCD12864_gotoXY(1,0) //第1行,第1列显示

LCD12864_sendstr("3.Test_KEY")

LCD12864_gotoXY(2,0) //第2行,第1列显示

LCD12864_sendstr("按键:")

for(i<16i++)

{

Int_char(Get_Keycode())

LCD12864_gotoXY(2,3)

LCD12864_sendstr("")

LCD12864_gotoXY(2,3)

LCD12864_sendstr(Result)

Delayms(100)//防抖

}

}

void Test_Uart()

{

LCD12864_gotoXY(1,0) //第1行,第1列显示

LCD12864_sendstr("4.Test_UART")

LCD12864_gotoXY(2,0) //第2行,第1列显示

}

void Test_Compa()

{

LCD12864_gotoXY(1,0) //第1行,第1列显示

LCD12864_sendstr("5.Test_COMPA")

}

void Test_ADC()

{

int i=0

LCD12864_gotoXY(1,0) //第1行,第1列显示

LCD12864_sendstr("6.Test_ADC")

for(i<200i++)

{

Start_ADC()

Delayms(10)

}

}

void Test_IIC()

{

LCD12864_gotoXY(1,0) //第1行,第1列显示

LCD12864_sendstr("7.Test_IIC")

EEPROM_Write(0x00,0x40,7)// 字节写

Delayms(10)

EEPROM_read(0x00,0x40)

}

void main( void )

{

// Stop watchdog timer to prevent time out reset

WDTCTL = WDTPW + WDTHOLD//关闭看门狗

Init_all()

while(1){

Test_Led() //1.测试LED

LCD12864_gotoXY(2,0) //第1行,第1列显示

LCD12864_sendstr("请按16键!")

while(!(Get_Keycode()==15)) //按下最后一键测试下一个例子

LCD12864_clear()

Test_Seg() //2.测试数码管

LCD12864_gotoXY(2,0) //第1行,第1列显示

LCD12864_sendstr("请按16键!")

while(!(Get_Keycode()==15)) //按下最后一键测试下一个例子

LCD12864_clear()

Test_Key() //3.测试按键扫描

LCD12864_gotoXY(2,0) //第1行,第1列显示

LCD12864_sendstr("请按16键!")

while(!(Get_Keycode()==15)) //按下最后一键测试下一个例子

LCD12864_clear()

LCD12864_gotoXY(3,0) //第3行,第1列显示

LCD12864_sendstr("请按16键!")

Test_Uart() //4.测试串口

while(!(Get_Keycode()==15)) //按下最后一键测试下一个例子

LCD12864_clear()

Test_Compa() //5.测试比较器

LCD12864_gotoXY(2,0) //第2行,第1列显示

LCD12864_sendstr("请按16键!")

while(!(Get_Keycode()==15)) //按下最后一键测试下一个例子

LCD12864_clear()

Test_ADC() //6.测试ADC

LCD12864_gotoXY(3,0) //第3行,第1列显示

LCD12864_sendstr("请按16键!")

while(!(Get_Keycode()==15)) //按下最后一键测试下一个例子

LCD12864_clear()

Test_IIC() //7.测试IIC

Delayms(100)

LCD12864_gotoXY(3,0) //第3行,第1列显示

LCD12864_sendstr("测试完成")

while(!(Get_Keycode()==15)) //按下最后一键测试下一个例子

LCD12864_clear()

}

}

顶层文件 万年历.C

#include<reg51.h>坦孙

#include "LCD1602.h"

#include "DS1302.h"

#define uchar unsigned char

#define uint unsigned int

sbit speaker=P2^4

bit key_flag1=0,key_flag2=0

SYSTEMTIME adjusted

uchar sec_add=0,min_add=0,hou_add=0,day_add=0,mon_add=0,yea_add=0

uchar data_alarm[7]={0}

/************键盘控制******************************/芦模

int key_scan() //扫描是否有键按下

{ int i=0

uint temp

P1=0xf0

temp=P1

if(temp!=0xf0)

i=1

else

i=0

return i

}

uchar key_value() //确定按键的值

{

uint m=0,n=0,temp

uchar value

uchar v[4][3]={'2','1','0','5','4','3','8','7','6','b','a','9'}

P1=0xfe temp=P1if(temp!=0xfe)m=0

P1=0xfdtemp=P1 if(temp!=0xfd)m=1

P1=0xfbtemp=P1 if(temp!=0xfb)m=2

P1=0xf7temp=P1 if(temp!=0xf7)m=3

P1=0xeftemp=P1 if(temp!=0xef)n=0

P1=0xdftemp=P1 if(temp!=0xdf)n=1

P1=0xbftemp=P1 if(temp!=0xbf)n=2

value=v[m][n]

return value

}

/***************************设置闹铃函数*******************************/

void naoling(void)

{

uchar i=0,l=0,j

init1602()

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 k

if(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=1

else k=0

return k

}

void speak(void)

{uint i=50

while(i)

{speaker=0

delay(1)

speaker=1

delay(1)

i--

}

}

void alarm(void)

{uint i=10

while(i)

{

speak()

delay(10)

i--

}

}

/**************************修改时间 *** 作********************************/陪信缓

void reset(void)

{

sec_add=0

min_add=0

hou_add=0

day_add=0

mon_add=0

yea_add=0

}

void adjust(void)

{

if(key_scan()&&key_flag1)

switch(key_value())

{case '0':sec_add++break

case '1':min_add++break

case '2':hou_add++break

case '3':day_add++break

case '4':mon_add++break

case '5':yea_add++break

case 'b':reset()break

default: break

}

adjusted.Second+=sec_add

adjusted.Minute+=min_add

adjusted.Hour+=hou_add

adjusted.Day+=day_add

adjusted.Month+=mon_add

adjusted.Year+=yea_add

if(adjusted.Second>59) adjusted.Second=adjusted.Second%60

if(adjusted.Minute>59) adjusted.Minute=adjusted.Minute%60

if(adjusted.Hour>23) adjusted.Hour=adjusted.Hour%24

if(adjusted.Day>31) adjusted.Day=adjusted.Day%31

if(adjusted.Month>12) adjusted.Month=adjusted.Month%12

if(adjusted.Year>100) adjusted.Year=adjusted.Year%100

}

/**************************中断处理函数*********************************/

void changing(void) interrupt 0 using 0 //需要修改时间和日期,或者停止修改

{

if(key_flag1)key_flag1=0

else key_flag1=1

}

void alarming(void) interrupt 3 using 0 //需要设置闹铃或者停止设置

{

if(key_flag2)key_flag2=0

else key_flag2=1

}

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

main()

{uint i

uchar *l

uchar p1[]="D:",p2[]="T:"

SYSTEMTIME T

EA=1

EX0=1

IT0=1

EA=1

EX1=1

IT1=1

init1602()

Initial_DS1302()

while(1)

{ write_com(0x80)

write_string(p1,2)

write_com(0xc0)

write_string(p2,2)

DS1302_GetTime(&T)

adjusted.Second=T.Second

adjusted.Minute=T.Minute

adjusted.Hour=T.Hour

adjusted.Week=T.Week

adjusted.Day=T.Day

adjusted.Month=T.Month

adjusted.Year=T.Year

for(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^0

sbit lcdrw = P2^1

sbit lcden = P2^2

void delay(uint z)// 延时

{

uint x,y

for(x=zx>0x--)

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

}

void write_com(uchar com) // 写入指令数据到 lcd

{

lcdrw=0

lcdrs=0

P0=com

delay(5)

lcden=1

delay(5)

lcden=0

}

void write_data(uchar date) // 写入字符显示数据到 lcd

{

lcdrw=0

lcdrs=1

P0=date

delay(5)

lcden=1

delay(5)

lcden=0

}

void init1602() // 初始化设定

{

lcdrw=0

lcden=0

write_com(0x3C)

write_com(0x0c)

write_com(0x06)

write_com(0x01)

write_com(0x80)

}

void write_string(uchar *pp,uint n)

{

int i

for(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^0

sbit ACC7 = ACC^7

typedef struct SYSTEM_TIME

{

unsigned char Second

unsigned char Minute

unsigned char Hour

unsigned char Week

unsigned char Day

unsigned char Month

unsigned char Year

unsigned 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 i

ACC = d

for(i=8i>0i--)

{

DS1302_IO = ACC0

DS1302_CLK = 1

DS1302_CLK = 0

ACC = ACC >>1//因为在前面已经定义了ACC0 = ACC^0以便再次利用DS1302_IO = ACC0

}

}

unsigned char DS1302OutputByte(void) //实时时钟读取一字节(内部函数)

{

unsigned char i

for(i=8i>0i--)

{

ACC = ACC >>1

ACC7 = DS1302_IO

DS1302_CLK = 1

DS1302_CLK = 0

}

return(ACC)

}

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

void Write1302(unsigned char ucAddr, unsigned char ucDa) //ucAddr: DS1302地址, ucData: 要写的数据

{

DS1302_RST = 0

DS1302_CLK = 0

DS1302_RST = 1

DS1302InputByte(ucAddr) // 地址,命令

DS1302InputByte(ucDa) // 写1Byte数据

DS1302_CLK = 1

DS1302_RST = 0

}

unsigned char Read1302(unsigned char ucAddr) //读取DS1302某地址的数据

{

unsigned char ucData

DS1302_RST = 0

DS1302_CLK = 0

DS1302_RST = 1

DS1302InputByte(ucAddr|0x01) // 地址,命令

ucData = DS1302OutputByte()// 读1Byte数据

DS1302_CLK = 1

DS1302_RST = 0

return(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 ReadValue

ReadValue = 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%10

D[1]=Time->Minute/10<<4+Time->Minute%10

D[2]=Time->Hour/10<<4+Time->Hour%10

D[3]=Time->Day/10<<4+Time->Day%10

D[4]=Time->Month/10<<4+Time->Month%10

D[5]=Time->Week/10<<4+Time->Week%10

D[6]=Time->Year/10<<4+Time->Year%10

return 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 Second

Second=Read1302(DS1302_SECOND)

if(Second&0x80) //初始化时间

DS1302_SetTime(DS1302_SECOND,0)

}

void DS1302_TimeStop(bit flag) // 是否将时钟停止

{

unsigned char Data

Data=Read1302(DS1302_SECOND)

DS1302_SetProtect(0)

if(flag)

Write1302(DS1302_SECOND, Data|0x80)

else

Write1302(DS1302_SECOND, Data&0x7F)

}

#endif

LCD12864显示时钟乱码的问题,扒慧可能是你的程春洞答序没有写对,

你可以查看你的程序语言有没有出现错误,

具体的还需要对接颤差下LCD12864的产品规格书,

南京罗姆液晶希望对你的问题有所帮助!!!


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

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

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

发表评论

登录后才能评论

评论列表(0条)

保存