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单片机STVD环境新建工程笔记
下一篇:stm8s: GPIO配置,点亮LED!PB4,PB5引脚LED点不亮
推荐阅读最新更新时间:2024-11-09 16:29
设计资源 培训 开发板 精华推荐
- AD8534ARZ-REEL 高输出电流基准/稳压器的典型应用
- 基于RP2040的高分辨率磁性旋转编码器
- ADR5045B 5V 输出精密微功率并联模式电压基准的典型应用,适用于 ±15V 输出,采用堆叠式 ADR5045 器件
- 3合1 USB转TTL RS485 RS232 通讯模块
- LT3761EMSE-1 升压 LED 驱动器的典型应用电路,具有输出短路保护和外部驱动 PWM
- 适用于 ±10V 范围精密测量系统的 LT1236BILS8-5 精密电压基准的典型应用电路
- 窗户隐形磁传感器
- 使用 Analog Devices 的 ADR435B 的参考设计
- 80W、400V 交流转直流单输出台式电脑电源
- CCR230PS3AGEVB:用于 LED 照明解决方案的恒流稳压器驱动器评估板,230 V
- 【XILINX 主题分享月】 设计工具资料大搜集!!!
- 年末芯币竞价最后一期——RIGOL 数字示波器
- 邀你参加DIY,大家一起“搞事情”!
- 【赠书100%有礼】一本书教你如何开发测试系统,深入了解数据采集系统
- 有奖活动|Mentor PCB 手册:《利用自动验证消除原理图设计错误》
- 我爱 TI Webench 之学课程传设计,赢礼100% !
- 学视频,晒WEBENCH® 设计方案,免费获得TI样片!
- 有奖直播 | 同质化严重,缺乏创新,ST60毫米波非接触连接器,赋予你独特的产品设计,重拾市场话语权
- 【已结束】浩亭直播【电气控制柜如何快速低成本地进行线缆安装】
- 有奖直播|贝能国际推出基于英飞凌技术的毫米波雷达模组,完美解决PIR市场痛点
- 外媒曝三星S10+采用后置水平三摄:电池增至4000mAh
- 针对开发者 三星手机即将获得完整的Linux发行版支持
- 外媒评价苹果“双11”表现:中国手机市场的积极信号
- 科恩实验室研究员:iPhone XS Max iOS 12.1越狱成功
- 苹果原装18瓦PD充电器兼容性测试:iPhone X能快充
- 苹果向开发者推送iOS/iPadOS 14.2第二个Beta:加入新表情符
- 微软开始在iPad版Word与Excel中测试鼠标和触控板支持
- 全新技术 12.9英寸mini-LED高端iPad Pro或明年发布
- 富士康24小时全天候运转生产iPhone 12:确保及时交付
- 10月1日起!Android拒绝让步:将强制执行收费