void Timer1_Start(uint32_t Delay) // Задержка в сек * 10 { TIM1_Match.MatchValue = Delay; TIM_ConfigMatch(TIMER1,&TIM1_Match); TIM_ResetCounter(TIMER1); TIM_Cmd(TIMER1,ENABLE); }
void timer_init(int frequency) { TIM_TIMERCFG_Type TIM_ConfigStruct; TIM_MATCHCFG_Type TIM_MatchConfigStruct; PINSEL_CFG_Type PinCfg; PinCfg.Funcnum = 3; PinCfg.OpenDrain = 0; PinCfg.Pinmode = 0; PinCfg.Portnum = 1; PinCfg.Pinnum = 28; PINSEL_ConfigPin(&PinCfg); TIM_ConfigStruct.PrescaleOption = TIM_PRESCALE_USVAL; TIM_ConfigStruct.PrescaleValue = 10; TIM_MatchConfigStruct.MatchChannel = 0; // Enable interrupt when MR0 matches the value in TC register TIM_MatchConfigStruct.IntOnMatch = TRUE; // Enable reset on MR0: TIMER will reset if MR0 matches it TIM_MatchConfigStruct.ResetOnMatch = TRUE; // Stop on MR0 if MR0 matches it TIM_MatchConfigStruct.StopOnMatch = FALSE; // Toggle MR0.0 pin if MR0 matches it TIM_MatchConfigStruct.ExtMatchOutputType = TIM_EXTMATCH_NOTHING; // Set Match value, count value of 100000 (100000 * 10uS = 1000000us = 1s --> 1 Hz) TIM_MatchConfigStruct.MatchValue = (int) 100000.0 / frequency; TIM_Init(LPC_TIM0, TIM_TIMER_MODE, &TIM_ConfigStruct); TIM_ConfigMatch(LPC_TIM0, &TIM_MatchConfigStruct); // preemption = 1, sub-priority = 1 NVIC_SetPriority(TIMER0_IRQn, ((0x01<<3)|0x01)); }
void Timer(uint32_t period){ TIM_MATCHCFG_Type matchcfg; matchcfg.MatchChannel = 0; matchcfg.IntOnMatch = ENABLE; matchcfg.ResetOnMatch = DISABLE; matchcfg.StopOnMatch = DISABLE; matchcfg.MatchValue = period; matchcfg.ExtMatchOutputType = TIM_EXTMATCH_NOTHING; TIM_TIMERCFG_Type timercfg; timercfg.PrescaleOption = TIM_PRESCALE_USVAL; timercfg.PrescaleValue = 1; TIM_Init(LPC_TIM0, TIM_TIMER_MODE, &timercfg); TIM_ConfigMatch(LPC_TIM0, &matchcfg); tim0_flag = 0; NVIC_SetPriority(TIMER0_IRQn, ((0x01<<3)|0x01)); NVIC_EnableIRQ(TIMER0_IRQn); TIM_Cmd(LPC_TIM0, ENABLE); while(!tim0_flag); TIM_DeInit(LPC_TIM0); NVIC_DisableIRQ(TIMER0_IRQn); }
/*********************************************************************//** * @brief Delay millisecond * @param[in] time (ms) * @return None **********************************************************************/ void Timer_Wait(uint32_t time) { // Initialize timer 0, prescale count time of 1ms TIM_ConfigStruct.PrescaleOption = TIM_PRESCALE_USVAL; TIM_ConfigStruct.PrescaleValue = 1000; // use channel 0, MR0 TIM_MatchConfigStruct.MatchChannel = 0; // Enable interrupt when MR0 matches the value in TC register TIM_MatchConfigStruct.IntOnMatch = TRUE; //Enable reset on MR0: TIMER will reset if MR0 matches it TIM_MatchConfigStruct.ResetOnMatch = TRUE; //Stop on MR0 if MR0 matches it TIM_MatchConfigStruct.StopOnMatch = FALSE; //do no thing for external output TIM_MatchConfigStruct.ExtMatchOutputType =TIM_EXTMATCH_NOTHING; // Set Match value, count value is time (timer * 1000uS =timer mS ) TIM_MatchConfigStruct.MatchValue = time; // Set configuration for Tim_config and Tim_MatchConfig TIM_Init(LPC_TIM0, TIM_TIMER_MODE,&TIM_ConfigStruct); TIM_ConfigMatch(LPC_TIM0,&TIM_MatchConfigStruct); // To start timer 0 TIM_Cmd(LPC_TIM0,ENABLE); }
void InitTimerProcessSensorRMSValue(void) { TIM_TIMERCFG_Type TIM_ConfigStruct; TIM_MATCHCFG_Type TIM_MatchConfigStruct ; // Initialize timer 1, prescale count time of tickval TIM_ConfigStruct.PrescaleOption = TIM_PRESCALE_USVAL; TIM_ConfigStruct.PrescaleValue = 1; // use channel 0, MR0 TIM_MatchConfigStruct.MatchChannel = 0; // Enable interrupt when MR0 matches the value in TC register TIM_MatchConfigStruct.IntOnMatch = TRUE; //Enable reset on MR0: TIMER will reset if MR0 matches it TIM_MatchConfigStruct.ResetOnMatch = TRUE; //Stop on MR0 if MR0 matches it TIM_MatchConfigStruct.StopOnMatch = FALSE; TIM_MatchConfigStruct.MatchValue = 20000; //20000us TIM_MatchConfigStruct.ExtMatchOutputType = TIM_EXTMATCH_NOTHING; // Set configuration for Tim_config and Tim_MatchConfig TIM_Init(LPC_TIM1, TIM_TIMER_MODE,&TIM_ConfigStruct); TIM_ConfigMatch(LPC_TIM1,&TIM_MatchConfigStruct); /* preemption = 1, sub-priority = 0 */ NVIC_SetPriority(TIMER1_IRQn, ((0x01<<3)|PRIORITY_TIMER1)); /* Enable interrupt for timer 0 */ NVIC_EnableIRQ(TIMER1_IRQn); // To start timer 0 TIM_Cmd(LPC_TIM1,ENABLE); }
// timerNumber 1,2,3 uses Timer1,Timer2,Timer3. Timer0 is used for initializeHOPE() sequence static void enableTimer(int timerNumber, uint32_t time){ TIM_TIMERCFG_Type TIM_ConfigStruct; TIM_MATCHCFG_Type TIM_MatchConfigStruct ; // Initialize timer 0, prescale count time of 1ms TIM_ConfigStruct.PrescaleOption = TIM_PRESCALE_USVAL; TIM_ConfigStruct.PrescaleValue = 1000; // use channel 0, MR0 TIM_MatchConfigStruct.MatchChannel = 0; // Enable interrupt when MR0 matches the value in TC register TIM_MatchConfigStruct.IntOnMatch = TRUE; //Enable reset on MR0: TIMER will reset if interrupt triggered TIM_MatchConfigStruct.ResetOnMatch = TRUE; //Continue running after interrupt has occurred TIM_MatchConfigStruct.StopOnMatch = FALSE; //do no thing for external output TIM_MatchConfigStruct.ExtMatchOutputType = TIM_EXTMATCH_NOTHING; // Set Match value, count value is time (timer * 1000uS =timer mS ) TIM_MatchConfigStruct.MatchValue = time; LPC_TIM_TypeDef *TIMx; // select Timer1, Timer2 or Timer3 if (timerNumber == 1) { TIMx = LPC_TIM1; } else if (timerNumber == 2) { TIMx = LPC_TIM2; } else if (timerNumber == 3) { TIMx = LPC_TIM3; } // Set configuration for Tim_config and Tim_MatchConfig TIM_Init(TIMx,TIM_TIMER_MODE,&TIM_ConfigStruct); TIM_ConfigMatch(TIMx,&TIM_MatchConfigStruct); TIM_Cmd(TIMx, ENABLE); }
void Timer::initialize() { callbackFunction = new FunctionPointer<void, void>; callbackActive = false; if(!timerInitialized) { timerInitialized = true; //configure timer for 25MHz, overflow at 2^32 / 25000000 = ~171.798 seconds TIM_TIMERCFG_Type TIM_ConfigStruct; TIM_ConfigStruct.PrescaleOption = TIM_PRESCALE_TICKVAL; TIM_ConfigStruct.PrescaleValue = 1; TIM_Init(TIMER_PERIPHERAL, TIM_TIMER_MODE, &TIM_ConfigStruct); TIM_MATCHCFG_Type TIM_MatchConfigStruct; TIM_MatchConfigStruct.MatchChannel = 0; TIM_MatchConfigStruct.IntOnMatch = TRUE; TIM_MatchConfigStruct.ResetOnMatch = TRUE; TIM_MatchConfigStruct.StopOnMatch = FALSE; TIM_MatchConfigStruct.ExtMatchOutputType = TIM_EXTMATCH_NOTHING; TIM_MatchConfigStruct.MatchValue = 0xffffffff; TIM_ConfigMatch(TIMER_PERIPHERAL,&TIM_MatchConfigStruct); TIM_ResetCounter(TIMER_PERIPHERAL); TIM_Cmd(TIMER_PERIPHERAL,ENABLE); } }
static void TIM2_CFG( ) { TIM_TIMERCFG_Type TMR2_Cfg; TIM_MATCHCFG_Type TMR2_Match; /* On reset, Timer0/1 are enabled (PCTIM0/1 = 1), and Timer2/3 are disabled (PCTIM2/3 = 0).*/ /* Initialize timer 0, prescale count time of 100uS */ TMR2_Cfg.PrescaleOption = TIM_PRESCALE_USVAL; TMR2_Cfg.PrescaleValue = 10000; /* Use channel 0, MR0 */ TMR2_Match.MatchChannel = 0; /* Enable interrupt when MR0 matches the value in TC register */ TMR2_Match.IntOnMatch = ENABLE; /* Enable reset on MR0: TIMER will reset if MR0 matches it */ TMR2_Match.ResetOnMatch = FALSE; /* Don't stop on MR0 if MR0 matches it*/ TMR2_Match.StopOnMatch = TRUE; /* Do nothing for external output pin if match (see cmsis help, there are another options) */ TMR2_Match.ExtMatchOutputType = TIM_EXTMATCH_NOTHING; /* Set Match value, count value of 2 (2 * 100uS = 200us ) */ TMR2_Match.MatchValue = 250; /* Set configuration for Tim_config and Tim_MatchConfig */ TIM_Init(LPC_TIM2, TIM_TIMER_MODE, &TMR2_Cfg); TIM_ConfigMatch(LPC_TIM2, &TMR2_Match); NVIC_SetPriorityGrouping(3); /* Preemption = 1, sub-priority = 1 */ NVIC_SetPriority(TIMER2_IRQn, 2); /* Enable interrupt for timer 0 */ NVIC_EnableIRQ(TIMER2_IRQn); /* Start timer 0 */ TIM_Cmd(LPC_TIM2, ENABLE); }
//===========================================================// // But: Init Timer0 pour generer 1 signal à 1ms //===========================================================// void T0_Init(void) { TIM_TIMERCFG_Type Timer_Config_Structure; // structures pour passer les valeurs d'initialisation d'un timer TIM_MATCHCFG_Type Timer_MatchConfig_Structure; // Timer0 Initialization Timer_Config_Structure.PrescaleOption = TIM_PRESCALE_USVAL; // Timer en Mode us Timer_Config_Structure.PrescaleValue = TIMER_TIME_STEP; // TC incrementé de 1 chaque 10us TIM_Init(LPC_TIM0, TIM_TIMER_MODE,&Timer_Config_Structure); // Configuration of Match channels Timer_MatchConfig_Structure.MatchChannel = 0; // Use channel 0, MR0 for both Timers Timer_MatchConfig_Structure.IntOnMatch = FALSE; // En Timer IT qd MR0 = TC Register Timer_MatchConfig_Structure.ResetOnMatch = TRUE; // En Timer RST qd MR0 = TC Register Timer_MatchConfig_Structure.StopOnMatch = FALSE; // No Stop Timer qd MR0 = TC Register Timer_MatchConfig_Structure.ExtMatchOutputType = TIM_EXTMATCH_TOGGLE; // Toggle MATx.y pin qd MR0 = TC Register Timer_MatchConfig_Structure.MatchValue = TIMER_MATCH_VALUE_1ms; // Match Value Timer0 TIM_ConfigMatch(LPC_TIM0,&Timer_MatchConfig_Structure); // Config Timer0 TIM_Cmd(LPC_TIM0,ENABLE); // Start Timer0 }
void hal_tick_init(void){ //initialize timer struct TTC0.PrescaleOption = TIM_PRESCALE_TICKVAL; TTC0.PrescaleValue = 1; //intialize match channel struct MATC0.MatchChannel = 0; MATC0.IntOnMatch = ENABLE; MATC0.StopOnMatch = DISABLE; MATC0.ResetOnMatch = ENABLE; MATC0.ExtMatchOutputType = TIM_EXTMATCH_NOTHING; MATC0.MatchValue = TIMER_COUNTDOWN; // this should give approximately 1/4 of a sec //initialize TIMER0 TIM_Init((LPC_TIM_TypeDef *) LPC_TIM0, TIM_TIMER_MODE, &TTC0); //initialize MAT0 TIM_ConfigMatch((LPC_TIM_TypeDef *) LPC_TIM0, &MATC0); //reset Timer0 TIM_ResetCounter((LPC_TIM_TypeDef *) LPC_TIM0); //enable Timer0 TIM_Cmd((LPC_TIM_TypeDef *) LPC_TIM0, ENABLE); NVIC_EnableIRQ(TIMER0_IRQn); }
int Timer_delay_ms(int count) { // Initialize timer 0, prescale count time of 100uS TIM_ConfigStruct.PrescaleOption = TIM_PRESCALE_USVAL; TIM_ConfigStruct.PrescaleValue = 1000; // use channel 0, MR0 TIM_MatchConfigStruct.MatchChannel = 0; // Disable interrupt when MR0 matches the value in TC register TIM_MatchConfigStruct.IntOnMatch = TRUE; //Enable reset on MR0: TIMER will reset if MR0 matches it TIM_MatchConfigStruct.ResetOnMatch = TRUE; //Stop on MR0 if MR0 matches it TIM_MatchConfigStruct.StopOnMatch = FALSE; //Toggle MR0.0 pin if MR0 matches it TIM_MatchConfigStruct.ExtMatchOutputType =TIM_EXTMATCH_TOGGLE; // Set Match value, count value of 1000 (1000 * 100uS = 100mS --> 10Hz) TIM_MatchConfigStruct.MatchValue = count; // Set configuration for Tim_config and Tim_MatchConfig TIM_Init(LPC_TIM0, TIM_TIMER_MODE,&TIM_ConfigStruct); TIM_ConfigMatch(LPC_TIM0,&TIM_MatchConfigStruct); TIM_Cmd(LPC_TIM0,ENABLE); // Wait for 1000 millisecond while(!(TIM_GetIntStatus(LPC_TIM0,TIM_MR0_INT))); TIM_ClearIntPending(LPC_TIM0,TIM_MR0_INT); return 0; }
// setup hardware timer timerNum // timerCallback: if not-NULL, will be called on any timer interrupt. Default is Match0 // For convenience various default values are setup: a default reload/Match0 period of // 10us is set, this can be changed by setHwTimerInterval. void setupHwTimer (uint16_t timerNum, tHwTimerCallback timerCallback) { TIM_TIMERCFG_Type TIM_ConfigStruct; TIM_MATCHCFG_Type TIM_MatchConfigStruct ; // Prescale in absolute value TIM_ConfigStruct.PrescaleOption = TIM_PRESCALE_TICKVAL; TIM_ConfigStruct.PrescaleValue = 1; TIM_Init (pTimerRegs[timerNum], TIM_TIMER_MODE, &TIM_ConfigStruct); /* Configure Timer to have the same clock as CPU: 100MHz */ CLKPWR_SetPCLKDiv (TimerConfig[timerNum].ClkPwr_PClkSel, CLKPWR_PCLKSEL_CCLK_DIV_1); // use channel 0, MR0 TIM_MatchConfigStruct.MatchChannel = 0; // Enable interrupt when MR0 matches the value in TC register TIM_MatchConfigStruct.IntOnMatch = 1; //Enable reset on MR0: TIMER will reset if MR0 matches it TIM_MatchConfigStruct.ResetOnMatch = 1; //Do not stop on MR0 if MR0 matches it TIM_MatchConfigStruct.StopOnMatch = 0; //Do not toggle MR0.0 pin if MR0 matches it TIM_MatchConfigStruct.ExtMatchOutputType = TIM_EXTMATCH_NOTHING; // Set Match value, count value of 100000000 (100000000 * 10ns = 100000000ns = 1s --> 1 Hz) TIM_MatchConfigStruct.MatchValue = 1000; TIM_ConfigMatch (pTimerRegs[timerNum], &TIM_MatchConfigStruct); /* Set to have highest priority = 0 */ NVIC_SetPriority(TimerConfig[timerNum].TimerIrq, 0); // store the callback address for later use HwTimer[timerNum].timerCallback = timerCallback; }
void InitTimerADCRead(void) { TIM_TIMERCFG_Type TIM_ConfigStruct; TIM_MATCHCFG_Type TIM_MatchConfigStruct ; // Initialize timer 1, prescale count time of tickval TIM_ConfigStruct.PrescaleOption = TIM_PRESCALE_TICKVAL; TIM_ConfigStruct.PrescaleValue = 1; // use channel 0, MR0 TIM_MatchConfigStruct.MatchChannel = 0; // Enable interrupt when MR0 matches the value in TC register TIM_MatchConfigStruct.IntOnMatch = TRUE; //Enable reset on MR0: TIMER will reset if MR0 matches it TIM_MatchConfigStruct.ResetOnMatch = TRUE; //Stop on MR0 if MR0 matches it TIM_MatchConfigStruct.StopOnMatch = FALSE; TIM_MatchConfigStruct.MatchValue = CLKPWR_GetPCLK (CLKPWR_PCLKSEL_TIMER0)/ADC_GET_FREQUENCY; TIM_MatchConfigStruct.ExtMatchOutputType = TIM_EXTMATCH_NOTHING; // Set configuration for Tim_config and Tim_MatchConfig TIM_Init(LPC_TIM3, TIM_TIMER_MODE,&TIM_ConfigStruct); TIM_ConfigMatch(LPC_TIM3,&TIM_MatchConfigStruct); /* preemption = 1, sub-priority = 0 */ NVIC_SetPriority(TIMER3_IRQn, ((0x01<<3)|PRIORITY_TIMER3)); /* Enable interrupt for timer 0 */ NVIC_EnableIRQ(TIMER3_IRQn); // To start timer 0 TIM_Cmd(LPC_TIM3,ENABLE); }
void Timer0_Start(uint32_t Delay) { TIM0_Match.MatchValue = Delay; TIM_ConfigMatch(TIMER0,&TIM0_Match); TIM_ResetCounter(TIMER0); TIM_Cmd(TIMER0,ENABLE); }
/*********************************************************************//** * @brief c_entry: Main program body * @param[in] None * @return int **********************************************************************/ int c_entry (void) { PINSEL_CFG_Type PinCfg; TIM_TIMERCFG_Type TIM_ConfigStruct; TIM_MATCHCFG_Type TIM_MatchConfigStruct; // Conifg P1.28 as MAT0.0 PinCfg.Funcnum = 3; PinCfg.OpenDrain = 0; PinCfg.Pinmode = 0; PinCfg.Portnum = 1; PinCfg.Pinnum = 28; PINSEL_ConfigPin(&PinCfg); /* P3.26 as STCLK */ PinCfg.Funcnum = 1; PinCfg.OpenDrain = 0; PinCfg.Pinmode = 0; PinCfg.Portnum = 3; PinCfg.Pinnum = 26; PINSEL_ConfigPin(&PinCfg); // Initialize timer 0, prescale count time of 10uS TIM_ConfigStruct.PrescaleOption = TIM_PRESCALE_USVAL; TIM_ConfigStruct.PrescaleValue = 10; // use channel 0, MR0 TIM_MatchConfigStruct.MatchChannel = 0; // Disable interrupt when MR0 matches the value in TC register TIM_MatchConfigStruct.IntOnMatch = TRUE; //Enable reset on MR0: TIMER will reset if MR0 matches it TIM_MatchConfigStruct.ResetOnMatch = TRUE; //Stop on MR0 if MR0 matches it TIM_MatchConfigStruct.StopOnMatch = FALSE; //Toggle MR0.0 pin if MR0 matches it TIM_MatchConfigStruct.ExtMatchOutputType =TIM_EXTMATCH_TOGGLE; // Set Match value, count value of 10 (10 * 10uS = 100uS --> 10KHz) TIM_MatchConfigStruct.MatchValue = 10; TIM_Init(LPC_TIM0, TIM_TIMER_MODE,&TIM_ConfigStruct); TIM_ConfigMatch(LPC_TIM0,&TIM_MatchConfigStruct); TIM_Cmd(LPC_TIM0,ENABLE); GPIO_SetDir(0, (1<<0), 1); //Set P0.0 as output //Use P0.0 to test System Tick interrupt /* Initialize System Tick with 10ms time interval * Frequency input = 10kHz /2 = 5kHz * Time input = 10ms */ SYSTICK_ExternalInit(5000, 10); //Enable System Tick interrupt SYSTICK_IntCmd(ENABLE); //Enable System Tick Counter SYSTICK_Cmd(ENABLE); while(1); return 1; }
void Timer::_clearMatch() { TIM_MATCHCFG_Type TIM_MatchConfigStruct; TIM_MatchConfigStruct.MatchChannel = 1; TIM_MatchConfigStruct.IntOnMatch = FALSE; TIM_MatchConfigStruct.ResetOnMatch = FALSE; TIM_MatchConfigStruct.StopOnMatch = FALSE; TIM_MatchConfigStruct.ExtMatchOutputType = TIM_EXTMATCH_NOTHING; TIM_MatchConfigStruct.MatchValue = 0; TIM_ConfigMatch(TIMER_PERIPHERAL, &TIM_MatchConfigStruct); }
void timer_init() { TIM_TIMERCFG_Type TIM_ConfigStruct; TIM_MATCHCFG_Type TIM_MatchConfigStruct; //Set timer prescaler to 0 (increment every tick) TIM_ConfigStruct.PrescaleOption = TIM_PRESCALE_TICKVAL; TIM_ConfigStruct.PrescaleValue = 1; //Init timer 0 TIM_Init(LPC_TIM0, TIM_TIMER_MODE, &TIM_ConfigStruct); //Init timer 1 TIM_Init(LPC_TIM1, TIM_TIMER_MODE, &TIM_ConfigStruct); //Configure match 0 TIM_MatchConfigStruct.MatchChannel = 0; TIM_MatchConfigStruct.IntOnMatch = TRUE; TIM_MatchConfigStruct.ResetOnMatch = TRUE; TIM_MatchConfigStruct.StopOnMatch = FALSE; TIM_MatchConfigStruct.ExtMatchOutputType = TIM_EXTMATCH_NOTHING; //Set match 0 on timer 0 (10uS @ 24Mhz) TIM_MatchConfigStruct.MatchValue = 240; TIM_ConfigMatch(LPC_TIM0, &TIM_MatchConfigStruct); //Set match 0 on timer 1 (12.5uS @ 24Mhz) TIM_MatchConfigStruct.MatchValue = 300; TIM_ConfigMatch(LPC_TIM1, &TIM_MatchConfigStruct); //Set timer 0 interrupt priority NVIC_SetPriority(TIMER0_IRQn, ((0x01<<3)|0x01)); //Set timer 1 interrupt priority NVIC_SetPriority(TIMER1_IRQn, ((0x01<<3)|0x02)); //Enable interrupt for timer 0 NVIC_EnableIRQ(TIMER0_IRQn); //Enable interrupt for timer 1 NVIC_EnableIRQ(TIMER1_IRQn); }
void Install_Timer(uint32_t ms, uint32_t timer, uint32_t prio, uint8_t mtchstop){ #ifndef AVR #if defined(STM32F10X_MD) || defined(STM32F30X) TIM_ITConfig(((TIM_TypeDef *) (APB1PERIPH_BASE + ((timer-2)*0x400))), TIM_IT_Update, DISABLE); EnableClk(timer+6); ///< Just to use created functions // RCC_APB1PeriphClockCmd(1<<(timer-2), ENABLE); TIM_TimeBaseStructure.TIM_Prescaler = (ms-1); TIM_TimeBaseStructure.TIM_Period = ((SystemCoreClock) / 1000) - 1;//(ms); TIM_TimeBaseInit(((TIM_TypeDef *) (APB1PERIPH_BASE + ((timer-2)*0x400))), &TIM_TimeBaseStructure); TIM_ClearITPendingBit(((TIM_TypeDef *) (APB1PERIPH_BASE + ((timer-2)*0x400))), TIM_IT_Update); //TODO: Install Interrupt here? Well it actually SHOULD be initialized before activating TIM_IT //but that would force me to create more doubles. Keep an eye on this TIM_ITConfig(((TIM_TypeDef *) (APB1PERIPH_BASE + ((timer-2)*0x400))), TIM_IT_Update, ENABLE); #else // configure timer TIM_TIMERCFG_Type tc; TIM_ConfigStructInit(TIM_TIMER_MODE, &tc); TIM_Init((( TIM_TypeDef *) timers[timer]), TIM_TIMER_MODE, &tc); // set up match register TIM_MATCHCFG_Type mc; mc.MatchChannel = 0; mc.IntOnMatch = ENABLE; mc.StopOnMatch = mtchstop; mc.ResetOnMatch = ENABLE; mc.ExtMatchOutputType = 0; #ifdef _LPC23XX_ //Yes it actually IS running at one Mhz. This isn't right but I don't have time to spend days in the lpc23xx bible again. mc.MatchValue = (((1000000/1000)*ms)-1); #else mc.MatchValue = (ms * CLKPWR_GetPCLK(CLKPWR_PCLKSEL_TIMER0)) / 1000; #endif TIM_ConfigMatch((( TIM_TypeDef *) (timers[timer])), &mc); // InstallINT(timer, prio); // TIM_Cmd((( TIM_TypeDef *) (timers[timer])), ENABLE);// enable timer #endif //Must really do something neater than this if(mtchstop==0) mtchstop=1; else mtchstop=0; InstallINT(timer, prio); TIM_Cmd((( TIM_TypeDef *) (timers[timer])), mtchstop); #endif }
// set and enable a MatchX value and interrupt. If set, the timer callback will be called // when timer is enabled void setHwTimerMatch (uint16_t timerNum, uint16_t matchReg, uint32_t interval) { TIM_MATCHCFG_Type TIM_MatchConfigStruct ; TIM_MatchConfigStruct.MatchChannel = matchReg; // Enable interrupt when value matches the value in TC register TIM_MatchConfigStruct.IntOnMatch = 1; // Do not toggle pin if timer matches it TIM_MatchConfigStruct.ExtMatchOutputType = TIM_EXTMATCH_NOTHING; // Set Match value TIM_MatchConfigStruct.MatchValue = interval; TIM_ConfigMatch (pTimerRegs[timerNum], &TIM_MatchConfigStruct); }
void tim_configm(int dev, int chan, int irq, int stop, int rst, int val) { TIM_MATCHCFG_Type mcfg; mcfg.MatchChannel = chan; // Enable interrupt when MR0 matches the value in TC register mcfg.IntOnMatch = irq; //Enable reset on MR0: TIMER will reset if MR0 matches it mcfg.ResetOnMatch = rst; //Stop on MR0 if MR0 matches it mcfg.StopOnMatch = stop; //Toggle MR0.0 pin if MR0 matches it mcfg.ExtMatchOutputType = TIM_EXTMATCH_NOTHING; // Set Match value, count value of 10000 (10000 * 100uS = 1000000us = 1s --> 1 Hz) mcfg.MatchValue = val; TIM_ConfigMatch(tim_devs[dev], &mcfg); }
/*********************************************************************//** * @brief Timer wait (microseconds) * @param[in] time number of microseconds waiting * @return None **********************************************************************/ void TIM_Waitus(uint32_t time) { TIM_MATCHCFG_Type MatchConfigStruct; LPC_TIM0->IR = 0xFFFFFFFF; MatchConfigStruct.MatchChannel = 0; MatchConfigStruct.IntOnMatch = ENABLE; MatchConfigStruct.ResetOnMatch = ENABLE; MatchConfigStruct.StopOnMatch = ENABLE; MatchConfigStruct.ExtMatchOutputType = 0; MatchConfigStruct.MatchValue = time; TIM_ConfigMatch(LPC_TIM0, &MatchConfigStruct); TIM_Cmd(LPC_TIM0,ENABLE); //wait until interrupt flag occur while(!(LPC_TIM0->IR & 0x01)); TIM_ResetCounter(LPC_TIM0); }
static rt_err_t timer_start(rt_hwtimer_t *timer, rt_uint32_t t, rt_hwtimer_mode_t opmode) { LPC_TIM_TypeDef *tim; TIM_MATCHCFG_Type match; tim = (LPC_TIM_TypeDef *)timer->parent.user_data; match.MatchChannel = 0; match.IntOnMatch = ENABLE; match.ResetOnMatch = ENABLE; match.StopOnMatch = (opmode == HWTIMER_MODE_ONESHOT)? ENABLE : DISABLE; match.ExtMatchOutputType = 0; match.MatchValue = t; TIM_ConfigMatch(tim, &match); TIM_Cmd(tim, ENABLE); return RT_EOK; }
void InitTimer(uint32_t frequency) { TIM_TIMERCFG_Type TIM_ConfigStruct; TIM_MATCHCFG_Type TIM_MatchConfigStruct ; uint32_t matchvalue; if(frequency > 100000) return; NVIC_DisableIRQ(TIMER0_IRQn); matchvalue = 1000000 / frequency; // Initialize timer 0, prescale count time of 1uS TIM_ConfigStruct.PrescaleOption = TIM_PRESCALE_USVAL; TIM_ConfigStruct.PrescaleValue = 1; // use channel 0, MR0 TIM_MatchConfigStruct.MatchChannel = 0; // Enable interrupt when MR0 matches the value in TC register TIM_MatchConfigStruct.IntOnMatch = TRUE; //Enable reset on MR0: TIMER will reset if MR0 matches it TIM_MatchConfigStruct.ResetOnMatch = TRUE; //Stop on MR0 if MR0 matches it TIM_MatchConfigStruct.StopOnMatch = FALSE; //Toggle MR0.0 pin if MR0 matches it TIM_MatchConfigStruct.ExtMatchOutputType =TIM_EXTMATCH_NOTHING; // Set Match value TIM_MatchConfigStruct.MatchValue = matchvalue; // Set configuration for Tim_config and Tim_MatchConfig TIM_Init(LPC_TIMER0, TIM_TIMER_MODE,&TIM_ConfigStruct); TIM_ConfigMatch(LPC_TIMER0,&TIM_MatchConfigStruct); /* preemption = 1, sub-priority = 1 */ NVIC_SetPriority(TIMER0_IRQn, ((0x01<<3)|0x01)); /* Enable interrupt for timer 0 */ NVIC_EnableIRQ(TIMER0_IRQn); // To start timer 0 TIM_Cmd(LPC_TIMER0,ENABLE); }
void startTimerInt(uint8_t matchRegister, uint32_t us) { TIM_MATCHCFG_Type timerMatchCfg; TIM_TIMERCFG_Type timerCfg; uint32_t ticks = us; //Initialize timer for delays and interrupts TIM_ConfigStructInit(TIM_TIMER_MODE, &timerCfg); /* initialize timer config struct */ TIM_Init(LPC_TIM1, TIM_TIMER_MODE, &timerCfg); /* initialize timer0 */ timerMatchCfg.ExtMatchOutputType = TIM_EXTMATCH_NOTHING; timerMatchCfg.IntOnMatch = ENABLE; timerMatchCfg.MatchChannel = matchRegister; timerMatchCfg.MatchValue = ticks; timerMatchCfg.ResetOnMatch = DISABLE; timerMatchCfg.StopOnMatch = DISABLE; TIM_ConfigMatch(LPC_TIM1, &timerMatchCfg); TIM_ResetCounter(LPC_TIM1); NVIC_EnableIRQ(TIMER1_IRQn); TIM_Cmd(LPC_TIM1, ENABLE); }
/*********************************************************************//** * @brief Initializes signal supplying for QEI peripheral by using timer * match interrupt output, that will generate two virtual signal on * Phase-A and Phase-B. These two clock are 90 degrees out of phase. * In this case, a 'virtual encoder' that has these following parameter: * - Encoder type : Quadrature encoder * - Max velocity : MAX_VEL (Round Per Minute) * - Encoder Resolution : ENC_RES (Pulse Per Round) * The calculated frequency is: Freq = (MAX_VEL * ENC_RES * COUNT_MODE) / 60 (Hz) * The timer therefore should be set to tick every cycle T = 1/Freq (second) * Figure: * |-----| |-----| * Phase A --| |-----| |----- * |-----| |-----| * Phase B -----| |-----| |-- * * |--|--|--|--|--|--|--|-- * T T T T T T T * * @param[in] None * @return None **********************************************************************/ void VirtualQEISignal_Init(void) { uint32_t pclk; TIM_TIMERCFG_Type TimerConfig; TIM_MATCHCFG_Type TimerMatchConfig; _DBG_("Initializing Virtual QEI signal..."); // Initialize timer 0, Prescale value in tick value option with tick value = 1 TimerConfig.PrescaleOption = TIM_PRESCALE_TICKVAL; TimerConfig.PrescaleValue = 1; TIM_Init(LPC_TIM0, TIM_TIMER_MODE,&TimerConfig); // Get actual peripheral clock of timer 0 pclk = CLKPWR_GetPCLK(CLKPWR_PCLKSEL_TIMER0); pclk = pclk / ((MAX_VEL * ENC_RES * COUNT_MODE) / 60 ); // Set match for match channel 0 TimerMatchConfig.MatchChannel = 0; TimerMatchConfig.MatchValue = pclk; TimerMatchConfig.IntOnMatch = ENABLE; TimerMatchConfig.ExtMatchOutputType = 3; TimerMatchConfig.ResetOnMatch = ENABLE; TimerMatchConfig.StopOnMatch = DISABLE; TIM_ConfigMatch(LPC_TIM0, &TimerMatchConfig); // Reconfigures GPIO for pin used as Phase A and Phase B output GPIO_SetDir(0, PHASE_A_PIN | PHASE_B_PIN, 1); // Set default State after initializing GPIO_ClearValue(0, PHASE_A_PIN | PHASE_B_PIN); // Reset Phase Counter PhaseCnt = 0; /* preemption = 1, sub-priority = 2 */ NVIC_SetPriority(TIMER0_IRQn, ((0x02<<3)|0x01)); /* Enable interrupt for timer 0 */ NVIC_EnableIRQ(TIMER0_IRQn); // To start timer 0 TIM_Cmd(LPC_TIM0,ENABLE); }
// NOTE: On this platform, there is only one ADC, clock settings apply to the whole device u32 platform_adc_set_clock( unsigned id, u32 frequency ) { TIM_TIMERCFG_Type TIM_ConfigStruct; TIM_MATCHCFG_Type TIM_MatchConfigStruct ; elua_adc_dev_state *d = adc_get_dev_state( 0 ); if ( frequency > 0 ) { d->clocked = 1; // Max Sampling Rate on LPC1768 is 200 kS/s if ( frequency > 200000 ) frequency = 200000; // Run timer at 1MHz TIM_ConfigStruct.PrescaleOption = TIM_PRESCALE_USVAL; TIM_ConfigStruct.PrescaleValue = 1; TIM_MatchConfigStruct.MatchChannel = 1; TIM_MatchConfigStruct.IntOnMatch = FALSE; TIM_MatchConfigStruct.ResetOnMatch = TRUE; TIM_MatchConfigStruct.StopOnMatch = FALSE; TIM_MatchConfigStruct.ExtMatchOutputType = TIM_EXTMATCH_TOGGLE; // Set match value to period (in uS) associated with frequency TIM_MatchConfigStruct.MatchValue = ( 1000000ULL / ( frequency * 2 ) ) - 1; frequency = 1000000ULL / (TIM_MatchConfigStruct.MatchValue + 1); // Set configuration for Tim_config and Tim_MatchConfig TIM_Init( tmr[ d->timer_id ], TIM_TIMER_MODE, &TIM_ConfigStruct ); TIM_ConfigMatch( tmr[ d->timer_id ], &TIM_MatchConfigStruct ); TIM_ResetCounter( tmr[ d->timer_id ] ); } else d->clocked = 0; return frequency; }
void Timer10Sec() { TIM_TIMERCFG_Type TIM_ConfigStruct; TIM_MATCHCFG_Type TIM_MatchConfigStruct ; // Initialize timer 0, prescale count time of 100uS TIM_ConfigStruct.PrescaleOption = TIM_PRESCALE_USVAL; TIM_ConfigStruct.PrescaleValue = 1; // use channel 0, MR0 TIM_MatchConfigStruct.MatchChannel = 1; // Enable interrupt when MR0 matches the value in TC register TIM_MatchConfigStruct.IntOnMatch = TRUE; //Enable reset on MR0: TIMER will reset if MR0 matches it TIM_MatchConfigStruct.ResetOnMatch = TRUE; //Stop on MR0 if MR0 matches it TIM_MatchConfigStruct.StopOnMatch = FALSE; //Toggle MR0.0 pin if MR0 matches it TIM_MatchConfigStruct.ExtMatchOutputType =TIM_EXTMATCH_TOGGLE; // Set Match value, count value of 10000000 TIM_MatchConfigStruct.MatchValue = 10000000; // Set configuration for Tim_config and Tim_MatchConfig TIM_Init(LPC_TIM1, TIM_TIMER_MODE,&TIM_ConfigStruct); TIM_ConfigMatch(LPC_TIM1,&TIM_MatchConfigStruct); /* preemption = 1, sub-priority = 1 */ //NVIC_SetPriority(TIMER0_IRQn, ((0x01<<3)|0x01)); /* Enable interrupt for timer 0 */ NVIC_EnableIRQ(TIMER1_IRQn); // To start timer 0 TIM_Cmd(LPC_TIM1,ENABLE); //serialPrint("Int confg\n\r"); PWM_MATCHCFG_Type PWMMatchCfgDat; }
/*============== Таймер 0, таймауты обмена с крипто и модемом =============*/ void Init_Timer0 (void) // Начальная инициализация таймера таймаута обмена // с криптопроцессором и модемом { // Задавать будем в микросекундах TIM0_Config.PrescaleOption = TIM_PRESCALE_USVAL; // Изначально зададим интервал 1 мсек. TIM0_Config.PrescaleValue = 1000; // Используем канал 0, MR0 TIM0_Match.MatchChannel = 0; // Разрешим прерывание, если MR0 совпадет со счетчиком таймера TC TIM0_Match.IntOnMatch = TRUE; //Разрешить сброс по MR0: TIMER сбросится, если MR0 совпадет с ним TIM0_Match.ResetOnMatch = TRUE; //Остановить таймер при достижении MR0 TIM0_Match.StopOnMatch = TRUE; //Никаким выходом по таймеру не управляем TIM0_Match.ExtMatchOutputType =TIM_EXTMATCH_NOTHING; // Установим интервал срабатывания (1000 * 1 mS = 1 s) TIM0_Match.MatchValue = 1000; TIM_Init(TIMER0, TIM_TIMER_MODE,&TIM0_Config); TIM_ConfigMatch(TIMER0,&TIM0_Match); // TIM_Cmd(LPC_TIM0,ENABLE); }
/********************************************************************//** * @brief Configures the TIM3 peripheral according to the specified * parameters. * @param[in] None * @return None *********************************************************************/ void TIM3_Config (void) { // TIM Configuration structure variable TIM_TIMERCFG_Type TIM_ConfigStruct; // TIM Match configuration Structure variable TIM_MATCHCFG_Type TIM_MatchConfigStruct; // Initialize timer, prescale count time of 100uS TIM_ConfigStruct.PrescaleOption = TIM_PRESCALE_USVAL; TIM_ConfigStruct.PrescaleValue = 100; // Use channel PCfg TIM_MatchConfigStruct.MatchChannel = 0; // Disable interrupt when MR0 matches the value in TC register TIM_MatchConfigStruct.IntOnMatch = TRUE; // Enable reset on MR0: TIMER will reset if MR0 matches it TIM_MatchConfigStruct.ResetOnMatch = TRUE; // Stop on MR0 if MR0 matches it TIM_MatchConfigStruct.StopOnMatch = FALSE; // Toggle MR0 pin if MR0 matches it TIM_MatchConfigStruct.ExtMatchOutputType =TIM_EXTMATCH_TOGGLE; // Set Match value, count value of 1000 (1000 * 100uS = 100mS --> 10Hz) TIM_MatchConfigStruct.MatchValue = T1*10; // Set configuration for Tim_config and Tim_MatchConfig TIM_Init(LPC_TIM3, TIM_TIMER_MODE,&TIM_ConfigStruct); TIM_ConfigMatch(LPC_TIM3, &TIM_MatchConfigStruct); /* preemption = 1, sub-priority = 1 */ NVIC_SetPriority(TIMER3_IRQn, 1); /* Enable interrupt for timer 3 */ NVIC_EnableIRQ(TIMER3_IRQn); TIM_Cmd(LPC_TIM3, ENABLE); }
/*********************************************************************//** * @brief c_entry: Main TIMER program body * @param[in] None * @return int **********************************************************************/ int c_entry (void) { PINSEL_CFG_Type PinCfg; /* Initialize debug via UART0 * – 115200bps * – 8 data bit * – No parity * – 1 stop bit * – No flow control */ debug_frmwrk_init(); // print welcome screen print_menu(); /* Select pin: * P1.28: MAT0.0 * P1.22: MAT1.0 * P0.6: MAT2.0 * P0.10: MAT3.0 */ PinCfg.Funcnum = 3; PinCfg.OpenDrain = 0; PinCfg.Pinmode = 0; PinCfg.Portnum = 1; PinCfg.Pinnum = 28; PINSEL_ConfigPin(&PinCfg); PinCfg.Pinnum = 22; PINSEL_ConfigPin(&PinCfg); PinCfg.Portnum = 0; PinCfg.Pinnum = 6; PINSEL_ConfigPin(&PinCfg); PinCfg.Pinnum = 10; PINSEL_ConfigPin(&PinCfg); // Initialize 4 timers, prescale count time of 100uS TIM_ConfigStruct.PrescaleOption = TIM_PRESCALE_USVAL; TIM_ConfigStruct.PrescaleValue = 100; TIM_Init(LPC_TIM0, TIM_TIMER_MODE,&TIM_ConfigStruct); TIM_Init(LPC_TIM1, TIM_TIMER_MODE,&TIM_ConfigStruct); TIM_Init(LPC_TIM2, TIM_TIMER_MODE,&TIM_ConfigStruct); TIM_Init(LPC_TIM3, TIM_TIMER_MODE,&TIM_ConfigStruct); // Configure 4 match channels // use channel 0, MR0 TIM_MatchConfigStruct.MatchChannel = 0; // Enable interrupt when MR0 matches the value in TC register TIM_MatchConfigStruct.IntOnMatch = TRUE; //Enable reset on MR0: TIMER will reset if MR0 matches it TIM_MatchConfigStruct.ResetOnMatch = TRUE; //Stop on MR0 if MR0 matches it TIM_MatchConfigStruct.StopOnMatch = FALSE; //Toggle MR0 pin if MR0 matches it TIM_MatchConfigStruct.ExtMatchOutputType = TIM_EXTMATCH_TOGGLE; // Set Match value TIM_MatchConfigStruct.MatchValue = 700; // Set configuration for Tim_MatchConfig TIM_ConfigMatch(LPC_TIM0,&TIM_MatchConfigStruct); TIM_MatchConfigStruct.MatchValue = 600; TIM_ConfigMatch(LPC_TIM1,&TIM_MatchConfigStruct); TIM_MatchConfigStruct.MatchValue = 500; TIM_ConfigMatch(LPC_TIM2,&TIM_MatchConfigStruct); TIM_MatchConfigStruct.MatchValue = 800; TIM_ConfigMatch(LPC_TIM3,&TIM_MatchConfigStruct); /* preemption = 1, sub-priority = 1 */ NVIC_SetPriority(TIMER0_IRQn, ((0x01<<3)|0x01)); /* Enable interrupt for timer 0 */ NVIC_EnableIRQ(TIMER0_IRQn); // To start timer 0 TIM_Cmd(LPC_TIM0,ENABLE); /* preemption = 1, sub-priority = 1 */ NVIC_SetPriority(TIMER1_IRQn, ((0x01<<3)|0x02)); /* Enable interrupt for timer 1 */ NVIC_EnableIRQ(TIMER1_IRQn); // To start timer 1 TIM_Cmd(LPC_TIM1,ENABLE); /* preemption = 1, sub-priority = 1 */ NVIC_SetPriority(TIMER2_IRQn, ((0x01<<3)|0x03)); /* Enable interrupt for timer 1 */ NVIC_EnableIRQ(TIMER2_IRQn); // To start timer 1 TIM_Cmd(LPC_TIM2,ENABLE); /* preemption = 1, sub-priority = 1 */ NVIC_SetPriority(TIMER3_IRQn, ((0x01<<3)|0x04)); /* Enable interrupt for timer 1 */ NVIC_EnableIRQ(TIMER3_IRQn); // To start timer 1 TIM_Cmd(LPC_TIM3,ENABLE); while(1); return (1); }