SY8835_For_Demo_Ourself/UsrSrc/led/led.c

354 lines
7.4 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
******************************************************************************
*
* @file led.c
* @brief led module
* @ic sy8835
*
* @version 1.0
* @date 2024/11/01 15:00:40
* @author Alex Xu
*
* Copyright (c) 2013-2099,Tkplusemi Technology Co.,Ltd.
* All Rights Reserved
*
* History:
* Revision Date Author Desc
* 1.0.0 2024/11/01 Alex build this file
******************************************************************************/
/*_____ I N C L U D E S ____________________________________________________*/
#include "led.h"
#include "pwm.h"
#ifdef LED_DISPLAY
volatile uint8_t Display_RSta = LED_OFF; // 显示状态
volatile uint8_t Display_GSta = LED_OFF; // 显示状态
volatile uint8_t Display_YSta = LED_OFF; // 显示状态
volatile uint8_t Display_BSta = LED_OFF; // 显示状态
/******************************************************************************\
Macro definitions
\******************************************************************************/
/******************************************************************************\
Variables definitions
\******************************************************************************/
static TS_LED_INFO LED;
/******************************************************************************\
Functions definitions
\******************************************************************************/
/*
* 函数名称 : LED_Init
* 功能描述 : LED初始化
* 参 数 : NONE
* 返回值 : NONE
*/
/******************************************************************************/
void LED_Init(void)
/******************************************************************************/
{
SFRADDR = P1_OE;
SFRDATA = 0x0F;
LEDR_OFF();
LEDG_OFF();
LEDB_OFF();
LEDY_OFF();
}
/*
* 函数名称 : LED_On
* 功能描述 : 点亮LED
* 参 数 : LED的ID
* 返回值 : NONE
*/
/******************************************************************************/
static void LED_On(uint8_t LedId)
/******************************************************************************/
{
switch(LedId)
{
case LED_R:
LEDR_ON();
break;
case LED_G:
LEDG_ON();
break;
case LED_Y:
LEDY_ON();
break;
case LED_B:
LEDB_ON();
break;
default: break;
}
}
/*
* 函数名称 : LED_Off
* 功能描述 : 熄灭LED
* 参 数 : LED的ID
* 返回值 : NONE
*/
/******************************************************************************/
static void LED_Off(uint8_t LedId)
/******************************************************************************/
{
switch(LedId)
{
case LED_R:
LEDR_OFF();
break;
case LED_G:
LEDG_OFF();
break;
case LED_Y:
LEDY_OFF();
break;
case LED_B:
LEDB_OFF();
break;
default: break;
}
}
/*
* 函数名称 : LED_Clr
* 功能描述 : 熄灭全部LED
* 参 数 : NONE
* 返回值 : NONE
*/
/******************************************************************************/
void LED_Clr(void)
/******************************************************************************/
{
uint8_t i;
for(i=0;i<LED_ID_MAX;i++)
{
LED.State[i]=LED_OFF;
LED.Light_Dir[i] = SHOW_NONE;
LED.Flash_Duty[i]=0;
LED.Flash_Period[i]=0;
LED.Flash_Times[i]=0;
LED.Timer[i]=0;
}
}
/*
* 函数名称 : LED_Set
* 功能描述 : LED工作方式设置
* 参 数 : State工作方式 / Period:闪烁周期(频率单位ms) / Times:闪烁次数当次数等于0xff时一直闪。
* 返回值 : NONE
*/
/******************************************************************************/
void LED_Set(uint8_t LedId,uint8_t State,uint16_t Period,uint16_t Times)
/******************************************************************************/
{
LED.State[LedId] = State;
LED.Flash_Duty[LedId] = Period / 2;
LED.Flash_Period[LedId] = Period;
LED.Flash_Times[LedId] = Times;
LED.Timer[LedId] = 0;
}
#ifdef PWM_ENABLE
/*
* 函数名称 : HuXiLED_Set
* 功能描述 : 呼吸灯LED工作方式设置
* 参 数 : State工作方式 / Period:闪烁周期(频率单位ms) / Times:闪烁次数当次数等于0xff时一直闪。
* 返回值 : NONE
*/
/******************************************************************************/
void HuXiLED_Set(uint8_t LedId,uint8_t State,uint8_t Dir)
/******************************************************************************/
{
LED.State[LedId] = State;
LED.Light_Dir[LedId] = Dir;
LED.Flash_Duty[LedId] = PWM_REL_VALUE;
LED.Timer[LedId] = 0;
}
#endif
/*
* 函数名称 : LED_Service
* 功能描述 : LED驱动需放在100ms时间片中
* 参 数 : NONE
* 返回值 : NONE
*/
/******************************************************************************/
void LED_Drv(void)
/******************************************************************************/
{
uint8_t i = 0;
static bit PWM_Dir_Flag;
for(i = 0;i < LED_ID_MAX;i++)
{
switch(LED.State[i])
{
case LED_ON:
LED_On(i);
break;
case LED_OFF:
LED_Off(i);
break;
case LED_FLASH:
if(LED.Timer[i] < LED.Flash_Duty[i])
{
LED_On(i);
}
else
{
LED_Off(i);
}
if(LED.Timer[i] >= LED.Flash_Period[i]) //LED.Timer[i]表示累加的时间
{
LED.Timer[i] = 0;
if(LED.Flash_Times[i] > 0)
{
LED.Flash_Times[i]--;
}
if(LED.Flash_Times[i] == 0)
{
LED.State[i] = LED_OFF;
}
}
break;
case LED_KEEP_FLASHING:
if(LED.Timer[i] <= LED.Flash_Duty[i])
{
LED_On(i);
}
else
{
LED_Off(i);
}
if(LED.Timer[i] >= LED.Flash_Period[i])
{
LED.Timer[i] = 0;
}
break;
#ifdef PWM_ENABLE
case LED_BREATHING_LIGHT:
if( LED.Light_Dir[i] == SHOW_NONE )
{
PWM_Disable(i);
PWM_Dir_Flag = 0;
LED.State[i] = LED_OFF;
}
else
{
if( LED.Light_Dir[i] == SLOW_LIGHT )
{
if( !PWM_Dir_Flag )
{
PWM_Dir_Flag = 1;
LED.Flash_Duty[i] = PWM_REL_VALUE;
}
LED.Flash_Duty[i]++;
if( LED.Flash_Duty[i] >= PWM_REL_FULL )
{
LED.Light_Dir[i] = SHOW_NONE;
}
}
else
if( LED.Light_Dir[i] == SLOW_DARK )
{
if( !PWM_Dir_Flag )
{
PWM_Dir_Flag = 1;
LED.Flash_Duty[i] = PWM_REL_FULL;
}
LED.Flash_Duty[i]--;
if( LED.Flash_Duty[i] <= PWM_REL_VALUE )
{
LED.Light_Dir[i] = SHOW_NONE;
}
}
else
if( LED.Light_Dir[i] == SHOW_NORMAL )
{
if( LED.Flash_Duty[i] >= PWM_REL_FULL )
{
PWM_Dir_Flag = 1;
}
else
if( LED.Flash_Duty[i] <= (PWM_REL_VALUE + 1) )
{
PWM_Dir_Flag = 0;
}
if(PWM_Dir_Flag)
{
LED.Flash_Duty[i]--;
}
else
{
LED.Flash_Duty[i]++;
}
}
PWM_Duty_Set(i, LED.Flash_Duty[i]);
}
break;
#endif
default:
break;
}
if (i == LED_R)
{
Display_RSta = LED.State[i]; // LED_OFF == DISPLAY_OFF
}
else
if (i == LED_G)
{
Display_GSta = LED.State[i]; // LED_OFF == DISPLAY_OFF
}
else
if (i == LED_Y)
{
Display_YSta = LED.State[i]; // LED_OFF == DISPLAY_OFF
}
else
if (i == LED_B)
{
Display_BSta = LED.State[i]; // LED_OFF == DISPLAY_OFF
}
// printf("LED.Flash_Duty[%d]:%d,LED.State:%d,LED.Light_Dir:%d.\r\n",(u16)i,(u16)(LED.Flash_Duty[i]),(u16)(LED.State[i]),(u16)(LED.Light_Dir[i]));
}
for(i=0;i<LED_ID_MAX;i++)
{
LED.Timer[i] += LED_PLUS_TIME;
}
}
#endif