I am trying to store the contents of Port Data Input Register into an array data[i].
code is as follows:
#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 reset it
SysTick->CTRL=0x00000005;//101//1=bus clock,0= Disable interrupt,1= enable systick timer
}
int main()
{
volatile unsigned long time[50];
volatile 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->PDIR;
last=now;
i++;
}
for(j=0;j<=800000;j++)
{}
}
}
When I write data[i]=PTD->PDIR; and debug, it goes to HardFault_Handler. I don't know what is the problem with this statement.Please Help?
Hi
Presumably there is something wrong with PTD->PDIR. I would step the code in disassemble mode and see which addresses are being accessed.
Other possibilities:
SIM->SCGC5=SIM_SCGC5_PORTD_MASK;// Enable clock to PORTD
looks risky.
It is OK to enable the port but if you happen to have the same style somewhere (that is not visible in what you have shown eg.
SIM->SCGC5=SIM_SCGC5_PORTC_MASK;// Enable clock to PORTC
it will be disabing the clock to Port D and then any accesses will hard fault).
The usual style is
SIM->SCGC5|=SIM_SCGC5_PORTD_MASK;// Enable clock to PORTD
Also view the Port D registers in the debugger - if any can't be accessed it will also be displayed there.
Regards
Mark
Kinetis: http://www.utasker.com/kinetis.html
KL46: http://www.utasker.com/kinetis/FRDM-KL46Z.html / http://www.utasker.com/kinetis/TWR-KL46Z48M.html
For the complete "out-of-the-box" Kinetis experience and faster time to market
:smileyinfo: Out-of-the-box support for 46 Kinetis boards and 10 IDEs (460 combinations from a single code source with no porting required)