Reading Input using PDIR(Port Data Input Register)

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

Reading Input using PDIR(Port Data Input Register)

Jump to solution
7,124 Views
rohananand
Contributor II

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.

0 Kudos
1 Solution
3,346 Views
adriancano
NXP Employee
NXP Employee

Hi,

You need to enable the internal Pull-up for the pin since in the Board are not externally pulled-up. To enable the pull-up you need to write 1 to the PS and PE fields in the PCR register.

pcr.jpg

Hope this information can help you

Best Regards,
Adrian Sanchez Cano
-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------

View solution in original post

5 Replies
3,347 Views
adriancano
NXP Employee
NXP Employee

Hi,

You need to enable the internal Pull-up for the pin since in the Board are not externally pulled-up. To enable the pull-up you need to write 1 to the PS and PE fields in the PCR register.

pcr.jpg

Hope this information can help you

Best Regards,
Adrian Sanchez Cano
-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------

3,346 Views
rohananand
Contributor II

Thanks,This worked.

Can I make any pin as a pullup / pulldown?

Can this switch pin PTC3 become pulldown?

0 Kudos
3,346 Views
adriancano
NXP Employee
NXP Employee

Hi,

You can select a pull-up or pull-down internal resistor for the pins. There are some pins, like the SDA and SCL for I2C functionality, that are configured as true open drain when selected; in this case the pull up or down internal resistor has no effect on them.

You need to consider also that according to the Datasheet the internal pull-up resistor has a value of 20 to 50 kOhms, it will probably will not be enough for some applications but, like in this case, will help you to ensure a constant value in the pin.

You can pull-down the PTC3 by selecting in the PORTx_PCRn register PE= 1 and PS= 0.

Hope this information can help you

Best Regards,
Adrian Sanchez Cano
-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------

3,346 Views
rohananand
Contributor II

I am trying to make the PTC3(Switch1)pin as pull-down but it is not working.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|2;//PIN 3 of portc as GPIO and pullup/pulldown+pulldown

  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 above code to the board the led switches on but when i press the switch it does not switch off.The schematic says that the switch is of pull-up type ,can i really use it as a pull-down?

0 Kudos
3,346 Views
adriancano
NXP Employee
NXP Employee

Hi,

According to the schematic the Button is connected to GND, this means that any time you pressed the value it will drive is 0 (zero). The same way with the internal pull-down the value of the pin is 0 and when you press the button the value will not change.

Using the pull-up the value of the pin is 1 and when you press the button the value will be 0 because the button will short to GND. This means you cannot use pull-down for the button.

Hope this information can help you

Best Regards,
Adrian Sanchez Cano
-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------

0 Kudos