单片机-LED实验

1、51工程模版

#include "reg52.h"

void main(){

while(1){

}

}

2、LED灯亮

#include "reg52.h"

sbit LED1=P2^0;

void main(){

while(1){

LED1=0;

}

}

3、LED闪烁

#include "reg52.h"

sbit LED1=P2^0; //P2大写,默认是高电平

typedef unsigned int u16; //0~65535

typedef unsigned char u8; //0~255

void delay_10us(u16 ten_us){

while(ten_us--); //当传入ten_su=1,大约延时10us

}

void main(){

while(1){

LED1=0; //灯亮

delay_10us(50000); //延时 //500ms 450-500

LED1=1; //灯灭

delay_10us(50000);

}

}

4、LED流水灯

4.1从左向右依次亮

方法一

#include "reg52.h"

#define LED_PORT P2

typedef unsigned int u16;

typedef unsigned char u8;

void delay_10us(u16 ten_us){

while(ten_us--);

}

void main(){

u16 i=0;

while(1){

for (i=0;i<8;i++){

LED_PORT=~(0X01<<i);

delay_10us(50000);

}

}

}

方法二 左移函数 corl

#include "reg52.h"
#include "intrins.h"

typedef unsigned int u16;

typedef unsigned char u8;

#define LED_PORT P2//宏定义

void delay_10us(u16 ten_us){

while(ten_us--);

}

void main(){

u16 i=0;

LED_PORT=0XFE; //LED_PORT=~0X01; //1111 1110

delay_10us(50000);

while(1){

for(i=0;i<7;i++){

LED_PORT=crol(LED_PORT,1);

delay_10us(50000);

}

}

}

4.2从右向左依次亮

#include "reg52.h"
#include "intrins.h"

typedef unsigned int u16;

typedef unsigned char u8;

#define LED_PORT P2//宏定义

void delay_10us(u16 ten_us){

while(ten_us--);

}

void main(){

u16 i=0;

LED_PORT=0X7F; //0111 1111

delay_10us(50000);

while(1){

for(i=0;i<7;i++){

LED_PORT=cror(LED_PORT,1);

delay_10us(50000);

}

}

}

相关推荐
SmartRadio1 小时前
在CH585M代码中如何精细化配置PMU(电源管理单元)和RAM保留
linux·c语言·开发语言·人工智能·单片机·嵌入式硬件·lora
qq_411262421 小时前
纯图像传感器(只出像素),还是 Himax WiseEye/WE1/WE-I Plus 这类带处理器、能在端侧跑模型并输出“metadata”的模块
人工智能·嵌入式硬件·esp32·四博智联
不脱发的程序猿2 小时前
嵌入式开发中C++内存泄漏的场景与解决办法
单片机·嵌入式硬件·嵌入式
至为芯2 小时前
IP5326至为芯支持TYPE-C协议的2.4A充放电移动电源方案芯片
单片机·嵌入式硬件
小何code2 小时前
STM32入门教程,第10课(下),Keil调试模式
stm32·单片机·嵌入式硬件
SmartRadio2 小时前
进一步优化CH585M的低功耗模式
c语言·开发语言·单片机·嵌入式硬件·物联网
XjtDZ2 小时前
CS5715E异步升压DC-DC控制器
单片机·嵌入式硬件·智能音箱
Jason_zhao_MR2 小时前
V2G 实战:SECC GreenPHY 通信开发方案
嵌入式硬件·物联网·嵌入式·边缘计算
清风6666663 小时前
基于单片机的智能一体化自动咖啡机设计
单片机·嵌入式硬件·毕业设计·课程设计·期末大作业
安生生申3 小时前
STM32 软件IIC ADS1115 的使用
单片机·嵌入式硬件