How do I read or write a GPIO pin on a FRDM-K82F?

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

How do I read or write a GPIO pin on a FRDM-K82F?

9,307 Views
briankrohn
Contributor II

I am new to this board.  If I want to read a value on a pin from a button press, or write a pin high to turn on an LED (not a built in LED), how do I do that?  What about reading a value from a rotary encoder?

Labels (1)
Tags (2)
14 Replies

3,706 Views
mjbcswitzerland
Specialist V

Brian

You need to read the K82's user's manual - the section about GPIO. It explains what all registers like GPIOn_PDOR are and do.

Also you may prefer to start with Arduino code from the Teensy project: https://www.pjrc.com/store/teensy32.html

which will also contain port control interfaceing to get you started.

Another simple method to start learning is to connect the debugger to the board, view the GPIO registers and play around with them until you have worked out all control possibilities (by configuring, reading input values and controlling output state). This is the most practical of methods and usualyl results in complete understanding after just a few minutes with little need to consult the documentation and worry about coding.

Regards

Mark

0 Kudos

3,706 Views
briankrohn
Contributor II

When you say "debugger" do you mean in KDS or are you referring to uTasker?

0 Kudos

3,706 Views
mjbcswitzerland
Specialist V

Brian

I mean 'any' debugger. KDS will do once you have installed the plug-in for peripheral viewing.

I find IAR Kickstart the simplest to get started with - it is much more stream-lined than KDS and has much simpler and powerful viewing features.

If you load the uTasker reference at the link (any reference application) it also has a memory debug interface on its command line interface (in the I/O menu) which also allows you to read and write registers; in this case you will however need to know the address of the register in question. Eg. to read the port A pin 0 configuration register (PORTA_PCR0) you would use "md 40049000 l 1" to view just one long word address . To modify it "mm 40049000 l 12345678" to write 0x12345678.

Regards

Mark

0 Kudos

3,706 Views
mjbcswitzerland
Specialist V

Hi

There is no difference for the K82 to any other Kinetis. The uTasker project uses port macros which make it simple:

http://www.utasker.com/forum/index.php?topic=1875.0

FRDM-K82F - http://www.utasker.com/kinetis/FRDM-K82F.html

Basically you need to ensure the port is clocked (in SIM_SCGC5), configure its pin (and characteristics) in the PORTn_PCRx register and set the data direction in GPIOn_PDDR.

To read the state of the input read GPIOn_PDIR.

To control its output state (when output) use GPIOn_PDOR to write the complete port value, or GPIOn_PTOR to toggle individual bits, GPIOn_PSOR to set individual bits to '1' or GPIOn_PCOR to set individual bits to '0'.

Note that the pins can also be used as inputs (falling, rising edge or both , as well as level sensitive) or for triggering DMA (on programmable edge).

Regards

Mark

3,706 Views
briankrohn
Contributor II

So, I've seen those links.  As I mentioned, I am new to these.  My experience is with Arduino.  What you said, makes some sense, but there are no examples of how to do this.  All the documentation reads exactly like your response.  I don't know what a GPIOn_PDOR is.  I get GPIO and the "n" being a number, but "PDOR"?  What is that a reference to?  "PTOR", "PCOR"?

0 Kudos

3,706 Views
kerryzhou
NXP TechSupport
NXP TechSupport

Hi Brian Krohn,

      Actually, we have the according sample code for FRDM-K82F.

      Please download the KSDK2.0_FRDM-K82F from this link:Welcome to Kinetis Expert | Kinetis Expert

      Choose the FRDM-K82 board, and generate the KSDK2.0 package and download it.

      After you download it, you will find the code in path:SDK_2.0_FRDM-K82F\boards\frdmk82f\driver_examples\gpio\input_interrupt

      The example uses the software button to control/toggle the LED.

Wish it helps you!

If you still have question, please contact me!


Have a great day,
Jingjing

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

0 Kudos

3,706 Views
briankrohn
Contributor II

Hi Jingjing,

This is helpful, but it still uses the on-board LED and button.  I want to know how to use the rest of the GPIO pins.  I will take a look at this example along with other info in this discussion and see if I can come up with something.  Thanks.

0 Kudos

3,706 Views
kerryzhou
NXP TechSupport
NXP TechSupport

Hi Brian,

    If you want to use the rest of the GPIO pins, just refer to the on board LED and button example which I give you, besides, associate with the K82 reference manual.

chapter 3.4 Interrupt channel assignments

Chapter 13 Port Control and Interrupts (PORT)

Chapter 14 System Integration Module (SIM)

Chapter 64 General-Purpose Input/Output (GPIO)

