0

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

huzi
  • 1
  • 1

1 Answers1

1

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.

Tarmo
  • 3,728
  • 1
  • 8
  • 25
  • I am new to interrupts .Can you please elaborate hoe to install handlers? – huzi Jan 13 '21 at 14:24
  • Sorry, no idea how Keil does it. More importantly, you don't actually use interrupts, so better to disable them altogether. – Tarmo Jan 13 '21 at 14:48
  • I am using Timer interrupt which sets the flag when timeout occurs and the led is toggled based on that.how do you say so? – huzi Jan 13 '21 at 15:23
  • @huzi you can certainly poll for the interrupt status and not actually have a handler but you need to not enable the interrupt to the core. fix the items stated in this answer, get rid of the nvic stuff, and go from there. Interrupt handlers are an advanced topic, you need to work on basics first. – old_timer Jan 13 '21 at 15:35
  • `NVIC_EnableIRQ(PIT0_IRQn); //Enable Timer Interrupts` is one line, where you request enabling the interrupt from PIT0. In `PIT->CHANNEL[0].TCTRL=0x3;` you tell the PIT to raise an interrupt (one of those two bits, I've forgotten which one). Have a look at the reference manual for this micro for the details. – Tarmo Jan 13 '21 at 16:16