和上一篇文章一样,用到了 TM1638。计时器初值设为 23:57:45,计时到 23:59:59 时重置为 0点 0分 0秒。
(和上次的定时器一样,主函数中的delay是我自己写的,是为了便于调试,并没有真正起到延时1s的作用)
源の代码:
#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;
}
void main(void) {
WDTCTL = WDTPW+WDTHOLD;
init_port();
init_TM1638();
int hourA = 2;
int hourB = 3;
int minuteA = 5;
int minuteB = 7;
int secondA = 4;
int secondB = 5;
while(1) {
secondB++;
if(secondB == 10) {
secondB = 0;
secondA++;
if(secondA == 6) {
secondA = 0;
minuteB++;
if(minuteB == 10) {
minuteB = 0;
minuteA++;
if(minuteA == 6) {
minuteA = 0;
hourB++;
if(hourB == 10) {
hourB = 0;
hourA++;
}
if(hourA == 2 && hourB == 4) {
hourA = 0;
hourB = 0;
}
}
}
}
}
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();
}
}
上一篇:MSP430f5529之PWM控制舵机程序
下一篇:MSP430单片机AD9851 AD9850源码
推荐阅读最新更新时间:2024-11-13 06:08
设计资源 培训 开发板 精华推荐
- OP113ESZ耳机输出放大器多媒体声音编解码器典型应用
- 【RA】瑞萨Mini开发板冰墩墩小夜灯-402304A
- 【初】NUCL-XHP70移动光源
- LTC2637-HMI12 四通道、12 位数模转换器的典型应用
- FEBFLS0116_L32U003A,基于 FLS0116 2.7 W LED 驱动器的通用线路评估板
- 用于手机的 1.8V、3.3V ADC 差分到单端转换
- NIS5420MT6GEVB:NIS5420MT6GEVB:12 V eFuse 评估板,自动重试
- FEBFSL4110LR-CS01U06A-GEVB:6.0 W 辅助电源
- LT2940IMS 12.5W PWM热源典型应用电路
- ICE1PCS01 独立功率因数校正 (PFC) 控制器在连续导通模式 (CCM) 下的典型应用