一作煌、功能介紹
用數(shù)碼管顯示按鍵的次數(shù)隨之增加款违。
二向胡、程序
#include "reg52.h"
#define uchar unsigned char
#define uint unsigned int
sbit C1 = P2^7;
sbit C2 = P2^6;
sbit C3 = P2^5;
sbit C4 = P2^4;
sbit D1 = P1^0;
sbit D2 = P1^1;
sbit D3 = P1^2;
sbit D4 = P1^3;
sbit D5 = P1^4;
sbit D6 = P1^5;
sbit D7 = P1^6;
sbit D8 = P1^7;
sbit SW1 = P3^4;
sbit SW2 = P3^3;
sbit SW3 = P2^1;
//?¨ò?ò???±íê?SW2°′?ü°′??×′ì?μ?±ê??±?á?
unsigned char Stat_SW3 = 0;
//?¨ò?12??êy??1ü?TD?êyμ?μ?êy×??????μ
unsigned char SMGNoDot_CA[9] = {0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x82, 0xf8, 0x80};
unsigned char s1 = 0, s2 = 0;
unsigned char i;
void delay(uint ms)
{
uchar i;
uint? j;
for(j=0;j<ms;j++)
{
for(i=0;i<125;i++)
{;}
}
}
/*==================êy??1ü?ˉì???ê?רó??óê±oˉêy==================*/
void DelaySMG(unsigned int t)
{
while(t--);
}
/*=====================μ¥??êy??1ü??ê?oˉêy=======================*/
void DisPlaySMG_Bit(unsigned char pos, unsigned char dat)
{
P0 = 0xff; //??òt
switch(pos) //????êy??1ü????
{
case 1:
C1 = 1; C2 = 0; C3 = 0; C4 = 0;
break;
case 2:
C1 = 0; C2 = 1; C3 = 0; C4 = 0;
break;
case 3:
C1 = 0; C2 = 0; C3 = 1; C4 = 0;
break;
case 4:
C1 = 0; C2 = 0; C3 = 0; C4 = 1;
break;
}
P0 = dat; //ê?3?êy??1ü??ê?μ??úèY
}
/*====================4??êy??1ü?ˉì???ê?oˉêy======================*/
void DisPlaySMG()
{
DisPlaySMG_Bit(1, SMGNoDot_CA[s1]);
DelaySMG(100);
DisPlaySMG_Bit(2, 0xbf);
DelaySMG(100);
DisPlaySMG_Bit(3, 0xbf);
DelaySMG(100);
DisPlaySMG_Bit(4, SMGNoDot_CA[s2]);
DelaySMG(100);
}
/*======================°′?üé¨?è′|àíoˉêy=======================*/
void Scan_Keys()
{
//SW1°′?üé¨?è′|àí
if(SW1 == 0)
{
DelaySMG(200);
if(SW1 == 0)
{
while(SW1 == 0)
{
DisPlaySMG();
}
s1++;
if(s1 == 9)
{
s1 = 0;
}
}
}
//SW2°′?üé¨?è′|àí
if(SW2 == 0)
{
DelaySMG(200);
if(SW2 == 0)
{
while(SW2 == 0)
{
DisPlaySMG();
}
s2++;
if(s2 == 9)
{
s2 = 0;
}
}
}
//SW3°′?üé¨?è′|àí
if(SW3 == 0) //é¨?èSW1°′?üê?è?D?o?
{
delay(200); //è¥???ˉ′|àí
if(SW3 == 0) //è·è?SW1°′?ü°′??D?o?
{
while(SW3 == 0); //μè′y°′?ü?é?a
{
? D1=0;
delay(1000);
D2=0;
delay(1000);
D3=0;
delay(1000);
D4=0;
delay(1000);
D5=0;
delay(1000);
D6=0;
delay(1000);
D7=0;
delay(1000);
D8=0;
delay(1000);
Stat_SW3 = 0;
}
}
}
}
/*==========================?÷oˉêy============================*/
void main()
{
while(1)
{
DisPlaySMG();
Scan_Keys();
}
}
三琐旁、結(jié)構(gòu)圖
四倍靡、流程圖
五、小結(jié)
這次讓我們自由設(shè)計(jì)程序厢汹,實(shí)現(xiàn)不同的功能缎讼,我想設(shè)計(jì)第三個(gè)按鍵控制燈光閃爍。