各位CSDN的uu们好呀,这是小雅兰的最新专栏噢,最近小雅兰学习了51单片机的知识,所以就想迫不及待地分享出来呢!!!下面,让我们进入51单片机的世界吧!!!
点亮一个LED
LED闪烁
LED流水灯
LED流水灯PLUS
源代码
点亮一个LED
首先,我们得对进制的转换有一个了解:
|---------|----------|----------|
| 十进制 | 二进制 | 十六进制 |
| 0 | 0000 | 0 |
| 1 | 0001 | 1 |
| 2 | 0010 | 2 |
| 3 | 0011 | 3 |
| 4 | 0100 | 4 |
| 5 | 0101 | 5 |
| 6 | 0110 | 6 |
| 7 | 0111 | 7 |
| 8 | 1000 | 8 |
| 9 | 1001 | 9 |
| 10 | 1010 | A |
| 11 | 1011 | B |
| 12 | 1100 | C |
| 13 | 1101 | D |
| 14 | 1110 | E |
| 15 | 1111 | F |
然后,点亮一个LED是除了第一个灯是亮的,其余的都不亮。
意思是:它的二进制代码序列是1111 1110
但是,写C语言程序是不支持二进制的,所以我们需要用十六进制。
知道了上面的知识之后,就可以开始写代码啦:
cpp
#include <REGX52.H>
void main()
{
P2=0xFE;//1111 1110
}

如果是想要八个LED间隔着亮,那么它的二进制代码就应该是:0101 0101
cpp
#include <REGX52.H>
void main()
{
P2=0x55;//0101 0101
}

LED闪烁
这个的意思就是第一个灯一会儿亮,一会儿不亮。
这里我们可以用到一个工具,就是STC-ISP的软件延时计算器。
选择系统频率为12.000MHz,定时长度为500毫秒,这样它会自动生成一个代码。
void Delay500ms(void) //@12.000MHz
{
unsigned char data i, j, k;nop();
i = 4;
j = 205;
k = 187;
do
{
do
{
while (--k);
} while (--j);
} while (--i);
}

那么,我们最后的代码就是这样的:
cpp
#include <REGX52.H>
#include <INTRINS.H>
void Delay500ms() //@12.000MHz
{
unsigned char data i, j, k;
_nop_();
i = 4;
j = 205;
k = 187;
do
{
do
{
while (--k);
} while (--j);
} while (--i);
}
void main()
{
while(1)
{
//1111 1110
P2=0xFE;//只有第一个LED亮
Delay500ms();
//1111 1111
P2=0xFF;//所有LED都不亮
Delay500ms();
}
}


LED流水灯
这边的意思是让LED一个接一个的亮,那么,它的二进制代码序列应该就是这样:
1111 1110
1111 1101
1111 1011
1111 0111
1110 1111
1101 1111
1011 1111
0111 1111
那么,我们的代码也就出来了:
cpp
#include <REGX52.H>
#include <INTRINS.H>
void Delay500ms() //@12.000MHz
{
unsigned char data i, j, k;
_nop_();
i = 4;
j = 205;
k = 187;
do
{
do
{
while (--k);
} while (--j);
} while (--i);
}
void main()
{
while(1)
{
P2=0xFE;//1111 1110
Delay500ms();
P2=0xFD;//1111 1101
Delay500ms();
P2=0xFB;//1111 1011
Delay500ms();
P2=0xF7;//1111 0111
Delay500ms();
P2=0xEF;//1110 1111
Delay500ms();
P2=0xDF;//1101 1111
Delay500ms();
P2=0xBF;//1011 1111
Delay500ms();
P2=0x7F;//0111 1111
Delay500ms();
}
}





