System Timer

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

System Timer

1,401 Views
rohananand
Contributor II

I have written the following code to undergo dump debugging.The idea is to store the value of time and the PDOR register value in array.

#include "MKL46Z4.h"                    // Device header

void InitLED(void)

{

  SIM->SCGC5=SIM_SCGC5_PORTD_MASK;// Enable clock to PORTD

  PORTD->PCR[5]=256;// Set pin 5 of PORTD as GPIO

  PTD->PDDR=(1u<<5);// Set pin 5 of PORTD as OUTPUT

}

void InitSYSTICK(void)

{

  SysTick->CTRL=0;//Disable the systick timer

  SysTick->LOAD=0x00FFFFFF;//Reload it to its full value 24 bits

  SysTick->VAL=0;//Write something in current register to clear it

  SysTick->CTRL=0x00000050;//010

}

int main()

{

  unsigned long time[50];

  unsigned long data[50];

  unsigned long i=0,now=0,last=0;

  InitLED();

  InitSYSTICK();

  last=SysTick->VAL;

  while(1)

  {

 

 

    PTD->PTOR=(1u<<5);//Toggle LED

  if(i<50)

  {

  now=SysTick->VAL;

  time[i]=(last-now)&0x00FFFFFF;

  data[i]=PTD->PDOR;

  last=now;

  i++;

  }

  for(i=0;i<=800000;i++)

  {}

  }

}

Led is not blinking.Code is compiling.I dont know if i am write in using the systick timer code syntax and the line data[i]=PTD->PDOR;

Please help me.

Labels (1)
6 Replies

877 Views
Alice_Yang
NXP TechSupport
NXP TechSupport

Hello Rohan,

Please check the pin for LED, are sure is PTD5 ?

I think it isn't . Does you use FRDM-KL46 board, if yes , the pin is PTE29 .( Please pay attention to D5 on SCH is not the port PTD 5 )

pastedImage_0.png

pastedImage_1.png

So you can configuration as this :

void InitLED(void)

{

  SIM->SCGC5=SIM_SCGC5_PORTE_MASK;// Enable clock to PORTE

  PORTE->PCR[29]=(0|PORT_PCR_MUX(1));// Set pin 29 of PORTE as GPIO

  GPIOE_PDDR |= GPIO_PDDR_PDD(0x20000000);// Set pin 29 of PORTE as OUTPUT

  GPIOE_PDOR &= (uint32_t)~(uint32_t)(GPIO_PDOR_PDO(0x20000000));

}

I have test it on my side about this code , it can light the LED .

Hope it helps

Alice

0 Kudos

877 Views
rohananand
Contributor II

I am using the green led i.e on pin PTD5.

0 Kudos

877 Views
Alice_Yang
NXP TechSupport
NXP TechSupport

Hi,

OK,  it is the same with PTD5, please change to  this :

void InitLED(void)

{

  SIM->SCGC5=SIM_SCGC5_PORTD_MASK;

  PORTD->PCR[5]=(0|PORT_PCR_MUX(1));

  GPIOD_PDDR |= GPIO_PDDR_PDD(0x20);

  GPIOD_PDOR &= (uint32_t)~(uint32_t)(GPIO_PDOR_PDO(0x20));

}

I have test on my side , the LED can light !

Hope it helps

Alice

0 Kudos

877 Views
rohananand
Contributor II

I have blinked the led,I wanted to use the system timer to dump values into an array.when i am debugging it is not putting any value in the last and now  variable and also it is not stepiing over the line data[i]=PTD->PDOR. I dont know what is wrong with the code.

0 Kudos

877 Views
ndavies
Contributor V

You have coding error in your while loop. You are using the variable i in two places for two different things. One usage is corrupting the other.

The first time through your while (1) loop, you are incrementing i to 1, then your for loop is incrementing it to 800001. So i = 800001 the second time through  loop and all following times through the while loop.

Luckily since i is only <50 the first time you go through the loop, You never try to write data to time[800001] and data[800001]. This would cause a hard fault interrupt.

877 Views
rohananand
Contributor II

This is also going to HardFault_Handler.Please Help.

#include "MKL46Z4.h"                    // Device header

void InitLED(void)

{

  SIM->SCGC5=SIM_SCGC5_PORTD_MASK;// Enable clock to PORTD

  PORTD->PCR[5]=256;// Set pin 5 of PORTD as GPIO

  PTD->PDDR=(1u<<5);// Set pin 5 of PORTD as OUTPUT

}

void InitSYSTICK(void)

{

  SysTick->CTRL=0;//Disable the systick timer

  SysTick->LOAD=0x00FFFFFF;//Reload it to its full value 24 bits

  SysTick->VAL=0;//Write something in current register to clear it

  SysTick->CTRL=0x00000005;//101

}

int main()

{

  unsigned long time[50];

  unsigned long data[50];

  unsigned long i=0,now=0,last=0,j=0;

  InitLED();

  InitSYSTICK();

  last=SysTick->VAL;

  while(1)

  {

  

    PTD->PTOR=(1u<<5);//Toggle LED

  if(i<50)

  {

  now=SysTick->VAL;

  time[i]=(last-now);

  data[i]=PTD->PDOR;

  last=now;

  i++;

  }

  for(j=0;j<=800000;j++)

  {}

  }

}

0 Kudos