TA的每日心情 | 慵懒 2018-11-14 04:19 |
---|
签到天数: 46 天 连续签到: 1 天 [LV.5]常住居民I
|
发表于 2017-1-30 01:10:40
|
显示全部楼层
- #include <stm32f4xx.h>
- volatile u8 state = 0;
- const u16 TIM3_CCR1[] = {240,240,60,60,60,60,60,60,60,540};
- #define STATE_COUNT (sizeof(TIM3_CCR1)/sizeof(u16 ))
- static void TIM3_Config(void);
- int main()
- {
- TIM3_Config();
- TIM3->ARR = 65535;//Prescaler
- TIM3->PSC = 100;//Period
- TIM3->CCMR1 |= 0x3030;//Toggle Mode
- TIM3->CCMR2 |= 0x3030;//Toggle Mode
- TIM3->CCER |= 0x1111;//Capture Compare 1 output enable //CaptureCompare 1 output PolarityHigh
- TIM3->CCR1 = 240;// Duty
- TIM3->CCR2 = 140;// Duty
- TIM3->CCR3 = 540;// Duty
- TIM3->CCR4 = 640;// Duty
- TIM3->CR1 |= 0x0001;//Counter enabled
- TIM3->DIER |= 0x001E;//Capture Compare 1 interrupt enable
- while(1)
- {
- }
- }
- static void TIM3_Config(void)
- {
- NVIC_InitTypeDef NVIC_TIM3; // NVIC structure
- RCC->AHB1ENR |= 0x00000004; //GPIOC Clock
- RCC->APB1ENR |= 0x00000002;//TIM3 Clock
- GPIOC->MODER |= 0x000AA000;//PC6,7,8,9 AF Mode
- GPIOC->OSPEEDR |= 0x000AA000;//PC6,7,8,9 50MHz
- GPIOC->OTYPER |= 0x00000000;//PC6,7,8,9 Push-pull
- GPIOC->PUPDR |= 0x00055000;//PC6,7,8,9 Pull Up
- GPIOC->AFR[0] |= 0x22000000;//AF2 (TIM3) PC6-> TIM3_CH1,PC7->CH2
- GPIOC->AFR[1] |= 0x00000022;// PC8->CH3,PC9->CH4
- NVIC->ISER[0] |= 0x20000000;
- NVIC_TIM3.NVIC_IRQChannelPreemptionPriority = 0; // Only matters for multiple interrupts
- NVIC_TIM3.NVIC_IRQChannelSubPriority = 0; // Only matters for multiple interrupts
- NVIC_TIM3.NVIC_IRQChannelCmd = ENABLE; // Enabling global interrupt
- NVIC_Init(&NVIC_TIM3); // Initializing NVIC structure
- }
- void TIM3_IRQHandler(void)
- {
-
- static uint8_t state = 0;
- if(TIM3->SR &0x0002)//CC1
- {
- TIM3->SR &= ~0x0002;
- TIM3->CCR1 += TIM3_CCR1[state];
- if (++state >= STATE_COUNT)
- state = 0;
- }
- if(TIM3->SR &0x0004)//CC2
- {
- static uint8_t state1 = 0;
- TIM3->SR &= ~0x0004;
- if (state1 < 2 || state1 > 8)
- TIM3->CCR2 += 240;
- else
- TIM3->CCR2 += 60;
- if (++state1 > 9)
- state1 = 0;
-
- }
- if(TIM3->SR &0x0008)//CC3
- {
- static uint8_t state2 = 0;
- TIM3->SR &= ~0x0008;
- if (state2 < 2 || state2 > 8)
- TIM3->CCR3 += 240;
- else
- TIM3->CCR3 += 60;
- if (++state2 > 9)
- state2 = 0;
-
- }
- if(TIM3->SR &0x0010)//CC4
- {
- static uint8_t state3 = 0;
- TIM3->SR &= ~0x0010;
- if (state3 < 2 || state3 > 8)
- TIM3->CCR4 += 200;
- else
- TIM3->CCR4 += 100;
- if (++state3 > 9)
- state3 = 0;
- }
- }
复制代码 |
|