標(biāo)題:
1602+1302單片機(jī)電子鐘可以走時(shí),但不能調(diào)時(shí) 請(qǐng)教各位大神
[打印本頁(yè)]
作者:
鄭學(xué)基
時(shí)間:
2018-1-25 08:20
標(biāo)題:
1602+1302單片機(jī)電子鐘可以走時(shí),但不能調(diào)時(shí) 請(qǐng)教各位大神
我做1602+1302電子鐘,用郭老師TX-1學(xué)習(xí)板,可以走時(shí),但不能調(diào)時(shí),在主程序中放入keyscan();立刻不走,有什么辦法?
我的程序如下:
#include <reg52.h>
#include <intrins.h>
#define uint unsigned int
#define uchar unsigned char
#define nop() _nop_()
sbit lcd_rs_port = P3^5; /*定義LCD控制端口*/
sbit lcd_rw_port = P3^6; /*(在下面的程序中,去寫入wr=0,或wr=1)*/
sbit lcd_en_port = P3^4;
#define lcd_data_port P0
///////////////////////////////////////
sbit dula=P2^6;//數(shù)碼管的段選信號(hào)
sbit wela=P2^7; //數(shù)碼管的位選信號(hào)
void delay1 (void)//關(guān)閉數(shù)碼管延時(shí)程序
{
int k;
for (k=0; k<1000; k++);
}
//////////////////////////////////////
sbit T_CLK = P1^7; /*實(shí)時(shí)時(shí)鐘時(shí)鐘線引腳 */
sbit T_IO = P2^4; /*實(shí)時(shí)時(shí)鐘數(shù)據(jù)線引腳 */
sbit T_RST = P1^6; /*實(shí)時(shí)時(shí)鐘復(fù)位線引腳 (參考總原理圖)*/
sbit ACC0 = ACC^0;
sbit ACC7 = ACC^7;
uchar code mun_to_char[] = {"0123456789ABCDEF"}; /*定義數(shù)字跟ASCII碼的關(guān)系*/
uchar data time_data_buff[7]={0x00,0x00,0x09,0x01,0x01,0x04,0x09};/*格式為: 秒 分 時(shí) 日 月 星期 年 */
uchar data lcd1602_line1[]={" 2000/00/00 000"};
uchar data lcd1602_line2[]={" 00:00:00 "};
uchar code Weeks[][3]={{"SUN"},{"MON"},{"TUE"},{"WED"},{"THU"},{"FRI"},{"SAT"},{"SUN"}};
/********************************************************************
函 數(shù) 名:RTInputByte()
功 能:實(shí)時(shí)時(shí)鐘寫入一字節(jié)
說(shuō) 明:往DS1302寫入1Byte數(shù)據(jù) (內(nèi)部函數(shù))
入口參數(shù):d 寫入的數(shù)據(jù)
返 回 值:無(wú)
設(shè) 計(jì):zhaojunjie 日 期:2002-03-19
修 改: 日 期:
***********************************************************************/
void RTInputByte(uchar d)
{
uchar i;
ACC = d;
for(i=8; i>0; i--)
{
T_IO = ACC0; /*相當(dāng)于匯編中的 RRC */
T_CLK = 1;
T_CLK = 0;
ACC = ACC >> 1;
}
}
/********************************************************************
函 數(shù) 名:RTOutputByte()
功 能:實(shí)時(shí)時(shí)鐘讀取一字節(jié)
說(shuō) 明:從DS1302讀取1Byte數(shù)據(jù) (內(nèi)部函數(shù))
入口參數(shù):無(wú)
返 回 值:ACC
設(shè) 計(jì):zhaojunjie 日 期:2002-03-19
修 改: 日 期:
***********************************************************************/
uchar RTOutputByte(void)
{
uchar i;
for(i=8; i>0; i--)
{
ACC = ACC >>1; /*相當(dāng)于匯編中的 RRC */
ACC7 = T_IO;
T_CLK = 1;
T_CLK = 0;
}
return(ACC);
}
/********************************************************************
函 數(shù) 名:W1302()
功 能:往DS1302寫入數(shù)據(jù)
說(shuō) 明:先寫地址,后寫命令/數(shù)據(jù) (內(nèi)部函數(shù))
調(diào) 用:RTInputByte() , RTOutputByte()
入口參數(shù):ucAddr: DS1302地址, ucData: 要寫的數(shù)據(jù)
返 回 值:無(wú)
設(shè) 計(jì):zhaojunjie 日 期:2002-03-19
修 改: 日 期:
***********************************************************************/
void W1302(uchar ucAddr, uchar ucDa)
{
T_RST = 0;
T_CLK = 0;
T_RST = 1;
RTInputByte(ucAddr); /* 地址,命令 */
RTInputByte(ucDa); /* 寫1Byte數(shù)據(jù)*/
T_CLK = 1;
T_RST = 0;
}
/********************************************************************
函 數(shù) 名:R1302()
功 能:讀取DS1302某地址的數(shù)據(jù)
說(shuō) 明:先寫地址,后讀命令/數(shù)據(jù) (內(nèi)部函數(shù))
調(diào) 用:RTInputByte() , RTOutputByte()
入口參數(shù):ucAddr: DS1302地址
返 回 值:ucData :讀取的數(shù)據(jù)
設(shè) 計(jì):zhaojunjie 日 期:2002-03-19
修 改: 日 期:
***********************************************************************/
uchar R1302(uchar ucAddr)
{
uchar ucData;
T_RST = 0;
T_CLK = 0;
T_RST = 1;
RTInputByte(ucAddr); /* 地址,命令 */
ucData = RTOutputByte(); /* 讀1Byte數(shù)據(jù) */
T_CLK = 1;
T_RST = 0;
return(ucData);
}
/********************************************************************
函 數(shù) 名:Set1302()
功 能:設(shè)置初始時(shí)間
說(shuō) 明:先寫地址,后讀命令/數(shù)據(jù)(寄存器多字節(jié)方式)
調(diào) 用:W1302()
入口參數(shù):pClock: 設(shè)置時(shí)鐘數(shù)據(jù)地址 格式為: 秒 分 時(shí) 日 月 星期 年
7Byte (BCD碼)1B 1B 1B 1B 1B 1B 1B
返 回 值:無(wú)
設(shè) 計(jì):zhaojunjie 日 期:2002-03-19
修 改: 日 期:
***********************************************************************/
void Set1302(uchar *pClock)
{
uchar i;
uchar ucAddr = 0x80;
EA = 0;
W1302(0x8e,0x00); /* 控制命令,WP=0,寫操作?*/
for(i =7; i>0; i--)
{
W1302(ucAddr,*pClock); /* 秒 分 時(shí) 日 月 星期 年 */
pClock++;
ucAddr +=2;
}
W1302(0x8e,0x80); /* 控制命令,WP=1,寫保護(hù)?*/
EA = 1;
}
/********************************************************************
函 數(shù) 名:Get1302()
功 能:讀取DS1302當(dāng)前時(shí)間
說(shuō) 明:
調(diào) 用:R1302()
入口參數(shù):ucCurtime: 保存當(dāng)前時(shí)間地址。當(dāng)前時(shí)間格式為: 秒 分 時(shí) 日 月 星期 年
7Byte (BCD碼) 1B 1B 1B 1B 1B 1B 1B
返 回 值:無(wú)
設(shè) 計(jì):zhaojunjie 日 期:2002-03-19
修 改: 日 期:
***********************************************************************/
void Get1302(uchar ucCurtime[])
{
uchar i;
uchar ucAddr = 0x81;
EA = 0;
for (i=0; i<7; i++)
{
ucCurtime[i] = R1302(ucAddr);/*格式為: 秒 分 時(shí) 日 月 星期 年 */
ucAddr += 2;
}
EA = 1;
}
//---------------------------------------------
void lcd_delay(uchar ms) /*LCD1602 延時(shí)*/
{
uchar j;
while(ms--){
for(j=0;j<250;j++)
{;}
}
}
void lcd_busy_wait() /*LCD1602 忙等待*/
{
lcd_rs_port = 0;
lcd_rw_port = 1;
lcd_en_port = 1;
lcd_data_port = 0xff;
while (lcd_data_port&0x80);
lcd_en_port = 0;
}
void lcd_command_write(uchar command) /*LCD1602 命令字寫入*/
{
lcd_busy_wait();
lcd_rs_port = 0;
lcd_rw_port = 0;
lcd_en_port = 0;
lcd_data_port = command;
lcd_en_port = 1;
lcd_en_port = 0;
}
void lcd_system_reset() /*LCD1602 初始化*/
{
lcd_delay(20);
lcd_command_write(0x38);
lcd_delay(100);
lcd_command_write(0x38);
lcd_delay(50);
lcd_command_write(0x38);
lcd_delay(10);
lcd_command_write(0x08);
lcd_command_write(0x01);
lcd_command_write(0x06);
lcd_command_write(0x0c);
lcd_data_port = 0xff; /*釋放數(shù)據(jù)端口*/
}
void lcd_char_write(uchar x_pos,y_pos,lcd_dat) /*LCD1602 字符寫入*/
{
x_pos &= 0x0f; /* X位置范圍 0~15 */
y_pos &= 0x01; /* Y位置范圍 0~ 1 */
if(y_pos==1) x_pos += 0x40;
x_pos += 0x80;
lcd_command_write(x_pos);
lcd_busy_wait();
lcd_rs_port = 1;
lcd_rw_port = 0;
lcd_en_port = 0;
lcd_data_port = lcd_dat;
lcd_en_port = 1;
lcd_en_port = 0;
lcd_data_port = 0xff; /*釋放數(shù)據(jù)端口*/
}
void main()
{
uchar i;
lcd_system_reset(); /*LCD1602 初始化*/
/////////////////////////////////////////////////////////////////
P0=0X00;//關(guān)掉數(shù)碼管的信號(hào)。阻止數(shù)碼管受到P0口信號(hào)的影響。
dula=1;
wela=0;
delay1();
dula=0;
wela=0;
delay1();
////////////////////////////////////////////////////////////////
lcd_data_port = 0xff; /*釋放P0端口*/
Set1302(time_data_buff); /*設(shè)置時(shí)間*/
while(1){
Get1302(time_data_buff); /*讀取當(dāng)前時(shí)間*/
/*刷新顯示*/
lcd1602_line1[3] = mun_to_char[time_data_buff[6]/0x10];
lcd1602_line1[4] = mun_to_char[time_data_buff[6]%0x10]; /*年*/
lcd1602_line1[6] = mun_to_char[time_data_buff[4]/0x10];
lcd1602_line1[7] = mun_to_char[time_data_buff[4]%0x10]; /*月*/
lcd1602_line1[9] = mun_to_char[time_data_buff[3]/0x10];
lcd1602_line1[10] = mun_to_char[time_data_buff[3]%0x10]; /*日*/
for(i=0;i<3;i++) lcd1602_line1[i+13]=Weeks[time_data_buff[5]&0x07][i]; /*星期*/
lcd1602_line2[4] = mun_to_char[time_data_buff[2]/0x10];
lcd1602_line2[5] = mun_to_char[time_data_buff[2]%0x10]; /*時(shí)*/
lcd1602_line2[7] = mun_to_char[time_data_buff[1]/0x10];
lcd1602_line2[8] = mun_to_char[time_data_buff[1]%0x10]; /*分*/
lcd1602_line2[10] = mun_to_char[time_data_buff[0]/0x10];
lcd1602_line2[11] = mun_to_char[time_data_buff[0]%0x10]; /*秒*/
for(i=0;i<16;i++) lcd_char_write(i,0,lcd1602_line1[i]);
for(i=0;i<16;i++) lcd_char_write(i,1,lcd1602_line2[i]);
}
}
復(fù)制代碼
歡迎光臨 (http://www.torrancerestoration.com/bbs/)
Powered by Discuz! X3.1