51单片机LED点亮,闪烁以及流水灯实现的四种方法
程序员文章站
2022-03-11 21:32:37
【51单片机】LED点亮,闪烁以及流水灯实现的四种方法点亮LED#include sbit LED=P2^0;void main(){while(1){LED=0;}}LED闪烁(delay函数)#include sbit LED = P2^0;//管脚定义 void delay(unsigned char m)//m是形式参数 { unsigned char i,j;for (i=110;...
【51单片机】LED点亮,闪烁以及流水灯实现的四种方法
点亮LED
#include <reg52.h> sbit LED=P2^0; void main() { while(1) { LED=0; } }
LED闪烁(delay函数)
#include <reg52.h> sbit LED = P2^0;//管脚定义 void delay(unsigned char m)//m是形式参数 { unsigned char i,j; for (i=110;i>0;i--) for (j=m;j>0;j--); } void main() { while (1) { LED=0; delay(100); LED=1; delay(100); } }
LED闪烁
#include <reg52.h> sbit LED1=P1^0; unsigned int i; void main() { while(1) { i=65535; while(i--); LED1=~LED1; } }
流水灯(库函数法)
#include <reg52.h> #include <intrins.h> #define uint unsigned int #define uchar unsigned char uchar temp; void delay(uint m) { uint i,j; for (i=m;i>0;i--) for (j=114;j>0;j--); } void main() { temp = 0xfe; P1 = temp; delay(100); while(1) { temp = _crol_(temp,1); P1=temp; delay(100); } }
流水灯(左移法)
#include <reg52.h> #define uint unsigned int #define uchar unsigned char void delay(uint m) { uint i,j; for (i=110;i>0;i--) for (j=m;j>0;j--); } void main() { P1=0x01; while(1) { delay(500); P1=P1<<1; if(P1==0x00) P1=0x01; } }
流水灯(查找数组法)
#include <reg52.h> #define uint unsigned int #define uchar unsigned char uchar table[]={0x01,0x02,0x04,0x08, 0x10,0x20,0x40,0x80}; uchar p; void delay(uint m) { uint i,j; for (i=110;i>0;i--) for (j=m;j>0;j--) ; } void main() { while (1) { for(p=0;p<8;p++) { P1=table[p]; delay(500); } }
下一篇: go语言静态库的编译和使用方法