Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stm32f2xx.h"
- #include "stm32f2xx_gpio.h"
- #include "stm32f2xx_rcc.h"
- #include "stm32f2xx_tim.h"
- #include "system_stm32f2xx.h"
- /****************************************************************************/
- void main(void) {
- TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
- TIM_OCInitTypeDef TIM_OCInitStructure;
- GPIO_InitTypeDef GPIO_InitStructure;
- uint16_t period;
- uint16_t pulse;
- /* Initialize clock - defined in system_stm32f2xx.h */
- SystemInit();
- /* Compute the value for the ARR register to have a period of 20 KHz */
- period = (SystemCoreClock / 20000 ) - 1;
- /* Compute the CCR1 value to generate a PWN signal with 50% duty cycle */
- pulse = (uint16_t) (((uint32_t) 5 * (period - 1)) / 10);
- /* GPIOA clock enable */
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
- /* Initialize PA8, Alternative Function, 100Mhz, Output, Push-pull */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
- GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
- GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- GPIO_PinAFConfig(GPIOA, GPIO_PinSource8, GPIO_AF_TIM1);
- /* TIM1 clock enable */
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1 , ENABLE);
- /* Timer Base configuration */
- TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
- TIM_TimeBaseStructure.TIM_Prescaler = 0;
- TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
- TIM_TimeBaseStructure.TIM_Period = period;
- TIM_TimeBaseStructure.TIM_ClockDivision = 0;
- TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
- TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
- /* Channel 1 output configuration */
- TIM_OCStructInit(&TIM_OCInitStructure);
- TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
- TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
- TIM_OCInitStructure.TIM_Pulse = pulse;
- TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
- TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
- TIM_OC1Init(TIM1, &TIM_OCInitStructure);
- /* TIM1 counter enable */
- TIM_Cmd(TIM1, ENABLE);
- /* TIM1 Main Output Enable */
- TIM_CtrlPWMOutputs(TIM1, ENABLE);
- /* forever... */
- while (1) {
- __asm("nop");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement