藍橋杯單片機必備知識-----(10)DS1302時鐘
DS1302:

寫保護:

ds1302芯片:

ds1302.h添加代碼
void ds1302_write();
void ds1302_read();
ds1302.c
#include <reg52.h>
#include <intrins.h>
sbit SCK=P1^7;
sbit SDA=P2^3;
sbit RST = P1^3; // DS1302復位
void Write_Ds1302(unsigned char temp)
{
unsigned char i;
for (i=0;i<8;i++)
{
SCK=0;
SDA=temp&0x01;
temp>>=1;
SCK=1;
}
}
void Write_Ds1302_Byte( unsigned char address,unsigned char dat )
{
//需增加
unsigned char num;
RST=0; _nop_();
SCK=0; _nop_();
RST=1; _nop_();
Write_Ds1302(address);
//需增加
num=(dat/10<<4)|(dat%10);
//替換為num
Write_Ds1302(num);
//Write_Ds1302(dat);
RST=0;
}
unsigned char Read_Ds1302_Byte ( unsigned char address )
{
//需增加
unsigned char dat_low,dat_high;
unsigned char i,temp=0x00;
RST=0; _nop_();
SCK=0; _nop_();
RST=1; _nop_();
Write_Ds1302(address);
for (i=0;i<8;i++)
{
SCK=0;
temp>>=1;
if(SDA)
temp|=0x80;
SCK=1;
}
RST=0; _nop_();
SCK=0; _nop_();
SCK=1; _nop_();
SDA=0; _nop_();
SDA=1; _nop_();
//需增加
/*以下是進制轉化*/
dat_high=temp/16;
dat_low=temp%16;
temp=dat_high*10+dat_low;
/*以上是進制轉化*/
return (temp);
}
/***************以下都是需要自己寫的*******************************/
unsigned int code shijian[]={50,59,23,0,0,0,0};//ds1302賦值為23:59:50
unsigned int time[7];//用于存放1302讀取來的值
//1302讀
//不做注釋,比賽強行記憶
void ds1302_read()
{
unsigned int i;
unsigned char add;
add=0x81;
Write_Ds1302_Byte(0x8e,0x00); //寫保護關
for(i=0;i<7;i++)
{
time[i]=Read_Ds1302_Byte(add);
add=add+2;
}
Write_Ds1302_Byte(0x8e,0x80); //寫保護開
}
//1302寫
//不做注釋,比賽強行記憶
void ds1302_write()
{
unsigned int i;
unsigned char add;
add=0x80;
Write_Ds1302_Byte(0x8e,0x00);
for(i=0;i<7;i++)
{
Write_Ds1302_Byte(add,shijian[i]);
add=add+2;
}
Write_Ds1302_Byte(0x8e,0x80);
}
主函式中添加
extern time[]; //標示變數或者函式的定義在別的檔案中,提示編譯器遇到此變數和函式時在其他模塊中尋找其定義
extern time[];//標示變數或者函式的定義在別的檔案中,提示編譯器遇到此變數和函式時在其他模塊中尋找其定義
測驗結果:

