STM8 GPIO 学习(驱动LED、Button)

2020-01-19来源: eefocus关键字:STM8  GPIO  驱动LED  Button

STM8中GPIO寄存器有:输出寄存器(ODR),输入寄存器(IDR),方向寄存器(DDR),控制寄存器1(CR1)和控制寄存器2(CR2).后面三个寄存器组和可以配置为8种GPIO的模式


定义如下:


/**

  * @brief General Purpose I/Os (GPIO)

  */

typedef struct GPIO_struct

{

  __IO uint8_t ODR; /*!< Output Data Register */

  __IO uint8_t IDR; /*!< Input Data Register */

  __IO uint8_t DDR; /*!< Data Direction Register */

  __IO uint8_t CR1; /*!< Configuration Register 1 */

  __IO uint8_t CR2; /*!< Configuration Register 2 */

}

GPIO_TypeDef;


相关的配置模式定义如下


/**

  * @defgroup GPIO_Modes

  *

  * @brief

  *     

  * Bits definitions:

  * - Bit 7: 0 = INPUT mode

  *          1 = OUTPUT mode

  *          1 = PULL-UP (input) or PUSH-PULL (output)

  * - Bit 5: 0 = No external interrupt (input) or No slope control (output)

  *          1 = External interrupt (input) or Slow control enabled (output)

  * - Bit 4: 0 = Low level (output)

  *          1 = High level (output push-pull) or HI-Z (output open-drain)

  * @{

  */

typedef enum

{

  GPIO_Mode_In_FL_No_IT      = (uint8_t)0x00,   /*!< Input floating, no external interrupt */

  GPIO_Mode_In_PU_No_IT      = (uint8_t)0x40,   /*!< Input pull-up, no external interrupt */

  GPIO_Mode_In_FL_IT         = (uint8_t)0x20,   /*!< Input floating, external interrupt */

  GPIO_Mode_In_PU_IT         = (uint8_t)0x60,   /*!< Input pull-up, external interrupt */

  GPIO_Mode_Out_OD_Low_Fast  = (uint8_t)0xA0,   /*!< Output open-drain, low level, 10MHz */

  GPIO_Mode_Out_PP_Low_Fast  = (uint8_t)0xE0,   /*!< Output push-pull, low level, 10MHz */

  GPIO_Mode_Out_OD_Low_Slow  = (uint8_t)0x80,   /*!< Output open-drain, low level, 2MHz */

  GPIO_Mode_Out_PP_Low_Slow  = (uint8_t)0xC0,   /*!< Output push-pull, low level, 2MHz */

  GPIO_Mode_Out_OD_HiZ_Fast  = (uint8_t)0xB0,   /*!< Output open-drain, high-impedance level, 10MHz */

  GPIO_Mode_Out_PP_High_Fast = (uint8_t)0xF0,   /*!< Output push-pull, high level, 10MHz */

  GPIO_Mode_Out_OD_HiZ_Slow  = (uint8_t)0x90,   /*!< Output open-drain, high-impedance level, 2MHz */

  GPIO_Mode_Out_PP_High_Slow = (uint8_t)0xD0    /*!< Output push-pull, high level, 2MHz */

}GPIO_Mode_TypeDef;




GPIO_Mode_In_FL_No_IT浮空输入无中断

Input floating, no external interrupt

GPIO_Mode_In_PU_No_I上拉输入无中断

Input pull-up, no external interrupt

GPIO_Mode_In_FL_IT 浮空输入有中断

Input floating, external interrupt

GPIO_Mode_In_PU_IT 上拉输入有中断

Input pull-up, external interrupt

GPIO_Mode_Out_OD_Low_Fast 

开漏-输出低-高速

Output open-drain, low level, 10MHz

GPIO_Mode_Out_PP_Low_Fast 

推挽-输出低-高速

Output push-pull, low level, 10MHz

GPIO_Mode_Out_OD_Low_Slow 

开漏-输出低-低速

Output open-drain, low level, 2MHz

GPIO_Mode_Out_PP_Low_Slow 

推挽-输出低-低速

Output push-pull, low level, 2MHz

GPIO_Mode_Out_OD_HiZ_Fast 

开漏-输出高阻-高速

Output open-drain, high-impedance level, 10MHz

GPIO_Mode_Out_PP_High_Fast 

推挽-输出高-高速

Output push-pull, high level, 10MHz

GPIO_Mode_Out_OD_HiZ_Slow 

开漏-输出高阻-低速

Output open-drain, high-impedance level, 2MHz

GPIO_Mode_Out_PP_High_Slow 

推挽-输出高-低速


GPIO的固件库函数如下所示:


 void GPIO_DeInit (GPIO_TypeDef *GPIOx) Deinitializes the GPIOx peripheral registers to their default reset values.

void GPIO_ExternalPullUpConfig (GPIO_TypeDef *GPIOx,uint8_t GPIO_Pin, FunctionalState NewState) Configures the external pull-up on GPIOx pins.

