⑴ 單片機動態數碼管程序怎麼寫
從靜態顯示和動態顯示的區別說起,把動態顯示佔用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);
}
}