547 lines
11 KiB
C
547 lines
11 KiB
C
/*
|
||
******************************************************************************
|
||
*
|
||
* @file led.c
|
||
* @brief led module
|
||
* @ic TP3102
|
||
*
|
||
* @version 1.0
|
||
* @date 2022/08/04 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 2022/08/04 Alex build this file
|
||
******************************************************************************/
|
||
/*_____ I N C L U D E S ____________________________________________________*/
|
||
|
||
#include "led.h"
|
||
|
||
|
||
#ifdef LED_DISPLAY
|
||
|
||
/******************************************************************************\
|
||
Macro definitions
|
||
\******************************************************************************/
|
||
|
||
/******************************************************************************\
|
||
Variables definitions
|
||
\******************************************************************************/
|
||
static idata TS_LED_INFO LED;
|
||
|
||
/******************************************************************************\
|
||
Functions definitions
|
||
\******************************************************************************/
|
||
|
||
/*
|
||
* 函数名称 : LED_Init
|
||
* 功能描述 : LED初始化
|
||
* 参 数 : NONE
|
||
* 返回值 : NONE
|
||
*/
|
||
/******************************************************************************/
|
||
void LED_Init(void)
|
||
/******************************************************************************/
|
||
{
|
||
SFRADDR = P0_OE;
|
||
SFRDATA = 0x1C;
|
||
|
||
SFRADDR = P0_DRV;
|
||
SFRDATA = 0x3F;
|
||
|
||
P0 &= ~0x1C;
|
||
}
|
||
|
||
/*
|
||
* 函数名称 : 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_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_B:
|
||
LEDB_OFF();
|
||
break;
|
||
|
||
default:
|
||
break;
|
||
}
|
||
}
|
||
|
||
/*
|
||
* 函数名称 : LED_Clr
|
||
* 功能描述 : 熄灭全部LED
|
||
* 参 数 : NONE
|
||
* 返回值 : NONE
|
||
*/
|
||
#if 0
|
||
/******************************************************************************/
|
||
void LED_Clr(void)
|
||
/******************************************************************************/
|
||
{
|
||
uint8_t i = 0;
|
||
for(i=0; i<LED_ID_MAX; i++)
|
||
{
|
||
LED.State[i] = LED_OFF;
|
||
LED.Flash_Duty[i] = 0;
|
||
LED.Flash_Period[i] = 0;
|
||
LED.Flash_Times[i] = 0;
|
||
LED.Timer[i] = 0;
|
||
}
|
||
}
|
||
#endif
|
||
/*
|
||
* 函数名称 : LED_Set
|
||
* 功能描述 : LED工作方式设置
|
||
* 参 数 : LedId:ID / State:工作方式 / Duty:闪烁占空比(百分百:1-100) / Period:闪烁周期(频率,单位ms) / Times:闪烁次数
|
||
* 返回值 : NONE
|
||
*/
|
||
/******************************************************************************/
|
||
void LED_Set(uint8_t LedId,uint8_t State,uint8_t Period,uint8_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;
|
||
}
|
||
|
||
/*
|
||
* 函数名称 : LED_Service
|
||
* 功能描述 : LED驱动,需放在100ms时间片中
|
||
* 参 数 : NONE
|
||
* 返回值 : NONE
|
||
*/
|
||
/******************************************************************************/
|
||
void LED_Drv(void)
|
||
/******************************************************************************/
|
||
{
|
||
uint8_t i = 0;
|
||
|
||
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;
|
||
|
||
default:
|
||
LED_Off(i);
|
||
break;
|
||
}
|
||
}
|
||
|
||
for(i = 0;i < LED_ID_MAX;i++)
|
||
{
|
||
LED.Timer[i] += LED_PLUS_TIME;
|
||
}
|
||
}
|
||
|
||
#endif
|
||
|
||
|
||
#ifdef BREATHING_LIGHT
|
||
|
||
uint8_t BL_Timer = 0;
|
||
uint8_t BL_Flash_Duty = 0;
|
||
idata bit Flash_Duty_Rising = 1;
|
||
idata bit Flash_Duty_Declining = 0;
|
||
idata bit Breathing_Light_On = 0; //呼吸灯使能标志位。
|
||
/*
|
||
* 函数名称 : LED_Breathing_Light
|
||
* 功能描述 : LED呼吸灯驱动,需放在100us时间片中,每20ms改变一次占空比。
|
||
模拟人体呼吸,吸气和呼气各占1.5s,人眼的图像滞留时间0.04s(1/24帧画面)
|
||
按最快0.04s(40ms)。亮20ms,灭20ms,人眼看到的应该是一直亮。
|
||
呼吸灯,就是改变这40ms中,亮和灭所占用的百分百。
|
||
1500/20=75个周期,40ms/75=266us。75个变化周期中,每个周期增长1个单位266us,
|
||
|
||
75个周期刚好是40ms,这样就能达到全亮,反之全灭。
|
||
* 参 数 : NONE
|
||
* 返回值 : NONE
|
||
*/
|
||
/******************************************************************************/
|
||
void LED_Breathing_Light(void)
|
||
/******************************************************************************/
|
||
{
|
||
if( BL_Timer <= BL_Flash_Duty )
|
||
{
|
||
//LEDB_ON();
|
||
P04 = 1;
|
||
}
|
||
else
|
||
{
|
||
//LEDB_OFF();
|
||
P04 = 0;
|
||
}
|
||
|
||
if( BL_Timer >= 170 )
|
||
{
|
||
BL_Timer = 0;
|
||
|
||
if(Flash_Duty_Declining)
|
||
{
|
||
BL_Flash_Duty -= 1;
|
||
}
|
||
else
|
||
if(Flash_Duty_Rising)
|
||
{
|
||
BL_Flash_Duty += 1;
|
||
}
|
||
|
||
}
|
||
|
||
if( 170 <= BL_Flash_Duty )
|
||
{
|
||
Flash_Duty_Declining = 1;
|
||
Flash_Duty_Rising = 0;
|
||
}
|
||
else
|
||
if( BL_Flash_Duty <= 1 )
|
||
{
|
||
Flash_Duty_Rising = 1;
|
||
Flash_Duty_Declining = 0;
|
||
}
|
||
|
||
BL_Timer += 1;
|
||
}
|
||
|
||
#endif
|
||
|
||
|
||
#ifdef DISPLAY_LED_188
|
||
|
||
uint8_t g_188_Num = 0; //188数码管显示全局变量。
|
||
|
||
|
||
code uint16_t Segment[3][11] = {
|
||
|
||
{0x0000,0x0006,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000},//百位
|
||
|
||
{0x0770,0x0420,0x0741,0x0661,0x0431,0x0271,0x0371,0x0460,0x0771,0x0671,0x0000},//十位
|
||
|
||
{0xE888,0x8080,0xD808,0xD880,0xB080,0x7880,0x7888,0x8880,0xF888,0xF880,0x0000} //个位
|
||
|
||
};
|
||
|
||
/*
|
||
*******************************************************************************
|
||
* void LED_188_Init(void)
|
||
*
|
||
* Description : LED 188 Initialization. --- 系统初始化调用。
|
||
*
|
||
* Arguments : NONE
|
||
|
||
* Returns : NONE
|
||
|
||
* Notes : NONE
|
||
*
|
||
*******************************************************************************
|
||
*/
|
||
void LED_188_Init(void)
|
||
{
|
||
P0 &= ~0xE0; //P00、P01、P02、P03、P04
|
||
|
||
SFRADDR = P0_OE;
|
||
SFRDATA |= 0x1f;
|
||
|
||
}
|
||
|
||
|
||
/*
|
||
*******************************************************************************
|
||
* void Set_AllLed_Down(void)
|
||
*
|
||
* Description : LED 188 熄灭所有码段显示。
|
||
*
|
||
* Arguments : NONE
|
||
|
||
* Returns : NONE
|
||
|
||
* Notes : NONE
|
||
*
|
||
*******************************************************************************
|
||
*/
|
||
void Set_AllLed_Down(void)
|
||
{
|
||
PIN0_IN();
|
||
|
||
PIN1_IN();
|
||
|
||
PIN2_IN();
|
||
|
||
PIN3_IN();
|
||
|
||
PIN4_IN();
|
||
}
|
||
/*
|
||
*******************************************************************************
|
||
* void Display_Scan(uint8_t Pin,uint16_t Display_Sram)
|
||
*
|
||
* Description : LED 188 IO口和段码处理函数。
|
||
*
|
||
* Arguments : uint8_t Pin --- GPIO口
|
||
uint16_t Display_Sram --- 段码
|
||
|
||
* Returns : NONE
|
||
|
||
* Notes : NONE
|
||
*
|
||
*******************************************************************************
|
||
*/
|
||
static void Display_Scan(uint8_t Pin,uint16_t Display_Sram)
|
||
{
|
||
switch(Pin)
|
||
{
|
||
case 0:
|
||
{
|
||
PIN0_L(); //拉低Pin0
|
||
|
||
if(Display_Sram & 0x8000)
|
||
PIN1_H();
|
||
|
||
if(Display_Sram & 0x4000)
|
||
PIN2_H();
|
||
|
||
if(Display_Sram & 0x2000)
|
||
PIN3_H();
|
||
|
||
if(Display_Sram & 0x1000)
|
||
PIN4_H();
|
||
}
|
||
break;
|
||
|
||
case 1:
|
||
{
|
||
PIN1_L(); //拉低Pin1
|
||
|
||
if(Display_Sram & 0x0800)
|
||
PIN0_H();
|
||
|
||
if(Display_Sram & 0x0400)
|
||
PIN2_H();
|
||
|
||
if(Display_Sram & 0x0200)
|
||
PIN3_H();
|
||
|
||
if(Display_Sram & 0x0100)
|
||
PIN4_H();
|
||
}
|
||
break;
|
||
|
||
case 2:
|
||
{
|
||
PIN2_L(); //拉低Pin2
|
||
|
||
if(Display_Sram & 0x0080)
|
||
PIN0_H();
|
||
|
||
if(Display_Sram & 0x0040)
|
||
PIN1_H();
|
||
|
||
if(Display_Sram & 0x0020)
|
||
PIN3_H();
|
||
|
||
if(Display_Sram & 0x0010)
|
||
PIN4_H();
|
||
}
|
||
break;
|
||
|
||
case 3:
|
||
{
|
||
PIN3_L(); //拉低Pin3
|
||
|
||
if(Display_Sram & 0x0008)
|
||
PIN0_H();
|
||
|
||
if(Display_Sram & 0x0004)
|
||
PIN1_H();
|
||
|
||
if(Display_Sram & 0x0002)
|
||
PIN2_H();
|
||
|
||
if(Display_Sram & 0x0001)
|
||
PIN4_H();
|
||
}
|
||
break;
|
||
#if 0
|
||
case 4:
|
||
{
|
||
PIN4_L(); //拉低Pin4
|
||
|
||
if(Display_Sram & 0x0008)
|
||
PIN0_H();
|
||
|
||
if(Display_Sram & 0x0004)
|
||
PIN1_H();
|
||
|
||
if(Display_Sram & 0x0002)
|
||
PIN2_H();
|
||
|
||
if(Display_Sram & 0x0001)
|
||
PIN3_H();
|
||
}
|
||
break;
|
||
#endif
|
||
|
||
default:
|
||
break;
|
||
}
|
||
}
|
||
/*
|
||
*******************************************************************************
|
||
* void LED_188_Drive(uint8_t Num)
|
||
*
|
||
* Description : LED 188 Drive. --- 188 LED 数码管显示,定时扫描。
|
||
*
|
||
* Arguments : uint8_t Num (00~188)
|
||
|
||
* Returns : NONE
|
||
|
||
* Notes : NONE
|
||
*
|
||
*******************************************************************************
|
||
*/
|
||
void LED_188_Drive(uint8_t Num)
|
||
{
|
||
static uint8_t Case_Cnt; //逐行扫描
|
||
|
||
uint8_t Num_Hundred = 0 ,Num_Decade = 0 ,Num_Uint = 0;
|
||
|
||
uint16_t nDisplay_Sram = 0;
|
||
|
||
if(Num > 188)
|
||
{
|
||
Num = 188;
|
||
}
|
||
|
||
Num_Hundred = Num / 100; //百位
|
||
|
||
Num_Decade = (Num % 100) / 10; //十位
|
||
|
||
Num_Uint = (Num % 100) % 10; //个位
|
||
|
||
Set_AllLed_Down(); //消影
|
||
|
||
nDisplay_Sram = 0;
|
||
|
||
nDisplay_Sram = Segment[0][Num_Hundred] | Segment[1][Num_Decade] | Segment[2][Num_Uint]; //显示百位,十位,个位。
|
||
|
||
switch(Case_Cnt)
|
||
{
|
||
case 0:
|
||
Display_Scan(0,nDisplay_Sram);
|
||
Case_Cnt++;
|
||
break;
|
||
|
||
case 1:
|
||
Display_Scan(1,nDisplay_Sram);
|
||
Case_Cnt++;
|
||
break;
|
||
|
||
case 2:
|
||
Display_Scan(2,nDisplay_Sram);
|
||
Case_Cnt++;
|
||
break;
|
||
|
||
case 3:
|
||
Display_Scan(3,nDisplay_Sram);
|
||
Case_Cnt = 0;
|
||
break;
|
||
default:
|
||
Case_Cnt = 0;
|
||
break;
|
||
}
|
||
}
|
||
|
||
#endif
|
||
|