📄 drv_lampv1.c
字号:
#include "config_GD61.h"
#include "include_GD61.h"
#include <stdio.h>
////////////////////////////////////////////////////////////////////
uint16 TestDrvState;
extern CALENDAR CurrentTime;
extern uint8 AutoLightCntMode;
extern uint16 TransferKKK;
extern volatile uint16 mSecondTick;
extern volatile uint16 LD_AlarmCounter;
extern volatile uint16 PW_AlarmCounter;
extern volatile uint8 TestSecondTick;
////////////////////////////////////////////////////////////////////
void LampPinTOInput(void);
void LampPinInitialize(void);
void LampStateDec(uint16 DriveValue);
void LampStateInc(uint16 DriveValue);
void RestoreLightState(void);
void U_ChannelTest(void);
void DL1_RelayTest(void);
void DK_RelayTest(void);
void LightLoopTest(void);
void LampStateAbs(uint16 DriveValue);
void Rel_DQ_toOFF(void); //control DQ off
void Rel_DQ_toON(void); //control DQ on
void Cha_DQ_toOFF(void); //change DQ off
void Cha_DQ_toON(void); //control DQ on
void Rel_DK_toOFF(void);
void Rel_DK_toON(void);
void ForceLampState(uint16 DriveValue);
void _ForceLampState(uint16 DriveValue);
void PreForceDKxState(uint16 DriveValue);
void PreToLightOn(void);
void InputPinTest(void);
void OutputPinTest(void);
void SynZeroPoint_DH(void);
void SynZeroPoint_DL(void);
///////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////
uint8 VoltageState[]={
//////////////////// area_1
// 0 1 2 3 4
Minus_37V5,Minus_35V0,Minus_32V5,Minus_30V0,Minus_27V5, // 1
// 5 6 7 8 9
Minus_25V0,Minus_22V5,Minus_20V0,Minus_17V5,Minus_15V0, // 2
// 10 11 12 13 14
Minus_12V5,Minus_10V0,Minus_07V5,Minus_05V0,Minus_02V5, // 3
// 15
Minus_00V0,
//////////////////// area_2
// 16 17 18 19 20
Plus_00V0, Plus_02V5, Plus_05V0, Plus_07V5, Plus_10V0 // 4
};
///////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////
void LampPinTOInput(void)
{
//set pin to input mode
IO0DIR=IO0DIR&
(~DK4_P0_04)&
(~DK2_P0_05)&
(~DK1_P0_06)&
(~DQ__P0_07)&
(~LED_P0_22)&
(~DL2_P0_27)& // only for module test.
(~DL__P0_28);
////
IO1DIR=IO1DIR&
(~DH__P1_16)&
(~DL1_P1_17)&
(~DK__P1_24)&
(~DK3_P1_25)&
(~DK5_P1_26)&
(~EEP_P1_29)&
(~DT__P1_31); // ok
}
///////////////////////////////////////////////////////////////////
void LampPinInitialize(void)
{
//set pin to out mode
#ifdef INCLUDE_LAMP_CON
// PINSEL0 = 0;
// PINSEL1 = 0;
PINSEL2 = PINSEL2&(~0x08); // P1 [25:16] serve as GPIO
PINSEL2 = PINSEL2&(~0x04); // P1 [31:26] serve as GPIO
////
IO0DIR=IO0DIR
|DK4_P0_04
|DK2_P0_05
|DK1_P0_06
|DQ__P0_07
|LED_P0_22
|DL2_P0_27 // only for module test.
|DL__P0_28;
////
IO1DIR=IO1DIR
|DH__P1_16
|DL1_P1_17
|DK__P1_24
|DK3_P1_25
|DK5_P1_26
|EEP_P1_29
|DT__P1_31;
////
IO0DIR=IO0DIR&
(~VINT1_P0_26)&
(~VINT2_P0_25);
IO1DIR=IO1DIR&
(~VINT3_P1_18)&
(~VINT4_P1_19);
////
IO0SET=DL2_P0_27; // only for module test.
IO1SET=DL1_P1_17; // turn on DL1 relay
IO1SET=DK__P1_24; // turn off DK relay
IO1SET=EEP_P1_29; // eeprom write_protection pin
#endif // INCLUDE_LAMP_CON
}
///////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////
void LampStateDec(uint16 DriveValue)
{
uint16 buff;
uint16 CurState;
//// get the current position
buff = StateValue & 0x00ff;
if(buff<=Minus_vMAX){
CurState = 00+(Minus_vMAX-buff);
}
else if((buff>=Plus_00V0)&&(buff<=Plus_vMAX)){
CurState = 16+(buff-Plus_00V0);
}
//// get the executing position
DriveValue = CurState - DriveValue;
if((CurState>=16)&&(DriveValue<=15)){
DriveValue--; //pass the dead area
}
//// relay switching
Uart0SendString("LampStateDec()");
if((DriveValue&0x8000)==0x8000){ //over adjust
LampStateAbs(Minus_vMAX);
}
else{
LampStateAbs(VoltageState[DriveValue]);
}
}
///////////////////////////////////////////////////////////////////
void LampStateInc(uint16 DriveValue)
{
uint16 buff;
uint16 CurState;
//// get the current position
buff = StateValue & 0x00ff;
if(buff<=Minus_vMAX){
CurState = 00+(Minus_vMAX-buff);
}
else if((buff>=Plus_00V0)&&(buff<=Plus_vMAX)){
CurState = 16+(buff-Plus_00V0);
}
//// get the executing position
DriveValue = CurState + DriveValue;
if((CurState<=15)&&(DriveValue>=16)){
DriveValue++; //pass the dead area
}
//// relay switching
Uart0SendString("LampStateInc()");
if(DriveValue>Plus_vMAX){ //over adjust
LampStateAbs(Plus_vMAX);
}
else{
LampStateAbs(VoltageState[DriveValue]);
}
}
///////////////////////////////////////////////////////////////////
void LampStateAbs(uint16 DriveValue)
{
char dbuffer[100];
sprintf(dbuffer,"LampStateAbs() Changes state to 0x%x",DriveValue);
Uart0SendString(dbuffer);
////
if((((StateValue & LIGHT_ON_FLG_BIT)==LIGHT_ON_FLG_BIT)&&(DriveValue==(StateValue & 0x00ff)))||
(((StateValue & LIGHT_ON_FLG_BIT)==0x0000) &&(DriveValue==Light_toOFF))){
Uart0SendString("Keep current state.");
}
else{
ForceLampState(DriveValue); //switch action
}
}
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
void RestoreLightState(void)
{
#ifdef INCLUDE_LAMP_CON //>>>(1) light control function
uint16 databuf;
/////////////// warm boot ///////////////////////////////////////////////////
if((POWERUP_FLAG1==WARM_START_FLAG1)&&(POWERUP_FLAG2==WARM_START_FLAG2)){
if((StateValue & LIGHT_ON_FLG_BIT)==LIGHT_ON_FLG_BIT){
//// turn light ON
databuf = StateValue & 0x00ff;
if(databuf <= Light_toON){
_ForceLampState(databuf); //force to the state before reset
if(((RSIR & WDTR_RESET) == WDTR_RESET)||((RSIR & EXTR_RESET) == EXTR_RESET)){
// _ForceLampState(databuf); //force to the state before reset
Uart0SendString("WatchDog or ResetPIN reset,Restore light state.");
}else{ //POWR_RESET //BODR_RESET
// _ForceLampState(databuf); //force to the state before reset
Uart0SendString("BODR_RESET or POWR_RESET,Restore light state.");
}
}else{ //databuf > Light_toON
_ForceLampState(Light_toOFF);
Uart0SendString("Warm Boot,Force light OFF in unknown case.");
}
}
else{
//// turn light OFF
_ForceLampState(Light_toOFF);
Uart0SendString("Warm Boot,Restore light OFF");
}
}
///////////////////////// cold boot //////////////////////
else{
//all light control pins switch off
_ForceLampState(Light_toOFF);
Uart0SendString("Cold Boot,Force light OFF.");
//enable alarm sending
LD_AlarmCounter=0; //enable alarm message sending
PW_AlarmCounter=0;
//set the initialization state
AutoVoltageFlag = 0;
AutoVoltageTimer= 0;
TargetVoltage = 0;
//initialize the measuring data
CurInput_II = 0; CurInput_UU = 0;
CurInput_WP = 0; CurInput_QP = 0;
CurInput_SP = 0; CurInput_PF = 0;
CurInput_RO = 0; CurInput_LC = 0;
CurInputULevel = 0; CurInputILevel = 0;
CurOutput_I = 0; CurOutput_U = 0;
CurOutput_WP = 0; CurOutput_QP = 0;
CurOutput_SP = 0; CurOutput_PF = 0;
CurOutput_RO = 0; CurOutput_LC = 0;
CurOutputULevel = 0; CurOutputILevel = 0;
}
#endif //#ifedf INCLUDE_LAMP_CON //>>>(1) light control function
}
///////////////////////////////////////////////////////////////////
//////////////////////////////////////////////// SYNCHRO_CYCLE_NUM
void SynZeroPoint_DH(void)
{
uint32 tp1,tp2;
if((IO1PIN&DK__P1_24)==DK__P1_24) //set DK on
{
WatchDogClear();
IO1CLR=DK__P1_24;
DelayMS_(SWITCH_DELAY_TIME);
WatchDogClear();
}
StateValue = StateValue&(~SYNC__ALARM_TYPE); //clr alarm bit
#ifdef EN_SYNCHRO_MAIN_POWER
mSecondTick = 20*SYNCHRO_CYCLE_NUM; // waitting for SYNCHRO_CYCLE_NUM cycles at most
do{
tp1 = IO0PIN & VINT1_P0_26;
tp2 = IO0PIN & VINT2_P0_25;
if((tp1==VINT1_P0_26)&&(tp2==VINT2_P0_25)) break;
}while(mSecondTick>1);
if(mSecondTick<=1){
StateValue = StateValue|SYNC__ALARM_TYPE; //set alarm bit if syn failed
Uart0SendString("synchronization(1) failed");
}
#endif
}
///////////////////////////////////////////////////////////////////
void SynZeroPoint_DL(void)
{
uint32 tp1,tp2;
if((IO1PIN&DK__P1_24)==DK__P1_24) //set DK on
{
WatchDogClear();
IO1CLR=DK__P1_24;
DelayMS_(SWITCH_DELAY_TIME);
WatchDogClear();
}
StateValue = StateValue&(~SYNC__ALARM_TYPE); //clr alarm bit
#ifdef EN_SYNCHRO_MAIN_POWER
mSecondTick = 20*SYNCHRO_CYCLE_NUM; // waitting for SYNCHRO_CYCLE_NUM cycles at most
do{
tp1 = IO1PIN & VINT3_P1_18;
tp2 = IO1PIN & VINT4_P1_19;
if((tp1==VINT3_P1_18)&&(tp2==VINT4_P1_19)) break;
}while(mSecondTick>1);
if(mSecondTick<=1){
StateValue = StateValue|SYNC__ALARM_TYPE; //set alarm bit if syn failed
Uart0SendString("synchronization(2) failed");
}
#endif
}
//////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////
/*
void SynZeroPoint(void)
{
StateValue = StateValue&(~SYNC__ALARM_TYPE); //clr alarm bit
#ifdef EN_SYNCHRO_MAIN_POWER
uint32 tp1,tp2;
PINSEL0 = PINSEL0 & 0xffff3fff; // set p0.7 as gpio
mSecondTick = 20*SYNCHRO_CYCLE_NUM; // waitting for SYNCHRO_CYCLE_NUM cycles at most
tp2 = IO0PIN & MPF_P0_07;
do{
tp1 = tp2;
tp2 = IO0PIN & MPF_P0_07;
#ifdef SYNCHRO_MAIN_POWER_FALLING
if((tp1==MPF_P0_07)&&(tp2==0)) break ;
#else
if((tp1==0)&&(tp2==MPF_P0_07)) break ;
#endif
}while(mSecondTick>1);
if(mSecondTick<=1){
StateValue = StateValue|SYNC__ALARM_TYPE; //set alarm bit if syn failed
Uart0SendString("synchronization(3) failed");
}
IRQDisable();
EINT2_Initialize(); //restore MPF_P0_07 to interrupt mode
IRQEnable();
#endif
}
*/
////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////
void U_ChannelTest(void)
{
static uint8 U_State_buff=0;
if(U_State_buff==0){
Uart0SendString("IO1CLR=DT_P1_31(toInPut)");
RF_SendString("IO1CLR=DT_P1_31(toInPut)");
IO1CLR=DT__P1_31; //measure input voltage
U_State_buff=1;
}else{
Uart0SendString("IO1SET=DT_P1_31(toOutPut)");
RF_SendString("IO1SET=DT_P1_31(toOutPut)");
IO1SET=DT__P1_31; //measure output voltage
U_State_buff=0;
}
}
////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////
void DL1_RelayTest(void)
{
static uint8 DL1_buff=0;
if(DL1_buff==0){
Uart0SendString("IO1SET=DL1_P1_17(toON)");
RF_SendString("IO1SET=DL1_P1_17(toON)");
IO1SET=DL1_P1_17;
DL1_buff=1;
}else{
Uart0SendString("IO1CLR=DL1_P1_17(toOFF)");
RF_SendString("IO1CLR=DL1_P1_17(toOFF)");
IO1CLR=DL1_P1_17;
DL1_buff=0;
}
}
////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////
void DK_RelayTest(void)
{
static uint8 DK_buff=0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -