Advertisement
Hoksmur

Untitled

Jun 30th, 2015
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.27 KB | None | 0 0
  1. /* Includes ------------------------------------------------------------------*/
  2. #include "main.h"
  3.  
  4. /* Private typedef -----------------------------------------------------------*/
  5. /* Private define ------------------------------------------------------------*/
  6. /* Private macro -------------------------------------------------------------*/
  7. /* Private variables ---------------------------------------------------------*/
  8. TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
  9. TIM_OCInitTypeDef  TIM_OCInitStructure;
  10.  
  11. __IO uint16_t uhCCR1_Val = 40961;
  12. __IO uint16_t uhCCR2_Val = 20480;
  13. __IO uint16_t uhCCR3_Val = 10240;
  14. __IO uint16_t uhCCR4_Val = 5120;
  15.  
  16. uint16_t uhPrescalerValue = 0;
  17.  
  18. /* Private function prototypes -----------------------------------------------*/
  19. static void TIM_Config(void);
  20.  
  21. /* Private functions ---------------------------------------------------------*/
  22.  
  23. /**
  24.   * @brief  Main program
  25.   * @param  None
  26.   * @retval None
  27.   */
  28. int main(void)
  29. {
  30.   /*!< At this stage the microcontroller clock setting is already configured,
  31.        this is done through SystemInit() function which is called from startup
  32.        files (startup_stm32f40_41xxx.s/startup_stm32f427_437xx.s/startup_stm32f429_439xx.s)
  33.        before to branch to application main.
  34.        To reconfigure the default setting of SystemInit() function, refer to
  35.        system_stm32f4xx.c file
  36.      */
  37.        
  38.   /* TIM Configuration */
  39.   TIM_Config();
  40.  
  41.   /* ---------------------------------------------------------------------------
  42.     TIM3 Configuration: Output Compare Toggle Mode:
  43.    
  44.     In this example TIM3 input clock (TIM3CLK) is set to 2 * APB1 clock (PCLK1),
  45.     since APB1 prescaler is different from 1.  
  46.       TIM3CLK = 2 * PCLK1  
  47.       PCLK1 = HCLK / 4
  48.       => TIM3CLK = HCLK / 2 = SystemCoreClock /2
  49.          
  50.     To get TIM3 counter clock at 21 MHz, the prescaler is computed as follows:
  51.        Prescaler = (TIM3CLK / TIM3 counter clock) - 1
  52.        Prescaler = ((SystemCoreClock /2) /21 MHz) - 1
  53.                                              
  54.      CC1 update rate = TIM3 counter clock / uhCCR1_Val = 512.68 Hz
  55.        ==> So the TIM3 Channel 1 generates a periodic signal with a
  56.            frequency equal to 256.35 Hz.
  57.  
  58.      CC2 update rate = TIM3 counter clock / uhCCR2_Val = 1025.39 Hz
  59.        ==> So the TIM3 Channel 2 generates a periodic signal with a
  60.            frequency equal to 512.7 Hz.
  61.  
  62.      CC3 update rate = TIM3 counter clock / uhCCR3_Val = 2050.8 Hz
  63.        ==> So the TIM3 Channel 3 generates a periodic signal with a
  64.            frequency equal to 1025.4 Hz.
  65.  
  66.      CC4 update rate = TIM3 counter clock / uhCCR4_Val = 4101.56 Hz
  67.        ==> So the TIM3 Channel 4 generates a periodic signal with a
  68.            frequency equal to 2050.78 Hz.
  69.  
  70.     Note:
  71.      SystemCoreClock variable holds HCLK frequency and is defined in system_stm32f4xx.c file.
  72.      Each time the core clock (HCLK) changes, user had to call SystemCoreClockUpdate()
  73.      function to update SystemCoreClock variable value. Otherwise, any configuration
  74.      based on this variable will be incorrect.    
  75.   --------------------------------------------------------------------------- */  
  76.  
  77.   /* Compute the prescaler value */
  78.   uhPrescalerValue = (uint16_t) ((SystemCoreClock / 2) / 21000000) - 1;
  79.  
  80.   /* Time base configuration */
  81.   TIM_TimeBaseStructure.TIM_Period = 65535;
  82.   TIM_TimeBaseStructure.TIM_Prescaler = uhPrescalerValue;
  83.   TIM_TimeBaseStructure.TIM_ClockDivision = 0;
  84.   TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
  85.  
  86.   TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
  87.  
  88.   /* Output Compare Toggle Mode configuration: Channel1 */
  89.   TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle;
  90.   TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  91.   TIM_OCInitStructure.TIM_Pulse = uhCCR1_Val;
  92.   TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
  93.   TIM_OC1Init(TIM3, &TIM_OCInitStructure);
  94.  
  95.   TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Disable);
  96.  
  97.   /* Output Compare Toggle Mode configuration: Channel2 */
  98.   TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  99.   TIM_OCInitStructure.TIM_Pulse = uhCCR2_Val;
  100.  
  101.   TIM_OC2Init(TIM3, &TIM_OCInitStructure);
  102.  
  103.   TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Disable);
  104.  
  105.   /* Output Compare Toggle Mode configuration: Channel3 */
  106.   TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  107.   TIM_OCInitStructure.TIM_Pulse = uhCCR3_Val;
  108.  
  109.   TIM_OC3Init(TIM3, &TIM_OCInitStructure);
  110.  
  111.   TIM_OC3PreloadConfig(TIM3, TIM_OCPreload_Disable);
  112.  
  113.   /* Output Compare Toggle Mode configuration: Channel4 */
  114.   TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  115.   TIM_OCInitStructure.TIM_Pulse = uhCCR4_Val;
  116.  
  117.   TIM_OC4Init(TIM3, &TIM_OCInitStructure);
  118.  
  119.   TIM_OC4PreloadConfig(TIM3, TIM_OCPreload_Disable);
  120.  
  121.   /* TIM enable counter */
  122.   TIM_Cmd(TIM3, ENABLE);
  123.  
  124.   /* TIM IT enable */
  125.   TIM_ITConfig(TIM3, TIM_IT_CC1 | TIM_IT_CC2 | TIM_IT_CC3 | TIM_IT_CC4, ENABLE);
  126.  
  127.   while (1)
  128.   {
  129.   }
  130. }
  131.  
  132. /**
  133.   * @brief  Configure the TIM3 Pins.
  134.   * @param  None
  135.   * @retval None
  136.   */
  137. static void TIM_Config(void)
  138. {
  139.   GPIO_InitTypeDef GPIO_InitStructure;
  140.   NVIC_InitTypeDef NVIC_InitStructure;
  141.  
  142.   /* TIM3 clock enable */
  143.   RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
  144.  
  145.   /* GPIOC clock enable */
  146.   RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
  147.    
  148.   /* GPIOC Configuration: TIM3 CH1 (PC6), TIM3 CH2 (PC7), TIM3 CH2 (PC8) and TIM3 CH4 (PC9) */
  149.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9;
  150.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  151.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  152.   GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  153.   GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ;
  154.   GPIO_Init(GPIOC, &GPIO_InitStructure);
  155.    
  156.   /* Connect TIM Channels to AF2 */
  157.   GPIO_PinAFConfig(GPIOC, GPIO_PinSource6, GPIO_AF_TIM3);
  158.   GPIO_PinAFConfig(GPIOC, GPIO_PinSource7, GPIO_AF_TIM3);
  159.   GPIO_PinAFConfig(GPIOC, GPIO_PinSource8, GPIO_AF_TIM3);
  160.   GPIO_PinAFConfig(GPIOC, GPIO_PinSource9, GPIO_AF_TIM3);
  161.  
  162.   /* Enable the TIM3 global Interrupt */
  163.   NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
  164.   NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  165.   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  166.   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  167.   NVIC_Init(&NVIC_InitStructure);
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement