NXP Freescale Kinetis KEA128 Study Notes 3--GPIO Module (I)

Publisher:SereneHarmonyLatest update time:2021-04-06 Source: eefocusKeywords:Freescale Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

{

//Hope for input

BCLR(bit, GPIO_PDDR_REG(gpio_ptr));

}

}

 

//===========================================================================

//Function name: gpio_set

//Function returns: None

//Parameter description: port_pin: port number | pin number (e.g.: PORTB|(5) means pin 5 of port B)

// state: initial state of the pin (0 = low level, 1 = high level)

//Function summary: Set the pin state to low or high

//===========================================================================

void gpio_set(uint_16 port_pin, uint_8 state)

{

//Local variable declaration

GPIO_MemMapPtr gpio_ptr; //Declare port_ptr as a GPIO structure type pointer

gpio_ptr_bit(port_pin,&gpio_ptr,&bit); //Calculate the base address and the offset of the pin in the register

//Determine whether the pin is output 1 or 0 based on the parameter state

if (1==state) 

{

BSET(bit,gpio_ptr->PDOR); //The corresponding position is 1

} else {

BCLR(bit,gpio_ptr->PDOR);//The corresponding position is 0

}

}

 

//===========================================================================

//Function name: gpio_get

//Function returns: the state of the specified pin (1 or 0)

//Parameter description: port_pin: port number | pin number (e.g.: PORTB|(5) means pin 5 of port B)

//Function summary: Get the status of the specified pin (1 or 0)

//===========================================================================

uint_8 gpio_get(uint_16 port_pin)

{

//Local variable declaration

GPIO_MemMapPtr gpio_ptr; //Declare port_ptr as a GPIO structure type pointer

gpio_ptr_bit(port_pin,&gpio_ptr,&bit); //Calculate the base address and the offset of the pin in the register

// Return the status of the pin

return ((BGET(bit,gpio_ptr->PDIR))>=1 ? 1:0);

}

 

//===========================================================================

//Function name: gpio_reverse

//Function returns: None

//Parameter description: port_pin: port number | pin number (e.g.: PORTB|(5) means pin 5 of port B)

//Function summary: Invert the output state of the specified pin.

//===========================================================================

void gpio_reverse(uint_16 port_pin)

{

//Local variable declaration

GPIO_MemMapPtr gpio_ptr; //Declare port_ptr as a GPIO structure type pointer

gpio_ptr_bit(port_pin,&gpio_ptr,&bit); //Calculate the base address and the offset of the pin in the register

//Reverse the output state of the specified pin

BSET(bit,gpio_ptr->PTOR);

}

 

//===========================================================================

//Function name: gpio_pull

//Function returns: None

//Parameter description: port_pin: port number | pin number (e.g.: PORTB|(5) means pin 5 of port B)

// pullselect: pin pull-up enable selection (0 = pull-up disabled, 1 = pull-up enabled)

//Function summary: Pull the specified pin high

//===========================================================================

void gpio_pull(uint_16 port_pin, uint_8 pullselect)

