STM32F0xx Standard Peripherals Firmware Library: main.c Source File

STM32F0xx Standard Peripherals Library

STM32F0xx_StdPeriph_Examples/COMP/COMP_LDR/main.c
Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    COMP/COMP_LDR/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 COMP_LDR
00036   * @{
00037   */
00038 
00039 /* Private typedef -----------------------------------------------------------*/
00040 /* Private define ------------------------------------------------------------*/
00041 #define MESSAGE1   "        LDR         "
00042 
00043 /* Private macro -------------------------------------------------------------*/
00044 /* Private variables ---------------------------------------------------------*/
00045 uint8_t ldrlevel = 11, ldrlevelp = 0, daclevel = 0;
00046 uint16_t tmp = 0;
00047   
00048 /* Private function prototypes -----------------------------------------------*/
00049 /* Private functions ---------------------------------------------------------*/
00050 static void COMP_Config(void);
00051 static void DAC_Config(void);
00052 void Delay(__IO uint32_t nTime);
00053 
00054 /**
00055   * @brief  Main program.
00056   * @param  None
00057   * @retval None
00058   */
00059 int main(void)
00060 {
00061   /*!< At this stage the microcontroller clock setting is already configured, 
00062        this is done through SystemInit() function which is called from startup
00063        file (startup_stm32f0xx.s) before to branch to application main.
00064        To reconfigure the default setting of SystemInit() function, refer to
00065        system_stm32f0xx.c file
00066      */ 
00067 
00068 
00069   /* Initialize the TFT-LCD */
00070 #ifdef USE_STM320518_EVAL
00071   STM320518_LCD_Init();
00072 #else 
00073   STM32072B_LCD_Init();
00074 #endif /* USE_STM320518_EVAL */
00075   
00076   /* Clear the LCD */ 
00077   LCD_Clear(LCD_COLOR_BLACK);
00078   
00079   /* Set the LCD Back Color */
00080   LCD_SetBackColor(LCD_COLOR_BLACK);
00081   
00082   /* Set the LCD Text Color */
00083   LCD_SetTextColor(LCD_COLOR_WHITE);
00084   
00085   /* Displays Light dependent resistor (LDR) message on line 1 */
00086   LCD_DisplayStringLine(LINE(0), (uint8_t *)MESSAGE1);
00087   
00088   /* COMP Configuration */
00089   COMP_Config();
00090  
00091   /* AC channel 2 Configuration */
00092   DAC_Config();
00093   
00094   
00095   /* Infinite loop */
00096   while (1)
00097   {
00098     for(daclevel = 0; daclevel < 11; daclevel++)
00099     {
00100       /* Set DAC Channel2 DHR register: DAC_OUT2 = (3.3 * 868) / 4095 ~ 0.7 V */
00101       DAC_SetChannel1Data(DAC_Align_12b_R, (uint16_t)(daclevel * 150));
00102 
00103       Delay((10000));
00104       /* Check on the Comp output level*/
00105       if (COMP_GetOutputLevel(COMP_Selection_COMP1) == COMP_OutputLevel_High)
00106       {
00107         ldrlevel--;
00108       }
00109     }
00110 
00111     switch(ldrlevel)
00112     {
00113     case 1:
00114       /* Displays MESSAGE on line 7 */
00115       LCD_DisplayStringLine(LINE(7), "       Level 0      ");
00116       break;
00117       
00118     case 2:
00119       /* Displays MESSAGE on line 7 */
00120       LCD_DisplayStringLine(LINE(7), "       Level 1      ");
00121       break;
00122       
00123     case 3:
00124       /* Displays MESSAGE on line 7  */
00125       LCD_DisplayStringLine(LINE(7), "       Level 2      ");
00126       break;
00127       
00128     case 4:
00129       /* Displays MESSAGE on line 7  */
00130       LCD_DisplayStringLine(LINE(7), "       Level 3      ");
00131       break;
00132       
00133     case 5:
00134       /* Displays MESSAGE on line 7  */
00135       LCD_DisplayStringLine(LINE(7), "       Level 4      ");
00136       break;
00137       
00138     case 6:
00139       /* Displays MESSAGE on line 7  */
00140       LCD_DisplayStringLine(LINE(7), "       Level 5      ");
00141       break;
00142       
00143     case 7:
00144       /* Displays MESSAGE on line 7  */
00145       LCD_DisplayStringLine(LINE(7), "       Level 6      ");
00146       break;
00147       
00148     case 8:
00149       /* Displays MESSAGE on line 7  */
00150       LCD_DisplayStringLine(LINE(7), "       Level 7      ");
00151       break;
00152       
00153     case 9:
00154       /* Displays MESSAGE on line 7 */
00155       LCD_DisplayStringLine(LINE(7), "       Level 8      ");
00156       break;
00157       
00158     case 10:
00159       /* Displays MESSAGE on line 7  */
00160       LCD_DisplayStringLine(LINE(7), "       Level 9      ");
00161       break;
00162       
00163     case 11:
00164       /* Displays MESSAGE on line 7  */
00165       LCD_DisplayStringLine(LINE(7), "       Level 10     ");
00166       break;
00167       
00168     default :
00169       /* Displays MESSAGE on line 7  */
00170       LCD_DisplayStringLine(LINE(7), "       Level 0      ");
00171       ldrlevel = 1;
00172       break;
00173     }
00174     
00175     if(ldrlevelp != ldrlevel)
00176     {
00177       /* Set the LCD Text Color */
00178       LCD_SetTextColor(LCD_COLOR_WHITE);
00179       
00180       /* Displays a rectangle on the LCD */
00181       LCD_DrawRect(199, 311, 22, 302 );
00182       
00183       /* Set the LCD Back Color */
00184       LCD_SetBackColor(LCD_COLOR_BLACK);
00185       LCD_SetTextColor(LCD_COLOR_BLACK);
00186       LCD_DrawFullRect(200, 310,  300, 20);
00187       
00188       /* Set the LCD Text Color */
00189       LCD_SetTextColor(LCD_COLOR_YELLOW);
00190       
00191       /* Set the LCD Back Color */
00192       LCD_SetBackColor(LCD_COLOR_YELLOW);
00193       
00194       /* Displays a full rectangle */
00195       tmp = 30 * (ldrlevel-1);
00196       if (tmp ==0) tmp = 5;
00197       LCD_DrawFullRect(200, 310, tmp , 20);
00198     }
00199     
00200     ldrlevelp = ldrlevel;
00201     ldrlevel = 11;
00202     
00203     /* Set the LCD Back Color */
00204     LCD_SetBackColor(LCD_COLOR_BLACK);
00205   }
00206 
00207 }
00208 
00209 /**
00210   * @brief COMP Configuration.
00211   * @param None
00212   * @retval None
00213   */
00214 static void COMP_Config(void)
00215 {
00216   COMP_InitTypeDef COMP_InitStructure;
00217   GPIO_InitTypeDef GPIO_InitStructure;
00218 
00219   /* GPIOB Peripheral clock enable */
00220   RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
00221  
00222   /* Configure PA1 in analog mode: PA1 is connected to COMP1 non inverting input */
00223   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
00224   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
00225   GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
00226   GPIO_Init(GPIOA, &GPIO_InitStructure);
00227   
00228   /* Enable SYSCFG clock */
00229   RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
00230   
00231   /* COMP1 Init: COMP1 is enabled as soon as inverting input is selected */
00232   /* use DAC1 output as a reference voltage: DAC1 output is connected to COMP1
00233   inverting input */
00234   COMP_InitStructure.COMP_InvertingInput = COMP_InvertingInput_DAC1;
00235   COMP_InitStructure.COMP_Output = COMP_Output_None;
00236   COMP_InitStructure.COMP_OutputPol = COMP_OutputPol_NonInverted;
00237   COMP_InitStructure.COMP_Hysteresis = COMP_Hysteresis_High;
00238   COMP_InitStructure.COMP_Mode = COMP_Mode_UltraLowPower;
00239   COMP_Init(COMP_Selection_COMP1, &COMP_InitStructure);
00240   
00241   /* Enable the COMP peripheral */
00242   COMP_Cmd(COMP_Selection_COMP1, ENABLE);
00243 }
00244 
00245 /**
00246   * @brief  Configures the DAC channel 2 with output buffer enabled.
00247   * @param  None
00248   * @retval None
00249   */
00250 static void DAC_Config(void)
00251 {
00252   DAC_InitTypeDef  DAC_InitStructure;
00253   
00254   /* DAC clock enable */
00255   RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE);
00256   
00257   /* Deinitialize DAC */
00258   DAC_DeInit();
00259 
00260   DAC_StructInit(&DAC_InitStructure);
00261    
00262   /* Fill DAC InitStructure */ 
00263   /* DAC Channel1: 12bit right */
00264   /* DAC Channel1 Init */
00265   DAC_InitStructure.DAC_Trigger = DAC_Trigger_None;
00266   DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Disable;
00267   DAC_Init(DAC_Channel_1, &DAC_InitStructure);
00268 
00269   /* Enable DAC Channel1 */
00270   DAC_Cmd(DAC_Channel_1, ENABLE);
00271 }
00272 
00273 /**
00274   * @brief  Inserts a delay time.
00275   * @param  nTime: specifies the delay time length.
00276   * @retval None
00277   */
00278 void Delay(__IO uint32_t nTime)
00279 { 
00280   while(nTime != 0)
00281   {
00282   nTime--;
00283   }
00284 }
00285 
00286 #ifdef  USE_FULL_ASSERT
00287 
00288 /**
00289   * @brief  Reports the name of the source file and the source line number
00290   *         where the assert_param error has occurred.
00291   * @param  file: pointer to the source file name
00292   * @param  line: assert_param error line source number
00293   * @retval None
00294   */
00295 void assert_failed(uint8_t* file, uint32_t line)
00296 { 
00297   /* User can add his own implementation to report the file name and line number,
00298      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
00299 
00300   /* Infinite loop */
00301   while (1)
00302   {
00303   }
00304 }
00305 #endif
00306 
00307 /**
00308   * @}
00309   */
00310 
00311 /**
00312   * @}
00313   */
00314 
00315 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
STM32L1xx Standard Peripherals Library: Footer

 

 

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