Port configuration

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

Port configuration

Jump to solution
9,940 Views
eliezeroc
Contributor III

Hello friends. I'm programming my first kinets a MKL05, and I am unable to configure the ports to work as input or output.

I've read and re-read the datasheet but could not understand this very confusing.

Example of what I want to do is set the PTB8 pin as output to light an LED. I will be very grateful for the help.

thank you.

1 Solution
4,755 Views
EarlOrlando
Senior Contributor II

Hello Eliezer,

Configure a GPIO pin is very easy. You must follow the next steps:

1. As any other peripheral, enable the clock gating for the LED's PORT. This can be seen in the Chapter 12 - System Integration Module (SIM) in the Reference Manual.

    SIM_SCGC5 |= SIM_SCGC5_PORTB_MASK;

2. Select the pin function in the Signal Multiplexing module. This can be seen in the Chapter 10 - Signal Multiplexing and Signal Descriptions (Pinout table) in the Reference manual. In this case, for the GPIO function the alternative 1 has to be chosen.

     PORTB_PCR10 |= (PORT_PCR_MUX(1));    //Blue LED in the FRDM-KL05Z.

     PORTB_PCR11 |= (PORT_PCR_MUX(1));    //PORTB11 as input pin.

If the configured pin is needed as input you must configure the pull up or pull down resistor in this register (PE and PS bits) as well as the interrupts (IRQC bits). This can be seen in the Chapter 11 - Port control and interrupts (PORT) in the Reference Manual.

3. Select the pin direction (input or output). There is a 32-bit register for each port. If the n bit is set then the n pin for that port will be configured as output but if it is cleared it will be configured as input. This can be seen in the Chapter 38 - General-Purpose Input/Output (GPIO) in the Reference Manual.

     GPIOB_PDDR |= (1 << 10);    //Output pin.

     GPIOB_PDDR &= ~(1 << 11);   //Input pin.

4. If you want to change the state of the output pin you need to set some of the next registers:

        * GPIOx_PSOR - Set the pin.

        * GPIOx_PCOR - Clear the pin.

        * GPIOx_PTOR - Toggle the pin.

For example to set the blue LED in the FRDM-KL05Z.

    GPIOB_PSOR |= (1 << 10);

To clear the blue LED in the FRDM-KL05Z.

    GPIOB_PCOR |= (1 << 10);

To toggle the blue LED in the FRDM-KL05Z.

    GPIOB_PTOR |= (1 << 10);

If you want to read the state of the input pin you need to read the value of the next register. If the next expression is true, the input pin is in high state, otherwise it is in low state.

(GPIOB_PDIR & (1 << 11))

Full code:

     //    1. As any other peripheral, enable the clock gating for the LED's PORT. This can be seen in the Chapter 12 - System Integration Module (SIM) in the Reference Manual.

    SIM_SCGC5 |= SIM_SCGC5_PORTB_MASK;

    //    2. Select the pin function in the Signal Multiplexing module. This can be seen in the Chapter 10 - Signal Multiplexing and Signal Descriptions (Pinout table) in the Reference manual.

    // In this case, for the GPIO function the alternative 1 has to be chosen.

    PORTB_PCR10 |= (PORT_PCR_MUX(1));    //Blue LED in the FRDM-KL05Z.

    PORTB_PCR11 |= (PORT_PCR_MUX(1));    //PORTB11 as input pin.

    //If the configured pin is needed as input you must configure the pull up or pull down resistor in this register (PE and PS bits) as well as the interrupts (IRQC bits). This can be seen in the Chapter 11 - Port control and interrupts (PORT) in the Reference Manual.

    // 3. Select the pin direction (input or output). There is a 32-bit register for each port. If the n bit is set then the n pin for that port will be configured as output but if it is cleared it will be configured as input.

    //This can be seen in the Chapter 38 - General-Purpose Input/Output (GPIO) in the Reference Manual.

    GPIOB_PDDR |= (1 << 10);    //Output pin.

    GPIOB_PDDR &= ~(1 << 11);   //Input pin.

    //4. If you want to change the state of the output pin you need to set some of the next registers:

    //    * GPIOx_PSOR - Set the pin.

    //    * GPIOx_PCOR - Clear the pin.

    //    * GPIOx_PTOR - Toggle the pin.

    //For example to set the blue LED in the FRDM-KL05Z.

    GPIOB_PSOR |= (1 << 10);

    // To clear the blue LED in the FRDM-KL05Z.

    GPIOB_PCOR |= (1 << 10);

    // To toggle the blue LED in the FRDM-KL05Z.

    GPIOB_PTOR |= (1 << 10);

    //If you want to read the state of the input pin you need to read the value of the next register.

    //If the next expression is true, the input pin is in high state, otherwise it is in low state.

    if(GPIOB_PDIR & (1 << 11))

    {

         //Input pin is in high state.

    }

    else

    {

         //Input pin is in low state.

    }

I hope this helps.

Best regards,

Earl Orlando.

