cho mình hỏi chút? sao dùng code này nạp vào 89s52 lại không thể tắt hẳn được led nhỉ ( mặc dù set led ko sáng ) nhưng led ko tắt hẳn mà vẫn sáng mờ mờ!
code như sau:
#include<regx51.h>
char NumPort=16;
char PWM[32];
char chuki=0;
void SetPort(char i, char v)
{
switch (i)
{
case 0:
P1_0
= v; return;
case 1:
P1_1
= v; return;
case 2:
P1_2
= v; return;
case 3:
P1_3
= v; return;
case 4:
P1_4
= v; return;
case 5:
P1_5
= v; return;
case 6:
P1_6
= v; return;
case 7:
P1_7
= v; return;
case 8:
P3_0
= v; return;
case 9:
P3_1
= v; return;
case 10:
P3_2
= v; return;
case 11:
P3_3
= v; return;
case 12:
P3_4
= v; return;
case 13:
P3_5
= v; return;
case 14:
P3_6
= v; return;
case 15:
P3_7
= v; return;
case 16:
P2_0
= v; return;
case 17:
P2_1
= v; return;
case 18:
P2_2
= v; return;
case 19:
P2_3
= v; return;
case 20:
P2_4
= v; return;
case 21:
P2_5
= v; return;
case 22:
P2_6
= v; return;
case 23:
P2_7
= v; return;
case 24:
P0_0
= v; return;
case 25:
P0_1
= v; return;
case 26:
P0_2
= v; return;
case 27:
P0_3
= v; return;
case 28:
P0_4
= v; return;
case 29:
P0_5
= v; return;
case 30:
P0_6
= v; return;
case 31:
P0_7
= v; return;
}
}
/*--------------------------------------*/
void Timer0()interrupt 1
{
char i;
TF0=0;
TR0=0; //stop ngat
for (i=0 ; i<NumPort ; i++)
if (chuki <=
PWM[i]) SetPort(i,1);
else
SetPort(i,0);
chuki += 10; //Tang 7,8,9,10 se bot chop
if (chuki >= 100) chuki=0;
TR0=1; //tiep tuc ngat
}
void delay(unsigned int ms)
{
unsigned int i, j;
for(i=0 ; i<ms ; i++)
for(j=0 ; j<120 ; j++) {}
}
void ResetPWM()
{
unsigned char i;
for(i=0 ; i<NumPort ; i++)
PWM[i]=0;
}
main()
{
EA=0;
TMOD=0x02; //timer 0 - che do 2 - 8 bit tu dong nap lai
TH0 = 156;
TL0 = 156;
ET0=1;
EA=1;
TR0=1;
while (1)
{
delay(10);
ResetPWM();
PWM[8]=100;PWM[10]=5;PWM[11]=16;PWM[12]=32;PWM[13]=49;PWM[14]=64;PWM[15]=75;//1
delay(10);
ResetPWM();
PWM[8]=75;PWM[9]=100;PWM[11]=5;PWM[12]=16;PWM[13]=32;PWM[14]=49;PWM[15]=64;//2
delay(10);
ResetPWM();
PWM[8]=64;PWM[9]=75;PWM[10]=100;PWM[12]=5;PWM[13]=16;PWM[14]=32;PWM[15]=49;//3
delay(10);
ResetPWM();
PWM[8]=49;PWM[9]=64;PWM[10]=75;PWM[11]=100;PWM[13]=5;PWM[14]=16;PWM[15]=32;//4
delay(10);
ResetPWM();
PWM[0]=100;PWM[1]=100;PWM[2]=100;PWM[3]=100;PWM[4]=100;PWM[5]=100;PWM[6]=100;PWM[7]=100;PWM[8]=32;PWM[9]=49;PWM[10]=64;PWM[11]=75;PWM[12]=100;PWM[14]=5;PWM[15]=16;//5
delay(10);
ResetPWM();
PWM[0]=100;PWM[1]=100;PWM[2]=100;PWM[3]=100;PWM[4]=100;PWM[5]=100;PWM[6]=100;PWM[7]=100;PWM[8]=16;PWM[9]=32;PWM[10]=49;PWM[11]=64;PWM[12]=75;PWM[13]=100;PWM[15]=5;//6
delay(10);
ResetPWM();
PWM[8]=5;PWM[9]=16;PWM[10]=32;PWM[11]=49;PWM[12]=64;PWM[13]=75;PWM[14]=100;//7
delay(10);
ResetPWM();
PWM[9]=5;PWM[10]=16;PWM[11]=32;PWM[12]=49;PWM[13]=64;PWM[14]=75;PWM[15]=100;//8
delay(10);
ResetPWM();
PWM[0]=100;PWM[1]=100;PWM[2]=100;PWM[3]=100;PWM[4]=100;PWM[5]=100;PWM[6]=100;PWM[7]=100;PWM[8]=100;PWM[10]=5;PWM[11]=16;PWM[12]=32;PWM[13]=49;PWM[14]=64;PWM[15]=75;//9
delay(10);
ResetPWM();
PWM[0]=100;PWM[1]=100;PWM[2]=100;PWM[3]=100;PWM[4]=100;PWM[5]=100;PWM[6]=100;PWM[7]=100;PWM[8]=75;PWM[9]=100;PWM[11]=5;PWM[12]=16;PWM[13]=32;PWM[14]=49;PWM[15]=64;//10
delay(10);
ResetPWM();
PWM[8]=64;PWM[9]=75;PWM[10]=100;PWM[12]=5;PWM[13]=16;PWM[14]=32;PWM[15]=49;//11
delay(10);
ResetPWM();
PWM[8]=49;PWM[9]=64;PWM[10]=75;PWM[11]=100;PWM[13]=5;PWM[14]=16;PWM[15]=32;//12
delay(10);
ResetPWM();
PWM[8]=32;PWM[9]=49;PWM[10]=64;PWM[11]=75;PWM[12]=100;PWM[14]=5;PWM[15]=16;//13
delay(10);
ResetPWM();
PWM[8]=16;PWM[9]=32;PWM[10]=49;PWM[11]=64;PWM[12]=75;PWM[13]=100;PWM[15]=5;//14
delay(10);
ResetPWM();
PWM[8]=5;PWM[9]=16;PWM[10]=32;PWM[11]=49;PWM[12]=64;PWM[13]=75;PWM[14]=100;//15
delay(10);
ResetPWM();
PWM[9]=5;PWM[10]=16;PWM[11]=32;PWM[12]=49;PWM[13]=64;PWM[14]=75;PWM[15]=100;//16
}
}
thanks
code như sau:
#include<regx51.h>
char NumPort=16;
char PWM[32];
char chuki=0;
void SetPort(char i, char v)
{
switch (i)
{
case 0:
P1_0
= v; return;
case 1:
P1_1
= v; return;
case 2:
P1_2
= v; return;
case 3:
P1_3
= v; return;
case 4:
P1_4
= v; return;
case 5:
P1_5
= v; return;
case 6:
P1_6
= v; return;
case 7:
P1_7
= v; return;
case 8:
P3_0
= v; return;
case 9:
P3_1
= v; return;
case 10:
P3_2
= v; return;
case 11:
P3_3
= v; return;
case 12:
P3_4
= v; return;
case 13:
P3_5
= v; return;
case 14:
P3_6
= v; return;
case 15:
P3_7
= v; return;
case 16:
P2_0
= v; return;
case 17:
P2_1
= v; return;
case 18:
P2_2
= v; return;
case 19:
P2_3
= v; return;
case 20:
P2_4
= v; return;
case 21:
P2_5
= v; return;
case 22:
P2_6
= v; return;
case 23:
P2_7
= v; return;
case 24:
P0_0
= v; return;
case 25:
P0_1
= v; return;
case 26:
P0_2
= v; return;
case 27:
P0_3
= v; return;
case 28:
P0_4
= v; return;
case 29:
P0_5
= v; return;
case 30:
P0_6
= v; return;
case 31:
P0_7
= v; return;
}
}
/*--------------------------------------*/
void Timer0()interrupt 1
{
char i;
TF0=0;
TR0=0; //stop ngat
for (i=0 ; i<NumPort ; i++)
if (chuki <=
PWM[i]) SetPort(i,1);
else
SetPort(i,0);
chuki += 10; //Tang 7,8,9,10 se bot chop
if (chuki >= 100) chuki=0;
TR0=1; //tiep tuc ngat
}
void delay(unsigned int ms)
{
unsigned int i, j;
for(i=0 ; i<ms ; i++)
for(j=0 ; j<120 ; j++) {}
}
void ResetPWM()
{
unsigned char i;
for(i=0 ; i<NumPort ; i++)
PWM[i]=0;
}
main()
{
EA=0;
TMOD=0x02; //timer 0 - che do 2 - 8 bit tu dong nap lai
TH0 = 156;
TL0 = 156;
ET0=1;
EA=1;
TR0=1;
while (1)
{
delay(10);
ResetPWM();
PWM[8]=100;PWM[10]=5;PWM[11]=16;PWM[12]=32;PWM[13]=49;PWM[14]=64;PWM[15]=75;//1
delay(10);
ResetPWM();
PWM[8]=75;PWM[9]=100;PWM[11]=5;PWM[12]=16;PWM[13]=32;PWM[14]=49;PWM[15]=64;//2
delay(10);
ResetPWM();
PWM[8]=64;PWM[9]=75;PWM[10]=100;PWM[12]=5;PWM[13]=16;PWM[14]=32;PWM[15]=49;//3
delay(10);
ResetPWM();
PWM[8]=49;PWM[9]=64;PWM[10]=75;PWM[11]=100;PWM[13]=5;PWM[14]=16;PWM[15]=32;//4
delay(10);
ResetPWM();
PWM[0]=100;PWM[1]=100;PWM[2]=100;PWM[3]=100;PWM[4]=100;PWM[5]=100;PWM[6]=100;PWM[7]=100;PWM[8]=32;PWM[9]=49;PWM[10]=64;PWM[11]=75;PWM[12]=100;PWM[14]=5;PWM[15]=16;//5
delay(10);
ResetPWM();
PWM[0]=100;PWM[1]=100;PWM[2]=100;PWM[3]=100;PWM[4]=100;PWM[5]=100;PWM[6]=100;PWM[7]=100;PWM[8]=16;PWM[9]=32;PWM[10]=49;PWM[11]=64;PWM[12]=75;PWM[13]=100;PWM[15]=5;//6
delay(10);
ResetPWM();
PWM[8]=5;PWM[9]=16;PWM[10]=32;PWM[11]=49;PWM[12]=64;PWM[13]=75;PWM[14]=100;//7
delay(10);
ResetPWM();
PWM[9]=5;PWM[10]=16;PWM[11]=32;PWM[12]=49;PWM[13]=64;PWM[14]=75;PWM[15]=100;//8
delay(10);
ResetPWM();
PWM[0]=100;PWM[1]=100;PWM[2]=100;PWM[3]=100;PWM[4]=100;PWM[5]=100;PWM[6]=100;PWM[7]=100;PWM[8]=100;PWM[10]=5;PWM[11]=16;PWM[12]=32;PWM[13]=49;PWM[14]=64;PWM[15]=75;//9
delay(10);
ResetPWM();
PWM[0]=100;PWM[1]=100;PWM[2]=100;PWM[3]=100;PWM[4]=100;PWM[5]=100;PWM[6]=100;PWM[7]=100;PWM[8]=75;PWM[9]=100;PWM[11]=5;PWM[12]=16;PWM[13]=32;PWM[14]=49;PWM[15]=64;//10
delay(10);
ResetPWM();
PWM[8]=64;PWM[9]=75;PWM[10]=100;PWM[12]=5;PWM[13]=16;PWM[14]=32;PWM[15]=49;//11
delay(10);
ResetPWM();
PWM[8]=49;PWM[9]=64;PWM[10]=75;PWM[11]=100;PWM[13]=5;PWM[14]=16;PWM[15]=32;//12
delay(10);
ResetPWM();
PWM[8]=32;PWM[9]=49;PWM[10]=64;PWM[11]=75;PWM[12]=100;PWM[14]=5;PWM[15]=16;//13
delay(10);
ResetPWM();
PWM[8]=16;PWM[9]=32;PWM[10]=49;PWM[11]=64;PWM[12]=75;PWM[13]=100;PWM[15]=5;//14
delay(10);
ResetPWM();
PWM[8]=5;PWM[9]=16;PWM[10]=32;PWM[11]=49;PWM[12]=64;PWM[13]=75;PWM[14]=100;//15
delay(10);
ResetPWM();
PWM[9]=5;PWM[10]=16;PWM[11]=32;PWM[12]=49;PWM[13]=64;PWM[14]=75;PWM[15]=100;//16
}
}
thanks
Comment