针对 51 单片机驱动学习
# 一、LED 点灯一系列
参考 https://x509p6c8to.feishu.cn/wiki/WRNLwDd0iiG8OWkyatOcom6knHf
# 1. 点亮一个 LED 灯
| #include <REGX52.H> |
| |
| void main() |
| { |
| P1=0xEF; |
| while(1) |
| { |
| |
| } |
| } |
# 2. 闪烁一个 LED 灯
| #include <REGX52.H> |
| sbit p1_1=P1^0; |
| unsigned int a; |
| void main() |
| { |
| while(1) |
| { |
| a=50000; |
| p1_1=0; |
| while(a--); |
| a=50000; |
| p1_1=1; |
| while(a--); |
| } |
| } |
# 3. 流水灯
| |
| #include <REGX52.H> |
| #include <intrins.h> |
| #define uint unsigned int |
| #define uchar unsigned char |
| uchar temp; |
| void delay(uint z) |
| { |
| uint x,y; |
| for(x=100;x>0;x--) |
| for(y=z;y>0;y--); |
| } |
| |
| void main() |
| { |
| temp =0xfe; |
| P1=temp; |
| while(1) |
| { |
| temp=_crol_(temp,1); |
| delay(600); |
| P1=temp; |
| } |
| } |
# 二、按键控制 LED 灯
当按下按键时,LED 灯亮,松开按键时,LED 灯灭。
# 1. 按键控制 LED 亮灭
| #include <REGX52.H> |
| void main() |
| { |
| while(1) |
| { |
| if(P3_4==0) |
| { |
| P1_1=0; |
| } |
| else{ |
| P1_1=1; |
| } |
| } |
| |
| } |
# 2. 按键控制 LED 状态
| #include <REGX52.H> |
| void Delay(unsigned int xms) |
| { |
| unsigned char i, j; |
| while(xms) |
| { |
| i = 2; |
| j = 199; |
| do |
| { |
| while (--j); |
| } while (--i); |
| xms--; |
| } |
| } |
| void main() |
| { |
| while(1) |
| { |
| if(P3_4==0) |
| { |
| Delay(20); |
| while(P3_4==0); |
| Delay(20); |
| P1_1=~P1_1; |
| } |
| } |
| } |
| |
# 3. 独立按键控制 LED 显示二进制
| #include <REGX52.H> |
| void Delay(unsigned int xms) |
| { |
| unsigned char i, j; |
| while(xms--) |
| { |
| i = 2; |
| j = 199; |
| do |
| { |
| while (--j); |
| } while (--i); |
| } |
| |
| } |
| void main() |
| { |
| unsigned char LEDNum=0; |
| while(1) |
| { |
| if(P3_4==0) |
| { |
| Delay(20); |
| while(P3_4==0); |
| Delay(20); |
| |
| LEDNum++; |
| P1=~LEDNum; |
| } |
| } |
| } |
# 4. 独立按键控制 LED 移位
| |
| #include <REGX52.H> |
| unsigned char LEDNum; |
| |
| void Delay(unsigned int xms) |
| { |
| unsigned char i, j; |
| while(xms) |
| { |
| i = 2; |
| j = 199; |
| do |
| { |
| while (--j); |
| } while (--i); |
| xms--; |
| } |
| } |
| void main() |
| { |
| P1=~0x01; |
| while(1) |
| { |
| if(P3_4==0) |
| { |
| Delay(20); |
| while(P3_4==0); |
| Delay(20); |
| |
| LEDNum++; |
| if(LEDNum>=8) |
| LEDNum=0; |
| P1=~(0x01<<LEDNum); |
| |
| } |
| if(P3_4==0) |
| { |
| Delay(20); |
| while(P3_4==0); |
| Delay(20); |
| if(LEDNum==0) |
| LEDNum==7; |
| else |
| LEDNum--; |
| |
| } |
| } |
| } |
# 三、数码管显示
# 单个的数码管
2 进制来看,亮的显示 0,灭的显示 1。
# 四位一体的
工作原理:
- 首先,将要显示的数字编码成相应的二进制数。
- 然后,将编码后的二进制数的每一位依次连接到相应的数码管上。
- 最后,将数码管连接到相应的控制引脚上,使得数码管按照相应的二进制数显示。
- 首先上面的,正接正,负接负才能亮。下面的,产生电位差才能亮,因为是共阴,所以要高电平,就是 1
| |
| void main() |
| { |
| |
| P0=0x06; |
| while(1) |
| { |
| |
| } |
| } |
| |
| |
| #include <reg52.h> |
| unsigned char table[]={ |
| 0x3f,0x06,0x5b,0x4f, |
| 0x66,0x6d,0x7d,0x07, |
| 0x7f,0x6f |
| }; |
| unsigned char num; |
| void delay_ms(unsigned int xms) //@12MHz |
| { |
| unsigned int i, j; |
| for(i=xms;i>0;i--) |
| { |
| for(j=124;j>0;j--) |
| {} |
| } |
| } |
| |
| void main() |
| { |
| while(1) |
| { |
| for(num=0;num<10;num++) |
| { |
| P0=table[num]; |
| delay_ms(500); |
| } |
| } |
| } |
| */ |
| #include <reg52.h> |
| |
| sbit led1 = P0^5; |
| sbit led2 = P0^6; |
| sbit led3 = P0^7; |
| |
| void delay_ms(unsigned int xms) |
| { |
| unsigned int i, j; |
| for(i=xms;i>0;i--) |
| { |
| for(j=124;j>0;j--) |
| {} |
| } |
| } |
| |
| void main() |
| { |
| while(1) |
| { |
| led1 = 0; |
| led2 = 1; |
| led3 = 1; |
| delay_ms(1); |
| led1 = 1; |
| led2 = 0; |
| led3 = 1; |
| delay_ms(1); |
| led1 = 1; |
| led2 = 1; |
| led3 = 0; |
| delay_ms(1); |
| } |
| } |