SaltyFish's Bowl

用定时器写完时钟后,开发板上的DS1302自然是不能放过,看了半天文档,差不多写出来了,特此记录一下下吧

前言

一个利用DS1302模块所写的一个简单的时钟,使用的是普中的开发板

只能够实现基本的计时功能

代码

main.c 主程序
ds1302.c 驱动ds1302模块
led_1602.c 驱动1602液晶

main.c

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include "led_1602.h"
#include "ds1302.h"
#include <at89x52.h>

uint i=0;


void display_time_week(uint cl_week,uint w_local);
void display_time_date(uint *timep);

void delay (uint xms);

uchar dis_wel[]="Welcome!";
uchar dis_blog[]="www.erf172.tk";
uchar dis_week[]="SunMonTueWedThuFriSat";

extern uint *TIME_P;

uint time[7]={0x00,0x51,0x17,0x11,0x09,3,0x19};

void main()
{
TIME_P=time;

_1602_init();
_1602_command(0x80);

while(dis_blog[i]!='\0')
{
_1602_data(dis_blog[i]);
i++;
}

_1602_command(0xc0);
i=0;

while(dis_wel[i]!='\0')
{
_1602_data(dis_wel[i]);
i++;
}

delay(5000);

Ds1302Init();

_1602_command(1);



while (1){

Ds1302ReadTime();

display_time_date(TIME_P);




}

}

void display_time_date(uint *timep)
{


uint i=0;
uchar time_display_date[10]={'2','0',0,0,'/',0,0,'/',0,0};
uchar time_display_clock[8]={0,0,':',0,0,':',0,0};
uchar time_display_week=0;

time_display_date[2]=*(timep+6)/16+'0';
time_display_date[3]=*(timep+6)%16+'0';

time_display_date[5]=*(timep+4)/16+'0';
time_display_date[6]=*(timep+4)%16+'0';

time_display_date[8]=*(timep+3)/16+'0';
time_display_date[9]=*(timep+3)%16+'0';

time_display_clock[0]=*(timep+2)/16+'0';
time_display_clock[1]=*(timep+2)%16+'0';

time_display_clock[3]=*(timep+1)/16+'0';
time_display_clock[4]=*(timep+1)%16+'0';

time_display_clock[6]=*(timep+0)/16+'0';
time_display_clock[7]=*(timep+0)%16+'0';

time_display_week=*(timep+5)%7;

_1602_command(0x83);

for ( i = 0; i < 10; i++)
{
_1602_data(time_display_date[i]);
}

_1602_command(0xc6);

for ( i = 0; i < 8; i++)
{
_1602_data(time_display_clock[i]);
}

_1602_command(0xc2);
_1602_data(time_display_week);

display_time_week(time_display_week,0xc2);
}

void display_time_week(uint cl_week,uint w_local)
{

_1602_command(w_local);
uint j=0;
j=cl_week*3;
while(j<=cl_week*3+2)
{
_1602_data(dis_week[j]);
j++;
}

}

void delay(uint xms)
{
unsigned int x,y;
for(x=xms;x>0;x--)
for(y=110;y>0;y--);
}

ds1302.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#ifndef __DS1302_H_
#define __DS1302_H_


#include <at89x52.h>


#ifndef uchar
#define uchar unsigned char
#endif

#ifndef uint
#define uint unsigned int
#endif


/* 定义一下nop,延时一个机器周期 */
#define __nop() __asm__ ("NOP")

__sbit __at 0xB4 _DSIO;
__sbit __at 0xB5 _RST;
__sbit __at 0xB6 _SCLK;


void Ds1302Write(uchar ds1302_addr_w, uchar ds1302_data);
uchar Ds1302Read(uchar ds1302_addr_r);
void Ds1302Init();
void Ds1302ReadTime();



extern uchar clock_address_w[7];
extern uchar clock_address_r[7];

#endif

ds1302.c

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include "ds1302.h"


uint *TIME_P;


uchar clock_address_w[7]={0x80,0x82,0x84,0x86,0x88,0x8a,0x8c};
uchar clock_address_r[7]={0x81,0x83,0x85,0x87,0x89,0x8b,0x8d};

uchar *clock_address_p;


