Declare & read pin without Processor Expert

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

Declare & read pin without Processor Expert

Jump to solution
4,172 Views
joelesco
Contributor III

Hi,

I'm a newbie with freescale processor ( and english laguage also). I have a TWR-K20 and I've already code some little things but with PE. I would like now to enter in the detail of the µC. And my problem is really simple I suppose !

I understand how to program the port register ( PORTx_PCR, PORTx_GPCLR, PORTx_GPCHR. ) But I don't know how to declare and read a port. My question seems to be really stupid but I'm lost ! For exemple when i was working with microchip if I wanted to declare  a pin I writen              # define  PIN1  PORTAbit.RA1.

If someone has an exemple it will be nice !

Thanks a lot  !

Labels (1)
Tags (2)
1 Solution
1,037 Views
Jorge_Gonzalez
NXP Employee
NXP Employee

Hello Jeremie:

When you create a new bareboard project, the included MCU header file (e.g. MK20D50.h) already contains helpful macros to manage your pins. Please check the video in the next link:

Video Link : 1460

So, the definitions you'd want to create would be something like:

#define PIN0    (1<<0)

#define PIN1    (1<<1)

//...

#define PIN7    (1<<7)

//...

// Then you can use those definitions as next examples (e.g. port C):

// Enable the clock for the desired port:

SIM_SCGC5 |= SIM_SCGC5_PORTC_MASK;

// Configure the pins as GPIO, using the mux macro:

PORTC_PCR2 = PORT_PCR_MUX(1);    // Pin function alternative '1' is GPIO

PORTC_PCR7 = PORT_PCR_MUX(1);

// To configure pin 7 as output and pin 2 as input:

GPIOC_PDDR |= PIN7;

GPIOC_PDDR &= ~PIN2;

// To set pin 7:

GPIOC_PSOR |= PIN7;

//or

GPIOC_PDOR |= PIN7;

// To clear pin 7:

GPIOC_PCOR |= PIN7;

//or

GPIOC_PDOR &= ~PIN7;

// For reading pin 2:

temp_var = GPIOC_PDIR & PIN2;

You can also find non-processor expert based examples in the next links:

TWR-K20D50M (KINETIS_50MHZ_SC):

TWR-K20D50M Product Summary Page

TWR-K20D72M (TWR-K20D72M_LAB):

TWR-K20D72M Product Summary Page

Hope this is useful :smileyhappy:

/Jorge Gonzalez

View solution in original post

4 Replies
1,037 Views
nasreenshaikh
Contributor III

Hi Jeremie,

I defined a single port pin for HB_LED in the following way using macros.

You can try the same.

#define GPIO_PIN_MASK                        (31U)

#define GPIO_PIN_OUT(dir_reg, pin_no)       (dir_reg |= (((1U) << (pin_no & GPIO_PIN_MASK))))

#define GPIO_PIN_IN(dir_reg, pin_no)        (dir_reg &= (~(((1U) << (pin_no & GPIO_PIN_MASK)))))

#define GPIO_PIN_HIGH(dir_reg, pin_no)      (dir_reg |= (((1U) << (pin_no & GPIO_PIN_MASK))))

#define GPIO_PIN_LOW(dir_reg, pin_no)          (dir_reg &= (~(((1U) << (pin_no & GPIO_PIN_MASK)))))

//----------------------- HEART BEAT LED----------------------------------------

#define HB_LED_DIR_OUT            GPIO_PIN_OUT(GPIOA_PDDR, PIN_8)

#define HB_LED_OFF                GPIO_PIN_HIGH(GPIOA_PDOR, PIN_8)

#define HB_LED_ON                GPIO_PIN_LOW(GPIOA_PDOR, PIN_8)

#define HB_LED_TOGGLE            GPIO_PIN_HIGH(GPIOA_PTOR, PIN_8)

Regards ,

Nasreen

0 Kudos
1,037 Views
LuisCasado
NXP Employee
NXP Employee

Hi,

Thomas is right, the header file contains all the port and register definitions you need.

I am not sure if your TWR is K20D50 or K20D72, but you have the SC (Sample code) for both of them.

For example, for 50MHz:

http://www.freescale.com/files/32bit/software/KINETIS_50MHZ_SC.zip?WT_TYPE=Lab and Test Software&WT_VENDOR=FREESCALE&WT_FILE_FORMAT=zip&WT_ASSET=Downloads

You will find examples of managing LED's and switches

Luis

1,037 Views
thomasgrieger
Contributor III

Hi,

in your project a header file named "MK20F12.h" should be present which contains a mass of macro definitions.

The pin values can be read/written by using the "GPIO" macros which access the corresponding register which are described in the reference manual.

Best regards,

Thomas

1,038 Views
Jorge_Gonzalez
NXP Employee
NXP Employee

Hello Jeremie:

When you create a new bareboard project, the included MCU header file (e.g. MK20D50.h) already contains helpful macros to manage your pins. Please check the video in the next link:

Video Link : 1460

So, the definitions you'd want to create would be something like:

#define PIN0    (1<<0)

#define PIN1    (1<<1)

//...

#define PIN7    (1<<7)

//...

// Then you can use those definitions as next examples (e.g. port C):

// Enable the clock for the desired port:

SIM_SCGC5 |= SIM_SCGC5_PORTC_MASK;

// Configure the pins as GPIO, using the mux macro:

PORTC_PCR2 = PORT_PCR_MUX(1);    // Pin function alternative '1' is GPIO

PORTC_PCR7 = PORT_PCR_MUX(1);

// To configure pin 7 as output and pin 2 as input:

GPIOC_PDDR |= PIN7;

GPIOC_PDDR &= ~PIN2;

// To set pin 7:

GPIOC_PSOR |= PIN7;

//or

GPIOC_PDOR |= PIN7;

// To clear pin 7:

GPIOC_PCOR |= PIN7;

//or

GPIOC_PDOR &= ~PIN7;

// For reading pin 2:

temp_var = GPIOC_PDIR & PIN2;

You can also find non-processor expert based examples in the next links:

TWR-K20D50M (KINETIS_50MHZ_SC):

TWR-K20D50M Product Summary Page

TWR-K20D72M (TWR-K20D72M_LAB):

TWR-K20D72M Product Summary Page

Hope this is useful :smileyhappy:

/Jorge Gonzalez