/* If this post answers your question please click the Correct Answer button. */

View solution in original post

2 Replies
4,755 Views
sruthikilari
Contributor I

Hi, I am trying to configure GPIO ports to read the data from a rain drop sensor, using FRDMKEAZ128. Will the process be similar as mentioned above for KLZ05?

0 Kudos
Reply
4,756 Views
EarlOrlando
Senior Contributor II

Hello Eliezer,

Configure a GPIO pin is very easy. You must follow the next steps:

1. As any other peripheral, enable the clock gating for the LED's PORT. This can be seen in the Chapter 12 - System Integration Module (SIM) in the Reference Manual.

    SIM_SCGC5 |= SIM_SCGC5_PORTB_MASK;

2. Select the pin function in the Signal Multiplexing module. This can be seen in the Chapter 10 - Signal Multiplexing and Signal Descriptions (Pinout table) in the Reference manual. In this case, for the GPIO function the alternative 1 has to be chosen.

     PORTB_PCR10 |= (PORT_PCR_MUX(1));    //Blue LED in the FRDM-KL05Z.

     PORTB_PCR11 |= (PORT_PCR_MUX(1));    //PORTB11 as input pin.

If the configured pin is needed as input you must configure the pull up or pull down resistor in this register (PE and PS bits) as well as the interrupts (IRQC bits). This can be seen in the Chapter 11 - Port control and interrupts (PORT) in the Reference Manual.

3. Select the pin direction (input or output). There is a 32-bit register for each port. If the n bit is set then the n pin for that port will be configured as output but if it is cleared it will be configured as input. This can be seen in the Chapter 38 - General-Purpose Input/Output (GPIO) in the Reference Manual.

     GPIOB_PDDR |= (1 << 10);    //Output pin.

     GPIOB_PDDR &= ~(1 << 11);   //Input pin.

4. If you want to change the state of the output pin you need to set some of the next registers:

        * GPIOx_PSOR - Set the pin.

        * GPIOx_PCOR - Clear the pin.

        * GPIOx_PTOR - Toggle the pin.

For example to set the blue LED in the FRDM-KL05Z.

    GPIOB_PSOR |= (1 << 10);

To clear the blue LED in the FRDM-KL05Z.

    GPIOB_PCOR |= (1 << 10);

To toggle the blue LED in the FRDM-KL05Z.

    GPIOB_PTOR |= (1 << 10);

If you want to read the state of the input pin you need to read the value of the next register. If the next expression is true, the input pin is in high state, otherwise it is in low state.

(GPIOB_PDIR & (1 << 11))

Full code:

     //    1. As any other peripheral, enable the clock gating for the LED's PORT. This can be seen in the Chapter 12 - System Integration Module (SIM) in the Reference Manual.

    SIM_SCGC5 |= SIM_SCGC5_PORTB_MASK;

    //    2. Select the pin function in the Signal Multiplexing module. This can be seen in the Chapter 10 - Signal Multiplexing and Signal Descriptions (Pinout table) in the Reference manual.

    // In this case, for the GPIO function the alternative 1 has to be chosen.

    PORTB_PCR10 |= (PORT_PCR_MUX(1));    //Blue LED in the FRDM-KL05Z.

    PORTB_PCR11 |= (PORT_PCR_MUX(1));    //PORTB11 as input pin.

    //If the configured pin is needed as input you must configure the pull up or pull down resistor in this register (PE and PS bits) as well as the interrupts (IRQC bits). This can be seen in the Chapter 11 - Port control and interrupts (PORT) in the Reference Manual.

    // 3. Select the pin direction (input or output). There is a 32-bit register for each port. If the n bit is set then the n pin for that port will be configured as output but if it is cleared it will be configured as input.

    //This can be seen in the Chapter 38 - General-Purpose Input/Output (GPIO) in the Reference Manual.

    GPIOB_PDDR |= (1 << 10);    //Output pin.

    GPIOB_PDDR &= ~(1 << 11);   //Input pin.

    //4. If you want to change the state of the output pin you need to set some of the next registers:

    //    * GPIOx_PSOR - Set the pin.

    //    * GPIOx_PCOR - Clear the pin.

    //    * GPIOx_PTOR - Toggle the pin.

    //For example to set the blue LED in the FRDM-KL05Z.

    GPIOB_PSOR |= (1 << 10);

    // To clear the blue LED in the FRDM-KL05Z.

    GPIOB_PCOR |= (1 << 10);

    // To toggle the blue LED in the FRDM-KL05Z.

    GPIOB_PTOR |= (1 << 10);

    //If you want to read the state of the input pin you need to read the value of the next register.

    //If the next expression is true, the input pin is in high state, otherwise it is in low state.

    if(GPIOB_PDIR & (1 << 11))

    {

         //Input pin is in high state.

    }

    else

    {

         //Input pin is in low state.

    }

I hope this helps.

Best regards,

Earl Orlando.

/* If this post answers your question please click the Correct Answer button. */