1、点亮LED灯:
“`c
#include <REGX52.H>
void main() {
P2 = 0xFE; // 点亮第1个LED
while (1);
}
“`
2、LED闪烁:
“`c
#include <REGX52.H>
#include <INTRINS.H>
void Delay500ms(void) {
unsigned char i, j, k;
i = 4;
j = 129;
k = 119;
do {
do {
while (k);
} while (j);
} while (i);
}
void main() {
while (1) {
P2 = 0xFE; // 点亮第1个LED
Delay500ms();
P2 = 0xFF; // 熄灭所有LED
Delay500ms();
}
}
“`
3、LED流水灯:
“`c
#include <REGX52.H>
void Delay500ms(void) {
unsigned char i, j, k;
i = 4;
j = 129;
k = 119;
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();
}
}
“`
4、依次点亮LED:
“`c
#include <REGX52.H>
#include <math.h>
void delay(unsigned int n) {
int i;
for (i = 0; i < n; i++);
}
void LED_left_to_right() {
unsigned int ADDR;
unsigned char i = 1;
ADDR = 0x01;
delay(30000);
while (1) {
P2 = ~ADDR;
if (i == 8) break;
delay(30000);
ADDR += pow(2, i);
i++;
}
}
void LED_right_to_left() {
unsigned int ADDR;
unsigned char i = 6;
ADDR = 0x80;
delay(20000);
while (1) {
P2 = ~ADDR;
if (i == 1) break;
delay(30000);
ADDR += pow(2, i);
i;
}
}
void main() {
while (1) {
LED_left_to_right();
LED_right_to_left();
}
}
“`
5、数码管显示:
“`c
#include <REGX52.H>
typedef unsigned int u16;
typedef unsigned char u8;
sbit LSA = P2^2;
sbit LSB = P2^3;
sbit LSC = P2^4;
u8 code smgduan[17] = {0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07,
0x7f, 0x6f, 0x77, 0x7c, 0x39, 0x5e, 0x79, 0x71}; // 显示0~F的值
void delay(u16 i) {
while (i);
}
void DigDisplay() {
u8 i;
for (i = 0; i < 8; i++) {
switch (i) { // 位选,选择点亮的数码管,
case (0): LSA = 1; LSB = 1; LSC = 1; break; // 显示第0位
case (1): LSA = 0; LSB = 1; LSC = 1; break; // 显示第1位
case (2): LSA = 1; LSB = 0; LSC = 1; break; // 显示第2位
case (3): LSA = 0; LSB = 0; LSC = 1; break; // 显示第3位
case (4): LSA = 1; LSB = 1; LSC = 0; break; // 显示第4位
case (5): LSA = 0; LSB = 1; LSC = 0; break; // 显示第5位
case (6): LSA = 1; LSB = 0; LSC = 0; break; // 显示第6位
case (7): LSA = 0; LSB = 0; LSC = 0; break; // 显示第7位
}
P0 = smgduan[i];
delay(100); // 间隔一段时间扫描
P0 = 0x00; // 消隐
}
}
void main() {
while (1) {
DigDisplay(); // 数码管显示函数
}
}
“`
以上内容就是解答有关51单片机源码的详细内容了,我相信这篇文章可以为您解决一些疑惑,有任何问题欢迎留言反馈,谢谢阅读。
原创文章,作者:未希,如若转载,请注明出处:https://www.kdun.com/ask/1090116.html
本网站发布或转载的文章及图片均来自网络,其原创性以及文中表达的观点和判断不代表本网站。如有问题,请联系客服处理。
发表回复