解释
Delay100ms(unsigned char turn)为延时函数,延时时长: turn * 0.1s
mypow(unsigned int a,unsigned int b):计算a的b次方
控制LED挨个亮起来,就需要不断地循环执行下面的代码
T = 1111 1111
P2 = 1111 1110 //T - 1
P2 =1111 1101 //T - 2
P2 = 1111 1011 //T - 4
P2 = 1111 0111 //T - 8
P2 = 1110 1111 //T - 16
P2 = 1101 1111 //T - 32
P2 = 1011 1111 //T - 64
P2 = 0111 1111 //T - 128
实际上就是T减去 2 ^ i
代码
#include <REGX52.H>
#include <INTRINS.H>
void Delay100ms(unsigned char turn) //@11.0592MHz
{
while(turn--){
unsigned char i, j, k;
_nop_();
_nop_();
i = 5;
j = 52;
k = 195;
do
{
do
{
while (--k);
} while (--j);
} while (--i);
}
}
int mypow(unsigned int a,unsigned int b){
unsigned int i=0;
for(i;i<b;i++){
a=a*a;
}
return a;
}
void main(){
unsigned int T=0xff;
unsigned int i=0;
while(1){
for(i;i<8;i++){
P2=T-mypow(i,2);
Delay100ms(2); //500ms
}
}
}