1、选择不同接口代码,传参4是LED小灯接口,5是蜂鸣器继电器,6是数码管位置,7是数码管显示的数值
//选择接口
void SelectHC573(unsigned char n)
{
switch(n)
{
case 4:
P2=(P2&0x1f)|0x80;//LED
break;
case 5:
P2=(P2&0x1f)|0xa0;//蜂鸣器
break;
case 6:
P2=(P2&0x1f)|0xc0;//数码管位置
break;
case 7:
P2=(P2&0x1f)|0xe0;//数码管数值
break;
}
}
//流水灯
void LED(unsigned char channl)
{
unsigned char i;
SelectHC573(4);
P0=0xff;
if(channl)//右到左
{
for(i=0;i<8;++i)
{
P0>>=1;
Delay(10000);
}
for(i=0;i<8;++i)
{
P0=~(~P0>>1);
Delay(10000);
}
}
else//左到右
{
for(i=0;i<8;++i)
{
P0<<=1;
Delay(10000);
}
for(i=0;i<8;++i)
{
P0=~(~P0<<1);
Delay(10000);
}
}
}
//数码管对应的值
unsigned char code LedChar[]={
0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0x88,0x83,0xC6,0xA1,0x86,0x8E,0xBF};
//数码管显示
void DisplaySMG(unsigned char value,unsigned char pos)
{
SelectHC573(6);
P0=0x01<<pos;
SelectHC573(7);
P0=value;
}
//数码管的延迟
void DelaySMG(unsigned int t)
{
while(t--)
{
ShowSMG(num);
}
}
//数码管显示的数字
void ShowSMG(unsigned int n)
{
signed char i;
for(i=7;i>=0;--i)
{
DisplaySMG(LedChar[n%10],i);
n/=10;
Delay(500);
}
}
4、关掉蜂鸣器
SelectHC573(5); P0=0x00;
5、键盘初始化
sfr P4=0xc0;
sbit R1=P3^0;
sbit R2=P3^1;
sbit R3=P3^2;
sbit R4=P3^3;
sbit C4=P3^4;
sbit C3=P3^5;
sbit C2=P4^2;
sbit C1=P4^4;
void SetKey(unsigned char i)
{
switch(i)
{
case 0:R1=0;R4=1;break;
case 1:R2=0;R1=1;break;
case 2:R3=0;R2=1;break;
case 3:R4=0;R3=1;break;
}
}
void ScanKey()
{
unsigned char i;
for(i=0;i<4;++i)
{
SetKey(i);
if(C2==0)
{
while(C2==0)
{
ShowSMG(num);
}
if(i==3)
{
num=0;
num1=0;
}
else
{
num*=10;
num+=3*i+1;
}
}
else if(C3==0)
{
while(C3==0)
{
ShowSMG(num);
}
if(i==3)
{
num*=10;
}
else
{
num*=10;
num+=3*i+2;
}
}
else if(C4==0)
{
while(C4==0)
{
ShowSMG(num);
}
if(i==3)
{
count();
}
else
{
num*=10;
num+=3*i+3;
}
}
else if(C1==0)
{
while(C1==0)
{
ShowSMG(num);
}
num1=num;
num=0;
state=i;
}
}
}
6、定时器
void InitTimer0()
{
TMOD=0x01;//选择T0
TH0=(65535-50000)/256;
TL0=(65535-50000)%256;
ET0=1;
EA=1;
TR0=1;
}
void ServiceTimer0() interrupt 1//0外部中断 1是定时器0 2是外部中断1 3是定时器1 4是串口中断
{
TH0=(65535-50000)/256;//50ms
TL0=(65535-50000)%256;
}
void InitUart()
{
TMOD=0x20;
TH1=0xfd;
TL1=0xfd;
TR1=1;
SCON=0x50;
AUXR=0x8e;
ES=1;//串口
EA=1;//中断器
}
//串口中断器
void SeviceUart() interrupt 4
{
if(RI==1)
{
command=SBUF;
RI=0;
}
}
//发送单个字节
void SendByte(unsigned char dat)
{
SBUF=dat;
while(TI==0);
TI=0;
}
//发送字符串
void SendString(unsigned char *str)
{
while(*str!='\0')
{
SendByte(*str++);
}
}
//接受指令
void Working()
{
if(command!=0x00)
{
switch(command&0xf0)
{
case 0xa0:
P0=0xff;
P0=(P0|0x0f)&(~command|0xf0);//µÍλ
command=0x00;
break;
case 0xb0:
P0=0xff;
P0=(P0|0xf0)&(~command<<4|0x0f);//¸ßλ
command=0x00;
break;
case 0xc0:
SendString("欢迎来到我的单片机");
command=0x00;
break;
}
}
}
#include
#include
sbit S4=P3^3;
sbit S7=P3^0;
unsigned char pwm=0;
unsigned char count=0;
unsigned char pwm_duty=0;
unsigned char stat_go=0;
unsigned char stat=0;
unsigned char led_go=0;
unsigned char key_puse=0;
unsigned char times=0;
unsigned char code LedChar[]={
0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,
0xF8,0x80,0x90,0x88,0x83,0xC6,0xA1,0x86,0x8E,0xBF};
void Init_Timer0()
{
TMOD=0x01;
TH0=(65535-1000)/256;
TL0=(65535-1000)%256;
ET0=1;
TR0=1;
EA=1;
}
void Delay(unsigned char t)
{
while(t--);
}
void DisplaySMG(unsigned char pos,unsigned char value)
{
XBYTE[0xe000] = 0xff;
XBYTE[0xc000]=0x01<<pos;
XBYTE[0xe000]=value;
}
void ShowSMG(unsigned char pos,unsigned value)
{
DisplaySMG(0,LedChar[pos]);
Delay(200);
DisplaySMG(6,LedChar[value]);
Delay(200);
DisplaySMG(7,LedChar[0]);
Delay(200);
DisplaySMG(0,0xff);
DisplaySMG(6,0xff);
DisplaySMG(7,0xff);
}
void ServiceTimer0() interrupt 1
{
TH0=(65535-1000)/256;
TL0=(65535-1000)%256;
if(stat_go==0)
{
XBYTE[0x8000]=0xe7;
return;
}
pwm++;
if(pwm<=pwm_duty)
{
XBYTE[0x8000]=~(0x01<<stat);
}
else if(pwm<=10)
{
XBYTE[0x8000]=0xff;
}
else
{
XBYTE[0x8000]=~(0x01<<stat);
pwm=0;
if(key_puse==0)
{
times++;
}
}
}
void LED_Control()
{
if(times==5)
{
times=0;
if(led_go==0)
{
pwm_duty++;
if(pwm_duty==11)
{
pwm_duty=10;
led_go=1;
}
}
else if(led_go==1)
{
pwm_duty--;
if(pwm_duty==255)
{
pwm_duty=0;
led_go=0;
if(stat_go==1)
{
stat++;
stat%=8;
}
else if(stat_go==2)
{
stat--;
if(stat==255) stat=7;
}
}
}
}
}
void Scan_Keys()
{
if(S4==0)
{
Delay(100);
if(S4==0)
{
while(S4==0)
{
key_puse=1;
}
key_puse=0;
stat_go++;
if(stat_go==3) stat_go=1;
}
}
if(S7==0)
{
Delay(100);
if(S7==0)
{
while(S7==0)
{
key_puse=1;
ShowSMG(stat+1,pwm_duty);
}
key_puse=0;
}
}
}
void Init_System()
{
XBYTE[0xA000] = 0xff;
XBYTE[0xA000] = 0x00;
XBYTE[0xE000] = 0xff;
XBYTE[0xC000] = 0xff;
Init_Timer0();
}
void main()
{
Init_System();
while(1)
{
LED_Control();
Scan_Keys();
}
}