数码管显示DS1302的时钟我通通常采用的是一种指针传递参数的方式
下面放上代码
DS1302.c文件
#include "ds1302.h"
//写字节
void Write_Ds1302(unsigned char temp)
{
unsigned char i;
for (i=0;i<8;i++)
{
SCK = 0;
SDA = temp&0x01;
temp>>=1;
SCK=1;
}
}
//向DS1302寄存器写入数据
void Write_Ds1302_Byte( unsigned char address,unsigned char dat )
{
RST=0; _nop_();
SCK=0; _nop_();
RST=1; _nop_();
Write_Ds1302(address);
Write_Ds1302(dat);
RST=0;
}
//从DS1302寄存器读出数据
unsigned char Read_Ds1302_Byte ( unsigned char address )
{
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_();
return (temp);
}
void DS1302_SetTime(unsigned char *Time)
{
Write_Ds1302_Byte(0x8e,0x00);//关闭写保护
Write_Ds1302_Byte(0x84,Time[0]/10*16 + Time[0]%10);//将十进制数转为BCD码
Write_Ds1302_Byte(0x82,Time[1]/10*16 + Time[1]%10);
Write_Ds1302_Byte(0x80,Time[2]/10*16 + Time[2]%10);
Write_Ds1302_Byte(0x8e,0x80);//打开写保护
}
void DS1302_ReadTime(unsigned char *Time)
{//将时间格式从BCD码转化到十进制显示再传给指针
Time[0] = Read_Ds1302_Byte(0x85)/16*10+Read_Ds1302_Byte(0x85)%16;
Time[1] = Read_Ds1302_Byte(0x83)/16*10+Read_Ds1302_Byte(0x83)%16;
Time[2] = Read_Ds1302_Byte(0x81)/16*10+Read_Ds1302_Byte(0x81)%16;
}
DS1302.h源文件
#ifndef __DS1302_H
#define __DS1302_H
#include <STC15F2K60S2.H>
#include <intrins.h>
sbit SCK = P1^7;
sbit SDA = P2^3;
sbit RST = P1^3;
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_ReadTime(unsigned char *Time);
void DS1302_SetTime(unsigned char *Time);
#endif
Smg.c文件
#include <smg.h>
unsigned char smg_duan[] = {0xC0,0xF9,0xA4,0xB0,0x99,
0x92,0x82,0xF8,0x80,0x90,
0x00,0xBF};
unsigned char smg_wei[] = {0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
unsigned int i;
void smg_show(unsigned char pos,num/*,point*/)
{
P0 = 0xff;//清空P0
P2 = P2&0x1f | 0xe0;//1111 1111 Y7选择数码管显示
P2 &= 0x1f; //关闭锁存器
P0 = smg_wei[pos-1];//数码管的位选值
P2 = P2&0x1f | 0xc0;//1101 1111 Y6选择数码管位选
P2 &= 0x1f; //关闭锁存器
P0 = smg_duan[num]; //数码管段选
//if(point) P0 |= 0x7f;如果存在小数点则加上小数点
P2 = P2&0x1f | 0xe0; //1111 1111 Y7选择数码管显示
P2 &= 0x1f; //关闭锁存器
i = 200;while(i--);//根据数码管实际显示情况加一段延时
}
Smg.h文件
#include <STC15F2K60S2.H>
void smg_show(unsigned char pos,num);
main.c源文件
#include <STC15F2K60S2.H>
#include <ds1302.h>
#include <smg.h>
unsigned char Time[] = {23,59,59};
void main()
{
DS1302_SetTime(Time);
while(1)
{
DS1302_ReadTime(Time);
smg_show(1,Time[0]/10);
smg_show(2,Time[0]%10);
smg_show(4,Time[1]/10);
smg_show(5,Time[1]%10);
smg_show(7,Time[2]/10);
smg_show(8,Time[2]%10);
smg_show(3,11);
smg_show(6,11);
}
}
文章详细介绍了如何在嵌入式系统中通过C语言操作DS1302实时时钟模块,实现时间的读取和设置,并通过SMG库在数码管上显示。展示了Write_Ds1302、Write_Ds1302_Byte、Read_Ds1302_Byte等函数的具体用法。
2731

被折叠的 条评论
为什么被折叠?



