本文中使用外设:7SEG-MPX4-CA*2(四位七段共阴极led数码管),74HC573*2(带使能端8线锁存器),RESPACK-8(10k排阻),
#include
typedef unsigned char uchar;
typedef unsigned int uint;
sbit seg_sel = P1^4;
sbit bit_sel = P1^5; // P14 P15接使能段,即控制端
#define DATA P0 // P0接数码管
void delay(uint num)
{
uint i,j;
for(i=num; i>0; i--)
for(j=110; j>0; j--);
}
void main(void)
{
seg_sel = 0;
bit_sel = 0;
DATA = 0x00;
while(1)
{
u8 seg_set = 0xC0;
u8 bit_set = 0x80;
P0 = seg_set; //数值设置
seg_sel = 1; //置1读取
seg_sel = 0; //置0锁存
delay(10); // 基本定时稳定程序
P0 = bit_set; //数码管选择
bit_sel = 1;
bit_sel = 0;
}
}
关于74HC573
关于LE端:Level Enabled(L可能是Level的意思,电平)
LE = 1时,即高电平使能,使输入能影响输出,即同步;
LE = 0时,即低电平失能,使输入不能影响输出,即锁存。
代码优化:
#include
typedef unsigned char uchar;
typedef unsigned int uint;
sbit seg_sel = P1^4;
sbit bit_sel = P1^5; // P14 P15接使能段,即控制端
#define DATA P0 // P0接数码管
void delay(uint num)
{
uint i,j;
for(i=num; i>0; i--)
for(j=110; j>0; j--);
}
void main(void)
{
P0 = 0xff; // 初始化
P1 = 0xff;
seg_sel = 0;
bit_sel = 0;
while(1)
{
u8 seg_set = 0xC0;
u8 bit_set = 0x80;
seg_sel = 1; //置1读取
P0 = seg_set; //数值设置
seg_sel = 0; //置0锁存
delay(5); // 基本定时稳定程序
P0 = bit_set; //数码管选择
bit_sel = 1;
bit_sel = 0;
delay(5);
}
}