整個代碼粘貼
ds1302.h
#ifndef __DS1302_H
#define __DS1302_H
void Write_Ds1302(unsigned char temp);
void Write_Ds1302_Byte( unsigned char address,unsigned char dat );
unsigned char Read_Ds1302_Byte( unsigned char address );
void ds1302_write();
void ds1302_read();
#endif
ds1302.c
#include <reg52.h>
#include <intrins.h>
sbit SCK=P1^7;
sbit SDA=P2^3;
sbit RST = P1^3; // DS1302復位
void Write_Ds1302(unsigned char temp)
{
unsigned char i;
for (i=0;i<8;i++)
{
SCK=0;
SDA=temp&0x01;
temp>>=1;
SCK=1;
}
}
void Write_Ds1302_Byte( unsigned char address,unsigned char dat )
{
//需增加
unsigned char num;
RST=0; _nop_();
SCK=0; _nop_();
RST=1; _nop_();
Write_Ds1302(address);
//需增加
num=(dat/10<<4)|(dat%10);
//替換為num
Write_Ds1302(num);
//Write_Ds1302(dat);
RST=0;
}
unsigned char Read_Ds1302_Byte ( unsigned char address )
{
//需增加
unsigned char dat_low,dat_high;
unsigned char i,temp=0x00;
RST=0; _nop_();
SCK=0; _nop_();
RST=1; _nop_();
Write_Ds1302(address);
for (i=0;i<8;i++)
{
SCK=0;
temp>>=1;
if(SDA)
temp|=0x80;
SCK=1;
}
RST=0; _nop_();
SCK=0; _nop_();
SCK=1; _nop_();
SDA=0; _nop_();
SDA=1; _nop_();
//需增加
/*以下是進制轉化*/
dat_high=temp/16;
dat_low=temp%16;
temp=dat_high*10+dat_low;
/*以上是進制轉化*/
return (temp);
}
/***************以下都是需要自己寫的*******************************/
unsigned int code shijian[]={50,59,23,0,0,0,0};//ds1302賦值為23:59:50
unsigned int time[7];//用于存放1302讀取來的值
//1302讀
//不做注釋,比賽強行記憶
void ds1302_read()
{
unsigned int i;
unsigned char add;
add=0x81;
Write_Ds1302_Byte(0x8e,0x00);
for(i=0;i<7;i++)
{
time[i]=Read_Ds1302_Byte(add);
add=add+2;
}
Write_Ds1302_Byte(0x8e,0x80);
}
//1302寫
//不做注釋,比賽強行記憶
void ds1302_write()
{
unsigned int i;
unsigned char add;
add=0x80;
Write_Ds1302_Byte(0x8e,0x00);
for(i=0;i<7;i++)
{
Write_Ds1302_Byte(add,shijian[i]);
add=add+2;
}
Write_Ds1302_Byte(0x8e,0x80);
}
main.c
#include <stc15f2k60s2.h>
#include "ds1302.h"
#define uchar unsigned char
#define uint unsigned int
uchar tab[] = {0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x82, 0xf8, 0x80, 0x90, 0xff,0xbf};
uchar dspbuf[] = {10,10,10,10,10,10,10,10};
extern time[];
void display();
void load();
void cls()
{
P2 = (P2 & 0x1f) | 0x80;
P0 = 0xff;
P2 = 0x1f;
P2 = (P2 & 0x1f) | 0xa0;
P0 = 0x00;
P2 = 0x1f;
}
void main()
{
cls();
AUXR = 0xc0;
TMOD = 0x00;
TL0 = 0xcd;
TH0 = 0xd4;
TR0 = 1;
ET0 = 1;
EA = 1;
ds1302_write();
while(1)
{
ds1302_read();
}
}
void time0() interrupt 1
{
display();
}
void load()
{
dspbuf[0] = time[2]/10;
dspbuf[1] = time[2]%10;
dspbuf[2] = 11;
dspbuf[3] = time[1]/10;
dspbuf[4] = time[1]%10;
dspbuf[5] = 11;
dspbuf[6] = time[0]/10;
dspbuf[7] = time[0]%10;
}
void display()
{
static unsigned char dspcom = 0;
load();
P2 = (P2 & 0x1f) | 0xe0;
P0 = 0xff;
P2 = 0x1f;
P2 = (P2 & 0x1f) | 0xc0;
P0 = 1 << dspcom;
P2 = 0x1f;
P2 = (P2 & 0x1f) | 0xe0;
P0 = tab[dspbuf[dspcom]];
P2 = 0x1f;
if(++dspcom == 8) dspcom = 0;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/227585.html
標籤:其他
上一篇:2020前端工程師的發展前景
下一篇:草地與石頭模型邊緣混合