LED流水灯PLUS
上面写的LED流水灯的代码不是特别灵活,如果是我们想要修改定时长度呢,难道我们还要把代码全部修改一遍吗,这样显然是比较cuo的,我们可以利用传参的方式,想修改成多少定时长度就可以修改成多少定时长度。
当然,在这之前,又有一个小知识点需要分享:
|---------|--------|------------------------|----------|-----------------------------|
| 类型 | 符号 | 关键字 | 所占位数 | 数的表示范围 |
| 整型 | 有 | (signed)int | 16 | -32768~32767 |
| | | (signed)short | 16 | -32768~32767 |
| | | (signed)long | 32 | -2147483648~2147483647 |
| | 无 | unsigned int | 16 | 0~65535 |
| | | unsigned short int | 16 | 0~65535 |
| | | unsigned long int | 32 | 0~4294967295 |
| 实型 | 有 | float | 32 | 3.4e-38~3.4e38 |
| | 有 | double | 64 | 1.7e-308~1.7e308 |
| 字符型 | 有 | char | 8 | -128~127 |
| | 无 | unsigned char | 8 | 0~255 |
cpp
#include <REGX52.H>
void Delay1ms(unsigned int xms) //@12.000MHz
{
unsigned char data i, j;
while(xms)
{
i = 2;
j = 239;
do
{
while (--j);
} while (--i);
xms=xms-1;
}
}
void main()
{
while(1)
{
P2=0xFE;//1111 1110
Delay1ms(200);
P2=0xFD;//1111 1101
Delay1ms(200);
P2=0xFB;//1111 1011
Delay1ms(200);
P2=0xF7;//1111 0111
Delay1ms(200);
P2=0xEF;//1110 1111
Delay1ms(200);
P2=0xDF;//1101 1111
Delay1ms(200);
P2=0xBF;//1011 1111
Delay1ms(200);
P2=0x7F;//0111 1111
Delay1ms(200);
}
}

源代码
源代码如下:
点亮一个LED
#include <REGX52.H>
void main()
{
P2=0xFE;//1111 1110
}间隔点亮LED
#include <REGX52.H>
void main()
{
P2=0x55;//0101 0101
}
LED闪烁#include <REGX52.H>
#include <INTRINS.H>void Delay500ms() //@12.000MHz
{
unsigned char data i, j, k;nop();
i = 4;
j = 205;
k = 187;
do
{
do
{
while (--k);
} while (--j);
} while (--i);
}void main()
{
while(1)
{
//1111 1110
P2=0xFE;//只有第一个LED亮
Delay500ms();
//1111 1111
P2=0xFF;//所有LED都不亮
Delay500ms();
}
}
LED流水灯#include <REGX52.H>
#include <INTRINS.H>void Delay500ms() //@12.000MHz
{
unsigned char data i, j, k;nop();
i = 4;
j = 205;
k = 187;
do
{
do
{
while (--k);
} while (--j);
} while (--i);
}void main()
{
while(1)
{
P2=0xFE;//1111 1110
Delay500ms();
P2=0xFD;//1111 1101
Delay500ms();
P2=0xFB;//1111 1011
Delay500ms();
P2=0xF7;//1111 0111
Delay500ms();
P2=0xEF;//1110 1111
Delay500ms();
P2=0xDF;//1101 1111
Delay500ms();
P2=0xBF;//1011 1111
Delay500ms();
P2=0x7F;//0111 1111
Delay500ms();
}
}
LED流水灯PLUS#include <REGX52.H>
void Delay1ms(unsigned int xms) //@12.000MHz
{
unsigned char data i, j;
while(xms)
{
i = 2;
j = 239;
do
{
while (--j);
} while (--i);
xms=xms-1;
}
}void main()
{
while(1)
{
P2=0xFE;//1111 1110
Delay1ms(200);
P2=0xFD;//1111 1101
Delay1ms(200);
P2=0xFB;//1111 1011
Delay1ms(200);
P2=0xF7;//1111 0111
Delay1ms(200);
P2=0xEF;//1110 1111
Delay1ms(200);
P2=0xDF;//1101 1111
Delay1ms(200);
P2=0xBF;//1011 1111
Delay1ms(200);
P2=0x7F;//0111 1111
Delay1ms(200);
}
}
好啦,小雅兰今天的内容就到这里啦,还要继续加油呀!!!