void GPIO_Init (GPIO_TypeDef *GPIOx,uint8_t GPIO_Pin, GPIO_Mode_TypeDef GPIO_Mode) Initializes the GPIOx according to the specified parameters.

uint8_t GPIO_ReadInputData (GPIO_TypeDef *GPIOx) Reads the specified GPIO input data port.

BitStatus GPIO_ReadInputDataBit (GPIO_TypeDef *GPIOx,GPIO_Pin_TypeDef GPIO_Pin) Reads the specified GPIO input data pin.

uint8_t GPIO_ReadOutputData (GPIO_TypeDef *GPIOx) Reads the specified GPIO output data port.

BitStatus GPIO_ReadOutputDataBit (GPIO_TypeDef *GPIOx,GPIO_Pin_TypeDef GPIO_Pin) Reads the specified GPIO Output data pin.

void GPIO_ResetBits (GPIO_TypeDef *GPIOx,uint8_t GPIO_Pin) Writes low level to the specified GPIO pins.

void GPIO_SetBits (GPIO_TypeDef *GPIOx,uint8_t GPIO_Pin) Writes high level to the specified GPIO pins.

void GPIO_ToggleBits (GPIO_TypeDef *GPIOx,uint8_t GPIO_Pin) Toggles the specified GPIO pins. 

void GPIO_Write (GPIO_TypeDef *GPIOx,uint8_t GPIO_PortVal) Writes data to the specified GPIO data port.

void GPIO_WriteBit (GPIO_TypeDef *GPIOx,GPIO_Pin_TypeDef GPIO_Pin, BitAction GPIO_BitVal) Sets or clears the selected data port bit. 




下面是GPIO操作的应用,GPIO写操作---板上的两个LED灯闪烁的代码




/**

  ******************************************************************************

  * @file    Project/Template/main.c

  * @author  MCD Application Team

  * @version V1.3.0

  * @date    07/14/2010

  * @brief   Main program body

  ******************************************************************************

  * @copy

  *

  * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS

  * WITH CODING INFORMATION REGARDIN THEIR PRODUCTS IN ORDER FOR THEM TO SAVE

  * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY

  * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING

  * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE

  * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.

  *

  *

© COPYRIGHT 2010 STMicroelectronics

  */ 


/* Includes ------------------------------------------------------------------*/

#include "stm8l15x.h"



/** @addtogroup Template

  * @{

  */



/* Private typedef -----------------------------------------------------------*/

/* Private define ------------------------------------------------------------*/

/* Private macro -------------------------------------------------------------*/

/* Private variables ---------------------------------------------------------*/

/* Private function prototypes -----------------------------------------------*/



/* Private functions ---------------------------------------------------------*/



/**

  * @brief  Main program.

  * @param  None

  * @retval None

  */



/*discover_board  MACROs for SET, RESET or TOGGLE Output port */

#define GPIO_HIGH(a,b) a->ODR|=b

#define GPIO_LOW(a,b) a->ODR&=~b

#define GPIO_TOGGLE(a,b) a->ODR^=b



#define BUTTON_GPIO_PORT GPIOC

#define USER_GPIO_PIN GPIO_Pin_1



#define LED_GREEN_PORT GPIOE

#define LED_GREEN_PIN GPIO_Pin_7

  

#define LED_BLUE_PORT GPIOC

#define LED_BLUE_PIN            GPIO_Pin_7



#define CTN_GPIO_PORT           GPIOC

#define CTN_CNTEN_GPIO_PIN      GPIO_Pin_4



#define WAKEUP_GPIO_PORT        GPIOE

#define ICC_WAKEUP_GPIO_PIN     GPIO_Pin_6

#define ICC_WAKEUP_EXTI_PIN     EXTI_Pin_3

void Delay(__IO uint16_t nCount)

{

   /* Decrement nCount value */

   while (nCount != 0)

   {

    nCount--;

   } 

}



void main(void)

{

  /* Infinite loop */

  GPIO_Init(LED_GREEN_PORT, LED_GREEN_PIN, GPIO_Mode_Out_PP_Low_Fast);

  GPIO_Init(LED_BLUE_PORT, LED_BLUE_PIN, GPIO_Mode_Out_PP_Low_Fast);

  while (1)

  {

    GPIO_ToggleBits(LED_GREEN_PORT, LED_GREEN_PIN);

    Delay(0xFFFF);

    GPIO_ToggleBits(LED_BLUE_PORT, LED_BLUE_PIN);

    Delay(0xFFFF);

  }

}



/**

  * @brief  Inserts a delay time.

  * @param  nCount: specifies the delay time length.

  * @retval None

  */



#ifdef  USE_FULL_ASSERT



/**

  * @brief  Reports the name of the source file and the source line number

  *   where the assert_param error has occurred.

  * @param  file: pointer to the source file name

  * @param  line: assert_param error line source number

  * @retval None

  */

void assert_failed(uint8_t* file, uint32_t line)

  /* User can add his own implementation to report the file name and line number,

     ex: printf("Wrong parameters value: file %s on line %drn", file, line) */



  /* Infinite loop */

  while (1)

  {

  }

}

