STM8S/A Standard Peripherals Firmware Library: main.c Source File

STM8S/A

STM8S_StdPeriph_Examples/BEEP/BEEP_3SignalsGeneration/main.c
Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    BEEP_3SignalsGeneration\main.c
00004   * @author  MCD Application Team
00005   * @version  V2.2.0
00006   * @date     30-September-2014
00007   * @brief   This file contains the main function for BEEP 3 Signals Generation example
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 "stm8s.h"
00030 #include "stm8s_eval.h"
00031 #include "stm8s_eval_lcd.h"
00032 
00033 /**
00034   * @addtogroup BEEP_3SignalsGeneration
00035   * @{
00036   */
00037 
00038 /* Private typedef -----------------------------------------------------------*/
00039 
00040 /* Private define ------------------------------------------------------------*/
00041 /* Private macro -------------------------------------------------------------*/
00042 /* Private variables ---------------------------------------------------------*/
00043 /* Global variables ----------------------------------------------------------*/
00044 bool ButtonPressed = FALSE;
00045 /* Private function prototypes -----------------------------------------------*/
00046 static void CLK_Config(void);
00047 static void GPIO_Config(void);
00048 static void LCD_Config(void);
00049 void Delay (uint16_t nCount);
00050 uint32_t LSIMeasurment(void);
00051 /* Private functions ---------------------------------------------------------*/
00052 
00053 /**
00054   * @brief  Main program.
00055   * @param  None
00056   * @retval None
00057   */
00058 void main(void)
00059 {
00060     uint8_t i = 0;
00061 
00062     /* Clock configuration -----------------------------------------*/
00063     CLK_Config();
00064 
00065     /* GPIO Configuration ------------------------------------------*/
00066     GPIO_Config();
00067 
00068     /* Configure LCD mounted on STM8-128 EVAL board ----------------*/
00069     LCD_Config();
00070 
00071     /* BEEP calibration */
00072     BEEP_LSICalibrationConfig(LSIMeasurment());
00073     
00074     /* Enable general interrupts for Key button reading */
00075     enableInterrupts();
00076 
00077     while (1)
00078     {
00079         /* Check button status */
00080         if (ButtonPressed == TRUE) /* Button is pressed */
00081         {
00082             ButtonPressed = FALSE;
00083             /* Change BEEP frequency */
00084             switch (i)
00085             {
00086             case 0:
00087                 BEEP_Cmd(DISABLE);
00088                 Delay(100);
00089                 BEEP_Init(BEEP_FREQUENCY_1KHZ);
00090                 BEEP_Cmd(ENABLE);
00091                 /* LED1 ON */
00092                 STM_EVAL_LEDOn(LED1);
00093                 STM_EVAL_LEDOff(LED2);
00094                 STM_EVAL_LEDOff(LED3);
00095                 STM_EVAL_LEDOff(LED4); 
00096                 LCD_SetCursorPos(LCD_LINE1, 0);
00097                 LCD_Print("  BEEPER 1kHz ");
00098                 i = 1;
00099                 break;
00100             case 1:
00101                 BEEP_Cmd(DISABLE);
00102                 Delay(100);
00103                 BEEP_Init(BEEP_FREQUENCY_2KHZ);
00104                 BEEP_Cmd(ENABLE);
00105                 /* LED2 ON */
00106                 STM_EVAL_LEDOn(LED2);
00107                 STM_EVAL_LEDOff(LED1);
00108                 STM_EVAL_LEDOff(LED3);
00109                 STM_EVAL_LEDOff(LED4);
00110                 LCD_SetCursorPos(LCD_LINE1, 0);
00111                 LCD_Print("  BEEPER 2kHz ");
00112                 i = 2;
00113                 break;
00114             case 2:
00115                 BEEP_Cmd(DISABLE);
00116                 Delay(100);
00117                 BEEP_Init(BEEP_FREQUENCY_4KHZ);
00118                 BEEP_Cmd(ENABLE);
00119                 /* LED3 ON */
00120                 STM_EVAL_LEDOn(LED3);
00121                 STM_EVAL_LEDOff(LED2);
00122                 STM_EVAL_LEDOff(LED1);
00123                 STM_EVAL_LEDOff(LED4);
00124                 LCD_SetCursorPos(LCD_LINE1, 0);
00125                 LCD_Print("  BEEPER 4kHz ");
00126                 i = 3;
00127                 break;
00128             case 3:
00129                 BEEP_Cmd(DISABLE);
00130                 /* LED4 ON */
00131                 STM_EVAL_LEDOn(LED4);
00132                 STM_EVAL_LEDOff(LED2);
00133                 STM_EVAL_LEDOff(LED3);
00134                 STM_EVAL_LEDOff(LED1);
00135                 LCD_SetCursorPos(LCD_LINE1, 0);
00136                 LCD_Print("  BEEPER OFF  ");
00137                 i = 0;
00138                 break;
00139                                                 default:
00140                                                     break;
00141             }
00142         }
00143 
00144     }
00145 
00146 }
00147 
00148 /**
00149   * @brief  Configure system clock to run at 16Mhz
00150   * @param  None
00151   * @retval None
00152   */
00153 static void CLK_Config(void)
00154 {
00155     /* Initialization of the clock */
00156     /* Clock divider to HSI/1 */
00157     CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1);
00158 }
00159 
00160 /**
00161   * @brief  Configure GPIO for LEDs and buttons available on the evaluation board
00162   * @param  None
00163   * @retval None
00164   */
00165 static void GPIO_Config(void)
00166 {
00167     /* Initialize LEDs mounted on STM8-128 EVAL board */
00168     STM_EVAL_LEDInit(LED1);
00169     STM_EVAL_LEDInit(LED2);
00170     STM_EVAL_LEDInit(LED3);
00171     STM_EVAL_LEDInit(LED4);
00172 
00173     /* Initialize KEY pushbutton mounted on STM8-128 EVAL board */
00174     STM_EVAL_PBInit(BUTTON_KEY, BUTTON_MODE_EXTI);
00175     
00176     /* Switch LEDs On */
00177     STM_EVAL_LEDOn(LED1);
00178     STM_EVAL_LEDOn(LED2);
00179     STM_EVAL_LEDOn(LED3);
00180     STM_EVAL_LEDOn(LED4);
00181 }
00182 
00183 /**
00184   * @brief  Configure the LCD available on the evaluation board
00185   * @param  None
00186   * @retval None
00187   */
00188 static void LCD_Config(void)
00189 {
00190     /* Init the Eval board LCD */
00191     STM8S_EVAL_LCD_Init();
00192 
00193     /* Clear LCD */
00194     LCD_Clear();
00195  
00196     /* Print "  BEEPER OFF  " on LCD line1*/
00197     LCD_SetCursorPos(LCD_LINE1, 0);
00198     LCD_Print("  BEEPER OFF  ");
00199     /* Print "Press Key" on LCD line2*/
00200     LCD_SetCursorPos(LCD_LINE2, 0);
00201     LCD_Print("  Press Key   ");
00202 }
00203 
00204 /**
00205   * @brief  Measure the LSI frequency using timer IC1 and update the calibration registers.
00206   * @note   It is recommended to use a timer clock frequency of at least 10MHz in order 
00207         *         to obtain a better in the LSI frequency measurement.
00208   * @param  None
00209   * @retval None
00210   */
00211 uint32_t LSIMeasurment(void)
00212 {
00213 
00214   uint32_t lsi_freq_hz = 0x0;
00215   uint32_t fmaster = 0x0;
00216   uint16_t ICValue1 = 0x0;
00217   uint16_t ICValue2 = 0x0;
00218 
00219   /* Get master frequency */
00220   fmaster = CLK_GetClockFreq();
00221 
00222   /* Enable the LSI measurement: LSI clock connected to timer Input Capture 1 */
00223   AWU->CSR |= AWU_CSR_MSR;
00224 
00225 #if defined (STM8S903) || defined (STM8S103) || defined (STM8S003)
00226   /* Measure the LSI frequency with TIMER Input Capture 1 */
00227   
00228   /* Capture only every 8 events!!! */
00229   /* Enable capture of TI1 */
00230         TIM1_ICInit(TIM1_CHANNEL_1, TIM1_ICPOLARITY_RISING, TIM1_ICSELECTION_DIRECTTI, TIM1_ICPSC_DIV8, 0);
00231         
00232   /* Enable TIM1 */
00233   TIM1_Cmd(ENABLE);
00234   
00235   /* wait a capture on cc1 */
00236   while((TIM1->SR1 & TIM1_FLAG_CC1) != TIM1_FLAG_CC1);
00237   /* Get CCR1 value*/
00238   ICValue1 = TIM1_GetCapture1();
00239   TIM1_ClearFlag(TIM1_FLAG_CC1);
00240   
00241   /* wait a capture on cc1 */
00242   while((TIM1->SR1 & TIM1_FLAG_CC1) != TIM1_FLAG_CC1);
00243   /* Get CCR1 value*/
00244   ICValue2 = TIM1_GetCapture1();
00245   TIM1_ClearFlag(TIM1_FLAG_CC1);
00246   
00247   /* Disable IC1 input capture */
00248   TIM1->CCER1 &= (uint8_t)(~TIM1_CCER1_CC1E);
00249   /* Disable timer2 */
00250   TIM1_Cmd(DISABLE);
00251   
00252 #else  
00253   /* Measure the LSI frequency with TIMER Input Capture 1 */
00254   
00255   /* Capture only every 8 events!!! */
00256   /* Enable capture of TI1 */
00257   TIM3_ICInit(TIM3_CHANNEL_1, TIM3_ICPOLARITY_RISING, TIM3_ICSELECTION_DIRECTTI, TIM3_ICPSC_DIV8, 0);
00258 
00259   /* Enable TIM3 */
00260   TIM3_Cmd(ENABLE);
00261 
00262         /* wait a capture on cc1 */
00263   while ((TIM3->SR1 & TIM3_FLAG_CC1) != TIM3_FLAG_CC1);
00264         /* Get CCR1 value*/
00265   ICValue1 = TIM3_GetCapture1();
00266   TIM3_ClearFlag(TIM3_FLAG_CC1);
00267 
00268   /* wait a capture on cc1 */
00269   while ((TIM3->SR1 & TIM3_FLAG_CC1) != TIM3_FLAG_CC1);
00270     /* Get CCR1 value*/
00271   ICValue2 = TIM3_GetCapture1();
00272         TIM3_ClearFlag(TIM3_FLAG_CC1);
00273 
00274   /* Disable IC1 input capture */
00275   TIM3->CCER1 &= (uint8_t)(~TIM3_CCER1_CC1E);
00276   /* Disable timer3 */
00277   TIM3_Cmd(DISABLE);
00278 #endif
00279 
00280   /* Compute LSI clock frequency */
00281   lsi_freq_hz = (8 * fmaster) / (ICValue2 - ICValue1);
00282   
00283   /* Disable the LSI measurement: LSI clock disconnected from timer Input Capture 1 */
00284   AWU->CSR &= (uint8_t)(~AWU_CSR_MSR);
00285 
00286  return (lsi_freq_hz);
00287 }
00288 
00289 /**
00290   * @brief  Delay.
00291   * @param  nCount
00292   * @retval None
00293   */
00294 void Delay(uint16_t nCount)
00295 {
00296     /* Decrement nCount value */
00297     while (nCount != 0)
00298     {
00299         nCount--;
00300     }
00301 }
00302 
00303 #ifdef USE_FULL_ASSERT
00304 
00305 /**
00306   * @brief  Reports the name of the source file and the source line number
00307   *         where the assert_param error has occurred.
00308   * @param  file: pointer to the source file name
00309   * @param  line: assert_param error line source number
00310   * @retval None
00311   */
00312 void assert_failed(uint8_t* file, uint32_t line)
00313 { 
00314   /* User can add his own implementation to report the file name and line number,
00315      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
00316 
00317   /* Infinite loop */
00318   while (1)
00319   {
00320   }
00321 }
00322 #endif
00323 
00324 /**
00325   * @}
00326   */
00327 
00328 
00329 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
STM8 Standard Peripherals Library: Footer

 

 

 

      For complete documentation on STM8 8-bit Microcontrollers platform visit www.st.com