AnsweredAssumed Answered

Reading Input using PDIR(Port Data Input Register)

Question asked by Rohan Anand on Jun 20, 2015
Latest reply on Jun 25, 2015 by Adrian Cano

Hi,

I have a FRDM-KL46Z Board.I am using Keil uvision.I am trying to read from a switch and turn on a led.When the switch is pressed,the LED should turn on.The code is as follows:

 

#include "MKL46Z4.h"                    // Device header

void InitLED(void)

{

  SIM->SCGC5=SIM_SCGC5_PORTD_MASK;//Clock to PortD

  PORTD->PCR[5]=256;//PIN 5 of portd as GPIO

  PTD->PDDR=(1u<<5);//PIN 5 of portd as OUTPUT

}

void InitSW1(void)

{

  SIM->SCGC5=SIM_SCGC5_PORTC_MASK;//Clock to PortC

  PORTC->PCR[3]=256;//PIN 5 of portd as GPIO

  PTC->PDDR=(0u<<3);//PIN 5 of portd as INPUT

}

int main()

{

  while(1)

  {

  InitLED();

  InitSW1();

  if(PTC->PDIR==(0u<<3))//Read 0 switch pressed

  {

  PTD->PCOR=(1u<<5);//LED turns on

  }

  else if(PTC->PDIR==(1u<<3))//Read 1 switch not pressed

  {

  PTD->PSOR=(1u<<5);//Led turns off

  }

  }

}

when i download the code on the board,the led turns on without even pressing the onboard button and i dont know what is the problem with the code.

Outcomes