51 单片机 led 灯光操作

led流水灯

c 复制代码
#include <REGX52.H>
#include "INTRINS.H"

void Delay(unsigned int xms)		
{
	unsigned char i, j;
	while(xms--){
			_nop_();
			i = 2;
			j = 199;
			do
			{
				while (--j);
			} while (--i);
	}


}

void main(){
	while(1){
		P2=0xFE;
		Delay(500);
			P2=0xFD;
		Delay(500);
			P2=0xFB;
		Delay(500);
		P2=0xF7;
		Delay(500);
			P2=0xEF;
		Delay(500);
			P2=0xDF;
		Delay(500);	
			P2=0xBF;
		Delay(500);
			P2=0x7F;
		Delay(500);
	}

}

延时代码

c 复制代码
#include "INTRINS.H"

void Delay(unsigned int xms)		
{
	unsigned char i, j;
	while(xms--){
			_nop_();
			i = 2;
			j = 199;
			do
			{
				while (--j);
			} while (--i);
	}


}

独立按键控制led(松发式)

c 复制代码
#include <REGX52.H>

void main(){


	while(1){
	
		if(P3_1==0)
			P2_0=0;
		else
			P2=0xFF;
		if(P3_0==0)
			P2=0xFD;
		else
			P2=0xFF;
		if(P3_2==0)
			P2=0xFB;
		else
			P2=0xFF;
		if(P3_3==0)
			P2=0xF7;
		else
			P2=0xFF;
	}

}

独立按键控制led 按键去除抖动

c 复制代码
#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(){


	while(1){
	
		if(P3_1==0){
			Delay(20);
			while(P3_1==0);
			Delay(20);
			P2_0=~P2_0;
		}

	
	}

}

独立按键 led二进制

c 复制代码
#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 x=0;

	while(1){
	
		if(P3_1==0){
			Delay(20);
			while(P3_1==0);
			Delay(20);
			
			x++;
			P2=~x;
		}

	
	}

}

独立按键控制led移位

c 复制代码
#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 x=0;
	P2_0=0;
	while(1){
	
		if(P3_0==0){
			Delay(20);
			while(P3_0==0);
			Delay(20);
			
			x++;
			x=x%8;
		
			P2=~(0x01<<x);
		}
		if(P3_1==0){
			Delay(20);
			while(P3_1==0);
			Delay(20);
			
		
			if(x==0)
				x=7;
			else
				x--;
		
			P2=~(0x01<<x);
		}
	
	}

}
相关推荐
项目題供诗4 小时前
51单片机入门(八)
单片机·嵌入式硬件·51单片机
羽获飞4 小时前
从零开始学嵌入式之STM32——9.STM32的时钟系统
stm32·单片机·嵌入式硬件
飞睿科技5 小时前
乐鑫智能开关方案解析:基于ESP32-C系列的低功耗、高集成设计
嵌入式硬件·物联网·esp32·智能家居·乐鑫科技
来自晴朗的明天6 小时前
13、NMOS 电源防反接电路
单片机·嵌入式硬件·硬件工程
17(无规则自律)6 小时前
深入浅出 Linux 内核模块,写一个内核版的 Hello World
linux·arm开发·嵌入式硬件
芯岭技术7 小时前
PY32MD310单片机:高性能、低功耗的32位电机控制微控制器
单片机·嵌入式硬件
wotaifuzao7 小时前
STM32 + FreeRTOS 的订阅通知组件架构
stm32·嵌入式硬件·架构·freertos·事件驱动·嵌入式架构
小龙报8 小时前
【51单片机】深度解析 51 串口 UART:原理、配置、收发实现与工程化应用全总结
c语言·开发语言·c++·stm32·单片机·嵌入式硬件·51单片机
Lester_110115 小时前
STM32 高级定时器PWM互补输出模式--如果没有死区,突然关闭PWM有产生瞬间导通的可能吗
stm32·单片机·嵌入式硬件·嵌入式软件
小李独爱秋17 小时前
“bootmgr is compressed”错误:根源、笔记本与台式机差异化解决方案深度指南
运维·stm32·单片机·嵌入式硬件·文件系统·电脑故障