void Ds1302Write(uchar ds1302_addr_w, uchar ds1302_data)
{
uint i=0;

_RST=0;
__nop();

_SCLK=0;
__nop();
_RST=1;
__nop();

for ( i = 0; i < 8; i++)
{
_DSIO=ds1302_addr_w&0x01;
ds1302_addr_w>>=1;

_SCLK=1;
__nop();
_SCLK=0;
__nop();

}

for ( i = 0; i < 8; i++)
{
_DSIO=ds1302_data&0x01;
ds1302_data>>=1;

_SCLK=1;
__nop();
_SCLK=0;
__nop();

}

_RST=0;
__nop();
}


uchar Ds1302Read(uchar ds1302_addr_r)
{
uint i=0,ds1302_data_r,temp;

_RST=0;
__nop();

_SCLK=0;
__nop();
_RST=1;
__nop();


for(i=0; i<8; i++)
{
_DSIO = ds1302_addr_r & 0x01;
ds1302_addr_r >>= 1;
_SCLK = 1;
__nop();
_SCLK = 0;
__nop();
}

__nop();

for ( i = 0; i < 8; i++)
{
temp=_DSIO;
ds1302_data_r= (ds1302_data_r>>1) | (temp<<7);

_SCLK=1;
__nop();
_SCLK=0;
__nop();
}

_RST=0;
__nop();
_SCLK=1;
__nop();
_DSIO=0;
__nop();
_DSIO=1;
__nop();


return ds1302_data_r;

}


void Ds1302Init()
{

uint i=0;
Ds1302Write(0x8e,0);
clock_address_p=clock_address_w;
for ( i = 0; i < 7; i++)
{
Ds1302Write(*(clock_address_p+i),*(TIME_P+i));
}
Ds1302Write(0x8e,0x80);

}


void Ds1302ReadTime()
{

uint i=0;
clock_address_p=clock_address_r;
for ( i = 0; i < 7; i++)
{
*(TIME_P+i)=Ds1302Read(*(clock_address_p+i));
}

}

led_1602.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#ifndef _LED_1602_H_
#define _LED_1602_H_

#include <at89x52.h>

#ifndef uchar
#define uchar unsigned char
#endif

#ifndef uint
#define uint unsigned int
#endif


__sbit __at 0xA6 _LED_RS;
__sbit __at 0xA5 _LED_RW;
__sbit __at 0xA7 _LED_E;

#define LED_IO P0



void _1602_busy();
void _1602_init();
void _1602_command(uint com);
void _1602_data(uint data);



#endif

led_1602.c

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include "led_1602.h"


void _1602_init()
{
P1=P1&0xf0;
_1602_command(0x38);
_1602_command(0x0c);
_1602_command(0x06);
_1602_command(0x01);

}


void _1602_command(uint com)
{
_1602_busy();
_LED_RS=0;
_LED_RW=0;
LED_IO=com;
_LED_E=1;
_LED_E=0;



}


void _1602_data(uint data)
{
_1602_busy();
_LED_RS=1;
_LED_RW=0;
LED_IO=data;
_LED_E=1;
_LED_E=0;

}


void _1602_busy() //忙检测函数,判断bit7是0,允许执行;1禁止
{
unsigned char sta; //
LED_IO = 0xff;
_LED_RS = 0;
_LED_RW = 1;
do
{
_LED_E = 1;
sta = LED_IO;
_LED_E = 0; //使能,用完就拉低,释放总线
}while(sta & 0x80);
}

makefile

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
src=$(wildcard ./*.c)

target=project

cc=sdcc
cflags=-mmcs51 --model-large


main.hex:main.ihx
packihx main.ihx > main.hex


main.ihx: main.c ds1302.rel led_1602.rel
$(cc) $(cflags) main.c ds1302.rel led_1602.rel




ds1302.rel:ds1302.c
$(cc) $(cflags) -c ds1302.c

led_1602.rel:led_1602.c
$(cc) $(cflags) -c led_1602.c



.PHONY:clean
clean:
-rm -rf *.lk *.bin *.asm *.lst *.mem *.rst *.lnk *.rel *.sym *.ihx *.hex *.map

运行截图

https://kod.erf172.tk/index.php?user/publicLink&fid=2b64-1p3OoyDXWfkpHeHDgkDd4mr6K15WMiupINtI7hO3TGBVbdr0tdR3ziLEju7c64IRUEM6e0X-PrhaHzZUqtZXRk9rw0Ls70r-OGjQ8JaqChdKI2_z9TUkZqeFvUIzYPapxj5mkdXWWHpTw&file_name=/ds1302.jpeg


扫码领红包

 评论




博客内容遵循 署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0) 协议

本站使用 Volantis 作为主题 , 总访问量为 字数统计:26.6k。 载入天数...载入时分秒...