/*
******************************************************************************
File: main.c
******************************************************************************
*/
/* Includes */
#include "LiquidCrystal4BIT.h"
#include "ADC1Lib.h"
#define TIM2_SR_UIF 0x0001
#define TIM3_SR_UIF 0x0001
#define TIM4_SR_UIF 0x0001
#define TIM5_SR_UIF 0x0001
volatile uint32_t cnt=0;
volatile uint32_t cnt1=0;
volatile uint32_t cnt2=0;
volatile uint32_t cnt3=0;
void timerConfig(TIM_TypeDef *timer, uint16_t psc, uint32_t arr);
void TIM2_IRQHandler()
{
if(TIM2->SR&TIM2_SR_UIF)
{
cnt++;
TIM2->SR&=~(1<<0); //clear pending UIF bit
}
}
void TIM3_IRQHandler()
{
if(TIM3->SR&TIM3_SR_UIF)
{
cnt1++;
TIM3->SR&=~(1<<0); //clear pending UIF bit
}
}
void TIM4_IRQHandler()
{
if(TIM4->SR&TIM4_SR_UIF)
{
cnt2++;
TIM4->SR&=~(1<<0); //clear pending UIF bit
}
}
void TIM5_IRQHandler()
{
if(TIM5->SR&TIM5_SR_UIF)
{
cnt3++;
TIM5->SR&=~(1<<0); //clear pending UIF bit
}
}
int main(void)
{
pinInit();
LCDInit();
ADCPinConfig(ADCPort, ADCPin);
ADCInit(ADC1, ADCChannel);
LCDSetCursor(0,0);
LCDWriteString("TIMER 2:");
LCDSetCursor(0,1);
LCDWriteString("TIMER 3:");
LCDSetCursor(0,2);
LCDWriteString("TIMER 4:");
LCDSetCursor(0,3);
LCDWriteString("TIMER 5:");
timerConfig(TIM2, 42000, 499);
timerConfig(TIM3, 42000, 999);
timerConfig(TIM4, 42000, 1999);
timerConfig(TIM5, 42000, 3999);
NVIC_EnableIRQ(TIM2_IRQn);
NVIC_EnableIRQ(TIM3_IRQn);
NVIC_EnableIRQ(TIM4_IRQn);
NVIC_EnableIRQ(TIM5_IRQn);
while(1)
{
LCDSetCursor(10,0);
LCDWriteInteger(cnt);
LCDSetCursor(10,1);
LCDWriteInteger(cnt1);
LCDSetCursor(10,2);
LCDWriteInteger(cnt2);
LCDSetCursor(10,3);
LCDWriteInteger(cnt3);
LCDSetCursor(10,0);
}
}
void timerConfig(TIM_TypeDef *timer, uint16_t psc, uint32_t arr)
{
if(timer==TIM2)
{
RCC->APB1ENR|=(1<<0); //CLOCK FOR TIMER 2
}
else if(timer==TIM3)
{
RCC->APB1ENR|=(1<<1); //CLOCK FOR TIMER 3
}
else if(timer==TIM4)
{
RCC->APB1ENR|=(1<<2); //CLOCK FOR TIMER 4
}
else if(timer==TIM5)
{
RCC->APB1ENR|=(1<<3); //CLOCK FOR TIMER 5
}
else
{
LCDWriteCmd(0x80);
LCDSetCursor(0,0);
LCDWriteString("INVALID PARAMETER");
LCDSetCursor(0,1);
LCDWriteString("DURING INIT....");
exit(0);
}
timer->CR1&=~(1<<1); //UPDATE ENABLE
timer->CR1&=~(1<<4); //UP COUNTER
timer->CNT=0;
timer->DIER|=(1<<0); //uie interrupt
timer->PSC=(uint16_t)psc;
if(timer==TIM2||timer==TIM5)
{
timer->ARR=(uint32_t)arr;
}
else
{
timer->ARR=(uint16_t)arr;
}
timer->CR1|=(1<<0); //TIMER ENABLE
}