⑴ 单片机动态数码管程序怎么写
从静态显示和动态显示的区别说起,把动态显示占用IO少,利用视觉残留的方法说清楚就可以啊.
附上电路图和程序供你参考,别忘记给分哦.
#include "STC12C5A.h" //包含头文件
/***数字编码表 0~9***/
unsigned char const seg[10] = {0xfc,0x60,0xda,0xf2,0x66,0xb6,0xbe,0xe0,0xfe,0xf6}; //段选
unsigned char const position[]={1,2,4,8}; //位选
sbit HC595_SCK = P0^4;
sbit HC595_RCK = P0^5;
sbit HC595_RST = P0^6;
sbit HC595_DAT = P0^7;
//us延时
void delay_us(unsigned int t)
{
while(t--);
}
//延时函数(24M 晶振下延时 1ms)
void delay_ms(unsigned int time)
{
unsigned int t;
for(;time>0;time--)
{
t = 1500;
while(t--);
}
}
//向 HC595 发送一个字节
void HC595_send_byte(unsigned char byte)
{
unsigned char i,temp;
for(i=0;i<=7;i++)
{
temp = byte&1<<i;
if(temp)
{
HC595_DAT = 1; //数据线
}
else
{
HC595_DAT=0;
}
//下面是写时序
HC595_SCK=1; //SCK(11 脚)
delay_us(1);
HC595_SCK=0;
delay_us(1);
}
HC595_RCK=0; //RCK(12 脚)
delay_us(1);
HC595_RCK=1;
}
//显示一个数 num
void SMG_ShowNum(unsigned char num)
{
HC595_send_byte(seg[num]);
}
void SMG_Display(unsigned int dat)
{
SMG_ShowNum(dat/1000);
P0 = (P0&0xf0)|position[0];
delay_ms(2);
SMG_ShowNum((dat%1000)/100);
P0 = (P0&0xf0)|position[1];
delay_ms(2);
SMG_ShowNum(dat%100/10);
P0 = (P0&0xf0)|position[2];
delay_ms(2);
SMG_ShowNum(dat%10);
P0 = (P0&0xf0)|position[3];
delay_ms(2);
}
//主函数
void main()
{
unsigned int num=0;
unsigned char time=0;
P0=0X00; //初始化低四位为高电平,使能四位数码管
HC595_RST=0; //HC595 的复位端,不能让他复位
while(1)
{
num = (num+1)%10000; //动态显示 0~9999
time = 100;
while(time--)
SMG_Display(num);
}
}