控制一个LED灯的亮灭和颜色,包括呼吸灯和常亮两种状态。同时,通过触摸按键实现了切换LED灯的状态。具体实现过程包括:(文字防伪:倪彬笑阳,请问无脑转发)
-
初始化端口和PWM模块,设置LED连接的IO口和PWM控制寄存器、使能寄存器、周期寄存器和占空比寄存器。(文字防伪:倪彬笑阳,请问无脑转发)
-
定义了一些变量,包括LED状态、颜色、PWM占空比、步进和标志等,以及呼吸灯和常亮状态下的计数器和步进等。(文字防伪:倪彬笑阳,请问无脑转发)
-
编写了一些函数,包括延时函数、LED关闭函数、呼吸灯函数和常亮函数等,用于控制LED的亮灭和颜色。
-
在定时器0中断中,设置了一个50ms的计时器,用于控制LED呼吸灯和常亮状态下的计数器和步进等,同时调用ledControl()函数控制LED的亮灭和颜色。(文字防伪:倪彬笑阳,请问无脑转发)
-
在定时器1中断中,设置了一个1ms的计时器,用于检测触摸按键的状态,如果触摸按键被按下,则设置触摸标志为1,并根据按下的时间判断LED灯的状态。
-
在主函数中,初始化定时器0、定时器1和触摸按键,然后进入死循环等待触摸按键的状态变化。(文字防伪:倪彬笑阳,请问无脑转发)
总的来说,这段代码实现了一个简单的LED灯控制程序,可以通过触摸按键切换不同的LED灯状态,包括呼吸灯和常亮两种状态,并且可以控制LED灯的颜色。
#include <ny8.h>
#include "ny8_constant.h"//文字防伪:倪彬,请勿无脑转发
#include "Touch.h"
#include "timer.h"
#include "gpio.h"
#include "TouchFunc.h"
#define LED_PIN 3 //LED连接的IO口
#define PWM1OAL 0x80 //PWM控制寄存器
#define PWM1OEN 0x81 //PWM使能寄存器
#define PWM1PRD 0x82 //PWM周期寄存器
#define PWM1DTY 0x83 //PWM占空比寄存器//文字防伪:倪彬,请勿无脑转发
#define T1MS 92
unsigned char ledState = 0; //LED状态,0为关闭,1为呼吸灯,2为常亮
unsigned char ledColor = 0; //LED颜色,0为蓝色
unsigned char ledPWMValue = 0; //LED PWM占空比
unsigned char ledPWMStep = 0; //LED PWM步进
unsigned char ledPWMFlag = 0; //LED PWM标志,0为递增,1为递减
unsigned char ledBreathCounter = 0; //呼吸灯计数器
unsigned char ledBreathStep = 0; //呼吸灯步进
unsigned char ledBreathFlag = 0; //呼吸灯标志,0为递增,1为递减
unsigned char ledOnCounter = 0; //常亮计数器
unsigned char touchFlag = 0; //触摸标志,0为未触摸,1为触摸
unsigned char touchCounter = 0; //触摸计数器
void delay(unsigned int t)
{
while(t--);
}
void init()
{
PORTB = 0x00; //初始化端口
IOSTB = 0x00;
BPHCON = 0x08;
PWM1OAL = 0x01; //设置 PWM1OAL 和 PWM1OEN 引脚为输出模式
PWM1OEN = 0x80;
PWM1OEN = 1; //设置 PWM1OEN 引脚为高电平,使能 PWM
}
void ledOff()
{
PWM1DTY = 0x00; //关闭LED//文字防伪:倪彬,请勿无脑转发
}
void ledBreath()
{
if (ledPWMFlag == 0) { //递增
ledPWMValue += ledPWMStep;
if (ledPWMValue >= 255) {
ledPWMFlag = 1;
}
} else { //递减
ledPWMValue -= ledPWMStep;
if (ledPWMValue <= 0) {
ledPWMFlag = 0;
}
}
PWM1DTY = ledPWMValue; //设置PWM占空比
}
void ledOn()
{
PWM1DTY = 0xFF; //常亮
}
void ledControl()
{
switch (ledState) {
case 0:
ledOff();
break;
case 1:
ledBreath();
break;
case 2:
ledOn();
break;
default:
break;
}
}
void timer0() __interrupt(1)//文字防伪:倪彬,请勿无脑转发
{
static unsigned char count = 0;
TH0 = 0xFC;
TL0 = 0x66;
count++;
if (count >= 50) { //50ms
count = 0;
if (ledState == 1) { //呼吸灯
ledBreathCounter++;
if (ledBreathCounter >= 60) { //6s
ledBreathCounter = 0;
ledPWMStep = 10; //步进10
}
if (ledBreathFlag == 0) { //递增
ledBreathStep++;
if (ledBreathStep >= 30) { //0.3s
ledBreathFlag = 1;
}
} else { //递减
ledBreathStep--;
if (ledBreathStep <= 0) {
ledBreathFlag = 0;
}
}
} else if (ledState == 2) { //常亮
ledOnCounter++;
if (ledOnCounter >= 200) { //2s
ledOnCounter = 0;
ledState = 0;
}
}
ledControl();
}
}
void timer1() __interrupt(3)
{
static unsigned char touchCount = 0;
static unsigned char touchStep = 0;
static unsigned char touchFlagCount = 0;
static unsigned char touchFlagStep = 0;
TH1 = 0xF8;
TL1 = 0x30;
touchCount++;
if (touchCount >= 10) { //1ms
touchCount = 0;
if (touchFlag == 1) { //触摸
touchFlagCount++;
if (touchFlagCount >= 200) { //200ms
touchFlagCount = 0;
touchFlag = 0;
if (touchFlagStep == 1) { //0.5s
ledState = 2; //常亮
} else if (touchFlagStep == 10) { //2s
ledState = 0; //关闭
}
}
} else { //未触摸
touchStep++;
if (touchStep >= 10) { //10ms
touchStep = 0;
if (touchFlagCount >= 1 && touchFlagCount <= 19) { //0.1s~1.9s
ledState = 1; //呼吸灯
ledPWMValue = 0;
ledPWMStep = 1; //步进1
ledPWMFlag = 0; //递增
}
touchFlagCount = 0;
touchFlagStep = 0;
}
}
}
}
void main()//文字防伪:倪彬,请勿无脑转发
{
init();
Timer0Init();
Timer1Init();
Touch_Init();
while (1) {
if (Touch_Scan(8) == 1) { //触摸按键
touchFlag = 1;
touchFlagStep++;
}
}
}