SY883x_For_WEIHUA_W13/UsrSrc/led/led.c

1204 lines
21 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 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;
volatile uint8_t Display_RSta = LED_OFF; // 显示状态
volatile uint8_t Display_GSta = LED_OFF; // 显示状态
volatile uint8_t Display_YSta = LED_OFF; // 显示状态
/******************************************************************************\
Functions definitions
\******************************************************************************/
#if 0
/*
* 函数名称 : LED_Init
* 功能描述 : LED初始化
* 参 数 : NONE
* 返回值 : NONE
*/
/******************************************************************************/
void LED_Init(void)
/******************************************************************************/
{
SFRADDR = P0_OE;
SFRDATA = 0x1C;
SFRADDR = P0_DRV;
SFRDATA = 0x3F;
P0 &= ~0x1C;
}
#endif
/*
* 函数名称 : 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;
#if 0
case LED_B:
LEDB_ON();
break;
#endif
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;
#if 0
case LED_B:
LEDB_OFF();
break;
#endif
default: break;
}
}
/*
* 函数名称 : LED_Set
* 功能描述 : LED工作方式设置
* 参 数 : State工作方式 / Period:闪烁周期(频率单位ms) / Times:闪烁次数当次数等于0xff时一直闪。
* 返回值 : 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] == 0xff )
{
//LED.State = LED_KEEP_FLASHING;
break;
}
else
{
if( LED.Flash_Times[i] > 0 )
{
LED.Flash_Times[i]--;
}
if( LED.Flash_Times[i] == 0 )
{
LED.State[i] = LED_OFF;
}
}
}
break;
default:
LED_Off(i);
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
}
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.04s1/24帧画面
按最快0.04s40ms。亮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
#if DISPLAY_6PIN_188
uint8_t g_188_Num = 188; //188数码管显示全局变量。
uint8_t Display_Ram_Tab[25] = {
//显示RAM实际用到24个段最后一个无用
0,0,0,0, //左耳电量显示占用4个RAM
0,0,
0,0,0,0,0,0,0,
0,0,0,0,0,0,0, //188显示占用16个RAM
0,0,0,0, //右耳电量显示占用4个RAM
0
};
enum
{
L1=0,L2,L3,L4, //左耳电量显示占用4个RAM
K1,K2,
A1,B1,C1,D1,E1,F1,G1, //188显示占用16个RAM
A2,B2,C2,D2,E2,F2,G2,
T1,T2,T3,T4, //右耳电量显示占用4个RAM
NG
};
/*数字0-9显示的表格。个位和百位有相关性相差7段*/
idata uint8_t NumX_Tab[10][7] =
{
{A1,B1,C1,D1,E1,F1,NG}, //0
{B1,C1,NG,NG,NG,NG,NG}, //1
{A1,B1,G1,E1,D1,NG,NG}, //1
{A1,B1,G1,C1,D1,NG,NG}, //3
{F1,G1,B1,C1,NG,NG,NG}, //4
{A1,F1,G1,C1,D1,NG,NG}, //5
{A1,F1,G1,C1,D1,E1,NG}, //6
{A1,B1,C1,NG,NG,NG,NG}, //7
{A1,F1,G1,C1,D1,E1,B1}, //8
{A1,B1,C1,D1,F1,G1,NG}, //9
};
idata uint8_t EarL_Display_Tab[5][4]=
{
{NG,NG,NG,NG}, //0%
{L1,NG,NG,NG}, //电池框
{L1,L2,NG,NG}, //第一格电量
{L1,L2,L3,NG}, //前两格电量
{L1,L2,L3,L4}, //前三格电量
};
idata uint8_t EarR_Display_Tab[5][4]=
{
{NG,NG,NG,NG}, //0%
{T1,NG,NG,NG}, //电池框
{T1,T2,NG,NG}, //第一格电量
{T1,T2,T3,NG}, //前两格电量
{T1,T2,T3,T4}, //前三格电量
};
/*
*******************************************************************************
* void LED_188_Init(void)
*
* Description : LED 188 Initialization. --- 系统初始化调用。
*
* Arguments : NONE
* Returns : NONE
* Notes : NONE
*
*******************************************************************************
*/
#if 0
void LED_188_Init(void)
{
SFRADDR = MFP_CTL1;
SFRDATA &= ~0x0C;
P0 &= ~0xC0; //P00、P01、P02、P03、P04、P05
SFRADDR = P0_OE;
SFRDATA |= 0x3f;
}
void Set_AllLed_Input(void)
{
PIN0_IN();
PIN1_IN();
PIN2_IN();
PIN3_IN();
PIN4_IN();
PIN5_IN(); //消影
}
#endif
/*
*******************************************************************************
* void Display_Scan_6Pin_188(void)
*
* Description : LED 188 扫描驱动函数在Timer1中调用调用周期1ms。
*
* Arguments : NONE
* Returns : NONE
* Notes : NONE
*
*******************************************************************************
*/
/*
数码管:
第1行 第2行 第3行 第4行 第5行 第6行
2 <-L1<- 1 1 <-T1<- 2 1 <-K1<- 3 1 <-E1<- 4 1 <-B1<- 5 1 ->G2<- 6
3 <-L2<- 1 3 <-T2<- 2 2 <-A1<- 3 2 <-D1<- 4 2 <-F2<- 5 2 ->C2<- 6
4 <-L3<- 1 4 <-T3<- 2 4 <-F1<- 3 3 <-C1<- 4 3 <-A2<- 5 3 ->D2<- 6
5 <-L4<- 1 5 <-T4<- 2 5 <-G1<- 3 5 <-E2<- 4 4 ->B2<- 5 4 ->K2<- 6
*/
void Display_Scan_6Pin_188(void)
{
static uint8_t gSacn_Row;
gSacn_Row++;
Set_AllLed_Input();
#if 1
if( gSacn_Row > 24 )
{
gSacn_Row = 1;
}
switch (gSacn_Row)
{
case 1:
{
PIN0_H();
if( Display_Ram_Tab[L1] == 1 )
PIN1_L();
}
break;
case 2:
{
PIN0_H();
if( Display_Ram_Tab[L2] == 1 )
PIN2_L();
}
break;
case 3:
{
PIN0_H();
if( Display_Ram_Tab[L3] == 1 )
PIN3_L();
}
break;
case 4:
{
PIN0_H();
if( Display_Ram_Tab[L4] == 1 )
PIN4_L();
}
break;
case 5:
{
PIN1_H();
if( Display_Ram_Tab[T1] == 1 )
PIN0_L();
}
break;
case 6:
{
PIN1_H();
if( Display_Ram_Tab[T2] == 1 )
PIN2_L();
}
break;
case 7:
{
PIN1_H();
if( Display_Ram_Tab[T3] == 1 )
PIN3_L();
}
break;
case 8:
{
PIN1_H();
if( Display_Ram_Tab[T4] == 1 )
PIN4_L();
}
break;
case 9:
{
PIN2_H();
if( Display_Ram_Tab[K1] == 1 )
PIN0_L();
}
break;
case 10:
{
PIN2_H();
if( Display_Ram_Tab[A1] == 1 )
PIN1_L();
}
break;
case 11:
{
PIN2_H();
if( Display_Ram_Tab[F1] == 1 )
PIN3_L();
}
break;
case 12:
{
PIN2_H();
if( Display_Ram_Tab[G1] == 1 )
PIN4_L();
}
break;
case 13:
{
PIN3_H();
if( Display_Ram_Tab[E1] == 1 )
PIN0_L();
}
break;
case 14:
{
PIN3_H();
if( Display_Ram_Tab[D1] == 1 )
PIN1_L();
}
break;
case 15:
{
PIN3_H();
if( Display_Ram_Tab[C1] == 1 )
PIN2_L();
}
break;
case 16:
{
PIN3_H();
if( Display_Ram_Tab[E2] == 1 )
PIN4_L();
}
break;
case 17:
{
PIN4_H();
if( Display_Ram_Tab[B1] == 1 )
PIN0_L();
}
break;
case 18:
{
PIN4_H();
if( Display_Ram_Tab[F2] == 1 )
PIN1_L();
}
break;
case 19:
{
PIN4_H();
if( Display_Ram_Tab[A2] == 1 )
PIN2_L();
}
break;
case 20:
{
PIN4_H();
if( Display_Ram_Tab[B2] == 1 )
PIN3_L();
}
break;
case 21:
{
PIN5_H();
if( Display_Ram_Tab[G2] == 1 )
PIN0_L();
}
break;
case 22:
{
PIN5_H();
if( Display_Ram_Tab[C2] == 1 )
PIN1_L();
}
break;
case 23:
{
PIN5_H();
if( Display_Ram_Tab[D2] == 1 )
PIN2_L();
}
break;
case 24:
{
PIN5_H();
if( Display_Ram_Tab[K2] == 1 )
PIN3_L();
}
break;
}
#else
if( gSacn_Row > 6 )
{
gSacn_Row = 1;
}
switch (gSacn_Row)
{
case 1: //扫描显示第1行数码管
{
PIN0_H();
if( Display_Ram_Tab[L1] == 1 )
PIN1_L();
if( Display_Ram_Tab[L2] == 1 )
PIN2_L();
if( Display_Ram_Tab[L3] == 1 )
PIN3_L();
if( Display_Ram_Tab[L4] == 1 )
PIN4_L();
}
break;
case 2: //扫描显示第2行数码管
{
PIN1_H();
if( Display_Ram_Tab[T1] == 1 )
PIN0_L();
if( Display_Ram_Tab[T2] == 1 )
PIN2_L();
if( Display_Ram_Tab[T3] == 1 )
PIN3_L();
if( Display_Ram_Tab[T4] == 1 )
PIN4_L();
}
break;
case 3: //扫描显示第3行数码管
{
PIN2_H();
if( Display_Ram_Tab[K1] == 1 )
PIN0_L();
if( Display_Ram_Tab[A1] == 1 )
PIN1_L();
if( Display_Ram_Tab[F1] == 1 )
PIN3_L();
if( Display_Ram_Tab[G1] == 1 )
PIN4_L();
}
break;
case 4: //扫描显示第4行数码管
{
PIN3_H();
if( Display_Ram_Tab[E1] == 1 )
PIN0_L();
if( Display_Ram_Tab[D1] == 1 )
PIN1_L();
if( Display_Ram_Tab[C1] == 1 )
PIN2_L();
if( Display_Ram_Tab[E2] == 1 )
PIN4_L();
}
break;
case 5: //扫描显示第5行数码管
{
PIN4_H();
if( Display_Ram_Tab[B1] == 1 )
PIN0_L();
if( Display_Ram_Tab[F2] == 1 )
PIN1_L();
if( Display_Ram_Tab[A2] == 1 )
PIN2_L();
if( Display_Ram_Tab[B2] == 1 )
PIN3_L();
}
break;
case 6: //扫描显示第6行数码管
{
PIN5_H();
if( Display_Ram_Tab[G2] == 1 )
PIN0_L();
if( Display_Ram_Tab[C2] == 1 )
PIN1_L();
if( Display_Ram_Tab[D2] == 1 )
PIN2_L();
if( Display_Ram_Tab[K2] == 1 )
PIN3_L();
}
break;
}
#endif
}
/*
*******************************************************************************
* void Set_AllLed_Down(void)
*
* Description : LED 熄灭所有码段显示。。
*
* Arguments : NONE
* Returns : NONE
* Notes : NONE
*
*******************************************************************************
*/
void Set_AllLed_Down(void)
{
uint8_t i = 0;
for(i=0;i<25;i++)
{
Display_Ram_Tab[i] = 0;
}
}
/*
*******************************************************************************
* void Set_NumLED_Down(void)
*
* Description : LED 188 熄灭所有数字显示码段显示。
*
* Arguments : NONE
* Returns : NONE
* Notes : NONE
*
*******************************************************************************
*/
void Set_NumLED_Down(void)
{
uint8_t i = 0;
for(i=4;i<20;i++)
{
Display_Ram_Tab[i] = 0;
}
}
/*
*******************************************************************************
* void Display_Show_Num(uint8_t num)
*
* Description : LED 188 码段显示。
*
* Arguments : NONE
* Returns : NONE
* Notes : NONE
*
*******************************************************************************
*/
void Display_Show_Num(uint8_t num)
{
uint8_t i = 0;
uint8_t seg = 0;
uint8_t Num_Hundred = 0,Num_Decade = 0,Num_Uint = 0;
#if 0
if(num > 100)
{
num = 100;
return;
}
#endif
Set_NumLED_Down();
Num_Hundred = num / 100;
Num_Decade = (num % 100) / 10;
Num_Uint = (num % 100) % 10;
if( Num_Hundred )
{
Display_Ram_Tab[K1] = 1;
Display_Ram_Tab[K2] = 1;
}
for(i=0;i<7;i++)
{
if( num > 9 )//显示的数字大于9十位需要显示
{
seg = NumX_Tab[Num_Decade][i];
Display_Ram_Tab[seg] = 1;
}
seg = NumX_Tab[Num_Uint][i];
if(seg != NG)
{
Display_Ram_Tab[seg + 7] = 1;
}
}
}
/*
*******************************************************************************
* void Display_Show_Vor(uint8_t earR_bat)
*
* Description : LED 右耳电量 码段显示。
*
* Arguments : uint8_t earR_bat右耳耳机电量
* Returns : NONE
* Notes : NONE
*
*******************************************************************************
*/
void Display_Show_Vor(uint8_t earR_bat)
{
uint8_t i = 0;
uint8_t seg = 0;
for(i=20;i<24;i++)
{
Display_Ram_Tab[i] = 0;
}
//显示耳机电量
for(i=0;i<4;i++)
{
seg = EarR_Display_Tab[earR_bat][i];
Display_Ram_Tab[seg] = 1;
}
}
/*
*******************************************************************************
* void Display_Show_Vol(uint8_t earL_bat)
*
* Description : LED 左耳码段显示。
*
* Arguments : uint8_t earL_bat左耳电量显示
* Returns : NONE
* Notes : NONE
*
*******************************************************************************
*/
void Display_Show_Vol(uint8_t earL_bat)
{
uint8_t i = 0;
uint8_t seg = 0;
for(i=0;i<4;i++)
{
Display_Ram_Tab[i] = 0;
}
//显示耳机电量
for(i=0;i<4;i++)
{
seg = EarL_Display_Tab[earL_bat][i];
Display_Ram_Tab[seg] = 1;
}
}
#endif
#if DISPLAY_5PIN_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;
}
#endif
#ifdef LED_188
/*
*******************************************************************************
* void Set_AllLed_Down(void)
*
* Description : LED 188 熄灭所有码段显示。
*
* Arguments : NONE
* Returns : NONE
* Notes : NONE
*
*******************************************************************************
*/
void Set_AllLed_Input(void)
{
PIN0_IN();
PIN1_IN();
PIN2_IN();
PIN3_IN();
PIN4_IN();
#if DISPLAY_6PIN_188
PIN5_IN();
#endif
}
#endif
#if DISPLAY_5PIN_188
/*
*******************************************************************************
* 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_Input(); //消影
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