//led灯为共阳接法 P0 P1 口 单片机型号STC89C52
#include<reg52.h>
unsigned int a=0;
unsigned char num;
unsigned char pp0=0;//LED占空比值 0-16 0最亮
unsigned char pp1=1;
unsigned char pp2=2;
unsigned char pp3=3;
unsigned char pp4=4;
unsigned char pp5=5;
unsigned char pp6=6;
unsigned char pp7=7;
unsigned char pp8=8;
unsigned char pp9=9;
unsigned char pp10=10;
unsigned char pp11=11;
unsigned char pp12=12;
unsigned char pp13=13;
unsigned char pp14=14;
unsigned char pp15=15;
sbit LED0=P2^0;
sbit LED1=P2^1;
sbit LED2=P2^2;
sbit LED3=P2^3;
sbit LED4=P2^4;
sbit LED5=P2^5;
sbit LED6=P2^6;
sbit LED7=P2^7;
sbit LED8=P0^0;
sbit LED9=P0^1;
sbit LED10=P0^2;
sbit LED11=P0^3;
sbit LED12=P0^4;
sbit LED13=P0^5;
sbit LED14=P0^6;
sbit LED15=P0^7;
unsigned char pwm_t;//
//定时器1中断
void timer1() interrupt 3
{
a++;
if(a>8192)
{
a=0;
}
pwm_t++;
if(pwm_t > 15)
{
pwm_t = 0;
P2 = 0xff;
P0 = 0xff;
}
if(a/512==0){//状态一
pp0=0;//LED占空比值 此时只有0亮, 0最亮
pp1=16;
pp2=16;
pp3=16;
pp4=16;
pp5=16;
pp6=16;
pp7=16;
pp0=16;
pp1=16;
pp2=16;
pp3=16;
pp4=16;
pp5=16;
pp6=16;
pp7=16;
}else if(a/512==1){状态二
pp0=1;
pp1=0;
pp2=16;
pp3=16;
pp4=16;
pp5=16;
pp6=16;
pp7=16;
pp8=16;
pp9=16;
pp10=16;
pp11=16;
pp12=16;
pp13=16;
pp14=16;
pp15=16;
}else if(a/512==2){
pp0=2;
pp1=1;
pp2=0;
pp3=16;
pp4=16;
pp5=16;
pp6=16;
pp7=16;
pp8=16;
pp9=16;
pp10=16;
pp11=16;
pp12=16;
pp13=16;
pp14=16;
pp15=16;
}else if(a/512==3){
pp0=3;
pp1=2;
pp2=1;
pp3=0;
pp4=16;
pp5=16;
pp6=16;
pp7=16;
pp8=16;
pp9=16;
pp10=16;
pp11=16;
pp12=16;
pp13=16;
pp14=16;
pp15=16;
}else if(a/512==4){
pp0=4;
pp1=3;
pp2=2;
pp3=1;
pp4=0;
pp5=16;
pp6=16;
pp7=16;
pp8=16;
pp9=16;
pp10=16;
pp11=16;
pp12=16;
pp13=16;
pp14=16;
pp15=16;
}else if(a/512==5){
pp0=5;
pp1=4;
pp2=3;
pp3=2;
pp4=1;
pp5=0;
pp6=16;
pp7=16;
pp8=16;
pp9=16;
pp10=16;
pp11=16;
pp12=16;
pp13=16;
pp14=16;
pp15=16;
}else if(a/512==6){
pp0=6;
pp1=5;
pp2=4;
pp3=3;
pp4=2;
pp5=1;
pp6=0;
pp7=16;
pp8=16;
pp9=16;
pp10=16;
pp11=16;
pp12=16;
pp13=16;
pp14=16;
pp15=16;
}
else if(a/512==7){
pp0=7;
pp1=6;
pp2=5;
pp3=4;
pp4=3;
pp5=2;
pp6=1;
pp7=0;
pp8=16;
pp9=16;
pp10=16;
pp11=16;
pp12=16;
pp13=16;
pp14=16;
pp15=16;
}
else if(a/512==8){
pp0=8;
pp1=7;
pp2=6;
pp3=5;
pp4=4;
pp5=3;
pp6=2;
pp7=1;
pp8=0;
pp9=16;
pp10=16;
pp11=16;
pp12=16;
pp13=16;
pp14=16;
pp15=16;
}
else if(a/512==9){
pp0=9;
pp1=8;
pp2=7;
pp3=6;
pp4=5;
pp5=4;
pp6=3;
pp7=2;
pp8=1;
pp9=0;
pp10=16;
pp11=16;
pp12=16;
pp13=16;
pp14=16;
pp15=16;
}
else if(a/512==10){
pp0=10;
pp1=9;
pp2=8;
pp3=7;
pp4=6;
pp5=5;
pp6=4;
pp7=3;
pp8=2;
pp9=1;
pp10=0;
pp11=16;
pp12=16;
pp13=16;
pp14=16;
pp15=16;
}
else if(a/512==11){
pp0=11;
pp1=10;
pp2=9;
pp3=8;
pp4=7;
pp5=6;
pp6=5;
pp7=4;
pp8=3;
pp9=2;
pp10=1;
pp11=0;
pp12=16;
pp13=16;
pp14=16;
pp15=16;
}
else if(a/512==12){
pp0=12;
pp1=11;
pp2=10;
pp3=9;
pp4=8;
pp5=7;
pp6=6;
pp7=5;
pp8=4;
pp9=3;
pp10=2;
pp11=1;
pp12=0;
pp13=16;
pp14=16;
pp15=16;
}
else if(a/512==13){
pp0=13;
pp1=12;
pp2=11;
pp3=10;
pp4=9;
pp5=8;
pp6=7;
pp7=6;
pp8=5;
pp9=4;
pp10=3;
pp11=2;
pp12=1;
pp13=0;
pp14=16;
pp15=16;
}
else if(a/512==14){
pp0=14;
pp1=13;
pp2=12;
pp3=11;
pp4=10;
pp5=9;
pp6=8;
pp7=7;
pp8=6;
pp9=5;
pp10=4;
pp11=3;
pp12=2;
pp13=1;
pp14=0;
pp15=16;
}
else if(a/512==15){
pp0=15;
pp1=14;
pp2=13;
pp3=12;
pp4=11;
pp5=10;
pp6=9;
pp7=8;
pp8=7;
pp9=6;
pp10=5;
pp11=4;
pp12=3;
pp13=2;
pp14=1;
pp15=0;
}
if(pp0 == pwm_t)
LED0 = 0;
if(pp1 == pwm_t)
LED1 =0 ;
if(pp2== pwm_t)
LED2 = 0;
if(pp3 == pwm_t)
LED3 = 0;
if(pp4 == pwm_t)
LED4 = 0;
if(pp5 == pwm_t)
LED5 = 0;
if(pp6 == pwm_t)
LED6 = 0;
if(pp7 == pwm_t)
LED7 = 0;
if(pp8 == pwm_t)
LED8 = 0;
if(pp9 == pwm_t)
LED9 = 0;
if(pp10 == pwm_t)
LED10 = 0;
if(pp11 == pwm_t)
LED11 = 0;
if(pp12 == pwm_t)
LED12 = 0;
if(pp13 == pwm_t)
LED13 = 0;
if(pp14 == pwm_t)
LED14 = 0;
if(pp15 == pwm_t)
LED15 = 0;
}
void main()
{
TMOD |= 0x20;//8位自动重装模块
TH1 = 240;
TL1 = 240;//11.0592M晶振下占空比最大比值是20,输出2880HZ
TR1 = 1;//启动定时器1
ET1 = 1;//允许定时器1中断
EA = 1;//总中断允许
while(1)
{
}
}
交流QQ:1461264417
|