stm8s development (II) Use of GPIO: light up the LED!

Publisher:算法之手Latest update time:2017-09-12 Source: eefocusKeywords:stm8s  GPIO Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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 ModeDDRCR1CR2Configuration ModePull-up resistor
enter000Hover Input OFF
010Pull-up inputON
001Interrupt suspension input OFF
011Interrupt pull-up inputON
Output100Open-drain output OFF
110Push-pull output
1x1Output (maximum speed 10MHZ) 
xxxTrue open-drain outputNot 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:


  1. //CTRL0 PA1  

  2. //CTRL1 PA2  

  3. //CTRL2 PD4  

  4.   

  5. void GPIO_init(void)  

  6. {  

  7.   PA_DDR|=0x03;  

  8.   PA_CR1|=0x03;  

  9.   PA_CR2|=0x00;  

  10.     

  11.   PD_DDR|=0x10;  

  12.   PD_CR1|=0x10;  

  13.   PD_CR2|=0x00;  

  14. }  



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.


  1. void CTRL0_HIGH(void)  

  2. {  

  3.   PA_ODR|=0x01;  

  4. }  

  5. void CTRL0_LOW(void)  

  6. {  

  7.   PA_ODR&=0xFE;  

  8. }  

  9. void CTRL0_Reverse(void)  

  10. {  

  11.   PA_ODR^=0x01;  

  12. }  


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


Keywords:stm8s  GPIO Reference address:stm8s development (II) Use of GPIO: light up the LED!

Previous article:stm8s development (three) UART usage: serial port communication!
Next article:ADC0832 acquisition and Bluetooth communication system based on STM8

Latest Microcontroller Articles
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号