/** * @brief Description 系统初始化 * @param None * @retval None */ void SysInit(void) { FirstScanSysData(); // set_time(); read_time(); while(LCD_GetFlagStatus(LCD_FLAG_UDR) != RESET); displayTIME(Rtc.Hour,Rtc.Minute); showTIME; LCD_UpdateDisplayRequest(); Started_Channel = GetStartChanel(Channel_count); StartedChannelForDisplay = Started_Channel; Sensor1.sensor_infor[0] = 4; Sensor1.sensor_infor[1] = 2; Bat.Voltage_TestTime=11; Sensor1.sensor1_str.sensor_type = 1; Flag.MucReset = 1; Flag.IsDisplayRightNow = 1; JlyParam.WorkStatueIsStop = 1; Queue.FlashSectorPoint = 0; }
/** * @brief This function writes a char in the LCD RAM. * @param ch: The character to dispaly. * @param point: A point to add in front of char. * This parameter can be one of the following values: * @arg POINT_OFF: No point to add in front of char. * @arg POINT_ON: Add a point in front of char. * @param apostrophe: Flag indicating if a apostrophe has to be add in front * of displayed character. * This parameter can be one of the following values: * @arg APOSTROPHE_OFF: No apostrophe to add in back of char. * @arg APOSTROPHE_ON: Add an apostrophe in back of char. * @param position: Position in the LCD of the caracter to write. * This parameter can be any value in range [0:7]. * @retval None */ void LCD_GLASS_DisplayChar(uint8_t* ch, Point_Typedef point, Apostrophe_Typedef apostrophe, uint8_t position) { /*!< Wait Until the last LCD RAM update finish */ while(LCD_GetFlagStatus(LCD_FLAG_UDR) != RESET) { } /*!< LCD Write Char */ LCD_GLASS_WriteChar(ch, point, apostrophe, position); /*!< Requesy LCD RAM update */ LCD_UpdateDisplayRequest(); }
/** * @brief Configures the LCD GLASS relative GPIO port IOs and LCD peripheral. * @param None * @retval None */ void LCD_GLASS_Init(void) { LCD_InitTypeDef LCD_InitStructure; LCD_GPIOConfig(); /*!< Configure the LCD Glass GPIO pins */ /*!< Configure the LCD interface -------------------------------------------*/ RCC_APB1PeriphClockCmd(RCC_APB1Periph_LCD, ENABLE); /*!< Enable LCD APB1 Clock */ LCD_InitStructure.LCD_Prescaler = LCD_Prescaler_8; LCD_InitStructure.LCD_Divider = LCD_Divider_16; LCD_InitStructure.LCD_Duty = LCD_Duty_1_4; LCD_InitStructure.LCD_Bias = LCD_Bias_1_3; LCD_InitStructure.LCD_VoltageSource = LCD_VoltageSource_Internal; LCD_Init(&LCD_InitStructure); /*!< Configure the Pulse On Duration */ LCD_PulseOnDurationConfig(LCD_PulseOnDuration_2); /*!< Configure the LCD Contrast (3.51V) */ LCD_ContrastConfig(LCD_Contrast_Level_7); /*!< Wait Until the LCD FCR register is synchronized */ LCD_WaitForSynchro(); /*!< Enable LCD peripheral */ LCD_Cmd(ENABLE); /*!< Wait Until the LCD is enabled */ while(LCD_GetFlagStatus(LCD_FLAG_ENS) == RESET) { } /*!< Wait Until the LCD Booster is ready */ while(LCD_GetFlagStatus(LCD_FLAG_RDY) == RESET) { } }
/** * @brief Display the measured voltage on LCD glass. * @param None * @retval None */ void DisplayVoltage(uint32_t ADCVoltage) { uint32_t Digit1, Digit2, Digit3, Digit4 = 0; ADCVoltage = ADCVoltage * 3300; ADCVoltage = ADCVoltage / 4095; Digit1 = ADCVoltage / 1000; LCD_String[1] = Digit1 + 0x30; VoltageDisplay[1] = Digit1 + 0x30; Digit2 = (ADCVoltage - (Digit1 * 1000)) / 100; LCD_String[2] = Digit2 + 0x30; VoltageDisplay[3] = Digit2 + 0x30; Digit3 = ((ADCVoltage - ((Digit1 * 1000) + (Digit2 * 100))) / 10); LCD_String[3] = Digit3 + 0x30; VoltageDisplay[4] = Digit3 + 0x30; Digit4 = (ADCVoltage - ((Digit1 * 1000) + (Digit2 * 100) + (Digit3 * 10))); LCD_String[4] = Digit4 + 0x30; VoltageDisplay[5] = Digit4 + 0x30; /*!< Wait Until the last LCD RAM update finish */ while(LCD_GetFlagStatus(LCD_FLAG_UDR) != RESET) { } #ifdef USE_STM32L152_EVAL /* Display one character on LCD */ LCD_GLASS_WriteChar(&LCD_String[1], POINT_ON, APOSTROPHE_OFF, 1); /* Display one character on LCD */ LCD_GLASS_WriteChar(&LCD_String[2], POINT_OFF, APOSTROPHE_OFF, 2); /* Display one character on LCD */ LCD_GLASS_WriteChar(&LCD_String[3], POINT_OFF, APOSTROPHE_OFF, 3); /* Display one character on LCD */ LCD_GLASS_WriteChar(&LCD_String[4], POINT_OFF, APOSTROPHE_OFF, 4); /* Display one character on LCD */ LCD_GLASS_WriteChar(&LCD_String[5], POINT_OFF, APOSTROPHE_OFF, 5); /* Display one character on LCD */ LCD_GLASS_WriteChar(&LCD_String[6], POINT_OFF, APOSTROPHE_OFF, 6); #endif /*!< Request LCD RAM update */ LCD_UpdateDisplayRequest(); }
/** * @brief This function writes a char in the LCD RAM. * @param ptr: Pointer to string to display on the LCD Glass. * @retval None */ void LCD_GLASS_DisplayString(uint8_t* ptr) { uint32_t i = 0x00; /*!< Wait Until the last LCD RAM update finish */ while(LCD_GetFlagStatus(LCD_FLAG_UDR) != RESET) { } /*!< Send the string character by character on lCD */ while ((*ptr != 0) & (i < 8)) { /*!< Display one character on LCD */ LCD_GLASS_WriteChar(ptr, POINT_OFF, APOSTROPHE_OFF, i); /*!< Point on the next character */ ptr++; /*!< Increment the character counter */ i++; } /*!< Requesy LCD RAM update */ LCD_UpdateDisplayRequest(); }
/** * @brief Description 电池电压检测数据处理 * @param 无 * @retval 无 */ static void Voltage_ADC1config(void) { unsigned char i=0,j=0; unsigned int temp=0; unsigned int sum=0,adc_temp[vtest_cnt]; float temp2=0; unsigned char voltage_zhengshu=0; //电压整数位 unsigned char voltage_xiaoshu=0; //电压小数位 BATTEST_POWER(ON); //开启电池电压检测电源 for(i=0;i<vtest_cnt;i++)//???? { /* 由于没有采用外部触发,所以使用软件触发ADC转换 */ ADC_SoftwareStartConv(ADC1); Delay_ms(5); //延时5ms adc_temp[i] = Bat.ADC_BatConvertedValue; } BATTEST_POWER(OFF); //关闭电池电压检测电源 for(i=1;i<=vtest_cnt;i++)// for(j=0;j<=vtest_cnt-i;j++) if(adc_temp[j]>=adc_temp[j+1]) { temp=adc_temp[j]; adc_temp[j]=adc_temp[j+1]; adc_temp[j+1]=temp; } for(i=0;i<(vtest_cnt-2);i++)//舍弃最大、最小值,求累加和 { sum=sum+adc_temp[1+i]; } temp2=5.0/4095; temp2=temp2*sum; temp2=temp2/(vtest_cnt-2); temp =(uint16_t)(temp2*100); voltage_zhengshu=temp/100; voltage_xiaoshu=temp-voltage_zhengshu*100; voltage_xiaoshu=((voltage_xiaoshu/10)<<4)+(voltage_xiaoshu%10); if(Flag.ExPwOn==0) //没外接电,电池电压检测 { if(temp>=400) {while(LCD_GetFlagStatus(LCD_FLAG_UDR) != RESET){} showBATT;LCD_UpdateDisplayRequest();} else if((temp>=380)&&(temp<400)) {while(LCD_GetFlagStatus(LCD_FLAG_UDR) != RESET){} showBATT2;LCD_UpdateDisplayRequest();} else if((temp>=350)&&(temp<380)) {while(LCD_GetFlagStatus(LCD_FLAG_UDR) != RESET){} showBATT1;LCD_UpdateDisplayRequest();} else if(temp<350) {while(LCD_GetFlagStatus(LCD_FLAG_UDR) != RESET){} showBATT0;LCD_UpdateDisplayRequest();} if(temp<=340) Flag.Low_Voltage = 1; else Flag.Low_Voltage = 0; } else { if(temp>=390) { //在外接电下,电压>4.1v,认为充满电,这里置充满电标志位! Flag.BatteryFull=1; } else { Flag.BatteryFull=0; } } }
/** * @brief Current measurement in different MCU modes: * RUN/SLEEP/LowPower/STANDBY with/without RTC * @caller main and ADC_Icc_Test * @param MCU state * @retval ADC value. */ uint16_t ADC_Icc_Test(uint8_t Mcu_State) { GPIO_InitTypeDef GPIO_InitStructure; uint16_t adc_measure; uint32_t i; RCC_TypeDef SavRCC; /* Reset UserButton State */ UserButton = FALSE; /* Start counter */ GPIO_HIGH(CTN_GPIO_PORT,CTN_CNTEN_GPIO_PIN); /* Disable the RTC Wakeup Interrupt */ RTC_ITConfig(RTC_IT_WUT, DISABLE); /* Disable LCD */ LCD_Cmd(DISABLE); /* wait until LCD disable */ while (LCD_GetFlagStatus(LCD_FLAG_ENS) == SET); /*Reset Idd-WakeUP flag*/ Idd_WakeUP = FALSE; /* Set IO in lowpower configuration*/ GPIO_LowPower_Config(); /*Disable fast wakeUp*/ PWR_FastWakeUpCmd(DISABLE); /* Test MCU state for configuration */ switch (Mcu_State) { /* Run mode : Measurement Measurement performed with MSI 4 MHz without RTC*/ case MCU_RUN: /* switch on MSI clock */ SetHSICLKToMSI(RCC_MSIRange_6,NoDIV2,NoRTC) ; /* shitch on MSI clock */ Config_RCC(&SavRCC); SysTick->CTRL = 0; RCC->APB1ENR = 0; /* To run nops during measurement: it's the best case for low current */ for (i=0;i<0xffff;i++) { __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); } break; /* SLEEP mode : Measurement performed with MSI 4 MHz without RTC in WFI mode*/ case MCU_SLEEP: SetHSICLKToMSI(RCC_MSIRange_6,NoDIV2,NoRTC) ; Config_RCC(&SavRCC); Config_Systick_50ms(); Delay(1); /* Request Wait For Interrupt */ PWR_EnterSleepMode(PWR_Regulator_ON,PWR_SLEEPEntry_WFI); break; /* RUN LOW POWER mode : Measurement performed with MSI 32 Khz without RTC */ case MCU_LP_RUN: /* Disable PVD */ PWR_PVDCmd(DISABLE); /* Enable The ultra Low Power Mode */ PWR_UltraLowPowerCmd(ENABLE); /* Save the RCC configuration registers */ Config_RCC(&SavRCC); /* Stop the sys tick in order to avoid IT */ SysTick->CTRL = 0; #ifdef TESTINRAM SetHSICLKToMSI(RCC_MSIRange_0,DIV2,NoRTC) ; PWR_EnterLowPowerRunMode(ENABLE); while(PWR_GetFlagStatus(PWR_FLAG_REGLP) == RESET) ; DisableInterrupts(); EnterLPRUNModeRAM(); EnableInterrupts(); #else /* Swith in MSI 32KHz */ SetHSICLKToMSI(RCC_MSIRange_64KHz,DIV2,NoRTC) ; PWR_EnterLowPowerRunMode(ENABLE); while(PWR_GetFlagStatus(PWR_FLAG_REGLP) == RESET) ; /* Launch the counter */ GPIO_LOW(CTN_GPIO_PORT,CTN_CNTEN_GPIO_PIN); /* To run the nop during measurement: it's the best case for low current until counter reach detected by IT --> Idd_WakeUP */ do{ __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); } while (Idd_WakeUP == FALSE ); #endif PWR_EnterLowPowerRunMode(DISABLE); while(PWR_GetFlagStatus(PWR_FLAG_REGLP) != RESET) ; break; /* SLEEP LOW POWER mode Measurement done to MSI 32 Khz without RTC */ case MCU_LP_SLEEP: /* Disable PVD */ PWR_PVDCmd(DISABLE); /* Enable Ultra low power mode */ PWR_UltraLowPowerCmd(ENABLE); /* To save the RCC configuration registers */ Config_RCC(&SavRCC); /* To stop the sys tick for avoid IT */ SysTick->CTRL = 0; /* Swith in MSI 32KHz */ SetHSICLKToMSI(RCC_MSIRange_0,DIV2,NoRTC) ; #ifdef TESTINRAM DisableInterrupts(); EnterLPSLEEPModeRAM(); EnableInterrupts(); #else /* Falling edge for start counter */ GPIO_LOW(CTN_GPIO_PORT,CTN_CNTEN_GPIO_PIN); /* Request Wait For Interrupt */ PWR_EnterSleepMode(PWR_Regulator_LowPower,PWR_SLEEPEntry_WFI); #endif break; /* STOP modes Measurement done to MSI 32 Khz without or with RTC */ case MCU_STOP_NoRTC: case MCU_STOP_RTC: /* Disable PVD */ PWR_PVDCmd(DISABLE); /* Enable Ultra low power mode */ PWR_UltraLowPowerCmd(ENABLE); /* To save the RCC configuration registers */ Config_RCC(&SavRCC); /* To stop the sys tick for avoid IT */ SysTick->CTRL = 0; /* Swith in MSI 32KHz */ if( Mcu_State == MCU_STOP_NoRTC ) SetHSICLKToMSI(RCC_MSIRange_0,DIV2,NoRTC) ; else SetHSICLKToMSI(RCC_MSIRange_0,DIV2,WITHRTC) ; /* Falling edge for start counter */ GPIO_LOW(CTN_GPIO_PORT,CTN_CNTEN_GPIO_PIN); /* Request Wait For Interrupt */ PWR_EnterSTOPMode(PWR_Regulator_LowPower,PWR_STOPEntry_WFI); break; /* Standby mode without RTC Measurement done to MSI 32 Khz without RTC */ case MCU_STBY: /* Disable PVD */ PWR_PVDCmd(DISABLE); /* Enable Ultra low power mode */ PWR_UltraLowPowerCmd(ENABLE); RTC_OutputTypeConfig(RTC_OutputType_PushPull); RTC_OutputConfig(RTC_Output_WakeUp,RTC_OutputPolarity_High); /* To configure PC13 WakeUP output */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 ; //GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 ; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_400KHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_Init( GPIOC, &GPIO_InitStructure); // GPIO_Init( GPIOA, &GPIO_InitStructure); GPIO_PinAFConfig(GPIOC, GPIO_PinSource13,GPIO_AF_RTC_AF1) ; //GPIO_PinAFConfig(GPIOA, GPIO_PinSource0,GPIO_AF_RTC_AF1) ; Config_RCC(&SavRCC); SysTick->CTRL = 0; /* Swith in MSI 32KHz */ SetHSICLKToMSI(RCC_MSIRange_0,DIV2,NoRTC) ; PWR_WakeUpPinCmd(PWR_WakeUpPin_1,ENABLE); PWR_UltraLowPowerCmd(ENABLE); PWR_EnterSTANDBYMode(); /* Stop here WakeUp EXIT on RESET */ break; } SetHSICLK(); Config_Systick(); RCC->AHBENR = SavRCC.AHBENR; PWR_VoltageScalingConfig(PWR_VoltageScaling_Range1); /* Wait Until the Voltage Regulator is ready */ while (PWR_GetFlagStatus(PWR_FLAG_VOS) != RESET) ; /* Read ADC for current measurmeent */ adc_measure = Current_Measurement(); /* ICC_CNT_EN Hi */ GPIO_HIGH(CTN_GPIO_PORT,CTN_CNTEN_GPIO_PIN); UserButton = TRUE; /* To restore RCC registers */ RCC->APB1ENR = SavRCC.APB1ENR; RCC->APB2ENR = SavRCC.APB2ENR; RCC->AHBLPENR = SavRCC.AHBLPENR; RCC->APB1LPENR = SavRCC.APB1LPENR; RCC->APB2LPENR = SavRCC.APB2LPENR; /* Need to reinit RCC for LCD*/ RCC_Configuration(); PWR_EnterLowPowerRunMode(DISABLE); /* Disable Ultra low power mode */ PWR_UltraLowPowerCmd(DISABLE); /* Disable FLASH during SLeep LP */ FLASH_SLEEPPowerDownCmd(DISABLE); Restore_GPIO_Config(); /* Clear Wake Up flag */ PWR_ClearFlag(PWR_FLAG_WU); /* Enable PVD */ PWR_PVDCmd(ENABLE); LCD_GLASS_Init(); return (adc_measure); }
/** * @brief Display the ADC converted Values on the LCD Glass. * @param data1: VDD voltage. * @param data2: RV3 voltage. * @retval None */ void DisplayOnLCD(uint16_t data1, uint16_t data2) { uint16_t rv3voltage=0; uint32_t iddrun = 0; #ifdef USE_STM32L152D_EVAL uint8_t LCDString[7]="00V 00"; #elif defined USE_STM32L152_EVAL uint8_t LCDString[9]="00V 00MA"; #endif /* Calculate the current consumption */ #ifdef USE_STM32L152D_EVAL iddrun = (uint32_t) ((uint32_t)(((data1 * 1000 * EVAL_RESISTOR_RATIO) / EVAL_MAX9938_GAIN))\ / (uint32_t)(EVAL_RESISTOR_R62)); #elif defined USE_STM32L152_EVAL iddrun = (uint32_t) ((uint32_t)(((data1 * 1000 * EVAL_RESISTOR_RATIO) / EVAL_MAX9938_GAIN))\ / (uint32_t)(EVAL_RESISTOR_R36)); #endif /* digit1 value*/ digit1 = (uint8_t)(iddrun / 1000); /* digit2 value */ digit2 = (uint8_t)((iddrun % 1000) / 100); /* Fill the LCDString fields with the current Voltage */ #ifdef USE_STM32L152D_EVAL LCDString[5] = (uint8_t)((uint8_t)(digit1) + ASCII_NUM_0); LCDString[6] = (uint8_t)((uint8_t)(digit2) + ASCII_NUM_0); #elif defined USE_STM32L152_EVAL LCDString[4] = (uint8_t)((uint8_t)(digit1) + ASCII_NUM_0); LCDString[5] = (uint8_t)((uint8_t)(digit2) + ASCII_NUM_0); #endif /* Calculate RV3 voltage value*/ rv3voltage = (uint16_t)((uint32_t)((uint32_t)data2 * (uint32_t)ADC_RATIO) / (uint32_t)1000); /* digit1 value*/ digit1 = (uint8_t)(rv3voltage / 1000); /* digit2 value */ digit2 = (uint8_t)((rv3voltage % 1000) / 100); /* Fill the LCDString fields with the current Voltage */ LCDString[0] = (uint8_t)((uint8_t)(digit1) + ASCII_NUM_0); LCDString[1] = (uint8_t)((uint8_t)(digit2) + ASCII_NUM_0); /*!< Wait Until the last LCD RAM update finish */ while(LCD_GetFlagStatus(LCD_FLAG_UDR) != RESET) { } #ifdef USE_STM32L152_EVAL /* Display one character on LCD */ LCD_GLASS_WriteChar(&LCDString[0], POINT_ON, APOSTROPHE_OFF, 0); /* Display one character on LCD */ LCD_GLASS_WriteChar(&LCDString[1], POINT_OFF, APOSTROPHE_OFF, 1); /* Display one character on LCD */ LCD_GLASS_WriteChar(&LCDString[2], POINT_OFF, APOSTROPHE_OFF, 2); /* Display one character on LCD */ LCD_GLASS_WriteChar(&LCDString[3], POINT_OFF, APOSTROPHE_OFF, 3); /* Display one character on LCD */ LCD_GLASS_WriteChar(&LCDString[4], POINT_ON, APOSTROPHE_OFF , 4); /* Display one character on LCD */ LCD_GLASS_WriteChar(&LCDString[5], POINT_OFF, APOSTROPHE_OFF, 5); /* Display one character on LCD */ LCD_GLASS_WriteChar(&LCDString[6], POINT_OFF, APOSTROPHE_OFF, 6); /* Display one character on LCD */ LCD_GLASS_WriteChar(&LCDString[7], POINT_OFF, APOSTROPHE_OFF, 7); #elif defined USE_STM32L152D_EVAL /* Display one character on LCD */ LCD_GLASS_WriteChar(&LCDString[0], POINT_OFF, DOUBLEPOINT_OFF, 0); /* Display one character on LCD */ LCD_GLASS_WriteChar(&LCDString[1], POINT_ON, DOUBLEPOINT_OFF, 1); /* Display one character on LCD */ LCD_GLASS_WriteChar(&LCDString[2], POINT_OFF, DOUBLEPOINT_OFF, 2); /* Display one character on LCD */ LCD_GLASS_WriteChar(&LCDString[3], POINT_OFF, DOUBLEPOINT_OFF, 3); /* Display one character on LCD */ LCD_GLASS_WriteChar(&LCDString[4], POINT_OFF, DOUBLEPOINT_OFF , 4); /* Display one character on LCD */ LCD_GLASS_WriteChar(&LCDString[5], POINT_OFF, DOUBLEPOINT_OFF, 5); /* Display one character on LCD */ LCD_GLASS_WriteChar(&LCDString[6], POINT_ON, DOUBLEPOINT_OFF, 6); #endif /* Request LCD RAM update */ LCD_UpdateDisplayRequest(); }