新聞中心

EEPW首頁(yè) > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > 51單片機(jī)-時(shí)鐘芯片DS1302

51單片機(jī)-時(shí)鐘芯片DS1302

作者: 時(shí)間:2016-12-02 來(lái)源:網(wǎng)絡(luò) 收藏
void delay(uint t){
int i,j;
for(i = 0; i < t; i++)
for(j = 10; j > 0; j--)
;
}
void lcd_com(uchar s){
RS = 0; //低電平,寫(xiě)指令
P2 = s; //傳數(shù)據(jù)
delay(14); //看時(shí)序圖,數(shù)據(jù)需要穩(wěn)定一段時(shí)間
E = 1; //給一個(gè)高脈沖,發(fā)送命令
delay(14); //如圖,高脈沖延時(shí)一段時(shí)間,確保命令發(fā)送
E = 0; //發(fā)送結(jié)束E置為低電平
}
void lcd_data(uchar s){
RS = 1;
P2 = s;
delay(14);
E = 1;
delay(14);
E = 0;
}
uchar DS_Read(uchar command){
uchar value;
RST = 0;
SCLK = 0;
RST = 1;
value = 0x0;
for(i = 0; i < 8; i++){
IO = command & 0x01; //寫(xiě)入控制字
SCLK = 0;
SCLK = 1;
command >>= 1;
}
for(i = 0; i < 8; i++){
SCLK = 1;
SCLK = 0;
if(IO)
value |= (0x01<<i); //如果那位是1則value置1
}
RST = 0;
value = value/16*10 + value%16; //BCD碼到十進(jìn)制的轉(zhuǎn)換
return value;
}
void DS_Write(uchar command,uchar value){
RST = 0;
SCLK = 0;
RST = 1;
for(i = 0; i < 8; i++){ //寫(xiě)控制字
IO = command & 0x01;
SCLK = 0;
SCLK = 1;
command >>= 1;
}
for(i = 0; i < 8; i++){ //向IO引腳寫(xiě)數(shù)據(jù)
IO = value & 0x01;
SCLK = 0;
SCLK = 1;
value >>= 1;
}
}
void Init_ds(){
RST = 0;
SCLK = 0;
RST = 1;
DS_Write(0x8E,0x00); //打開(kāi)寫(xiě)保護(hù),WP位置0,這樣就可以寫(xiě)數(shù)據(jù)了
DS_Write(0x84,0x00); //對(duì)日歷時(shí)鐘寄存器的初始化
DS_Write(0x8c,0x10);
DS_Write(0x8a,0x05);
DS_Write(0x88,0x10);
DS_Write(0x86,0x01);
DS_Write(0x84,0x04);
DS_Write(0x82,0x22);
DS_Write(0x80,0x00); //啟動(dòng)時(shí)鐘
DS_Write(0x8e,0x80); //禁止寄存器寫(xiě)
}
void Init_lcd(){
RS = 1; //先發(fā)指令,在初始時(shí)刻RS是高,E和RW是低
E = 0;
RW = 0;
lcd_com(0x38); //設(shè)置為16*2顯示,5*7點(diǎn)陣,8位數(shù)據(jù)接口
lcd_com(0x0f); //開(kāi)顯示,顯示光標(biāo),光標(biāo)閃爍
lcd_com(0x06); //讀寫(xiě)一個(gè)字符后地址指針加一
lcd_com(0x01);
}
void Display_lcd(uchar y, uchar x, uchar value){
if(y)
lcd_com(0x80+0x40+x); //如果y為1,寫(xiě)在第二行
else
lcd_com(0x80+x);
lcd_data(value); //寫(xiě)到LCD602上
}
void main(){
Init_ds();
Init_lcd();
while(1){
Display_lcd(0,0,2);
Display_lcd(0,1,0);
Display_lcd(0,2,Time_Data[DS_Read(0x8D)/10]);
Display_lcd(0,3,Time_Data[DS_Read(0x8D)%10]);
Display_lcd(0,4,-);
Display_lcd(0,5,Time_Data[DS_Read(0x89)/10]);
Display_lcd(0,6,Time_Data[DS_Read(0x89)%10]);
Display_lcd(0,7,-);
Display_lcd(0,8,Time_Data[DS_Read(0x87)/10]);
Display_lcd(0,9,Time_Data[DS_Read(0x87)%10]);
Display_lcd(0,10,D);
Display_lcd(0,11,a);
Display_lcd(0,12,y);
Display_lcd(0,13,:);
Display_lcd(0,14,Time_Data[DS_Read(0x8b)/10]);
Display_lcd(0,15,Time_Data[DS_Read(0x8b)%10]);
Display_lcd(1,0,C);
Display_lcd(1,1,a);
Display_lcd(1,2,n);
Display_lcd(1,3,u);
Display_lcd(1,4,t);
Display_lcd(1,5,e);
Display_lcd(1,6, );
Display_lcd(1,7,Time_Data[DS_Read(0x85)/10]);
Display_lcd(1,8,Time_Data[DS_Read(0x85)%10]);
Display_lcd(1,9,:);
Display_lcd(1,10,Time_Data[DS_Read(0x83)/10]);
Display_lcd(1,11,Time_Data[DS_Read(0x83)%10]);
Display_lcd(1,12,:);
Display_lcd(1,13,Time_Data[DS_Read(0x81)/10]);
Display_lcd(1,14,Time_Data[DS_Read(0x81)%10]);
}
}
//2010-10-1 Day:5
//Canute 04:22:00
DS1302中的RAM:

上一頁(yè) 1 2 下一頁(yè)

評(píng)論


技術(shù)專(zhuān)區(qū)

關(guān)閉