STM32F0xx Standard Peripherals Firmware Library: main.c Source File

STM32F0xx Standard Peripherals Library

STM32F0xx_StdPeriph_Examples/ADC/ADC_DMA/main.c
Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    ADC/ADC_DMA/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>&copy; 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_DMA
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   #define MESSAGE3   "  Turn RV3(PC.01)    "
00045 #else 
00046   #define MESSAGE2   "   STM32072B-EVAL   " 
00047   #define MESSAGE3   "  Turn RV3(PC.00)    "
00048 #endif /* USE_STM320518_EVAL */
00049 #define ADC1_DR_Address    0x40012440
00050 
00051 /* Private macro -------------------------------------------------------------*/
00052 /* Private variables ---------------------------------------------------------*/
00053 __IO uint16_t  ADC1ConvertedValue = 0, ADC1ConvertedVoltage = 0;
00054 __IO uint16_t RegularConvData_Tab[4];
00055 
00056 /* Private function prototypes -----------------------------------------------*/
00057 /* Private functions ---------------------------------------------------------*/
00058 static void Display_Init(void);
00059 static void Display(void);
00060 static void ADC_Config(void);
00061 static void DMA_Config(void);
00062 
00063 /**
00064   * @brief  Main program.
00065   * @param  None
00066   * @retval None
00067   */
00068 int main(void)
00069 {
00070   /*!< At this stage the microcontroller clock setting is already configured, 
00071        this is done through SystemInit() function which is called from startup
00072        file (startup_stm32f0xx.s) before to branch to application main.
00073        To reconfigure the default setting of SystemInit() function, refer to
00074        system_stm32f0xx.c file
00075      */ 
00076 
00077   /* LCD Display init  */
00078   Display_Init();
00079    
00080   /* ADC1 configuration */
00081   ADC_Config();
00082   
00083   /* DMA configuration */
00084   DMA_Config();
00085 
00086   /* Infinite loop */
00087   while (1)
00088   {
00089     /* Test DMA1 TC flag */
00090     while((DMA_GetFlagStatus(DMA1_FLAG_TC1)) == RESET ); 
00091     
00092     /* Clear DMA TC flag */
00093     DMA_ClearFlag(DMA1_FLAG_TC1);
00094     
00095     /* Display converted data on the LCD */
00096     Display();
00097   }
00098 }
00099 
00100 /**
00101   * @brief  ADC1 channel configuration
00102   * @param  None
00103   * @retval None
00104   */
00105 static void ADC_Config(void)
00106 {
00107   ADC_InitTypeDef     ADC_InitStructure;
00108   GPIO_InitTypeDef    GPIO_InitStructure;
00109   /* ADC1 DeInit */  
00110   ADC_DeInit(ADC1);
00111   
00112   /* GPIOC Periph clock enable */
00113   RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE);
00114   
00115    /* ADC1 Periph clock enable */
00116   RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
00117   
00118   /* Configure ADC Channel11 and channel10 as analog input */
00119 #ifdef USE_STM320518_EVAL
00120   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 ;
00121 #else
00122   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 ;
00123 #endif /* USE_STM320518_EVAL */
00124   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
00125   GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
00126   GPIO_Init(GPIOC, &GPIO_InitStructure);
00127   
00128   /* Initialize ADC structure */
00129   ADC_StructInit(&ADC_InitStructure);
00130   
00131   /* Configure the ADC1 in continuous mode withe a resolution equal to 12 bits  */
00132   ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
00133   ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; 
00134   ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
00135   ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
00136   ADC_InitStructure.ADC_ScanDirection = ADC_ScanDirection_Backward;
00137   ADC_Init(ADC1, &ADC_InitStructure); 
00138 
00139   /* Convert the ADC1 Channel11 and channel10 with 55.5 Cycles as sampling time */ 
00140 #ifdef USE_STM320518_EVAL
00141   ADC_ChannelConfig(ADC1, ADC_Channel_11 , ADC_SampleTime_55_5Cycles); 
00142 #else
00143   ADC_ChannelConfig(ADC1, ADC_Channel_10 , ADC_SampleTime_55_5Cycles); 
00144 #endif /* USE_STM320518_EVAL */  
00145   
00146   
00147   /* Convert the ADC1 temperature sensor  with 55.5 Cycles as sampling time */ 
00148   ADC_ChannelConfig(ADC1, ADC_Channel_TempSensor , ADC_SampleTime_55_5Cycles);  
00149   ADC_TempSensorCmd(ENABLE);
00150   
00151   /* Convert the ADC1 Vref  with 55.5 Cycles as sampling time */ 
00152   ADC_ChannelConfig(ADC1, ADC_Channel_Vrefint , ADC_SampleTime_55_5Cycles); 
00153   ADC_VrefintCmd(ENABLE);
00154   
00155   /* Convert the ADC1 Vbat with 55.5 Cycles as sampling time */ 
00156   ADC_ChannelConfig(ADC1, ADC_Channel_Vbat , ADC_SampleTime_55_5Cycles);  
00157   ADC_VbatCmd(ENABLE);
00158   
00159   /* ADC Calibration */
00160   ADC_GetCalibrationFactor(ADC1);
00161   
00162   /* ADC DMA request in circular mode */
00163   ADC_DMARequestModeConfig(ADC1, ADC_DMAMode_Circular);
00164   
00165   /* Enable ADC_DMA */
00166   ADC_DMACmd(ADC1, ENABLE);  
00167   
00168   /* Enable the ADC peripheral */
00169   ADC_Cmd(ADC1, ENABLE);     
00170   
00171   /* Wait the ADRDY flag */
00172   while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_ADRDY)); 
00173   
00174   /* ADC1 regular Software Start Conv */ 
00175   ADC_StartOfConversion(ADC1);
00176 }
00177 
00178 /**
00179   * @brief  DMA channel1 configuration
00180   * @param  None
00181   * @retval None
00182   */
00183 static void DMA_Config(void)
00184 {
00185   DMA_InitTypeDef   DMA_InitStructure;
00186   /* DMA1 clock enable */
00187   RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1 , ENABLE);
00188   
00189   /* DMA1 Channel1 Config */
00190   DMA_DeInit(DMA1_Channel1);
00191   DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)ADC1_DR_Address;
00192   DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)RegularConvData_Tab;
00193   DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
00194   DMA_InitStructure.DMA_BufferSize = 4;
00195   DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
00196   DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
00197   DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
00198   DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
00199   DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
00200   DMA_InitStructure.DMA_Priority = DMA_Priority_High;
00201   DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
00202   DMA_Init(DMA1_Channel1, &DMA_InitStructure);
00203   /* DMA1 Channel1 enable */
00204   DMA_Cmd(DMA1_Channel1, ENABLE);
00205   
00206 }
00207 
00208 /**
00209   * @brief  Display ADC converted value on LCD
00210   * @param  None
00211   * @retval None
00212   */
00213 static void Display(void)
00214 {
00215   uint32_t v=0,mv=0;
00216   uint8_t text[50],index;
00217   
00218   /* Set the LCD Back Color and Text Color*/
00219   LCD_SetBackColor(White);
00220   LCD_SetTextColor(Green);
00221   for(index=0;index<4;index++)
00222   {
00223     if (index == 0)
00224     {
00225       v=((2* RegularConvData_Tab[index]* 3300) / 0xFFF) / 1000;
00226       mv = (((2* RegularConvData_Tab[index]* 3300) / 0xFFF)%1000)/100;
00227     }
00228     else
00229     {
00230       v=((RegularConvData_Tab[index]* 3300) / 0xFFF) / 1000;
00231       mv = (((RegularConvData_Tab[index]* 3300) / 0xFFF)%1000)/100;
00232     } 
00233     
00234     if (index == 3)
00235     {
00236       sprintf((char*)text," Pot (RV3)   = %d,%d V   ",v,mv);
00237     }
00238     else if (index == 2)
00239     {
00240       sprintf((char*)text," V(sense)    = %d,%d V   ",v,mv);
00241     }
00242      else if (index == 1)
00243     {
00244       sprintf((char*)text," V(ref int)  = %d,%d V   ",v,mv);
00245     }
00246      else
00247     {
00248       sprintf((char*)text," V(Battery)  = %d,%d V",v,mv);
00249     }
00250     
00251     LCD_DisplayStringLine(LINE(5+index),text);
00252   } 
00253 }
00254 
00255 /**
00256   * @brief  Display Init (LCD)
00257   * @param  None
00258   * @retval None
00259   */
00260 static void Display_Init(void)
00261 {
00262   /* Initialize the LCD */
00263 #ifdef USE_STM320518_EVAL
00264   STM320518_LCD_Init();
00265 #else
00266   STM32072B_LCD_Init();
00267 #endif /* USE_STM320518_EVAL */
00268 
00269   /* Clear the LCD */ 
00270   LCD_Clear(White);
00271 
00272   /* Set the LCD Text size */
00273   LCD_SetFont(&Font8x12);
00274 
00275   /* Set the LCD Back Color and Text Color*/
00276   LCD_SetBackColor(Blue);
00277   LCD_SetTextColor(White);
00278 
00279   /* Display */
00280   LCD_DisplayStringLine(LINE(0x13), "         ADC DMA conversion example         ");
00281 
00282   /* Set the LCD Text size */
00283   LCD_SetFont(&Font16x24);
00284 
00285   LCD_DisplayStringLine(LINE(0), MESSAGE1);
00286   LCD_DisplayStringLine(LINE(1), MESSAGE2);
00287   
00288   /* Set the LCD Back Color and Text Color*/
00289   LCD_SetBackColor(White);
00290   LCD_SetTextColor(Blue);
00291 
00292   /* Display */
00293   LCD_DisplayStringLine(LINE(3), MESSAGE3);
00294 }
00295 
00296 #ifdef  USE_FULL_ASSERT
00297 
00298 /**
00299   * @brief  Reports the name of the source file and the source line number
00300   *         where the assert_param error has occurred.
00301   * @param  file: pointer to the source file name
00302   * @param  line: assert_param error line source number
00303   * @retval None
00304   */
00305 void assert_failed(uint8_t* file, uint32_t line)
00306 { 
00307   /* User can add his own implementation to report the file name and line number,
00308      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
00309 
00310   /* Infinite loop */
00311   while (1)
00312   {
00313   }
00314 }
00315 #endif
00316 
00317 /**
00318   * @}
00319   */
00320 
00321 /**
00322   * @}
00323   */
00324 
00325 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
STM32L1xx Standard Peripherals Library: Footer

 

 

 For complete documentation on STM32 Microcontrollers visit www.st.com/STM32