新聞中心

EEPW首頁(yè) > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > 74HC595控制8位數(shù)碼管 51單片機(jī)

74HC595控制8位數(shù)碼管 51單片機(jī)

作者: 時(shí)間:2016-11-25 來源:網(wǎng)絡(luò) 收藏
74HC595是8位串行移位寄存器,亦能串行輸出控制下一級(jí)級(jí)聯(lián)芯片。關(guān)于級(jí)聯(lián)、595芯片具體我就不多介紹,大家自己查吧!下面直接貼代碼

此程序已成功移植到STM32平臺(tái),敬請(qǐng)關(guān)注!

本文引用地址:http://m.butianyuan.cn/article/201611/321353.htm

// 注意 此代碼沒有什么技術(shù)含量,也沒有按規(guī)范些,旨在說明595怎么控制,大家不要模仿。

// 此代碼刷新速度有點(diǎn)慢,注意想辦法優(yōu)化,time變量用定時(shí)器中斷處理,主函數(shù)不斷地刷新顯示函數(shù),這樣就不會(huì)有閃屏,當(dāng)然在STM32平臺(tái)毫無壓力

#include


sbit SCK = P3^2; // 數(shù)據(jù)輸入時(shí)鐘線,脈沖
sbit SI = P3^3; // 數(shù)據(jù)線
sbit RCK = P3^4; // 鎖存

unsigned char code SMG[10] = {0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8, 0x80, 0x90};
unsigned char Wei[8] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80};//位選
unsigned char Val[8] = {0};


void Output(void)
{
RCK = 0;
RCK = 1;
}


void Write_Byte(unsigned char dat)
{
unsigned char i = 0;
//每次發(fā)送一位數(shù)據(jù),8次發(fā)送完畢,先發(fā)送高位(切記)
for(i=0; i<8; i++)
{
SCK = 0;
SI = dat & 0x80;
SCK = 1;

dat <<= 1;
}
}


void Display(unsigned char * p)
{
unsigned char * pt = Wei;
//注意,先發(fā)送位選,因?yàn)榭刂莆贿x的595芯片在控制段選的595芯片下一級(jí)
Write_Byte(*(pt+0));//位選,
Write_Byte(SMG[*(p+0)]);//段選
Output();//發(fā)送到595 輸出

Write_Byte(*(pt+1));
Write_Byte(SMG[*(p+1)]);
Output();

Write_Byte(*(pt+2));
Write_Byte(SMG[*(p+2)]);
Output();

Write_Byte(*(pt+3));
Write_Byte(SMG[*(p+3)]);
Output();

Write_Byte(*(pt+4));
Write_Byte(SMG[*(p+4)]);
Output();

Write_Byte(*(pt+5));
Write_Byte(SMG[*(p+5)]);
Output();

Write_Byte(*(pt+6));
Write_Byte(SMG[*(p+6)]);
Output();

Write_Byte(*(pt+7));
Write_Byte(SMG[*(p+7)]);
Output();

}

int main(void)
{
unsigned long int time = 0;
unsigned int t = 0;

while(1)
{
t++;

if(100==t)
{
t= 0;
time++;

Val[0] = time / 10000000 % 10;
Val[1] = time / 1000000 % 10;
Val[2] = time / 100000 % 10;
Val[3] = time / 10000 % 10;
Val[4] = time / 1000 % 10;
Val[5] = time / 100 % 10;
Val[6] = time / 10 % 10;
Val[7] = time % 10;
}

Display(Val);
}

return 0;
}




評(píng)論


技術(shù)專區(qū)

關(guān)閉