一、单片机驱动单个发光二极管
1.电路
代码:
1.点亮单个LED二极管
#include《reg51.h>
sbit LED1=P1^0;void main(void){LED1=1;while(1);{LED1=0}
}
2.单个LED数码管以固定频率闪烁
#include<reg51.h>
sbit LED1=P1^0;void Delay(unsigned int t);void main(void){while(1){LED1=1;Delay(30000);LED1=0;Delay(30000);}
}
void Delay(unsigned int t){while(--t);
}
效果:
使单个LED数码管以固定频率烁https://www.zhihu.com/video/1245660428543778816二、单片机点亮多个LED电路
1.电路
代码:
1.多个LED二极管以固定频率闪烁
#include<reg51.h>void Delay(unsigned int t);void main(void){while(1){P1=0xFF;Delay(30000);P1=0x00;Delay(30000);}
}
void Delay(unsigned int t){while(--t);
}
效果
多个LED二极管以固定频率闪烁https://www.zhihu.com/video/12456704028667535362控制多个LED逐渐点亮(最后LED全亮)
代码:
#include<reg51.h>void Delay(unsigned int t);void main(void){while(1){unsigned char i;P1=0xfe;for(i=0;i<8;i++){Delay(30000);P1=P1<<1;}}
}
void Delay(unsigned int t){while(--t);
}
效果:
控制多个LED逐渐点亮https://www.zhihu.com/video/12457019560959713283控制LED循环点亮(始终只有一个亮)
1.代码
#include<reg51.h>void Delay(unsigned int t);void main(void){while(1){unsigned char i;P1=0xfe;for(i=0;i<8;i++){Delay(30000);P1=P1<<1|0x01;}}
}
void Delay(unsigned int t){while(--t);
}
2.效果
控制LED循环点亮https://www.zhihu.com/video/12457073264783933444.控制LED花样点亮
1.代码
#include<reg51.h>void Delay(unsigned int t);void main(void){while(1){unsigned char i;P1=0xfe;for(i=0;i<8;i++){Delay(30000);P1 <<=1;}P1=0x7f;for(i=0;i<8;i++){Delay(30000);P1 >>=1;}P1=0xfe;for(i=0;i<8;i++){Delay(30000);P1 >>=1;P1 |=0x01;}P1=0x7f;for(i=0;i<8;i++){Delay(30000);P1 >>=1;P1 |=0x80;}}
}
void Delay(unsigned int t){while(--t);
}
2.效果
控制LED花样点亮https://www.zhihu.com/video/1245710486429745152