/* ****************************************************************************** * * @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" #include "sys_tim.h" #include "system.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= 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) { //void tim1_Interrupt(void) interrupt Interrupt_Vector_TF1//interrupt address is 0x000B //{ static uint8_t gSacn_Row; SFRADDR = P0_OE; SFRDATA &= 0xC0; gSacn_Row++; #if 1 if( gSacn_Row > 24 ) { gSacn_Row = 1; } switch (gSacn_Row) { case 1: { PIN1_H(); if( Display_Ram_Tab[L1] == 1 ) { PIN2_L(); } } break; case 2: { PIN1_H(); if( Display_Ram_Tab[L2] == 1 ) { PIN3_L(); } } break; case 3: { PIN1_H(); if( Display_Ram_Tab[L3] == 1 ) { PIN4_L(); } } break; case 4: { PIN1_H(); if( Display_Ram_Tab[L4] == 1 ) { PIN5_L(); } } break; case 5: { PIN2_H(); if( Display_Ram_Tab[T1] == 1 ) { PIN1_L(); } } break; case 6: { PIN2_H(); if( Display_Ram_Tab[T2] == 1 ) { PIN3_L(); } } break; case 7: { PIN2_H(); if( Display_Ram_Tab[T3] == 1 ) { PIN4_L(); } } break; case 8: { PIN2_H(); if( Display_Ram_Tab[T4] == 1 ) { PIN5_L(); } } break; case 9: { PIN3_H(); if( Display_Ram_Tab[K1] == 1 ) { PIN1_L(); } } break; case 10: { PIN6_H(); if( Display_Ram_Tab[K2] == 1 ) { PIN4_L(); } } break; case 11: { PIN3_H(); if( Display_Ram_Tab[A1] == 1 ) { PIN2_L(); } } break; case 12: { PIN5_H(); if( Display_Ram_Tab[B1] == 1 ) { PIN1_L(); } } break; case 13: { PIN4_H(); if( Display_Ram_Tab[C1] == 1 ) { PIN3_L(); } } break; case 14: { PIN4_H(); if( Display_Ram_Tab[D1] == 1 ) { PIN2_L(); } } break; case 15: { PIN4_H(); if( Display_Ram_Tab[E1] == 1 ) { PIN1_L(); } } break; case 16: { PIN3_H(); if( Display_Ram_Tab[F1] == 1 ) { PIN4_L(); } } break; case 17: { PIN3_H(); if( Display_Ram_Tab[G1] == 1 ) { PIN5_L(); } } break; case 18: { PIN5_H(); if( Display_Ram_Tab[A2] == 1 ) { PIN3_L(); } } break; case 19: { PIN5_H(); if( Display_Ram_Tab[B2] == 1 ) { PIN4_L(); } } break; case 20: { PIN6_H(); if( Display_Ram_Tab[C2] == 1 ) { PIN2_L(); } } break; case 21: { PIN6_H(); if( Display_Ram_Tab[D2] == 1 ) { PIN3_L(); } } break; case 22: { PIN6_H(); if( Display_Ram_Tab[G2] == 1 ) { PIN1_L(); } } break; case 23: { PIN4_H(); if( Display_Ram_Tab[E2] == 1 ) { PIN5_L(); } } break; case 24: { PIN5_H(); if( Display_Ram_Tab[F2] == 1 ) { PIN2_L(); } } break; default: break; } #if 0 TF1 = 0; TH1 += C_TIM1_Reload >> 8; TL1 += C_TIM1_Reload & 0xFF; #endif #else if( gSacn_Row > 6 ) { gSacn_Row = 1; } switch (gSacn_Row) { case 1: //扫描显示第1行数码管 { 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(); PIN0_H(); } break; case 2: //扫描显示第2行数码管 { 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(); PIN1_H(); } break; case 3: //扫描显示第3行数码管 { 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(); PIN2_H(); } break; case 4: //扫描显示第4行数码管 { 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(); PIN3_H(); } break; case 5: //扫描显示第5行数码管 { 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(); PIN4_H(); } break; case 6: //扫描显示第6行数码管 { 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(); PIN5_H(); } break; } TH1 += C_TIM1_Reload1 >> 8; TL1 += C_TIM1_Reload1 & 0xFF; #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; 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 = 188; //188数码管显示全局变量。 uint8_t Display_Ram_Tab[17] = { //显示RAM,实际用到24个段,最后一个无用 0,0, 0,0,0,0,0,0,0, 0,0,0,0,0,0,0, //188显示,占用16个RAM 0 }; enum { B1=0,C1, A2,B2,C2,D2,E2,F2,G2, A3,B3,C3,D3,E3,F3,G3, //188显示,占用16个RAM NG }; /*数字0-9显示的表格。个位和百位有相关性,相差7段*/ idata uint8_t NumX_Tab[10][7] = { {A2,B2,C2,D2,E2,F2,NG}, //0 {B2,C2,NG,NG,NG,NG,NG}, //1 {A2,B2,G2,E2,D2,NG,NG}, //2 {A2,B2,G2,C2,D2,NG,NG}, //3 {F2,G2,B2,C2,NG,NG,NG}, //4 {A2,F2,G2,C2,D2,NG,NG}, //5 {A2,F2,G2,C2,D2,E2,NG}, //6 {A2,B2,C2,NG,NG,NG,NG}, //7 {A2,F2,G2,C2,D2,E2,B2}, //8 {A2,B2,C2,D2,F2,G2,NG}, //9 }; void Display_Scan_5Pin_188(void) { static uint8_t gSacn_Row; gSacn_Row++; PIN0_IN(); PIN1_IN(); PIN2_IN(); PIN3_IN(); PIN4_IN(); #if 0 if( gSacn_Row > 16 ) { gSacn_Row = 1; } switch (gSacn_Row) { case 1: { PIN2_H(); if( Display_Ram_Tab[B1] == 1 ) PIN3_L(); } break; case 2: { PIN1_H(); if( Display_Ram_Tab[C1] == 1 ) PIN3_L(); } break; case 3: { PIN1_H(); if( Display_Ram_Tab[A2] == 1 ) PIN2_L(); } break; case 4: { PIN2_H(); if( Display_Ram_Tab[B2] == 1 ) PIN1_L(); } break; case 5: { PIN3_H(); if( Display_Ram_Tab[C2] == 1 ) PIN2_L(); } break; case 6: { PIN3_H(); if( Display_Ram_Tab[D2] == 1 ) PIN1_L(); } break; case 7: { PIN4_H(); if( Display_Ram_Tab[E2] == 1 ) PIN1_L(); } break; case 8: { PIN4_H(); if( Display_Ram_Tab[F2] == 1 ) PIN2_L(); } break; case 9: { PIN4_H(); if( Display_Ram_Tab[G2] == 1 ) PIN3_L(); } break; case 10: { PIN0_H(); if( Display_Ram_Tab[A3] == 1 ) PIN1_L(); } break; case 11: { PIN1_H(); if( Display_Ram_Tab[B3] == 1 ) PIN0_L(); } break; case 12: { PIN0_H(); if( Display_Ram_Tab[C3] == 1 ) PIN2_L(); } break; case 13: { PIN2_H(); if( Display_Ram_Tab[D3] == 1 ) PIN0_L(); } break; case 14: { PIN0_H(); if( Display_Ram_Tab[E3] == 1 ) PIN3_L(); } break; case 15: { PIN3_H(); if( Display_Ram_Tab[F3] == 1 ) PIN0_L(); } break; case 16: { PIN4_H(); if( Display_Ram_Tab[G3] == 1 ) PIN0_L(); } break; } #else if( gSacn_Row > 5 ) { gSacn_Row = 1; } switch (gSacn_Row) { case 1: //扫描显示第1行数码管 { PIN0_H(); if( Display_Ram_Tab[A3] == 1 ) PIN1_L(); if( Display_Ram_Tab[C3] == 1 ) PIN2_L(); if( Display_Ram_Tab[E3] == 1 ) PIN3_L(); } break; case 2: //扫描显示第2行数码管 { PIN1_H(); if( Display_Ram_Tab[C1] == 1 ) PIN3_L(); if( Display_Ram_Tab[A2] == 1 ) PIN2_L(); if( Display_Ram_Tab[B3] == 1 ) PIN0_L(); } break; case 3: //扫描显示第3行数码管 { PIN2_H(); if( Display_Ram_Tab[B1] == 1 ) PIN3_L(); if( Display_Ram_Tab[B2] == 1 ) PIN1_L(); if( Display_Ram_Tab[D3] == 1 ) PIN0_L(); } break; case 4: //扫描显示第4行数码管 { PIN3_H(); if( Display_Ram_Tab[C2] == 1 ) PIN2_L(); if( Display_Ram_Tab[D2] == 1 ) PIN1_L(); if( Display_Ram_Tab[F3] == 1 ) PIN0_L(); } break; case 5: //扫描显示第5行数码管 { PIN4_H(); if( Display_Ram_Tab[E2] == 1 ) PIN1_L(); if( Display_Ram_Tab[F2] == 1 ) PIN2_L(); if( Display_Ram_Tab[G2] == 1 ) PIN3_L(); if( Display_Ram_Tab[G3] == 1 ) PIN0_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<17;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; Set_AllLed_Down(); Num_Hundred = num / 100; Num_Decade = (num % 100) / 10; Num_Uint = (num % 100) % 10; if( Num_Hundred ) { Display_Ram_Tab[B1] = 1; Display_Ram_Tab[C1] = 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; } } } #endif