The KEA128 chip has 7 groups of IO, PORTA--H has 8 pins in each group, and PORTI has 7 pins, totaling 71 pins.
The driving capacity of each pin is 2.5mA, and all pins can be pulled up to VDD internally, without internal pull-down. Unused pins should be pulled up internally through programming.
When the MCU is in the running, waiting, or debugging mode, the GPIO works normally. In the stop mode, the GPIO stops working.
The following is an introduction to the port control registers, mainly the port filter register, the pull-up enable register and the high drive capability enable register
GPIO has three groups of registers, namely GPIOA, GPIOB, and GPIOC. Each group has 7 registers, namely output register (PDOR), output set register (PSOR), output clear register (PCOR), output reverse register (PTOR), input register (PDIR), data direction register (PDDR), and input disable register (PIDR).
GPIO programming steps:
1. Set GPIO as input or output and set the data direction register
2. If it is output, set the value of GPIO, low 0 high 1
3. If it is input, get the pin status through the input register. Low 0 High 1.
//===========================================================================
//File name: common.h
//Functional overview: Common element header file
//Copyright: Freescale Embedded Center, Soochow University (sumcu.suda.edu.cn)
//Version update: 2015-06-05 V2.0
//Chip type: KEA128
//===========================================================================
#ifndef __COMMON_H //Prevent duplicate definition (starting with _COMMON_H)
#define __COMMON_H
// 1. Chip register mapping file and processor core attribute file
#include "core_cmFunc.h"
#include "core_cmInstr.h"
#include "core_cm0plus.h"
#include "SKEAZ1284.h" // Include chip header file
#include "system_SKEAZ1284.h" // Include chip system initialization file
#define SYSTEM_CLK_KHZ DEFAULT_SYSTEM_CLOCK/1000 // Chip system clock frequency (KHz)
#define CORE_CLK_KHZ SYSTEM_CLK_KHZ //Chip core clock frequency (KHz)
#define BUS_CLK_KHZ SYSTEM_CLK_KHZ/2 //Chip bus clock frequency (KHz)
// 2. Define the switch interrupt
#define ENABLE_INTERRUPTS __enable_irq // Enable general interrupt
#define DISABLE_INTERRUPTS __disable_irq // Disable total interrupt
// 3. Bit operation macro function (setting, clearing, obtaining the status of a register bit)
#define BSET(bit,Register) ((Register)|= (1<<(bit))) // Set a bit in the register
#define BCLR(bit,Register) ((Register) &= ~(1<<(bit))) // Clear one bit of the register
#define BGET(bit,Register) (((Register) >> (bit)) & 1) // Get the status of a bit in the register
// 4. Redefine basic data types (type alias macro definition)
typedef unsigned char uint_8; // unsigned 8-bit number, byte
typedef unsigned short int uint_16; // unsigned 16-bit number, word
typedef unsigned long int uint_32; // unsigned 32-bit number, long word
typedef char int_8; // Signed 8-bit number
typedef short int int_16; // signed 16-bit number
typedef int int_32; // signed 32-bit number
// Do not optimize type
typedef volatile uint_8 vuint_8; // Do not optimize unsigned 8-bit numbers, bytes
typedef volatile uint_16 vuint_16; // Do not optimize unsigned 16-bit numbers, word
typedef volatile uint_32 vuint_32; // Do not optimize unsigned 32-bit numbers, long words
typedef volatile int_8 vint_8; // Do not optimize signed 8-bit numbers
typedef volatile int_16 vint_16; // Do not optimize signed 16-bit numbers
typedef volatile int_32 vint_32; // Do not optimize signed 32-bit numbers
#endif //Prevent duplicate definition (ending with _COMMON_H)
//===========================================================================
//File name: gpio.h
//Functional summary: GPIO underlying driver component header file
//Copyright: Freescale Embedded Center, Soochow University (sumcu.suda.edu.cn)
//Version update: 2015-06-05 V2.0
//Chip type: KEA128
//===========================================================================
#ifndef GPIO_H //Prevent duplicate definition (starting with GPIO_H)
#define GPIO_H
#include "common.h" //Includes common element header files
//Port number address offset macro definition
#define PORTA (0<<8)
#define PORTB (1<<8)
#define PORTC (2<<8)
#define PORTD (3<<8)
#define PORTE (4<<8)
#define PORTF (5<<8)
#define PORTG (6<<8)
#define PORT (7<<8)
#define PORTS (8<<8)
//pin direction macro definition
#define GPIO_IN 0
#define GPIO_OUTPUT 1
//===========================================================================
//Function name: gpio_init
//Function returns: None
//Parameter description: port_pin: (port number)|(pin number) (e.g.: PORTB|(5) means pin 5 of port B)
// dir: pin direction (0 = input, 1 = output, can be defined by pin direction macro)
// state: initial state of the port pin (0 = low level, 1 = high level)
//Functional summary: Initialize the specified port pin as a GPIO pin function and define it as input or output. If it is output,
// Also specify whether the initial state is low or high
//===========================================================================
void gpio_init(uint_16 port_pin, uint_8 dir, uint_8 state);
//===========================================================================
//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: the port pin state you want to set (0 = low level, 1 = high level)
//Function Summary: When the specified port pin is defined as GPIO function and is output, this function sets the pin status
//===========================================================================
void gpio_set(uint_16 port_pin, uint_8 state);
//===========================================================================
//Function name: gpio_get
//Function returns: the state of the specified port pin (1 or 0)
//Parameter description: port_pin: (port number)|(pin number) (e.g.: PORTB|(5) means pin 5 of port B)
//Function summary: When the specified port pin is defined as GPIO function and is input, this function obtains the specified pin status
//===========================================================================
uint_8 gpio_get(uint_16 port_pin);
//===========================================================================
//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: When the specified port pin is defined as GPIO function and is output, this function inverts the pin state
//===========================================================================
void gpio_reverse(uint_16 port_pin);
//===========================================================================
//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);
#endif //Prevent duplicate definition (end of GPIO_H)
//===========================================================================
//statement:
//(1)The source code we developed has passed the test of the hardware system provided by our center. We sincerely contribute it to the society. If there are any shortcomings, please feel free to point them out.
//(2) For users who use hardware systems other than those of our center, please carefully match the system according to your own hardware when porting the code.
//
//Freescale Embedded Center, Suzhou University
//Technical consultation: 0512-65214835 http://sumcu.suda.edu.cn
//===========================================================================
//File name: gpio.c
//Functional summary: GPIO underlying driver component source file
//Copyright: Freescale Embedded Center, Soochow University (sumcu.suda.edu.cn)
//Version update: 2015-06-05 V2.0
//Chip type: KEA128
//===========================================================================
#include "gpio.h" //Include this component header file
uint_32 bit; //Internal variable used to record the offset of the pin in the port register
//Internal function declaration
//Parse the port number and pin
static void gpio_port_pin_num(uint_16 port_pin,uint_8* port,uint_8* pin);
// 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);
//===========================================================================
//Function name: gpio_init
//Function returns: None
//Parameter description: port_pin: (port number)|(pin number) (e.g.: PORTB|(5) means pin 5 of port B)
// dir: pin direction (0 = input, 1 = output, can be defined by pin direction macro)
// state: initial state of the port pin (0 = low level, 1 = high level)
//Functional summary: Initialize the specified port pin as a GPIO pin function and define it as input or output. If it is output,
// Also specify whether the initial state is low or high
//===========================================================================
void gpio_init(uint_16 port_pin, uint_8 dir, 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 or input based on the parameter dir
if (1 == dir) // hope for output
{
//Port data direction register is defined as output
BSET(bit, GPIO_PDDR_REG(gpio_ptr)); //1 is general output, 0 is input
// Output clear register
BSET(bit, GPIO_PCOR_REG(gpio_ptr)); //This register is set to 1, and the pin output is set to 0
//The initial state is low level
gpio_set(port_pin, state); //Call gpio_set function to set the initial state of the pin
}
else
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 00:07
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- Can't enter the main function
- Talk about the current mainstream microcontrollers, what are their advantages and disadvantages
- Is the domestic RISC-V laptop on sale? Equipped with the Pingtou Ge Yiying 1520 SoC
- USB drive cannot be used
- Asking a non-professional question
- 【Project source code】Knowledge related to NIOS II software program solidification
- FPGA Classic Design Case
- SparkRoad basic function lamp
- TMS320F28335P
- Array antenna that can withstand -170℃ low temperature