Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stm32f4xx.h"
- #include "stm32f4_discovery.h"
- int counter;
- int main(void)
- {
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
- GPIO_InitTypeDef GPIO_InitStructure;
- GPIO_PinAFConfig(GPIOD, GPIO_PinSource12, GPIO_AF_TIM4);
- GPIO_PinAFConfig(GPIOD, GPIO_PinSource13, GPIO_AF_TIM4);
- GPIO_PinAFConfig(GPIOD, GPIO_PinSource14, GPIO_AF_TIM4);
- GPIO_PinAFConfig(GPIOD, GPIO_PinSource15, GPIO_AF_TIM4);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13| GPIO_Pin_14| GPIO_Pin_15 ;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
- GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
- GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
- GPIO_Init(GPIOD, &GPIO_InitStructure);
- TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
- TIM_TimeBaseStructure.TIM_Period = 999;
- TIM_TimeBaseStructure.TIM_Prescaler = 83;
- TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
- TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
- TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
- TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure2;
- TIM_TimeBaseStructure2.TIM_Period = 9999;
- TIM_TimeBaseStructure2.TIM_Prescaler = 8399;
- TIM_TimeBaseStructure2.TIM_ClockDivision = TIM_CKD_DIV1;
- TIM_TimeBaseStructure2.TIM_CounterMode = TIM_CounterMode_Up;
- TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure2);
- TIM_OCInitTypeDef TIM_OCInitStructure;
- TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
- TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
- TIM_OCInitStructure.TIM_Pulse = 0;
- TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
- TIM_OC1Init(TIM4, &TIM_OCInitStructure);
- TIM_OC1PreloadConfig(TIM4, TIM_OCPreload_Enable);
- TIM_OC2Init(TIM4, &TIM_OCInitStructure);
- TIM_OC2PreloadConfig(TIM4, TIM_OCPreload_Enable);
- TIM_OC3Init(TIM4, &TIM_OCInitStructure);
- TIM_OC3PreloadConfig(TIM4, TIM_OCPreload_Enable);
- TIM_OC4Init(TIM4, &TIM_OCInitStructure);
- TIM_OC4PreloadConfig(TIM4, TIM_OCPreload_Enable);
- TIM_Cmd(TIM4, ENABLE);
- TIM_Cmd(TIM3, ENABLE);
- unsigned int i=0;
- unsigned int j=0;
- unsigned int k=0;
- unsigned int l=0;
- for(;;)
- {
- if(TIM_GetFlagStatus(TIM4, TIM_FLAG_Update)) {
- TIM_ClearFlag(TIM4, TIM_FLAG_Update);
- }
- if(TIM_GetFlagStatus(TIM3, TIM_FLAG_Update)) {
- if(i==14)
- i=0;
- if(j==13)
- j=0;
- if(k==12)
- k=0;
- if(l==11)
- l=0;
- TIM4->CCR1 = i*100;
- TIM4->CCR2 = j*100;
- TIM4->CCR3 = k*100;
- TIM4->CCR4 = l*100;
- i++;
- if(i>1)
- j++;
- if(j>1)
- k++;
- if(k>1)
- l++;
- TIM_ClearFlag(TIM3, TIM_FLAG_Update);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement