当前位置:网站首页>[Blue Bridge Road -- bug free code] DS1302 time module code analysis
[Blue Bridge Road -- bug free code] DS1302 time module code analysis
2022-06-30 05:25:00 【The journey of a bald girl is the sea of stars】
initialization DS1302 The time of the module is 23:59:55, And read the time in real time
main.c
#include"STC15F2K60S2.h"
#include"ds1302.h"
typedef unsigned char uchar;
typedef unsigned int uint;
uchar dsp_code[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
uchar dsp_Time[8]={0xff,0xff,0xbf,0xff,0xff,0xbf};
uchar hour,min,sec;
void Timer0Init(void) //1 millisecond @11.0592MHz
{
AUXR |= 0x80; // Timer clock 1T Pattern
TMOD &= 0xF0; // Set timer mode
TL0 = 0xCD; // Set initial value of timing
TH0 = 0xD4; // Set initial value of timing
TF0 = 0; // eliminate TF0 sign
TR0 = 1; // Timer 0 Start timing
ET0=1;
EA=1;
}
void serviceTimer() interrupt 1
{
static uchar dsp_com=0;
P0=0;P2=0xc0;P2=0;
P0=dsp_Time[dsp_com];P2=0xe0;P2=0;
P0=1<<dsp_com;P2=0xc0;P2=0;
if(++dsp_com==8)dsp_com=0;
}
void Initsystem()
{
P0=0xff;P2=0x80;P2=0;
P0=0x00;P2=0xa0;P2=0;
}
void main()
{
Initsystem();
Set_Time(23,59,55);
Timer0Init();
while(1)
{
if(TH0<0xf0)
{
hour=Read_Ds1302_Byte(0x85);
min=Read_Ds1302_Byte(0x83);
sec=Read_Ds1302_Byte(0x81);
dsp_Time[0]=dsp_code[hour/16];
dsp_Time[1]=dsp_code[hour%16];
dsp_Time[3]=dsp_code[min/16];
dsp_Time[4]=dsp_code[min%16];
dsp_Time[6]=dsp_code[sec/16];
dsp_Time[7]=dsp_code[sec%16];
}
}
}
ds1302.c
/*
Program description : DS1302 The driver
Software environment : Keil uVision 4.10
Hardware environment : CT107 SCM comprehensive training platform 8051,12MHz
Japan period : 2011-8-9
*/
#include <reg52.h>
#include <intrins.h>
sbit SCK=P1^7;
sbit SDA=P2^3;
sbit RST = P1^3; // DS1302 Reset
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 )
{
RST=0;
_nop_();
SCK=0;
_nop_();
RST=1;
_nop_();
Write_Ds1302(address);
Write_Ds1302(dat);
RST=0;
}
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_();
RST=0;
SCK=0;
_nop_();
SCK=1;
_nop_();
SDA=0;
_nop_();
SDA=1;
_nop_();
return (temp);
}
void Set_Time(char hour,char min,char sec)
{
Write_Ds1302_Byte(0x8e,0x00);
Write_Ds1302_Byte(0x84,(hour/10)*16+hour%10);
Write_Ds1302_Byte(0x82,(min/10)*16+min%10);
Write_Ds1302_Byte(0x80,(sec/10)*16+sec%10);
Write_Ds1302_Byte(0x8e,0x80);
}
ds1302.h
#ifndef __DS1302_H
#define __DS1302_H
void Write_Ds1302_Byte(unsigned char temp);
void Write_Ds1302( unsigned char address,unsigned char dat );
unsigned char Read_Ds1302_Byte ( unsigned char address );
void Set_Time(char hour,char min,char sec);
#endif
边栏推荐
- 旋转框目标检测mmrotate v0.3.1 训练DOTA数据集(二)
- Unity 3D model operation and UI conflict Scrollview
- Assembly learning tutorial: accessing memory (3)
- 企事业单位源代码防泄露工作该如何进行
- Unity screenshot method
- Pytorch的安装以及入门使用
- Chapter 11 advanced data management of OpenGL super classic (version 7)
- Chapter 12 pipeline monitoring of OpenGL super classic (version 7)
- [notes] unity Scrollview button page turning
- Remote sensing image /uda:curriculum style local to global adaptation for cross domain remote sensing image segmentation
猜你喜欢
Unity project hosting platform plasticscm (learn to use 1)
Assembly learning tutorial: accessing memory (3)
[Motrix] download Baidu cloud files using Motrix
Unity + hololens publishing settings
RedisTemplate 常用方法汇总
Unity project hosting platform plasticscm (learn to use 2)
Unit asynchronous jump progress
Redistemplate common method summary
Database base (Study & review for self use)
Database SQL language 03 sorting and paging
随机推荐
Question mark (?) in Cron expression Use of
Log writing specification
Unity publishing /build settings
使用码云PublicHoliday项目判断某天是否为工作日
Unity + hololens common basic functions
Detailed explanation of the loss module of mmdet
Gradient clip in dqn
Installation and getting started with pytoch
Unity3d position the model, rotate, drag and zoom around the model to obtain the center point of the model
Nestjs中控制器和路由的配置使用
Why can transformer break into the CV world and kill CNN?
Nestjs introduction and environment construction
How can the international trading platform for frying US crude oil guarantee capital security?
Learning about functions QAQ
Remote sensing image /uda:curriculum style local to global adaptation for cross domain remote sensing image segmentation
Another download address for typro
Wechat applet training 2
[Blue Bridge Road -- bug free code] analysis of AT24C02 storage code
Untiy3d controls scene screenshots through external JSON files
GoLand No Tests Were Run : 不能使用 fmt.Printf() &lt;BUG&gt;