二、51单片机实现0到99计时
- 51单片机实现0到99计时
- 51单片机设计0~99秒秒表_单片机程序设计
- 51单片机时钟如何倒计时
- 51单片机简易时钟的实现
- 51单片机-定时器
ps使用时钟频率,计时并不准确
单片机是普中STC89C516RD+
实现代码
#include <REGX52.H>
typedef unsigned int u16; // 对系统默认数据类型进行重定义
typedef unsigned char u8;
// 0~9数字
unsigned char numbers[] = {0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F};
sbit LSA = P2 ^ 2;
sbit LSB = P2 ^ 3;
sbit LSC = P2 ^ 4;
void delay_10us(u16 ten_us)
{
while (ten_us--)
;
}
void wz(u8 i) // 此显示位有8位
{
switch (i) // 位选
{
case 0:
LSC = 1;
LSB = 1;
LSA = 1;
break;
case 1:
LSC = 1;
LSB = 1;
LSA = 0;
break;
case 2:
LSC = 1;
LSB = 0;
LSA = 1;
break;
case 3:
LSC = 1;
LSB = 0;
LSA = 0;
break;
case 4:
LSC = 0;
LSB = 1;
LSA = 1;
break;
case 5:
LSC = 0;
LSB = 1;
LSA = 0;
break;
case 6:
LSC = 0;
LSB = 0;
LSA = 1;
break;
case 7:
LSC = 0;
LSB = 0;
LSA = 0;
break;
}
}
void LED_draw(unsigned char position, number) // 每一帧进行绘制
{
wz(position); // LED的位置
P0 = numbers[number]; // 显示数字 段选
delay_10us(300);
P0 = 0x00; // 消音
}
void main()
{
u16 time = 0, currentNumber = 0;
while (1) {
if (currentNumber < 10)
LED_draw(0, currentNumber % 10); // 个位
else {
LED_draw(0, currentNumber / 10); // 十位
LED_draw(1, currentNumber % 10); // 个位
}
time++;
if (time == 50) { // 用于控制间隔
time = 0;
currentNumber++;
if (currentNumber == 100)
currentNumber = 0; // reset number
}
}
}