前言
本文记录的是51单片机点亮LED灯,程序是LED流水灯。有两个方法供大家参考。
开发板:郭天祥51单片机
一、原理图
二、程序
1.电路分析
由原理图可以知道,74HC573是锁存器,OE是低电平有效,只要LED_LE置1,LED灯的亮灭就由IO口的高低电平控制。
锁存器的真值表
2.代码
方法1
#include "led.h"
#include <stdio.h>
#include <intrins.h>
#define uint32_t unsigned int
#define uint8_t unsigned char
sbit LED_LE = P2^5;
void delay(uint32_t i);
void main(void)
{
uint8_t i;
//uint8_t temp = 0;
LED_LE = 1; /* 锁存打开,直通 */
//P1 = 0xfe;
P1 = 0xff;
while(1)
{
// P1 = _crol_(P1, 1);
// delay(800);
for(i = 0; i < 8; i++) {
P1 = 0xff;
P1 &= ~(1 << i);
delay(800);
}
}
}
/*******延时函数*************/
void delay(uint32_t i)
{
uint8_t j;
for(i; i > 0; i--)
for(j = 255; j > 0; j--);
}
方法2
#include "led.h"
#include <stdio.h>
#include <intrins.h>
#define uint32_t unsigned int
#define uint8_t unsigned char
sbit LED_LE = P2^5;
void delay(uint32_t i);
void main(void)
{
uint8_t i;
LED_LE = 1; /* 锁存打开,直通 */
P1 = 0xfe;
//P1 = 0xff;
while(1)
{
P1 = _crol_(P1, 1);
delay(800);
// for(i = 0; i < 8; i++) {
// P1 = 0xff;
// P1 &= ~(1 << i);
// delay(800);
// }
}
}
/*******延时函数*************/
void delay(uint32_t i)
{
uint8_t j;
for(i; i > 0; i--)
for(j = 255; j > 0; j--);
}
3.代码(改进)
led.c
#include "./User/led/led.h"
sbit LED_LE = P2^5;
sbit D1 = P1^0;
sbit D2 = P1^1;
sbit D3 = P1^2;
sbit D4 = P1^3;
sbit D5 = P1^4;
sbit D6 = P1^5;
sbit D7 = P1^6;
sbit D8 = P1^7;
/*******延时函数*************/
void delay(uint32_t z)
{
uint32_t x, y = 0;
for(x = z; x > 0; x--)
for(y = 110; y > 0; y--);
}
void led_init(void)
{
LED_LE = 1; /* 锁存打开,直通 */
P1 = 0xff;
return ;
}
void water_fall_light(void)
{
uint8_t i;
LED_LE = 1; /* 锁存打开,直通 */
P1 = 0xff;
for(i = 0; i < 8; i++) {
P1 = 0xff;
P1 &= ~(1 << i);
delay(800);
}
return ;
}
void led1_toggle(void)
{
D1 = ~D1;
}
void led2_toggle(void)
{
D2 = ~D2;
}
void led3_toggle(void)
{
D3 = ~D3;
}
void led4_toggle(void)
{
D4 = ~D4;
}
void led5_toggle(void)
{
D5 = ~D5;
}
void led6_toggle(void)
{
D6 = ~D6;
}
void led7_toggle(void)
{
D7 = ~D7;
}
void led8_toggle(void)
{
D8 = ~D8;
}
led.h
#ifndef __LED_H__
#define __LED_H__
#include <reg52.h>
#include <stdio.h>
#include <intrins.h>
#include "./User/common/common.h"
#define ON 1
#define OFF (!ON)
#define LED1(a) if(a) \
{P2 |= (1 << 5);P1 &= ~(1 << 0);} \
else \
{P2 |= (1 << 5);P1 |= (1 << 0);}
#define LED2(a) if(a) \
{P2 |= (1 << 5);P1 &= ~(1 << 1);} \
else \
{P2 |= (1 << 5);P1 |= (1 << 1);}
#define LED3(a) if(a) \
{P2 |= (1 << 5);P1 &= ~(1 << 2);} \
else \
{P2 |= (1 << 5);P1 |= (1 << 2);}
#define LED4(a) if(a) \
{P2 |= (1 << 5);P1 &= ~(1 << 3);} \
else \
{P2 |= (1 << 5);P1 |= (1 << 3);}
#define LED5(a) if(a) \
{P2 |= (1 << 5);P1 &= ~(1 << 4);} \
else \
{P2 |= (1 << 5);P1 |= (1 << 4);}
#define LED6(a) if(a) \
{P2 |= (1 << 5);P1 &= ~(1 << 5);} \
else \
{P2 |= (1 << 5);P1 |= (1 << 5);}
#define LED7(a) if(a) \
{P2 |= (1 << 5);P1 &= ~(1 << 6);} \
else \
{P2 |= (1 << 5);P1 |= (1 << 6);}
#define LED8(a) if(a) \
{P2 |= (1 << 5);P1 &= ~(1 << 7);} \
else \
{P2 |= (1 << 5);P1 |= (1 << 7);}
void led_init(void);
void water_fall_light(void);
void led1_toggle(void);
void led2_toggle(void);
void led3_toggle(void);
void led4_toggle(void);
void led5_toggle(void);
void led6_toggle(void);
void led7_toggle(void);
void led8_toggle(void);
#endif