{

//Local variable declaration

uint_8 port; //port number

uint_8 pin; //pin number

gpio_port_pin_num(port_pin, &port, &pin); //parse out the port number and pin number

//Calculate the offset of the pin in the register

if (port < 4) //Port number is PORTA~PORTD

{

//The number of pins

bit = 8 * port + pin;

if (1==pullselect){BSET(bit,PORT_PUE0);}//Port pull-up enable low register 0 pull-up enable

else {BCLR(bit,PORT_PUE0);}//Pull-up disable (disabled)

}

else if(3 {

//The number of pins

bit = 8 * (port - 4) + pin;

if (1==pullselect){BSET(bit,PORT_PUE1);}//Port pull-up enable low register 1 pull-up enable

else {BCLR(bit,PORT_PUE1);}

}

else //Port number is PORTI

{

//The number of pins

bit = 8 * (port - 8) + pin;

if (1==pullselect){BSET(bit,PORT_PUE2);}//Port pull-up enable low register 2 pull-up enable

else {BCLR(bit,PORT_PUE2);}

}

}

 

//----------------------The following is where the internal functions are stored----------------------------------------

//===========================================================================

//Function name: gpio_port_pin_num

//Function returns: None

//Parameter description: port_pin: port number | pin number (e.g.: PORTB|(5) means pin 5 of port B)

// port: the parsed port number

// pin: parsed pin number (0~8, the actual value is determined by the physical pin of the chip)

//Function summary: Parse the parameter port_pin (e.g. PORTB|(5)) to get the specific port number and pin number, and pass them out through port and pin.

// For example, PORTB|(5) is parsed as *port=PORTB, *pin=5).

//Note: port and pin are address parameters. The purpose is to bring back the result. No value is required before calling the function.

//===========================================================================

static void gpio_port_pin_num(uint_16 port_pin,uint_8* port,uint_8* pin)

{

*port = port_pin>>8;

*pin = port_pin;

}

//===========================================================================

//Function name: gpio_ptr_bit

//Function returns: None

//Parameter description: port_pin: port number | pin number (e.g.: PORTB|(5) means pin 5 of port B)

// gpio_ptr: parsed base address

//       bit: the offset of the pin in the register

//Function summary: Parse the passed parameter port_pin (e.g. PORTB|(5)), parse the base address and the offset of the pin in the register, and pass it out through gpio_ptr and bit.

// For example: PORTB|(5) is parsed as base address GPIOA_BASE_PTR, offset 13

//Note: port and pin are address parameters. The purpose is to bring back the result. No value is required before calling the function.

//===========================================================================

// Parse the base address and the pin's offset in the register

static void gpio_ptr_bit(uint_16 port_pin,GPIO_MemMapPtr* gpio_ptr,uint_32* bit)

{

uint_8 port; //port number

uint_8 pin; //pin number

gpio_port_pin_num(port_pin, &port, &pin); //parse out the port number and pin number

//Calculate the offset of the pin in the register

if (port < 4) //Port number is PORTA~PORTD

{

//The register base address where the port is located

*gpio_ptr = GPIOA_BASE_PTR;

//The number of pins

*bit = 8 * port + pin;

else if(3 {

//The register base address where the port is located

*gpio_ptr = GPIOB_BASE_PTR;

//The number of pins

*bit = 8 * (port - 4) + pin;

}

else //Port number is PORTI

{

//The register base address where the port is located

*gpio_ptr = GPIOC_BASE_PTR;

//The number of pins

*bit = 8 * (port - 8) + pin;

}

}

//----------------------------End of internal function---------------------------------------

[1] [2]
Keywords:Freescale Reference address:NXP Freescale Kinetis KEA128 Study Notes 3--GPIO Module (I)

Previous article:Freescale S12 series (based on MC9S12XET256MAA and/MC9S12XEP100) CAN data
Next article:NXP Freescale Kinetis KEA128 Study Notes 3--GPIO Module (Part 2)

Recommended ReadingLatest update time:2024-11-17 02:34

NXP Freescale Kinetis KEA128 Study Notes 4 - ADC
KEA128 ADC has 8 registers Basic steps of ADC programming: 1. Turn on the ADC module clock 2. Turn on the AD function of the corresponding pins of the ADC_APCTL1 pin control register (that is, turn off the IO function of these pins) 3. Configure the bus clock and clock division of the ADC_SC3 register, and set
[Microcontroller]
NXP Freescale Kinetis KEA128 Study Notes 4 - ADC
Freescale HCS12 Interrupts
Default state: When entering the interrupt service routine, the I bit is automatically set to 1, disabling other maskable interrupts Even if there is an interrupt request with a higher priority, it must wait until the current interrupt service routine is executed before responding The role of priority can only be re
[Microcontroller]
Freescale MC9S12G series internal watchdog
I have been researching the Freescale watchdog issue in recent days and found that there is no very detailed information on the Internet. I want to record what I know to prevent myself from forgetting and for others to refer to. What is a watchdog? Watchdog is a counter with a limit. When the watchdog function is tu
[Microcontroller]
Freescale MC9S12G series internal watchdog
Freescale MC9S08AW60 Assembly Learning Notes (VI)
Delay is a function that is often used in assembly. That is, the MCU does nothing but delays for a period of time. The MCU itself has timers and counters, which can be used to realize delay. However, a programmer naturally hopes that the function to be realized is easier to control. We use code and program to realize
[Microcontroller]
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号