#endif



/**

  * @}

  */



/******************* (C) COPYRIGHT 2010 STMicroelectronics *****END OF FILE****/

关键字:STM8  GPIO  驱动LED  Button 编辑:什么鱼 引用地址:http://news.eeworld.com.cn/mcu/ic486119.html 本网站转载的所有的文章、图片、音频视频文件等资料的版权归版权所有人所有,本站采用的非本站原创文章及图片等内容无法一一联系确认版权者。如果本网所选内容的文章作者及编辑认为其作品不宜公开自由传播,或不应无偿使用,请及时通过电子邮件或电话通知我们,以迅速采取适当措施,避免给双方造成不必要的经济损失。

上一篇:STM8单片机STVD环境新建工程笔记
下一篇:stm8s: GPIO配置,点亮LED!PB4,PB5引脚LED点不亮

关注eeworld公众号 快捷获取更多信息
关注eeworld公众号
快捷获取更多信息
关注eeworld服务号 享受更多官方福利
关注eeworld服务号
享受更多官方福利

推荐阅读

STM8S库文件判断指定IO输入引脚电平GPIO_ReadInputPin有问题
/**  * @brief  Reads the specified GPIO input data pin.  * @param  GPIOx : Select the GPIO peripheral number (x = A to I).  * @param  GPIO_Pin : Specifies the pin number.  * @retval BitStatus : GPIO input pin status.  */BitStatus GPIO_ReadInputPin(GPIO_TypeDef* GPIOx
发表于 2020-02-08
STM8 GPIO输入输出模式
、P-MOS两个MOS均被接入电路,通过输出控制寄存器控制两个MOS管的导通或关闭。正常工作状态下,仅有一个MOS管导通,另一个MOS管截止,从而可以自如地控制高低数字信号的输出。开漏输出开漏输出(Open drain output),有时简称为OD输出。相比推挽输出,GPIO结构中的P-MOS管没有被接入,无法输出高电平状态,需要外接上拉电阻。在没有外接上拉电阻,引脚悬空的状态下,端口引脚状态将不确定。上拉电阻阻值的大小会引起输出信号上升沿时间的变化,上拉电阻越小,上升沿时间越短,速度越快。开漏输出模式吸收电流能力强(一般在20mA以内),适用于电流型驱动。用作电流型驱动时,驱动电流由外部电源经上拉电阻给引脚连接的电路或器件,单片机
发表于 2020-02-08
STM8 GPIO输入输出模式
STM8L的USART1串口通信详解 含例程
。/****************************************************************************************开发环境:IAR for stm8 v1.40.1*硬件平台:STM8L-DISCOVERY*功能说明:每隔500ms通过PC3引脚向上位机发送一组数据。串口配置为:波特为9600,8个数据位,1个停止位,无奇偶校验。*作    者:茗风****************************************************************************************/#include"
发表于 2020-02-08
使用STM8S105K4T6C 模数转换器的12通道
分享今天遇到的一个stm8s模数转换的小问题~~~这款单片机一共提供了7个模数转换通道,他们分别是AIN0-AIN5和AIN12。stm8s105k4t6c的管脚图其中AIN0-AIN5的配置和使用方法如下,配置为连续转换、扫描模式(代码写的不好,大神请见谅~):[mw_shl_code=c,true]/*---------------------------------包含头文件---------------------------------*/#include "adc.h" #define        ADC     
发表于 2020-02-08
stm8——LED流水灯实现
最近接触并学习了一款STM8系列的芯片。以前学习了的ARM9+Linux后,再来学习单片机就感到上手很快了。 芯片基本信息:Type:STM8L151G68-bit ultralow power MCU, up to 32 KB Flash, 1 KB Data EEPROM RTC, LCD, timers, USART, I2C, SPI, ADC, DAC, comparators具体可以查看datasheet:http://pdf-file.ic37.com/pdf4/STMICROELECTRONICS
发表于 2020-02-08
MCU程序设计之STM8S的optionbytes
今天使用STM8S在程序中修改optionbyte遇到问题一直读取为0,不能进入设置流程,之前的程序今天重新修改东西,使用新的片子,原来程序如下:  AFR_TEMP = (uint16_t)((uint16_t)0x01 << 8);  AFR_TEMP = AFR_TEMP | (uint16_t)0xFE;  AFR_TEMP = (AFR_TEMP >> 8);之所以直接赋值而不调用函数,是因为程序代码空间有限,使用函数调用方法修改没有问题,如下:  AFR_TEMP = FLASH_ReadOptionByte(0X4803);  AFR_TEMP
发表于 2020-02-08
小广播
何立民专栏 单片机及嵌入式宝典

北京航空航天大学教授,20余年来致力于单片机与嵌入式系统推广工作。

电子工程世界版权所有 京ICP证060456号 京ICP备10001474号 电信业务审批[2006]字第258号函 京公海网安备110108001534 Copyright © 2005-2020 EEWORLD.com.cn, Inc. All rights reserved