Reputation: 1
I am new to ARM programming .I am using K20 MK20DX256 MCU with 72MHz Clock to toggle an LED every 1second with Periodic Interrupt Timer 0. The code compiles Fine but LED does not toggle .I found out that configuring Timer together with LED does not work while LED alone does work I have written the following code in keil Uvision
#include "MK20D7.h"
#include <stdio.h>
int main(void)
{
SystemInit();
NVIC_EnableIRQ(PIT0_IRQn); //Enable Timer Interrupts
//Configuring Timer 1
PIT->MCR=0x00;
PIT->CHANNEL[0].LDVAL=13888;
PIT->CHANNEL[0].TCTRL=0x3;
//Configure LED
SIM->SCGC5 = (1UL << 11); /* Enable Clock to Port C */
PORTC->PCR[5] = (1UL << 8); /* Pin is GPIO */
PTC->PDDR = (1u<<5);
PTC->PSOR = (1u<<5); //Set PTC5 = 1, turns LED on
while(1){
if(PIT->CHANNEL[0].TFLG ==1)
{
PIT->CHANNEL[0].TFLG =0;
PIT->CHANNEL[0].LDVAL=13888;
if(PTC->PSOR!=(1u<<5))
{
PTC->PSOR = (1u<<5); //Set PTC5 = 1, turns LED on
}
else
{
PTC->PCOR = (1u<<5); //Set PTC5 = 1, turns LED off
}
}
}
}
Can anyone help me in finding out what is wrong with this code? I found out that none of the registers change their value during debugging enter image description here
Upvotes: 0
Views: 861
Reputation: 4770
A few options.
Firstly, you're requesting an interrupt from the PIT, but haven't installed an interrupt handler. Are you sure the default interrupt handler that Keil installs has no side effects, e.g. halting or rebooting?
PIT->CHANNEL[0].TCTRL=0x3;
Secondly, and more importantly, you're trying to clear the timer interrupt flag by writing a 0 to it. The data sheet says you need to write a 1 instead:
PIT->CHANNEL[0].TFLG =0;
The result will be that after the first interrupt triggers, the flag will remain up forever and you'll keep re-starting the PIT forever by writing 13888 into it. It never has a chance to count down again.
Upvotes: 1