/************************** Copyright (c) ********************************** ** FUZHOU FUGUANG ELECTRONICS Co.,LTD. ** ¸£Öݸ£¹âµç×ÓÓÐÏÞ¹«Ë¾ ** http://www.fuguang.com ** **-------------- File Info ------------------------------------------------- ** File name: M64ADC_Driver.c ** Last modified Date: 2008-11-16 ** Last Version: 1.0 ** Descriptions: M64ADC Drivers lib ** **-------------------------------------------------------------------------- ** Created by: mxpopstar ** Created date: 2008-11-16 ** Version: 1.0 ** Descriptions: The original version ** **-------------------------------------------------------------------------- ** Modified by: mxpopstar ** Modified date: 2008-11-20 ** Version: ** Descriptions: M64ADC Drivers lib ** ***************************************************************************/ // #include "M64ADC_Driver.h" #include "KeyBoard_Driver.h" #include "SYSSetPage.h" #include "TFT_Driver.h" #include #include #include "WorkingPage.h" const unsigned char TemperatureTable[80] PROGMEM = { 38, 40, 42, 44, 46, 48, 50, 52, 54, 57, 59, 61, 64, 66, 69, 72, 74, 77, 80, 83, 86, 88, 91, 94, 97, 100,104,107,110,113, 116,119,123,126,129,132,136,139,142,145, 149,152,155,158,161,165,168,171,174,177, 180,183,186,189,192,195,198,200,203,206, 209,211,214,216,219,221,224,226,228,231, 233,235,237,240,242,244,246,248,249,251, }; // const unsigned char TemperatureText[3][1] PROGMEM = { #if(SoftLanguage == CN) {101}, {115},//- {243},//¡æ #else {0}, {35},//- {32},//¡æ #endif }; // extern volatile SYS_State GB_SYS_State; extern volatile unsigned int GB_TestingParm[15]; //-------------------------------------------------------------------------- static unsigned int EEpAdjValue[8] __attribute__((section(".eeprom"))) = { 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, }; //-------------------------------------------------------------------------- static unsigned int ADCValueAdj[8] = {1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024}; static unsigned int DisChargeI = 0; static unsigned int ChargeI = 0; static unsigned int ExternalI = 0; static unsigned int InnerSumVol = 0; static unsigned int OnlineVol = 0; static unsigned int SumSubCurrent = 0; static unsigned int Temperature = 0; static unsigned char ACPOWER_OFF_COUNT = 0; static unsigned char ACPOWER_CK_EN = ACPOWER_CK_DISABLE; static unsigned int SumVolParam = 1535; //-------------------------------------------------------------------------- //--------- ÅäÖÃADCµÄÊäÈë¶Ë¿Ú ---------------------------------------------- void InitIO_M64ADC(void) { //------------ ¶ÔÓ¦¶Ë¿ÚÉèÖÃΪÊäÈë²¢½ûÖ¹ÉÏÀ­ -------------------- DDRF &= ~((1<1252)) { ADCValueAdj[n] = 1024; er = 1; } } if(er == 1) { eeprom_busy_wait(); eeprom_write_block(ADCValueAdj, EEpAdjValue, sizeof(EEpAdjValue)); } } // //--------- ÖØÆôADC¹¦ÄÜ ---------------------------------------------------- void Restart_ADC_System(void) { ADCSRA &= ~0x80; //unable adc _delay_us(1); ADCSRA |= 0x80; //enable adc _delay_us(1); } // //------------------- »ñÈ¡ºÍÉèÖõçѹµçÁ÷ÖµµÄУ׼ϵÊý ----------------------- //------------------- »ñÈ¡ºÍÉèÖõçѹµçÁ÷ÖµµÄУ׼ϵÊý ----------------------- unsigned int GetADCAdjustValue(const unsigned char index, const unsigned char readEEp) { unsigned int value = 0; if(readEEp == 1) { eeprom_busy_wait(); eeprom_read_block(ADCValueAdj, EEpAdjValue, sizeof(EEpAdjValue)); } if((index<=3) && (GetVolRange()==5)) { if(index == 2) value = ADCValueAdj[0]; else if(index == 3) value = ADCValueAdj[2]; } else value = ADCValueAdj[index]; return value; } void SetADCAdjustValue( const unsigned char index, const unsigned int value, const unsigned char writeEEp ) { if((index<=3) && (GetVolRange()==5)) { if(index == 2) ADCValueAdj[0] = value; //ÔÚÏß¶Ë else if(index == 3) ADCValueAdj[2] = value; //×é¶Ë } else ADCValueAdj[index] = value; if(writeEEp == 1) { eeprom_busy_wait(); eeprom_write_block(ADCValueAdj, EEpAdjValue, sizeof(EEpAdjValue)); eeprom_busy_wait(); eeprom_read_block(ADCValueAdj, EEpAdjValue, sizeof(EEpAdjValue)); } } //------------------------ ζÈÒì³£¸ß¼ì²â ---------------------------------- void CheckTempHighAlert(void) { if(TmpeTooHigh) { if(GB_SYS_State.TempHighCount < 20) GB_SYS_State.TempHighCount++; } else { GB_SYS_State.TempHighCount = 0; } } void ResetTempHighAlert(void) { GB_SYS_State.TempHighCount = 0; } // //-------------------------- ÊеçÖжϼì²â ---------------------------------- unsigned char Get_ACPOWER_State(void) { return ACPOWER_OFF_COUNT; } // void Set_ACPOWER_Work(const unsigned char state) { ACPOWER_OFF_COUNT = 0; ACPOWER_CK_EN = state; } // void Check_ACPOWER_State(void) { static unsigned int count = 0; if(ACPOWER_CK_EN == ACPOWER_CK_DISABLE) { ACPOWER_OFF_COUNT = 0; return; } if((count++ % 1024) != 0) return; count = 0; if(ACPOWER_OFF) { if(ACPOWER_OFF_COUNT < 20) ACPOWER_OFF_COUNT++; } else { ACPOWER_OFF_COUNT = 0; } } // //------------------ Æô¶¯µ¥¸öÄ£ÄâͨµÀµÄADC¹ý³Ì ----------------------------- unsigned int Get_ADC_Value( const unsigned char channel, const unsigned int parm1, const unsigned int parm2 ) { unsigned char n = 30; unsigned long res = 0; unsigned long tmpvalue = 0; unsigned long count = 0; ADMUX = channel&0x1F; //select channel _delay_ms(1); while(n--) { ADCSRA |= 0x40; //start adc while(!(ADCSRA&0x10)) //wait for adc to finish { count++; if(count>500000) { Restart_ADC_System(); //restart adc count=0; return 0; } } tmpvalue = ADC; //return adc value; tmpvalue *= 2500; tmpvalue *= parm1; tmpvalue /= 1024; tmpvalue /= parm2; res += tmpvalue; ADCSRA |= 0x10; //clear ADC finish tag } res /= 30; return res; } // //------------ »ñÈ¡ÎÂ¶È ------------------------------ unsigned int GetTemperature(void) { return Temperature; } //------------ »ñÈ¡±¾»ú²âÁ¿µÄ"×é¶Ëµçѹ" -------------- unsigned int GetInnerSumVol(void) { //InnerSumVol =800; return InnerSumVol; } //------------ »ñÈ¡±¾»ú²âÁ¿µÄ"ÔÚÏßµçѹ" -------------- unsigned int GetOnlineVol(void) { return OnlineVol; } //------------ »ñÈ¡±¾»úÄÚ²¿µÄ"·ÅµçµçÁ÷" -------------- unsigned int GetDisChargeI(void) { return DisChargeI; } //------------ »ñÈ¡±¾»úÄÚ²¿µÄ"³äµçµçÁ÷" -------------- unsigned int GetChargeI(void) { return ChargeI; } //------------ »ñÈ¡±¾»úÍⲿµÄ"ǯ¿ÚµçÁ÷" -------------- unsigned int GetExternalI(void) { return ExternalI; } // void SetSumSubCurrent(const unsigned int value) { SumSubCurrent = value; } // unsigned char GetTemperatureValue(const unsigned char data) { unsigned char n = 0; for(n=0; n<80; n++) { if(data <= pgm_read_byte(&TemperatureTable[n])) { break; } } return n; } // void DSPTemperature(const unsigned int x, const unsigned int y, const unsigned char color) { if(GetTemperature() >= 20) { DSP24x24Graph(TemperatureText[0], 1, 16, x, y, blue, color); PrintValue(3, 0, GetTemperature()-20, "", 16, x+24, y, blue, color); } else { DSP24x24Graph(TemperatureText[1], 1, 16, x, y, blue, color); PrintValue(3, 0, 20-GetTemperature(), "", 16, x+24, y, blue, color); } DSP24x24Graph(TemperatureText[2], 1, 16, x+56, y, blue, color); } // void SetSumVolParam(const unsigned char param) { switch(param) { case 1: SumVolParam = 350*1; break; case 5: SumVolParam = 350*5; break; default:break; } } //-------------------------------------------------------------------------- unsigned int GetADCSumVolValue(void) { unsigned long tmpvalue = 0; //if((GB_SYS_State.WorkState==Stopped) || (GB_SYS_State.WorkState==Paused)) { SetSumVolParam(GetVolParm());//SetSumVolParam(GetVolRange()); } tmpvalue = Get_ADC_Value(ADCDH, SumVolParam, 1169); if(GetVolRange() == 1) { if(tmpvalue < 30) tmpvalue = 0; //------------- ×é¶Ëµçѹ²âÁ¿ÖµÐ£×¼ ------------------------------------- tmpvalue = tmpvalue * (2048-ADCValueAdj[3]) * 10 / 1024 / 10; } else { if(tmpvalue < 200) tmpvalue = 0; //------------- ×é¶Ëµçѹ²âÁ¿ÖµÐ£×¼ ------------------------------------- tmpvalue = tmpvalue * (2048-(ADCValueAdj[2])) * 10 / 1024 / 10; } InnerSumVol = (unsigned int)tmpvalue; return ((unsigned int)tmpvalue); } //-------------------------------------------------------------------------- //---------------------------- ÂÖѯ²âÁ¿µçѹµçÁ÷Öµ -------------------------- void GetVolCurrent(volatile FBO_Data *fbodata) { static unsigned char index = 0; static unsigned int conindex = 0; unsigned long tmpvalue = 0; switch((index++) % 6) { //----------------- ±¾»úÔÚÏßµçѹ²âÁ¿ ------------------------------- case 0: //###################### Çë×¢ÒâÔ¤±àÒëÇø #################### #if(MashineType == FBO48CT) tmpvalue = Get_ADC_Value(ADCCH, SumVolParam, 1169); if(tmpvalue < 130) tmpvalue = 0; //---------- ÔÚÏßµçѹ²âÁ¿ÖµÐ£×¼ ------------------- tmpvalue = tmpvalue * (2048-(ADCValueAdj[0])) * 10 / 1024 / 10; OnlineVol = (unsigned int)tmpvalue; if(GB_TestingParm[0] == 0) //Èç¹ûÊÇÔÚÏ߷ŵç { fbodata->OnlineVol = GetOnlineVol(); } else fbodata->OnlineVol = 0; //·ñÔòÇåÁãÔÚÏßµçѹ #else fbodata->OnlineVol = 0; Check_ACPOWER_State(); #endif //###################### Çë×¢ÒâÔ¤±àÒëÇø #################### break; //----------------- ±¾»ú×é¶Ëµçѹ²âÁ¿ ------------------------------- case 1: conindex++;//µçѹ²É¼¯ËÙ¶ÈÊǵçÁ÷²É¼¯ËٶȵÄ1/4 if(conindex%4 == 0) { GetADCSumVolValue(); if(fbodata->BattSum == 0) { fbodata->SumVoltage = GetInnerSumVol(); } } break; //----------------- ±¾»úÍⲿǯ¿ÚµçÁ÷²âÁ¿ --------------------------- case 2: ; unsigned int param1 = (GetExtClamprange(0)/100); tmpvalue = Get_ADC_Value(ADCXA, param1, 2); //------------- ǯ¿ÚµçÁ÷²âÁ¿ÖµÐ£×¼ ---------------- tmpvalue = tmpvalue * (2048-(ADCValueAdj[6])) * 10 / 1024 / 10; ExternalI = (unsigned int)tmpvalue; if(ExternalI < 10) { ExternalI = 0; } break; //----------------- ±¾»úÄÚ²¿³ä·ÅµçµçÁ÷²âÁ¿ ------------------------- case 3: tmpvalue = Get_ADC_Value(ADCIA, 3, 4); if(GB_SYS_State.TestType == TestTypeDischarge) //ÈôÊǷŵç²âÊÔ { //--------- ±¾»ú·ÅµçµçÁ÷²âÁ¿ÖµÐ£×¼ ---------------- tmpvalue = tmpvalue * (2048-(ADCValueAdj[4])) * 10 / 1024 / 10; DisChargeI = (unsigned int)tmpvalue; ChargeI = 0; if(fbodata->SumCurrent < ExternalI + DisChargeI) fbodata->SumCurrent = ExternalI + DisChargeI; if(SumSubCurrent > ExternalI + DisChargeI) fbodata->SumCurrent = SumSubCurrent; else fbodata->SumCurrent = ExternalI + DisChargeI; /////////////////////----µçÁ÷Êý¾ÝÐÞÊÎ////2016-5-17--------- if((GB_SYS_State.WorkState==Discharging)&&(GB_TestingParm[14] == Static_Curr)) { if(((fbodata->SumCurrent>GB_TestingParm[4]*10)&&(fbodata->SumCurrent-GB_TestingParm[4]*10)<6) ||((fbodata->SumCurrentSumCurrent)<6)) fbodata->SumCurrent = GB_TestingParm[4]*10; } } else //·ñÔòÊdzäµç²âÊÔ { //--------- ±¾»ú³äµçµçÁ÷²âÁ¿ÖµÐ£×¼ ---------------- tmpvalue = tmpvalue * (2048-(ADCValueAdj[5])) * 10 / 1024 / 10; ChargeI = (unsigned int)tmpvalue; DisChargeI = 0; if(fbodata->SumCurrent < ExternalI + ChargeI) fbodata->SumCurrent = ExternalI + ChargeI; if(SumSubCurrent > ExternalI + ChargeI) fbodata->SumCurrent = SumSubCurrent; else fbodata->SumCurrent = ExternalI + ChargeI; } break; //------------------------ ²âÁ¿ÎÂ¶È ------------------------- case 4: tmpvalue = Get_ADC_Value(ADCTEMP, 1, 1); Temperature = GetTemperatureValue((unsigned char)(tmpvalue/10)); if(Temperature+1024 > ADCValueAdj[7]) { Temperature = Temperature+1024-ADCValueAdj[7]; if(Temperature > 99) Temperature = 99; } else Temperature = 0; break; case 5: Check_ACPOWER_State(); break; default:break; } } //