|
靜態(tài)數(shù)碼管
#include<reg52.h>
typedef unsigned char u8;
typedef unsigned int u16;
sbit LSA=P2^2;
sbit LSB=P2^3;
sbit LSC=P2^4;
u8 code smgduan[]={0x06,0x4f,0x6d,0x07,0x6f};
void delay(u16 i)
{
while(i--);
}
void display()
{
u8 a;
for(a=0;a<5;a++)
{
P0=smgduan[a];
delay(100000);
P0=0x00;
}
}
void main()
{
LSA=0;
LSB=0;
LSC=0;
while(1)
{
display();
}
}
矩陣鍵盤(pán)控制led
#include "reg52.h"
#include<intrins.h> //因?yàn)橐玫阶笥乙坪瘮?shù),所以加入這個(gè)頭文件
typedef unsigned int u16;
typedef unsigned char u8;
#define LED P2
#define GPIO_KEY P1
u8 KeyValue; //用來(lái)存放讀取到的鍵值
u8 code led[]={0xfe,0xfd,0xfb,0xf7,0xef };//led亮燈位置
/*******************************************************************************
* 函 數(shù) 名 : delay
* 函數(shù)功能 : 延時(shí)函數(shù),i=1時(shí),大約延時(shí)10us
*******************************************************************************/
void delay(u16 i)
{
while(i--);
}
/*******************************************************************************
* 函 數(shù) 名 : KeyDown
* 函數(shù)功能 : 檢測(cè)有按鍵按下并讀取鍵值
* 輸 入 : 無(wú)
* 輸 出 : 無(wú)
*******************************************************************************/
void KeyDown(void)
{
char a=0;
GPIO_KEY=0x0f;
if(GPIO_KEY!=0x0f)//讀取按鍵是否按下
{
delay(1000);//延時(shí)10ms進(jìn)行消抖
if(GPIO_KEY!=0x0f)//再次檢測(cè)鍵盤(pán)是否按下
{
//測(cè)試列
GPIO_KEY=0X0F;
switch(GPIO_KEY)
{
case(0X07): KeyValue=0;break;
case(0X0b): KeyValue=1;break;
case(0X0d): KeyValue=2;break;
case(0X0e): KeyValue=3;break;
}
//測(cè)試行
GPIO_KEY=0XF0;
switch(GPIO_KEY)
{
case(0X70): KeyValue=KeyValue;break;
case(0Xb0): KeyValue=KeyValue+4;break;
case(0Xd0): KeyValue=KeyValue+8;break;
case(0Xe0): KeyValue=KeyValue+12;break;
}
while((a<50)&&(GPIO_KEY!=0xf0)) //檢測(cè)按鍵松手檢測(cè)
{
delay(1000);
a++;
}
}
}
}
/*******************************************************************************
* 函 數(shù) 名 : main
* 函數(shù)功能 : 主函數(shù)
* 輸 入 : 無(wú)
* 輸 出 : 無(wú)
*******************************************************************************/
void main()
{
u8 i;
while(1)
{
KeyDown(); //按鍵判斷函數(shù)
LED=led[KeyValue];
if(LED==0xef)
{
delay(50000); //大約延時(shí)450ms
while(1)
{
/* for(i=0;i<8;i++)
{
P2=~(0x01<<i); //將1右移i位,然后將結(jié)果取反賦值到P2口
delay(50000); //大約延時(shí)450ms
}
*/
for(i=0;i<3;i++) //將led左移一位
{
LED=_crol_(LED,1);
delay(50000); //大約延時(shí)450ms
}
for(i=0;i<3;i++) //將led右移一位
{
LED=_cror_(LED,1);
delay(50000); //大約延時(shí)450ms
}
}
}
}
}
|
|