新聞中心

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

51單片機(jī)C編程(十五 74HC595控制四位數(shù)碼管)

作者: 時(shí)間:2016-11-27 來(lái)源:網(wǎng)絡(luò) 收藏


#include
#include

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

#define uchar unsigned char
#define uint unsigned int

uchar code DAT[10]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x02,0xf8,0x00,0x10};
sbit SDATA_595=P1^0;//串行數(shù)據(jù)輸入
sbit SCLK_595=P1^1;//移位時(shí)鐘脈沖
sbit RCK_595=P1^2;//輸出鎖存器控制脈沖

sbit P20=P2^0;
sbit P21=P2^1;
sbit P22=P2^2;
sbit P23=P2^3;
sbit P24=P2^4;
sbit P25=P2^5;
sbit P26=P2^6;
sbit P27=P2^7;
sbit P30=P3^0;
sbit P31=P3^1;

uchar temp;

void delay(int ms)
{
int k;
while(ms--)
{
for(k=0;k<250;k++)
{
_nop_();
_nop_();

}
}
}

void WR_595(void)//將顯示數(shù)據(jù)送入74HC595內(nèi)部移位寄存器
{
uchar j;
for(j=0;j<8;j++)
{
temp=temp<<1;
SDATA_595=CY;
SCLK_595=1;
_nop_();
_nop_();
SCLK_595=0;
}
}

void OUT_595(void)//將移位寄存器內(nèi)的數(shù)據(jù)所存到輸出寄存器并顯示
{
RCK_595=0;
_nop_();
_nop_();
RCK_595=1;//上升沿將數(shù)據(jù)送入輸出鎖存器
_nop_();
_nop_();
_nop_();
RCK_595=0;
}

void key_scan()
{
if(P20==0)
{
while(P20==0);
temp=DAT[1];
WR_595();
OUT_595();
}
if(P21==0)
{
while(P21==0);
temp=DAT[2];
WR_595();
OUT_595();
}
if(P22==0)
{
while(P22==0);
temp=DAT[3];
WR_595();
OUT_595();
}
if(P23==0)
{
while(P23==0);
temp=DAT[4];
WR_595();
OUT_595();
}
if(P24==0)
{
while(P24==0);
temp=DAT[5];
WR_595();
OUT_595();
}
if(P25==0)
{
while(P25==0);
temp=DAT[6];
WR_595();
OUT_595();
}
if(P26==0)
{
while(P26==0);
temp=DAT[7];
WR_595();
OUT_595();
}
if(P27==0)
{
while(P27==0);
temp=DAT[8];
WR_595();
OUT_595();
}
if(P30==0)
{
while(P30==0);
temp=DAT[9];
WR_595();
OUT_595();
}
if(P31==0)
{
while(P31==0);
temp=DAT[0];
WR_595();
OUT_595();
}
}

void main()
{
SCLK_595=0;
RCK_595=1;
while(1)
{
key_scan();
//uchar i;
//for(i=0;i<10;i++)
//{
// temp=DAT[i];//取顯示數(shù)據(jù)
//WR_595();
//OUT_595();
//delay(200);
//}
}
}



評(píng)論


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

關(guān)閉