working example of LED blinki with tim2 interrupt

redesign_interrupts
polymurph 3 years ago
parent 468c2e2d6b
commit 25678fc81e

@ -5,6 +5,7 @@
#include "ascii.h" #include "ascii.h"
#include "timer.h" #include "timer.h"
#include "i2c.h" #include "i2c.h"
#include "pin.h"
#include "stm32f042x6.h" #include "stm32f042x6.h"
#include "hardwareDescription.h" #include "hardwareDescription.h"
@ -54,15 +55,12 @@ void risingEdgeInterruptPinA1()
} }
#endif #endif
void TIM2_IRQHandler(void) void TIM2_IRQHandler(){
{ /* Check what type of event occurred */
if(TIM2->SR & TIM_SR_UIF) { if (TIM2->SR & TIM_SR_UIF)
{
// clear flag /* Clear the interrupt even flag. CPU will only respond to new flags thereafter */
//TIM2->SR &= ~TIM_SR_UIF; TIM2->SR &= ~(TIM_SR_UIF);
TIM2-> SR = 0;
//pinWrite(pinB3,1);
pinToggle(pinB3); pinToggle(pinB3);
} }
} }
@ -70,6 +68,7 @@ void TIM2_IRQHandler(void)
void LEDtoggleOnTimer2underflow() void LEDtoggleOnTimer2underflow()
{ {
/*
//timerInitCounter(timer_2, 0xFFFF, 0, upCounting); //timerInitCounter(timer_2, 0xFFFF, 0, upCounting);
// reset timer 2 periperal // reset timer 2 periperal
@ -101,7 +100,7 @@ void LEDtoggleOnTimer2underflow()
TIM2->SR = 0; TIM2->SR = 0;
//NVIC_SetVector(TIM2_IRQn, (uint32_t)&TIM2_IRQHandler); //NVIC_SetVector(TIM2_IRQn, (uint32_t)&TIM2_IRQHandler);
NVIC_SetPriority(TIM2_IRQn,0); NVIC_SetPriority(TIM2_IRQn,3);
NVIC_EnableIRQ(TIM2_IRQn); NVIC_EnableIRQ(TIM2_IRQn);
// timer enable // timer enable
@ -111,13 +110,44 @@ void LEDtoggleOnTimer2underflow()
while(1) while(1)
{ {
/* if(TIM2->CNT > 0xFFFF) {
pinWrite(pinB3,1);
} else {
pinWrite(pinB3,0);
}
if(led_on){ if(led_on){
pinWrite(pinB3,1); pinWrite(pinB3,1);
} else{ } else{
pinWrite(pinB3,0); pinWrite(pinB3,0);
} }
}
*/ */
// example based on
// https://alsaibie.github.io/me319/prelabsextra/lab4extra/
/* Enable Timer 2 peripheral */
RCC->APB1ENR |= RCC_APB1ENR_TIM2EN;
/* Default APB1 speed is 16MHz. Scale Timer 2 to 20kHz */
//TIM2->PSC = (uint32_t)(16E6 / 20E3) - 1;
TIM2->PSC = (8E6 / 20E3) - 1 ;
/* Disable Timer*/
TIM2->CR1 = 0;
/* Reinitialize counter and update registers */
TIM2->EGR = TIM_EGR_UG;
/* 1000 Counts at 20kHz => 20Hz IRQ */
//TIM2->ARR = 1000 - 1;
TIM2->ARR = 10000-1;
/* Enable hardware interrupts */
TIM2->DIER |= TIM_DIER_UIE;
/* Start Timer */
TIM2->CR1 |= TIM_CR1_CEN;
NVIC_SetPriority(TIM2_IRQn, 2);
NVIC_EnableIRQ(TIM2_IRQn);
while(1)
{
} }
} }

Loading…
Cancel
Save