STM32F0xx Standard Peripherals Firmware Library
|
STM32F0xx_StdPeriph_Examples/ADC/ADC_BatteryChargeMonitoring/main.c
Go to the documentation of this file.
00001 /** 00002 ****************************************************************************** 00003 * @file ADC/ADC_BatteryChargeMonitoring/main.c 00004 * @author MCD Application Team 00005 * @version V1.4.0 00006 * @date 24-July-2014 00007 * @brief Main program body 00008 ****************************************************************************** 00009 * @attention 00010 * 00011 * <h2><center>© COPYRIGHT 2014 STMicroelectronics</center></h2> 00012 * 00013 * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License"); 00014 * You may not use this file except in compliance with the License. 00015 * You may obtain a copy of the License at: 00016 * 00017 * http://www.st.com/software_license_agreement_liberty_v2 00018 * 00019 * Unless required by applicable law or agreed to in writing, software 00020 * distributed under the License is distributed on an "AS IS" BASIS, 00021 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00022 * See the License for the specific language governing permissions and 00023 * limitations under the License. 00024 * 00025 ****************************************************************************** 00026 */ 00027 00028 /* Includes ------------------------------------------------------------------*/ 00029 #include "main.h" 00030 00031 /** @addtogroup STM32F0xx_StdPeriph_Examples 00032 * @{ 00033 */ 00034 00035 /** @addtogroup ADC_BatteryChargeMonitoring 00036 * @{ 00037 */ 00038 00039 /* Private typedef -----------------------------------------------------------*/ 00040 /* Private define ------------------------------------------------------------*/ 00041 #define MESSAGE1 "STM32F0xx CortexM0 " 00042 #ifdef USE_STM320518_EVAL 00043 #define MESSAGE2 " STM320518-EVAL " 00044 #else 00045 #define MESSAGE2 " STM32072B-EVAL " 00046 #endif /* USE_STM320518_EVAL */ 00047 00048 /* Private macro -------------------------------------------------------------*/ 00049 /* Private variables ---------------------------------------------------------*/ 00050 __IO uint16_t ADC1ConvertedValue = 0, ADC1ConvertedVoltage = 0; 00051 __IO uint32_t ADCmvoltp = 0 ; 00052 00053 /* Private function prototypes -----------------------------------------------*/ 00054 static void Display_Init(void); 00055 static void Display(void); 00056 static void ADC_Config(void); 00057 static void TIM_Config(void); 00058 /* Private functions ---------------------------------------------------------*/ 00059 00060 /** 00061 * @brief Main program. 00062 * @param None 00063 * @retval None 00064 */ 00065 int main(void) 00066 { 00067 /*!< At this stage the microcontroller clock setting is already configured, 00068 this is done through SystemInit() function which is called from startup 00069 file (startup_stm32f0xx.s) before to branch to application main. 00070 To reconfigure the default setting of SystemInit() function, refer to 00071 system_stm32f0xx.c file 00072 */ 00073 00074 /* LCD Display init */ 00075 Display_Init(); 00076 00077 /* TIM1 configuration */ 00078 TIM_Config(); 00079 00080 /* ADC1 configuration */ 00081 ADC_Config(); 00082 00083 /* Infinite loop */ 00084 while (1) 00085 { 00086 /* Test EOC flag */ 00087 while(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET); 00088 00089 /* Get ADC1 converted data */ 00090 ADC1ConvertedValue =ADC_GetConversionValue(ADC1); 00091 00092 /* Compute the voltage */ 00093 ADC1ConvertedVoltage = 2 * (ADC1ConvertedValue *3300)/0xFFF; 00094 00095 /* Display converted data on the LCD */ 00096 Display(); 00097 } 00098 } 00099 00100 /** 00101 * @brief ADC configuration 00102 * @param None 00103 * @retval None 00104 */ 00105 static void ADC_Config(void) 00106 { 00107 ADC_InitTypeDef ADC_InitStructure; 00108 00109 /* GPIOC Periph clock enable */ 00110 RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE); 00111 00112 /* ADC1 Peripheral clock enable */ 00113 RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 , ENABLE); 00114 00115 /* ADCs DeInit */ 00116 ADC_DeInit(ADC1); 00117 00118 /* Initialize ADC structure */ 00119 ADC_StructInit(&ADC_InitStructure); 00120 00121 /* Configure the ADC1 in continuous mode withe a resolution equal to 12 bits */ 00122 ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b; 00123 ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; 00124 ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_Rising; 00125 ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC4; 00126 ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; 00127 ADC_InitStructure.ADC_ScanDirection = ADC_ScanDirection_Upward; 00128 ADC_Init(ADC1, &ADC_InitStructure); 00129 00130 /* Convert the ADC1 Channel 11 with 239.5 Cycles as sampling time */ 00131 ADC_ChannelConfig(ADC1, ADC_Channel_Vbat , ADC_SampleTime_239_5Cycles); 00132 ADC_VbatCmd(ENABLE); 00133 00134 /* ADC Calibration */ 00135 ADC_GetCalibrationFactor(ADC1); 00136 00137 /* Enable the ADC peripheral */ 00138 ADC_Cmd(ADC1, ENABLE); 00139 00140 /* Wait the ADRDY flag */ 00141 while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_ADRDY)); 00142 00143 /* ADC1 regular Software Start Conv */ 00144 ADC_StartOfConversion(ADC1); 00145 } 00146 /** 00147 * @brief TIM configuration 00148 * @param None 00149 * @retval None 00150 */ 00151 static void TIM_Config(void) 00152 { 00153 TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; 00154 TIM_OCInitTypeDef TIM_OCInitStructure; 00155 00156 /* TIM1 Periph clock enable */ 00157 RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1 , ENABLE); 00158 00159 /* TIM1 Configuration */ 00160 TIM_DeInit(TIM1); 00161 TIM_TimeBaseStructInit(&TIM_TimeBaseStructure); 00162 TIM_OCStructInit(&TIM_OCInitStructure); 00163 /* Time base configuration */ 00164 TIM_TimeBaseStructure.TIM_Period = 0xFF; 00165 TIM_TimeBaseStructure.TIM_Prescaler = 0x0; 00166 TIM_TimeBaseStructure.TIM_ClockDivision = 0x0; 00167 TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; 00168 TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure); 00169 00170 /* Output Compare PWM Mode configuration */ 00171 TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; /* low edge by default */ 00172 TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; 00173 TIM_OCInitStructure.TIM_Pulse = 0x01; 00174 TIM_OC4Init(TIM1, &TIM_OCInitStructure); 00175 00176 /* TIM1 enable counter */ 00177 TIM_Cmd(TIM1, ENABLE); 00178 00179 /* Main Output Enable */ 00180 TIM_CtrlPWMOutputs(TIM1, ENABLE); 00181 00182 } 00183 00184 /** 00185 * @brief Display ADC converted value on LCD 00186 * @param None 00187 * @retval None 00188 */ 00189 static void Display(void) 00190 { 00191 uint32_t v=0,mv=0, mvolt = 0; 00192 uint8_t text[50], voltp = 0; 00193 00194 v=(ADC1ConvertedVoltage)/1000; 00195 mv = (ADC1ConvertedVoltage%1000)/100; 00196 mvolt = (v *100) + (mv*10); 00197 00198 sprintf((char*)text," Battery Volt = %d,%d V ",v,mv); 00199 00200 /* Set the LCD Back Color and Text Color*/ 00201 LCD_SetBackColor(White); 00202 LCD_SetTextColor(Blue); 00203 00204 LCD_DisplayStringLine(LINE(3),text); 00205 00206 if(ADCmvoltp != mvolt ) 00207 { 00208 voltp = (uint8_t)((mvolt * 100) / 330); 00209 sprintf((char*)text," %d % ",voltp); 00210 LCD_DisplayStringLine(LINE(6),text); 00211 /* Set the LCD Text Color */ 00212 LCD_SetTextColor(Black); 00213 /* Displays a rectangle on the LCD */ 00214 LCD_DrawRect(169, 243, 22, 167 ); 00215 00216 00217 /* Set the LCD White Color */ 00218 LCD_SetBackColor(White); 00219 LCD_DrawFullRect(170, 242, 165, 20); 00220 00221 /* Set the LCD Green Color */ 00222 LCD_SetBackColor(Green); 00223 LCD_DrawFullRect(170, 242, (uint16_t)(mvolt / 2) , 20); 00224 00225 /* Update the new value */ 00226 ADCmvoltp = mvolt; 00227 } 00228 } 00229 00230 /** 00231 * @brief Display Init (LCD) 00232 * @param None 00233 * @retval None� 00234 */ 00235 static void Display_Init(void) 00236 { 00237 /* Initialize the LCD */ 00238 #ifdef USE_STM320518_EVAL 00239 STM320518_LCD_Init(); 00240 #else 00241 STM32072B_LCD_Init(); 00242 #endif /* USE_STM320518_EVAL */ 00243 00244 /* Clear the LCD */ 00245 LCD_Clear(White); 00246 00247 /* Set the LCD Text size */ 00248 LCD_SetFont(&Font8x12); 00249 00250 /* Set the LCD Back Color and Text Color*/ 00251 LCD_SetBackColor(Blue); 00252 LCD_SetTextColor(White); 00253 00254 /* Display */ 00255 LCD_DisplayStringLine(LINE(0x13), " ADC : Battery charge monitoring "); 00256 00257 /* Set the LCD Text size */ 00258 LCD_SetFont(&Font16x24); 00259 00260 LCD_DisplayStringLine(LINE(0), MESSAGE1 ); 00261 LCD_DisplayStringLine(LINE(1), MESSAGE2 ); 00262 00263 /* Set the LCD Back Color and Text Color*/ 00264 LCD_SetBackColor(White); 00265 LCD_SetTextColor(Blue); 00266 } 00267 00268 #ifdef USE_FULL_ASSERT 00269 00270 /** 00271 * @brief Reports the name of the source file and the source line number 00272 * where the assert_param error has occurred. 00273 * @param file: pointer to the source file name 00274 * @param line: assert_param error line source number 00275 * @retval None 00276 */ 00277 void assert_failed(uint8_t* file, uint32_t line) 00278 { 00279 /* User can add his own implementation to report the file name and line number, 00280 ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ 00281 00282 /* Infinite loop */ 00283 while (1) 00284 { 00285 } 00286 } 00287 #endif 00288 00289 /** 00290 * @} 00291 */ 00292 00293 /** 00294 * @} 00295 */ 00296 00297 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/