Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stm32f4xx.h>
- const uint16_t TIM_PRESCALER = 84000;
- const uint32_t TIM_PERIOD = 1000;
- static void init_timers(void) {
- TIM_TimeBaseInitTypeDef timer_struct;
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
- timer_struct.TIM_Prescaler = TIM_PRESCALER - 1; /* Scale value to microseconds */
- timer_struct.TIM_CounterMode = TIM_CounterMode_Up;
- timer_struct.TIM_Period = TIM_PERIOD - 1; /* Gives us a second interval */
- timer_struct.TIM_ClockDivision = TIM_CKD_DIV1; /* Tell timer to divide clocks */
- timer_struct.TIM_RepetitionCounter = 0;
- TIM_TimeBaseInit(TIM2, &timer_struct);
- // возьмем самое первое в списке TIM_IT_Update
- TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
- // misc.h
- NVIC_InitTypeDef nvic_struct;
- nvic_struct.NVIC_IRQChannel = TIM2_IRQn; // какое прерывание включить
- nvic_struct.NVIC_IRQChannelPreemptionPriority = 0; // приоритеты
- nvic_struct.NVIC_IRQChannelSubPriority = 1;
- nvic_struct.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&nvic_struct);
- /* После всех инициализаций включаем таймер */
- TIM_Cmd(TIM2, ENABLE);
- }
- void TIM2_IRQHandler(void) {
- // проверяем какое событие вызвало прерывание
- if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET) {
- // обязательно сбросить прерывание !!!
- TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
- static int j = 0;
- j++;
- if (j >= 3)
- j = 0;
- switch (j)
- {
- case 0:
- {
- GPIO_ResetBits(GPIOD, GPIO_Pin_14 );
- GPIO_SetBits(GPIOD, GPIO_Pin_12 );
- break;
- }
- case 1:
- {
- GPIO_ResetBits(GPIOD, GPIO_Pin_12 );
- GPIO_SetBits(GPIOD, GPIO_Pin_13 );
- break;
- }
- case 2:
- {
- GPIO_ResetBits(GPIOD, GPIO_Pin_13 );
- GPIO_SetBits(GPIOD, GPIO_Pin_14 );
- break;
- }
- }
- }
- }
- int main(void) {
- setup_clock(HSE);
- init_leds();
- init_button();
- init_timers();
- while (1) {
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement