标题:
MSP430实现定时器的源程序
[打印本页]
作者:
达达君
时间:
2018-11-30 22:00
标题:
MSP430实现定时器的源程序
该程序使用到了TM1638,附件中有。
(上一篇文章中的程序包含了此程序的公共代码部分,但是我忘了把 tm1638.h 放到附件里面了,这次补上)
定时器初值设为00:02:15,结束后重置为23:59:59
源の代码:
#include
"TM1638.h"
#include
"msp430f6638.h"
#include
"stdint.h"
uint8_t
num[
8
];
//各个数码管显示的值
uint8_t
led_flag[
8
];
const
uint8_t
tab[]
=
{
0x3F
,
0x06
,
0x5B
,
0x4F
,
0x66
,
0x6D
,
0x7D
,
0x07
,
0x7F
,
0x6F
,
0x77
,
0x7C
,
0x39
,
0x5E
,
0x79
,
0x00
};
void
init_port
(
void
)
{
P3DIR
|=
BIT5;
P3DIR
|=
BIT4
+
BIT2;
P1DIR
|=
BIT0;
// ACLK set out to pins
P1SEL
|=
BIT0;
}
const
uint8_t
tab2[]
=
{
0x3F
,
0x06
,
0x5B
,
0x4F
,
0x66
,
0x6D
,
0x7D
,
0x07
,
0x7F
,
0x6F
,
0x77
,
0x7C
,
0x39
,
0x5E
,
0x79
,
0x71
};
void
TM1638_Write
(
unsigned
char
DATA)
//写数据函数
{
unsigned
char
i;
DIO_OUT;
for
(i
=
0
;i
<
8
;i
++
)
{
CLK_low;
if
(DATA
&
0x01
)
DIO_high;
else
DIO_low;
CLK_high;
DATA
>>=
1
;
}
}
unsigned
char
TM1638_Read
(
void
)
//读数据函数
{
unsigned
char
i;
unsigned
char
temp
=
0
;
DIO_IN;
STB_low;
for
(i
=
0
;i
<
8
;i
++
)
{
temp
>>=
1
;
CLK_low;
_delay_us
(
100
);
if
(DIO_READ)
temp
|=
0x80
;
else
temp
&=
0x7f
;
CLK_high;
}
return
temp;
}
void
Write_COM
(
unsigned
char
cmd)
//发送命令字
{
CLK_high;
STB_high;
STB_low;
TM1638_Write
(cmd);
STB_high;
}
void
Write_DATA
(
unsigned
char
add,
unsigned
char
DATA)
//指定地址写入数据
{
Write_COM
(
0x44
);
STB_low;
TM1638_Write
(
0xc0
|
add);
TM1638_Write
(DATA);
STB_high;
}
void
init_TM1638
(
void
)
{
unsigned
char
count
=
0
;
unsigned
char
times
=
1
;
while
(times
--
){
Write_allLED
(tab2[count]);
count
++
;
if
(count
==
16
)
count
=
0
;
_delay_ms
(
100
);
}
Write_allLED
(
0xff
);
_delay_ms
(
100
);
Write_allLED
(
0
);
}
void
delay
(){
for
(
int
i
=
0
; i
<
10
; i
++
)
for
(
int
j
=
0
; j
<
1000
; j
++
);
}
void
Write_allLED
(
unsigned
char
DATA){
unsigned
char
i;
Write_COM
(
0x8F
);
//显示控制
Write_COM
(
0x40
);
//数据命令
STB_low;
TM1638_Write
(
0xc0
);
for
(i
=
0
;i
<
16
;i
++
)
TM1638_Write
(DATA);
STB_high;
}
int
main
(
void
)
{
WDTCTL
=
WDTPW
+
WDTHOLD;
init_port
();
init_TM1638
();
int
hourA
=
0
;
int
hourB
=
0
;
int
minuteA
=
0
;
int
minuteB
=
2
;
int
secondA
=
1
;
int
secondB
=
5
;
while
(
1
) {
secondB
--
;
if
(secondB
==
-
1
) {
secondB
=
9
;
secondA
--
;
if
(secondA
==
-
1
) {
secondA
=
5
;
minuteB
--
;
if
(minuteB
==
-
1
) {
minuteB
=
9
;
minuteA
--
;
if
(minuteA
==
-
1
) {
minuteA
=
5
;
hourB
--
;
if
(hourB
==
-
1
) {
hourB
=
9
;
hourA
--
;
if
(hourA
==
-
1
) {
hourA
=
2
;
hourB
=
3
;
}
}
}
}
}
}
Write_DATA
(
4
*
2
,tab[hourA]);
Write_DATA
(
5
*
2
,tab[hourB]);
Write_DATA
(
6
*
2
,tab[
15
]);
Write_DATA
(
7
*
2
,tab[minuteA]);
Write_DATA
(
0
*
2
,tab[minuteB]);
Write_DATA
(
1
*
2
,tab[
15
]);
Write_DATA
(
2
*
2
,tab[secondA]);
Write_DATA
(
3
*
2
,tab[secondB]);
delay
();
}
}
sy2-3-1.zip
2018-11-30 22:42 上传
点击文件名下载附件
下载积分: 黑币 -5
1.94 KB, 下载次数: 7, 下载积分: 黑币 -5
定时器
作者:
达达君
时间:
2018-11-30 22:17
补充说明一下,主函数中的delay是我自己写的,是为了便于调试,并没有真正起到延时1s的作用
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1