**stc15f2k60s2芯片编程(时钟DS1302)**
main.c函数
#include
#include
#define uchar unsigned char
#define uint unsigned int
void shumaguan();
uchar code abile[]={0XC0,0XF9,0XA4,0XB0,0X99,0X92,0X82,0XF8,0X80,0X90,0XBF,0XFF};
unsigned char saomiao[]={0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF};
uchar yi,er,san,si,wu ,liu,qi,ba;
extern uchar shijian[7];
void Delay100us();
void readtime();
void dsinit();
void main()
{
P2=0XC0;P0=0X01;P2=0XFF;P0=0XFF;
P2=P2&0X1F|0XA0;
P0=0X00;
P2&=0X1F;
dsinit();
while(1)
{
readtime();
yi=shijian[2]/16;er=shijian[2]%16;san=10;
si=shijian[1]/16;wu=shijian[1]%16;liu=10;
qi=shijian[0]/16;ba=shijian[0]%16;
shumaguan();
Delay100us();
}
}
void shumaguan()
{
unsigned char i;
P2=P2&0x1f|0xe0;
P0=0xff;
P2=0x1f;
P2=P2&0x1f|0xc0;
P0=1<<i;
P2=0x1f;
P2=P2&0x1f|0xe0;
P0=saomiao[i];
P2=0x1f;
i++;
if(i==8) i=0;
saomiao[0]=abile[yi];
saomiao[1]=abile[er];
saomiao[2]=abile[san];
saomiao[3]=abile[si];
saomiao[4]=abile[wu];
saomiao[5]=abile[liu];
saomiao[6]=abile[qi];
saomiao[7]=abile[ba];
}
void Delay100us() //@11.0592MHz
{
unsigned char i, j;
_nop_();
_nop_();
i = 2;
j = 15;
do
{
while (--j);
} while (--i);
}
ds1302.c函数
#include
#include "ds1302.h"
#include
sbit SCK=P1^7;
sbit SDA=P2^3;
sbit RST = P1^3; // DS1302¸´Î»
void Write_Ds1302_Byte(unsigned char temp)
{
unsigned char i;
for (i=0;i<8;i++)
{
SCK=0;
SDA=temp&0x01;
temp>>=1;
SCK=1;
}
}
void Write_Ds1302( unsigned char address,unsigned char dat )
{
RST=0;
_nop_();
SCK=0;
_nop_();
RST=1;
_nop_();
Write_Ds1302_Byte(address);
Write_Ds1302_Byte(dat);
RST=0;
}
unsigned char Read_Ds1302 ( unsigned char address )
{
unsigned char i,temp=0x00;
RST=0;
_nop_();
SCK=0;
_nop_();
RST=1;
_nop_();
Write_Ds1302_Byte(address);
for (i=0;i<8;i++)
{
SCK=0;
temp>>=1;
if(SDA)
temp|=0x80;
SCK=1;
}
RST=0;
_nop_();
RST=0;
SCK=0;
_nop_();
SCK=1;
_nop_();
SDA=0;
_nop_();
SDA=1;
_nop_();
return (temp);
}
void readtime()
{
unsigned char i;
unsigned char address=0x81;
Write_Ds1302(0x8e,0x00);
for(i=0;i<7;i++)
{
shijian[i]=Read_Ds1302(address);
address=address+2;
}
Write_Ds1302(0x8e,0x80);
}
void dsinit()
{
uchar i;
uchar address=0x80;
Write_Ds1302(0x8e,0x00);
for(i=0;i<7;i++)
{
Write_Ds1302(address,ds[i]);
address=address+2;
}
Write_Ds1302(0x8e,0x80);
}
ds1302.h函数
#ifndef __DS1302_H
#define __DS1302_H
#define uchar unsigned char
#define uint unsigned int
void Write_Ds1302_Byte(unsigned char temp);
void Write_Ds1302( unsigned char address,unsigned char dat );
unsigned char Read_Ds1302 ( unsigned char address );
uchar shijian[7];
uchar code ds[]={0x50,0x59,0x23,0x10,0x10,0x01,0x16};
#endif