Hi!
I'd like to use 16bit timer to get interrupt with 1s period. I have one question. This is my code
#include "board.h"
#include <cr_section_macros.h>
#define PORT 1
#define PIN 1#define LED_PORT 0
#define LED_PIN 7#define IR_MR0INT_MASK _BIT(0)
#define MCR_MR0I_MASK _BIT(0)
#define MCR_MR0R_MASK _BIT(1)#define TCR_CEN_MASK _BIT(0)
#define TCR_CRST_MASK _BIT(1)#define CT_CLOCK_MASK _BIT(SYSCTL_CLOCK_CT16B0)
#define NVIC_CT_INT_GROUP 0
#define NVIC_CT_INT_MASK _BIT(TIMER_16_0_IRQn)
/**
* @brief Timer interrupt handler
* @return Nothing
*/
void TIMER16_0_IRQHandler(void) {// Check if match channel 1 interrupt is pending
if (LPC_TIMER16_0->IR & IR_MR0INT_MASK) {
// If yes clear interrupt flag and toogle the led state
LPC_TIMER16_0->IR = IR_MR0INT_MASK;
LPC_GPIO_PORT->NOT[LED_PORT] = _BIT(LED_PIN);
}
}/**
* @return Function should not exit
*/
int main(void) {// Generic setup
SystemCoreClockUpdate();
Board_Init();
LPC_GPIO_PORT->B[LED_PORT][LED_PIN] = 0;// Turn on the timer clock
LPC_SYSCTL->SYSAHBCLKCTRL |= CT_CLOCK_MASK;// Reset the timer
LPC_TIMER16_0->TC = 1;
LPC_TIMER16_0->TCR |= TCR_CRST_MASK;
while (LPC_TIMER16_0->TC != 0) {}
LPC_TIMER16_0->TCR &= ~(TCR_CRST_MASK);// Clear the interrupt register
LPC_TIMER16_0->IR = IR_MR0INT_MASK;// Set the prescaler value
// System core clock = 72MHz, the wanted T is 0,5ms => f = 2kHz
// 72MHz / 2KHz = 36000
LPC_TIMER16_0->PR = 36000;// Configure the counter to generate an interrupt on match and reset the TC value
LPC_TIMER16_0->MCR |= (MCR_MR0I_MASK | MCR_MR0R_MASK);// Generate interrupt each 1s
// Each TC increment takes exactly 0,5ms => 2000 * 0,5ms = 1s
LPC_TIMER16_0->MR[0] = 2000;// Enable counting
LPC_TIMER16_0->TCR |= TCR_CEN_MASK;// Configure NVIC registers - clear pending and enable IRQ
NVIC->ICPR[NVIC_CT_INT_GROUP] = NVIC_CT_INT_MASK;
NVIC->ISER[NVIC_CT_INT_GROUP] = NVIC_CT_INT_MASK;
while (1) {
__WFI();
}return 0 ;
}
This works perfect but when I change line
LPC_TIMER16_0->MR[0] = 2000;
to
LPC_TIMER16_0->MR[1] = 2000; // or MR[2]
and don't change anything else program still works. My question is why? I have configured to have interrupt when MR[0] match TC value... Does anyone know the answer?
Hi ,
After a brief review of code, I've not found something wrong yet, so I'd highly recommend you to check the values of the set of CT16B0 registers in the visual window when debugging the demo.
It's a good way to figure out the root cause of issue.
Have a great day,
TIC
-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------