Please read it carefully!

If you still have question, please contact me!


Have a great day,
Jingjing

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

0 Kudos

3,706 Views
briankrohn
Contributor II

These documents are tremendously over-complicated.  I've read through them a couple times, but am no closer to figuring this board out.  If I have a button wired to input on pin 2 ("2-PTB16" on the diagram), and and LED properly wired up to pin 6 ("6-PTC12"), how to I read pin 2 and set 6 high?  This is something other platforms can do with a couple lines of code.  Referring to the examples in the SDL that use the built in button and LED don't help, because they are not referenced the same way.

0 Kudos

3,706 Views
kerryzhou
NXP TechSupport
NXP TechSupport

Hi Brian,

  Now tell you the configuration step, actually, it is really very simple, you also can find it in the sample code which I give you .

1. Open PORTC, PORTB gate clock in SIM_SCGC5

  Set bit SIM_SCGC5[PTC], SIM_SCGC5[PTB]

2.  Configure PTC12 as gpio output, PTB16 as input interrupt pin.

  PORTB_PCR16[IRQC] = 1010, ISF flag and Interrupt on falling-edge.

  PORTB_PCR16[MUX] = 001, GPIO function

  PORTB_PCR16[PE] = 1, internal pullup or pulldown resistor enable

  PORTB_PCR16[PS] = 1, internal pullup resistor is enabled

  PORTC_PCR12[MUX] = 001, GPIO function

  PORTC_PCR12[PE] = 1, internal pullup or pulldown resistor enable

  PORTC_PCR12[PS] = 1, internal pullup resistor is enabled

3. Enable IRQ

  IRQ is used to the switch input interrupt.

EnableIRQ(PORTB_IRQn);

4. Write the interrupt service code

void BOARD_SW_IRQ_HANDLER(void)

{

    /* Clear external interrupt flag. */

    GPIO_ClearPinsInterruptFlags(BOARD_SW_GPIO, 1U << BOARD_SW_GPIO_PIN);

    /* Change state of button. */

    g_ButtonPress = true;

    /* Toggle LED. */

    GPIO_TogglePinsOutput(BOARD_LED_GPIO, 1U << BOARD_LED_GPIO_PIN);

}

You can try to modify the sample code to meet your board's demand.

Please modify the board.h, the switch interface, and the led interface.

Please try to modify it, and problem, just let me know!


Have a great day,
Jingjing

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

0 Kudos

3,706 Views
briankrohn
Contributor II

Thanks to you both.  Between your responses and some sample code, I was able to figure it out.

One further question: I can act on a button press because an interrupt is fired.  If I had a sensor that I wanted to poll, say every 15 seconds, how would I do that?  Would I just trigger the interrupt in code, or is there another method?

0 Kudos

3,706 Views
kerryzhou
NXP TechSupport
NXP TechSupport

Hi Brain,

    If you want to periodically control teh external sensor, you can use the Timer, Eg, pit, lptmr, ftm,etc.

    For details, it is not the question about the GPIO, please create a new timer post, thank you!


Have a great day,
Jingjing

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

0 Kudos

3,706 Views
briankrohn
Contributor II

I am betting the answer to my question has something to do with GPIO_ReadPinInput.  I'll have to play around with it more.

0 Kudos

3,706 Views
mjbcswitzerland
Specialist V

Hi

Learn by doing is the fastest way to get over this (as I mentioned, connect the debugger and just try modifying some port registers). The GPIO operation is fairly simple - there are tons of other much more complicated peripherals in the device so you will need to get over this first hurdle and get up to speed to be able to make progress.

Or simply use the uTasker project which does this and removes complications:

Eg. to configure 3 pins on port A with a certain characteristic and drive initially with a value (001):

_CONFIG_DRIVE_PORT_OUTPUT_VALUE(A, (PORTA_BIT4 | PORTA_BIT7 | PORTA_BIT23), (PORTA_BIT4), (PORT_SRE_SLOW | PORT_DSE_HIGH));

or to define 4 pins as input and read their vaue:

_CONFIG_PORT_INPUT(B, (PORTB_BIT0 | PORTB_BIT1 | PORTB_BIT3 | PORTB_BIT4), PORT_PS_UP_ENABLE);

and to read the masked input value

input = _READ_PORT_MASK(B, (PORTB_BIT0 | PORTB_BIT1 | PORTB_BIT3 | PORTB_BIT4));

Nothing else is needed (all clocks are automatially enabled - no great understanding of any internal working required, compatible for all Kinetis families). Simulated in the uTasker Kinetis simulator so that all operation can be checked without need for HW.

Regards

Mark

0 Kudos