Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- -----------------------------------------------------------------*/
- #include "stm32f10x_gpio.h"
- #include "stm32f10x_rcc.h"
- #include "stm32f10x_tim.h"
- TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
- TIM_OCInitTypeDef TIM_OCInitStructure;
- uint16_t CCR1_Val = 333;
- uint16_t CCR2_Val = 249;
- uint16_t CCR3_Val = 166;
- uint16_t CCR4_Val = 83;
- uint16_t PrescalerValue ;
- /* Private function prototypes -----------------------------------------------*/
- void RCC_Configuration(void);
- void GPIO_Configuration(void);
- /* Private functions ---------------------------------------------------------*/
- /**
- int main(void)
- {
- RCC_Configuration();
- /*GPIO Configuration */
- GPIO_Configuration();
- PrescalerValue =(72000000 / 24000000) - 1;
- /* Time base configuration */
- TIM_TimeBaseStructure.TIM_Period = 665;
- TIM_TimeBaseStructure.TIM_Prescaler = PrescalerValue;
- TIM_TimeBaseStructure.TIM_ClockDivision = 4;
- TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
- TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
- /* PWM1 Mode configuration: Channel1 */
- TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
- TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
- TIM_OCInitStructure.TIM_Pulse = CCR1_Val;
- TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
- TIM_OC1Init(TIM3, &TIM_OCInitStructure);
- TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable);
- /* PWM1 Mode configuration: Channel2 */
- TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
- TIM_OCInitStructure.TIM_Pulse = CCR2_Val;
- TIM_OC2Init(TIM3, &TIM_OCInitStructure);
- TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable);
- /* PWM1 Mode configuration: Channel3 */
- TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
- TIM_OCInitStructure.TIM_Pulse = CCR3_Val;
- TIM_OC3Init(TIM3, &TIM_OCInitStructure);
- TIM_OC3PreloadConfig(TIM3, TIM_OCPreload_Enable);
- /* TIM3 enable counter */
- TIM_Cmd(TIM3, ENABLE);
- while (1)
- {}
- }
- void RCC_Configuration(void)
- {
- /* TIM3 clock enable */
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
- /* GPIOA and GPIOB clock enable */
- RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE);
- }
- void GPIO_Configuration(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- /*GPIOB Configuration: TIM3 channel1, 2, 3 and 4 */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_14|GPIO_Pin_15 ;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- GPIO_PinRemapConfig(GPIO_FullRemap_TIM3, ENABLE);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement