The general purpose input and output port (GPIO) is the most basic port we use for microcontrollers. Its main functions are:
● Each bit of the port can be configured individually
● Selectable input mode: floating input and input with pull-up
● Selectable output modes: push-pull output and open-drain output
● External interrupts can be enabled and disabled individually
The user manual describes:
Each port has an output data register (ODR), a pin input register (IDR) and a data direction register (DDR) that are always associated. Control register 1 (CR1) and control register 2 (CR2) are used to configure the input/output. Any I/O pin can be configured by programming the corresponding bits of the DDR, ODR, CR1 and CR2 registers.
The various configurations are summarized below:
Configuration Mode | DDR | CR1 | CR2 | Configuration Mode | Pull-up resistor |
enter | 0 | 0 | 0 | Hover Input | OFF |
0 | 1 | 0 | Pull-up input | ON | |
0 | 0 | 1 | Interrupt suspension input | OFF | |
0 | 1 | 1 | Interrupt pull-up input | ON | |
Output | 1 | 0 | 0 | Open-drain output | OFF |
1 | 1 | 0 | Push-pull output | ||
1 | x | 1 | Output (maximum speed 10MHZ) | ||
x | x | x | True open-drain output | Not adopted |
Here we use three LEDs as an exercise, namely PA1, PA2, and PD4, all of which are set to push-pull output mode.
Here is the initialization function:
//CTRL0 PA1
//CTRL1 PA2
//CTRL2 PD4
void GPIO_init(void)
{
PA_DDR|=0x03;
PA_CR1|=0x03;
PA_CR2|=0x00;
PD_DDR|=0x10;
PD_CR1|=0x10;
PD_CR2|=0x00;
}
If you need to control the level of the IO port, you need to modify the ODR register. By reading the table, you can know that 1 is a high level and 0 is a low level.
Here you only need to modify the value of the corresponding bit.
void CTRL0_HIGH(void)
{
PA_ODR|=0x01;
}
void CTRL0_LOW(void)
{
PA_ODR&=0xFE;
}
void CTRL0_Reverse(void)
{
PA_ODR^=0x01;
}
Attached is the project of stm8s in IAR environment, including the initialization code of on-chip hardware such as SPI, IIC, PWM, AWU, USART, EEPROM, etc.
http://download.csdn.net/detail/devintt/9454188
Previous article:stm8s development (three) UART usage: serial port communication!
Next article:ADC0832 acquisition and Bluetooth communication system based on STM8
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- CGD and Qorvo to jointly revolutionize motor control solutions
- CGD and Qorvo to jointly revolutionize motor control solutions
- Keysight Technologies FieldFox handheld analyzer with VDI spread spectrum module to achieve millimeter wave analysis function
- Infineon's PASCO2V15 XENSIV PAS CO2 5V Sensor Now Available at Mouser for Accurate CO2 Level Measurement
- Advanced gameplay, Harting takes your PCB board connection to a new level!
- Advanced gameplay, Harting takes your PCB board connection to a new level!
- A new chapter in Great Wall Motors R&D: solid-state battery technology leads the future
- Naxin Micro provides full-scenario GaN driver IC solutions
- Interpreting Huawei’s new solid-state battery patent, will it challenge CATL in 2030?
- Are pure electric/plug-in hybrid vehicles going crazy? A Chinese company has launched the world's first -40℃ dischargeable hybrid battery that is not afraid of cold
- Development environment installation (II) Cross-compilation environment
- The price has increased by at least 200 times! Identification of popular chip material numbers from TI, ST, Infineon, and Qualcomm
- About MSP430-Timer WDT
- Does anyone know the pin map of the YC1021 chip?
- How to select all device bit numbers or nominal values in the SCH file for AD1904 version
- Testing methods and diagnostic analysis for DSP-containing circuit boards
- Fundamentals of Circuits and Analog Electronics Technology
- Need a low current charging IC and a Bluetooth chip with integrated MCU
- PCB Layout Guidelines
- LAUNCHXL-F28379D comprehensive routine