STM8S/A Standard Peripherals Firmware Library
|
STM8S_StdPeriph_Examples/TIM1/TIM1_Sinewave_Generation/main.c
Go to the documentation of this file.
00001 /** 00002 ****************************************************************************** 00003 * @file TIM1_Sinewave_Generation\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 TIM1 Sinewave_Generation example. 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 "stm8s.h" 00030 #include "main.h" 00031 00032 /** 00033 * @addtogroup TIM1_Sinewave_Generation 00034 * @{ 00035 */ 00036 00037 /* Private typedef -----------------------------------------------------------*/ 00038 /* Private define ------------------------------------------------------------*/ 00039 /* Private macro -------------------------------------------------------------*/ 00040 /* Private variables ---------------------------------------------------------*/ 00041 /* Private variables ---------------------------------------------------------*/ 00042 __IO MySin_Typedef sin;/* Stores the sin structure in ram */ 00043 __IO uint8_t counter = 0; /* The counter variable for counting the Update cycles in TIM1 */ 00044 /* This shows the value of the samples for the different no of samples in half 00045 sine wave. The last value corresponds to the amplitude of the sine wave.*/ 00046 00047 #if nbr_of_samples_half == 16 00048 CONST uint8_t sample[9]={0,20,38,56,71,83,92,98,100}; 00049 00050 #elif nbr_of_samples_half == 18 00051 CONST uint8_t sample[10]={0,17,34,50,64,76,86,93,97,99}; 00052 00053 #elif nbr_of_samples_half == 22 00054 CONST uint8_t sample[12]={0,14,28,42,54,65,76,84,91,96,99,100}; 00055 00056 #elif nbr_of_samples_half == 24 00057 CONST uint8_t sample[13]={0,13,25,38,49,60,69,78,85,91,95,97,98}; 00058 00059 #elif nbr_of_samples_half == 26 00060 CONST uint8_t sample[14]={0,12,24,35,46,56,66,74,81,88,93,96,98,99}; 00061 00062 #elif nbr_of_samples_half == 28 00063 CONST uint8_t sample[15]={0,11,22,33,43,53,62,71,78,85,90,94,97,99,100}; 00064 00065 #elif nbr_of_samples_half == 30 00066 CONST uint8_t sample[16]={0,10,21,31,41,50,59,67,74,81,87,91,95,98,99,100}; 00067 00068 #elif nbr_of_samples_half == 46 00069 CONST uint8_t sample[24]={0,7,13,20,26,33,39,45,51,57,62,67,72,76,80,84,87,90,92, \ 00070 94,96,97,98,98}; 00071 #endif 00072 00073 /* Private function prototypes -----------------------------------------------*/ 00074 static void LoadValueIntoSinStruct(void); 00075 static void TIM1_Config(void); 00076 00077 00078 /* Public functions ----------------------------------------------------------*/ 00079 00080 /** 00081 * @brief Main program. 00082 * @param None 00083 * @retval None 00084 */ 00085 00086 void main(void) 00087 { 00088 /* select Clock = 4 MHz */ 00089 CLK_SYSCLKConfig(CLK_PRESCALER_HSIDIV4); 00090 CLK_HSICmd(ENABLE); 00091 00092 00093 /* The counter value is initialized to 0.*/ 00094 counter = 0; 00095 LoadValueIntoSinStruct(); 00096 00097 /* TIM1 configuration -----------------------------------------*/ 00098 TIM1_Config(); 00099 00100 /* Enable interrupts*/ 00101 enableInterrupts(); 00102 00103 while(1) 00104 {} 00105 } 00106 00107 /** 00108 * @brief Init Sin Struct 00109 * @param None 00110 * @retval None 00111 */ 00112 static void LoadValueIntoSinStruct(void) 00113 { 00114 uint8_t offset=0,diff=0; 00115 00116 while(offset <= (nbr_of_samples_half/2)) 00117 { 00118 /*First half of upper half samples of sine wave is loaded into "value" of sin structure.*/ 00119 sin.Value[offset] = sample[offset]; 00120 offset++; 00121 } 00122 diff = (uint8_t)(offset - 1); 00123 while(offset < nbr_of_samples_half) 00124 { 00125 diff--; 00126 /* Second half of upper half samples of sine wave is loaded into "value" of sin structure.*/ 00127 sin.Value[offset] = sample[diff]; 00128 offset++; 00129 } 00130 /*Index is initialized to 0.Sin wave upper half is generated at start*/ 00131 sin.Index = 0; 00132 sin.Up = TRUE; 00133 } 00134 00135 /** 00136 * @brief Configure TIM1 to allow sine wave generation 00137 * @param None 00138 * @retval None 00139 */ 00140 static void TIM1_Config(void) 00141 { 00142 TIM1_DeInit(); 00143 /* Time base configuration */ 00144 /* 00145 TIM1_Period = INIT_TIM1_ARR 00146 TIM1_Prescaler = 0 00147 TIM1_RepetitionCounter=0 00148 */ 00149 TIM1_TimeBaseInit(0, TIM1_COUNTERMODE_UP, INIT_TIM1_ARR, 0); 00150 00151 /* PWM configuration */ 00152 /* 00153 TIM1_OCMode = TIM1_OCMODE_PWM1 00154 TIM1_OutputState = TIM1_OUTPUTSTATE_ENABLE 00155 TIM1_Pulse = INIT_TIM1_CCR1 00156 TIM1_OCPolarity = TIM1_OCPOLARITY_LOW 00157 */ 00158 TIM1_OC1Init(TIM1_OCMODE_PWM1, TIM1_OUTPUTSTATE_ENABLE, TIM1_OUTPUTNSTATE_DISABLE, 00159 INIT_TIM1_CCR1, TIM1_OCPOLARITY_LOW, TIM1_OCNPOLARITY_LOW, 00160 TIM1_OCIDLESTATE_RESET, TIM1_OCNIDLESTATE_RESET); 00161 00162 /* Update Interrupt Enable */ 00163 TIM1_ITConfig(TIM1_IT_UPDATE, ENABLE); 00164 00165 /* ARRPreload Enable */ 00166 TIM1_ARRPreloadConfig(ENABLE); 00167 00168 /* Enable TIM1 */ 00169 TIM1_Cmd(ENABLE); 00170 00171 /* Enable CC1 output*/ 00172 TIM1_CtrlPWMOutputs(ENABLE); 00173 } 00174 00175 00176 #ifdef USE_FULL_ASSERT 00177 00178 /** 00179 * @brief Reports the name of the source file and the source line number 00180 * where the assert_param error has occurred. 00181 * @param file: pointer to the source file name 00182 * @param line: assert_param error line source number 00183 * @retval None 00184 */ 00185 void assert_failed(uint8_t* file, uint32_t line) 00186 { 00187 /* User can add his own implementation to report the file name and line number, 00188 ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ 00189 00190 /* Infinite loop */ 00191 while (1) 00192 { 00193 } 00194 } 00195 #endif 00196 00197 /** 00198 * @} 00199 */ 00200 00201 00202 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/