GPIO Config in K21D50

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

GPIO Config in K21D50

Jump to solution
1,756 Views
jitheshar
Contributor II

Hi all,

     I'm new to Freescale microcontrollers. I'm now using TWR-K21D50 board for one of my project. Initially i'm triying to control GPIO pins. I have to glow the D5 led connected to the PTD4 pin. I have put my code below. But not working.

void main(void)

{

    SIM_SCGC5 |= SIM_SCGC5_PORTD_MASK; // enabled clock for the portd

    PORTD_PCR4 = PORT_PCR_MUX(1); //selected gpio functionality for pin

    GPIOD_PDDR = GPIO_PDDR_PDD(4); //put as output pin configuration

    GPIOD_PCOR = GPIO_PCOR_PTCO(4); // to make my pin low to glow the led (anode connected to 3.3v and cathode to GPIO Pin)

 

    while(1){

     

    }

}

Please help me to diagnose the problem in my code.

Labels (1)
Tags (1)
0 Kudos
Reply
1 Solution
902 Views
jitheshar
Contributor II

moving the values directly to the registers without using the defined macros does the work for me.

may be macros are for different purposes.

Also in the headder i can find mask and shift values. what these shift values are used for??

View solution in original post

0 Kudos
Reply
3 Replies
903 Views
jitheshar
Contributor II

moving the values directly to the registers without using the defined macros does the work for me.

may be macros are for different purposes.

Also in the headder i can find mask and shift values. what these shift values are used for??

0 Kudos
Reply
902 Views
jitheshar
Contributor II

thanks for your reply.

Changing GPIOD_PDDR = GPIO_PDDR_PDD(4); to GPIOD_PDDR = GPIO_PDDR_PDD(0x10); and GPIOD_PCOR = GPIO_PCOR_PTCO(4); to GPIOD_PCOR = 0x00000010u; worked for me..

Looking into register section in debugging helped me.

0 Kudos
Reply
902 Views
carlos_chavez
NXP Employee
NXP Employee

Hi Jitesh,


I know how it is when working on new microcontrollers.  I am not at my desk right now, but here is an example that I was able to quickly find from the sample code.


I hope that it helps.


BR,

Carlos


/*

* File:        gpio.c

* Purpose:        LED and Switch Example

*

*                      Configures GPIO for the LED and push buttons on the TWR-K21D50M

*                      Blue LED - On

*                      Green LED - Toggles on/off

*                      Orange LED - On if SW2 pressed

*                      Yellow LED - On if SW1 pressed

*

*                      Also configures push buttons for falling IRQ's. ISR

*                        configured in vector table in isr.h

*

*/

#define GPIO_PIN_MASK            0x1Fu

#define GPIO_PIN(x)              (((1)<<(x & GPIO_PIN_MASK)))

#include "common.h"

#include "lptmr.h"

//Function declarations

void portd_isr(void);

void porte_isr(void);

void init_gpio(void);

/********************************************************************/

int main (void)

{

  printf("TWR-K21D50M GPIO Example!\n");

  /* Turn on all port clocks */

  SIM_SCGC5 = SIM_SCGC5_PORTA_MASK | SIM_SCGC5_PORTB_MASK | SIM_SCGC5_PORTC_MASK | SIM_SCGC5_PORTD_MASK | SIM_SCGC5_PORTE_MASK;

  /* Enable GPIOC interrupts in NVIC */

  enable_irq(61); //GPIOC Vector is 77. IRQ# is 77-16=61

 

  /* Initialize GPIO on TWR-K70F120M */

  init_gpio();

  while(1)

  {

    //Set PTD4 to 0 (turns on green LED)

    GPIOD_PDOR&=~GPIO_PDOR_PDO(GPIO_PIN(4));

    //Toggle the yellow LED on PTD5

    GPIOD_PTOR|=GPIO_PDOR_PDO(GPIO_PIN(5));

    //Look at status of SW1 on PTC6

    if((GPIOC_PDIR & GPIO_PDIR_PDI(GPIO_PIN(6)))==0) //if pressed...

    {

      GPIOD_PDOR&=~GPIO_PDOR_PDO(GPIO_PIN(6)); //Set PTA28 to 0 (turns on orange LED)

    }

    else //else if SW1 not pressed...

    {

      GPIOD_PDOR|=GPIO_PDOR_PDO(GPIO_PIN(6)); //Set PTA28 to 1 (turns off orange LED)

    }

    //Look at status of SW2 on PTE26

    if((GPIOC_PDIR & GPIO_PDIR_PDI(GPIO_PIN(7)))==0)  //If pressed...

    {

      GPIOD_PDOR&=~GPIO_PIN(7); //Set PTA11 to 0 (turns on blue LED)

    }

    else //else if SW2 not pressed...

    {

      GPIOD_PDOR|=GPIO_PIN(7); //Set PTA11 to 1 ((turns off blue LED)

    }

    time_delay_ms(500);

  }

}

/*

* Initialize GPIO for Tower switches and LED's

*

*   PTC6 - SW3

*   PTC7 - SW2

*

*   PTD4 - Green (D5)

*   PTD5 - Yellow (D6)

*   PTD6 - Orange (D8)

*   PTD7 - Blue (D9)

*

*/

void init_gpio()

{

  //Set PTC6 and PTC7 (connected to SW1 and SW2) for GPIO functionality, falling IRQ,

  //   and to use internal pull-ups. (pin defaults to input state)

  PORTC_PCR6=PORT_PCR_MUX(1)|PORT_PCR_IRQC(0xA)|PORT_PCR_PE_MASK|PORT_PCR_PS_MASK;

  PORTC_PCR7=PORT_PCR_MUX(1)|PORT_PCR_IRQC(0xA)|PORT_PCR_PE_MASK|PORT_PCR_PS_MASK;

  //Set PTD4, PTD5, PTD6, and PTD7 (connected to LED's) for GPIO functionality

  PORTD_PCR4=(0|PORT_PCR_MUX(1));

  PORTD_PCR5=(0|PORT_PCR_MUX(1));

  PORTD_PCR6=(0|PORT_PCR_MUX(1));

  PORTD_PCR7=(0|PORT_PCR_MUX(1));

  //Change PTD4, PTD5, PTD6, and PTD7 to outputs

  GPIOD_PDDR=GPIO_PDDR_PDD(GPIO_PIN(4) | GPIO_PIN(5) | GPIO_PIN(6) | GPIO_PIN(7) );   

}

/********************************************************************/

/*

* ISR for PORTC interrupts

*/

void portc_isr(void)

{

  if(PORTC_ISFR & GPIO_PIN(6))

  {

    printf("SW3 Pressed\n");

  }

  if(PORTC_ISFR & GPIO_PIN(7))

  {

    printf("SW2 Pressed\n");

  } 

  PORTC_ISFR=0xFFFFFFFF;  //Clear Port A ISR flags

}

0 Kudos
Reply