IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 嵌入式 -> STC 32位8051单片机开发实例教程 二 I/O工作模式及其配置 -> 正文阅读

[嵌入式]STC 32位8051单片机开发实例教程 二 I/O工作模式及其配置

1.?I/O工作模式 STC 32G系列单片机最多有64Pin引脚,最多有60个I/O口。如下图示:

STC32G系列单片机的?I/O口都有4种工作模式:准双向口(弱上拉)、推挽输出(强上拉)、高阻输入(电流不能流入也不能流出)、开漏输出。P30、P31上电复位状态为准双向(弱上拉)模式,其余I/O口上电复位状态为高阻输入,这与STC15系列,有所不同。

要使用I/O口需配置?I/O口的工作模式,STC32G系列单片机除有与工作模式配置有关的端口配置寄存器(PxM0、PxM1)、端口数据寄存器外Px,还有端口上拉电阻控制寄存器(enable/disable 内部4.1K上拉电阻)PxPU、端口施密特触发控制寄存器(enable/disable 施密特触发功能)PxNCS、端口电平转换速度控制寄存器(使转换速度可进行快慢切换)、端口驱动电流控制寄存器(使端口驱动能力能在一般驱动能力与增强驱动能力间切换)PxSR、端口数字信号输入使能控制寄存器(enable/disable 数字信号输入)PxIE、端口下拉电阻控制寄存器(enable/disable内部10K下拉电阻)。较STC15系列单片机要复杂得多。

STC32G系列单片机的?I/O口的工作模式通过端口配置寄存器(PxM0、PxM1)来配置,配置方法如下:

2.不同工作模式下的I/O结构图

?STC32G系列单片机的准双向?I/O结构图如下:

??STC32G系列单片机的推挽输出 I/O结构图如下:

STC32G系列单片机的高阻输入?I/O结构图如下:?

?STC32G系列单片机的开漏 I/O结构图如下:

??STC32G系列单片机相对于STC8位8051单片机新增了4.1K上拉电阻于10K下拉电阻,如下:

?4.库函数? STC 官网上提供了STC32G的库函数目前的最新为第4版,发布日期为2022年6月9号。下载解压后,在library文件夹下可看到的文件如下:

其中STC32G_GPIO.h的代码如下:

/*---------------------------------------------------------------------*/
/* --- STC MCU Limited ------------------------------------------------*/
/* --- STC 1T Series MCU Demo Programme -------------------------------*/
/* --- Mobile: (86)13922805190 ----------------------------------------*/
/* --- Fax: 86-0513-55012956,55012947,55012969 ------------------------*/
/* --- Tel: 86-0513-55012928,55012929,55012966 ------------------------*/
/* --- Web: www.STCMCU.com --------------------------------------------*/
/* --- Web: www.STCMCUDATA.com  ---------------------------------------*/
/* --- QQ:  800003751 -------------------------------------------------*/
/* 如果要在程序中使用此代码,请在程序中注明使用了STC的资料及程序            */
/*---------------------------------------------------------------------*/

#ifndef	__STC32G_GPIO_H
#define	__STC32G_GPIO_H

#include "config.h"

//========================================================================
//                            端口模式设置
//========================================================================
//准双向口
#define  P0_MODE_IO_PU(Pin)				{P0M1 &= ~(Pin), P0M0 &= ~(Pin);}
#define  P1_MODE_IO_PU(Pin)				{P1M1 &= ~(Pin), P1M0 &= ~(Pin);}
#define  P2_MODE_IO_PU(Pin)				{P2M1 &= ~(Pin), P2M0 &= ~(Pin);}
#define  P3_MODE_IO_PU(Pin)				{P3M1 &= ~(Pin), P3M0 &= ~(Pin);}
#define  P4_MODE_IO_PU(Pin)				{P4M1 &= ~(Pin), P4M0 &= ~(Pin);}
#define  P5_MODE_IO_PU(Pin)				{P5M1 &= ~(Pin), P5M0 &= ~(Pin);}
#define  P6_MODE_IO_PU(Pin)				{P6M1 &= ~(Pin), P6M0 &= ~(Pin);}
#define  P7_MODE_IO_PU(Pin)				{P7M1 &= ~(Pin), P7M0 &= ~(Pin);}
//高阻输入
#define  P0_MODE_IN_HIZ(Pin)			{P0M1 |= (Pin), P0M0 &= ~(Pin);}
#define  P1_MODE_IN_HIZ(Pin)			{P1M1 |= (Pin), P1M0 &= ~(Pin);}
#define  P2_MODE_IN_HIZ(Pin)			{P2M1 |= (Pin), P2M0 &= ~(Pin);}
#define  P3_MODE_IN_HIZ(Pin)			{P3M1 |= (Pin), P3M0 &= ~(Pin);}
#define  P4_MODE_IN_HIZ(Pin)			{P4M1 |= (Pin), P4M0 &= ~(Pin);}
#define  P5_MODE_IN_HIZ(Pin)			{P5M1 |= (Pin), P5M0 &= ~(Pin);}
#define  P6_MODE_IN_HIZ(Pin)			{P6M1 |= (Pin), P6M0 &= ~(Pin);}
#define  P7_MODE_IN_HIZ(Pin)			{P7M1 |= (Pin), P7M0 &= ~(Pin);}
//漏极开路
#define  P0_MODE_OUT_OD(Pin)			{P0M1 |= (Pin), P0M0 |= (Pin);}
#define  P1_MODE_OUT_OD(Pin)			{P1M1 |= (Pin), P1M0 |= (Pin);}
#define  P2_MODE_OUT_OD(Pin)			{P2M1 |= (Pin), P2M0 |= (Pin);}
#define  P3_MODE_OUT_OD(Pin)			{P3M1 |= (Pin), P3M0 |= (Pin);}
#define  P4_MODE_OUT_OD(Pin)			{P4M1 |= (Pin), P4M0 |= (Pin);}
#define  P5_MODE_OUT_OD(Pin)			{P5M1 |= (Pin), P5M0 |= (Pin);}
#define  P6_MODE_OUT_OD(Pin)			{P6M1 |= (Pin), P6M0 |= (Pin);}
#define  P7_MODE_OUT_OD(Pin)			{P7M1 |= (Pin), P7M0 |= (Pin);}
//推挽输出
#define  P0_MODE_OUT_PP(Pin)			{P0M1 &= ~(Pin), P0M0 |= (Pin);}
#define  P1_MODE_OUT_PP(Pin)			{P1M1 &= ~(Pin), P1M0 |= (Pin);}
#define  P2_MODE_OUT_PP(Pin)			{P2M1 &= ~(Pin), P2M0 |= (Pin);}
#define  P3_MODE_OUT_PP(Pin)			{P3M1 &= ~(Pin), P3M0 |= (Pin);}
#define  P4_MODE_OUT_PP(Pin)			{P4M1 &= ~(Pin), P4M0 |= (Pin);}
#define  P5_MODE_OUT_PP(Pin)			{P5M1 &= ~(Pin), P5M0 |= (Pin);}
#define  P6_MODE_OUT_PP(Pin)			{P6M1 &= ~(Pin), P6M0 |= (Pin);}
#define  P7_MODE_OUT_PP(Pin)			{P7M1 &= ~(Pin), P7M0 |= (Pin);}

//========================================================================
//                         端口内部4.1K上拉设置
//========================================================================
//上拉使能
#define P0_PULL_UP_ENABLE(Pin)		P0PU |= (Pin)
#define P1_PULL_UP_ENABLE(Pin)		P1PU |= (Pin)
#define P2_PULL_UP_ENABLE(Pin)		P2PU |= (Pin)
#define P3_PULL_UP_ENABLE(Pin)		P3PU |= (Pin)
#define P4_PULL_UP_ENABLE(Pin)		P4PU |= (Pin)
#define P5_PULL_UP_ENABLE(Pin)		P5PU |= (Pin)
#define P6_PULL_UP_ENABLE(Pin)		P6PU |= (Pin)
#define P7_PULL_UP_ENABLE(Pin)		P7PU |= (Pin)
//上拉禁止
#define P0_PULL_UP_DISABLE(Pin)		P0PU &= ~(Pin)
#define P1_PULL_UP_DISABLE(Pin)		P1PU &= ~(Pin)
#define P2_PULL_UP_DISABLE(Pin)		P2PU &= ~(Pin)
#define P3_PULL_UP_DISABLE(Pin)		P3PU &= ~(Pin)
#define P4_PULL_UP_DISABLE(Pin)		P4PU &= ~(Pin)
#define P5_PULL_UP_DISABLE(Pin)		P5PU &= ~(Pin)
#define P6_PULL_UP_DISABLE(Pin)		P6PU &= ~(Pin)
#define P7_PULL_UP_DISABLE(Pin)		P7PU &= ~(Pin)

//========================================================================
//                           端口施密特触发设置
//========================================================================
//施密特触发使能
#define P0_ST_ENABLE(Pin)					P0NCS &= ~(Pin)
#define P1_ST_ENABLE(Pin)					P1NCS &= ~(Pin)
#define P2_ST_ENABLE(Pin)					P2NCS &= ~(Pin)
#define P3_ST_ENABLE(Pin)					P3NCS &= ~(Pin)
#define P4_ST_ENABLE(Pin)					P4NCS &= ~(Pin)
#define P5_ST_ENABLE(Pin)					P5NCS &= ~(Pin)
#define P6_ST_ENABLE(Pin)					P6NCS &= ~(Pin)
#define P7_ST_ENABLE(Pin)					P7NCS &= ~(Pin)
//施密特触发禁止
#define P0_ST_DISABLE(Pin)				P0NCS |= (Pin)
#define P1_ST_DISABLE(Pin)				P1NCS |= (Pin)
#define P2_ST_DISABLE(Pin)				P2NCS |= (Pin)
#define P3_ST_DISABLE(Pin)				P3NCS |= (Pin)
#define P4_ST_DISABLE(Pin)				P4NCS |= (Pin)
#define P5_ST_DISABLE(Pin)				P5NCS |= (Pin)
#define P6_ST_DISABLE(Pin)				P6NCS |= (Pin)
#define P7_ST_DISABLE(Pin)				P7NCS |= (Pin)

//========================================================================
//                          端口电平转换速度设置
//========================================================================
//电平转换慢速,相应的上下冲比较小
#define P0_SPEED_LOW(Pin)					P0SR |= (Pin) 
#define P1_SPEED_LOW(Pin)					P1SR |= (Pin) 
#define P2_SPEED_LOW(Pin)					P2SR |= (Pin) 
#define P3_SPEED_LOW(Pin)					P3SR |= (Pin) 
#define P4_SPEED_LOW(Pin)					P4SR |= (Pin) 
#define P5_SPEED_LOW(Pin)					P5SR |= (Pin) 
#define P6_SPEED_LOW(Pin)					P6SR |= (Pin) 
#define P7_SPEED_LOW(Pin)					P7SR |= (Pin) 
//电平转换快速,相应的上下冲比较大
#define P0_SPEED_HIGH(Pin)				P0SR &= ~(Pin) 
#define P1_SPEED_HIGH(Pin)				P1SR &= ~(Pin) 
#define P2_SPEED_HIGH(Pin)				P2SR &= ~(Pin) 
#define P3_SPEED_HIGH(Pin)				P3SR &= ~(Pin) 
#define P4_SPEED_HIGH(Pin)				P4SR &= ~(Pin) 
#define P5_SPEED_HIGH(Pin)				P5SR &= ~(Pin) 
#define P6_SPEED_HIGH(Pin)				P6SR &= ~(Pin) 
#define P7_SPEED_HIGH(Pin)				P7SR &= ~(Pin) 

//========================================================================
//                          端口驱动电流控制设置
//========================================================================
//一般驱动能力
#define P0_DRIVE_MEDIUM(Pin)			P0DR |= (Pin)
#define P1_DRIVE_MEDIUM(Pin)			P1DR |= (Pin)
#define P2_DRIVE_MEDIUM(Pin)			P2DR |= (Pin)
#define P3_DRIVE_MEDIUM(Pin)			P3DR |= (Pin)
#define P4_DRIVE_MEDIUM(Pin)			P4DR |= (Pin)
#define P5_DRIVE_MEDIUM(Pin)			P5DR |= (Pin)
#define P6_DRIVE_MEDIUM(Pin)			P6DR |= (Pin)
#define P7_DRIVE_MEDIUM(Pin)			P7DR |= (Pin)
//增强驱动能力
#define P0_DRIVE_HIGH(Pin)				P0DR &= ~(Pin)
#define P1_DRIVE_HIGH(Pin)				P1DR &= ~(Pin)
#define P2_DRIVE_HIGH(Pin)				P2DR &= ~(Pin)
#define P3_DRIVE_HIGH(Pin)				P3DR &= ~(Pin)
#define P4_DRIVE_HIGH(Pin)				P4DR &= ~(Pin)
#define P5_DRIVE_HIGH(Pin)				P5DR &= ~(Pin)
#define P6_DRIVE_HIGH(Pin)				P6DR &= ~(Pin)
#define P7_DRIVE_HIGH(Pin)				P7DR &= ~(Pin)

//========================================================================
//                          端口数字信号输入使能
//========================================================================
//使能数字信号输入
#define P0_DIGIT_IN_ENABLE(Pin)		P0IE |= (Pin)
#define P1_DIGIT_IN_ENABLE(Pin)		P1IE |= (Pin)
#define P2_DIGIT_IN_ENABLE(Pin)		P2IE |= (Pin)
#define P3_DIGIT_IN_ENABLE(Pin)		P3IE |= (Pin)
#define P4_DIGIT_IN_ENABLE(Pin)		P4IE |= (Pin)
#define P5_DIGIT_IN_ENABLE(Pin)		P5IE |= (Pin)
#define P6_DIGIT_IN_ENABLE(Pin)		P6IE |= (Pin)
#define P7_DIGIT_IN_ENABLE(Pin)		P7IE |= (Pin)
//禁止数字信号输入
#define P0_DIGIT_IN_DISABLE(Pin)	P0IE &= ~(Pin)
#define P1_DIGIT_IN_DISABLE(Pin)	P1IE &= ~(Pin)
#define P2_DIGIT_IN_DISABLE(Pin)	P2IE &= ~(Pin)
#define P3_DIGIT_IN_DISABLE(Pin)	P3IE &= ~(Pin)
#define P4_DIGIT_IN_DISABLE(Pin)	P4IE &= ~(Pin)
#define P5_DIGIT_IN_DISABLE(Pin)	P5IE &= ~(Pin)
#define P6_DIGIT_IN_DISABLE(Pin)	P6IE &= ~(Pin)
#define P7_DIGIT_IN_DISABLE(Pin)	P7IE &= ~(Pin)

//========================================================================
//                              定义声明
//========================================================================

#define	GPIO_PullUp		0	//上拉准双向口
#define	GPIO_HighZ		1	//浮空输入
#define	GPIO_OUT_OD		2	//开漏输出
#define	GPIO_OUT_PP		3	//推挽输出

#define	GPIO_Pin_0		0x01	//IO引脚 Px.0
#define	GPIO_Pin_1		0x02	//IO引脚 Px.1
#define	GPIO_Pin_2		0x04	//IO引脚 Px.2
#define	GPIO_Pin_3		0x08	//IO引脚 Px.3
#define	GPIO_Pin_4		0x10	//IO引脚 Px.4
#define	GPIO_Pin_5		0x20	//IO引脚 Px.5
#define	GPIO_Pin_6		0x40	//IO引脚 Px.6
#define	GPIO_Pin_7		0x80	//IO引脚 Px.7
#define	GPIO_Pin_LOW	0x0F	//IO低4位引脚
#define	GPIO_Pin_HIGH	0xF0	//IO高4位引脚
#define	GPIO_Pin_All	0xFF	//IO所有引脚

#define	GPIO_P0			0		//
#define	GPIO_P1			1
#define	GPIO_P2			2
#define	GPIO_P3			3
#define	GPIO_P4			4
#define	GPIO_P5			5
#define	GPIO_P6			6
#define	GPIO_P7			7


typedef struct
{
	u8	Mode;		//IO模式,  		GPIO_PullUp,GPIO_HighZ,GPIO_OUT_OD,GPIO_OUT_PP
	u8	Pin;		//要设置的端口	
} GPIO_InitTypeDef;

u8	GPIO_Inilize(u8 GPIO, GPIO_InitTypeDef *GPIOx);

#endif

STC32G_GPIO.c中的代码如下:

/*---------------------------------------------------------------------*/
/* --- STC MCU Limited ------------------------------------------------*/
/* --- STC 1T Series MCU Demo Programme -------------------------------*/
/* --- Mobile: (86)13922805190 ----------------------------------------*/
/* --- Fax: 86-0513-55012956,55012947,55012969 ------------------------*/
/* --- Tel: 86-0513-55012928,55012929,55012966 ------------------------*/
/* --- Web: www.STCMCU.com --------------------------------------------*/
/* --- Web: www.STCMCUDATA.com  ---------------------------------------*/
/* --- QQ:  800003751 -------------------------------------------------*/
/* 如果要在程序中使用此代码,请在程序中注明使用了STC的资料及程序            */
/*---------------------------------------------------------------------*/

#include	"STC32G_GPIO.h"

//========================================================================
// 函数: u8	GPIO_Inilize(u8 GPIO, GPIO_InitTypeDef *GPIOx)
// 描述: 初始化IO口.
// 参数: GPIOx: 结构参数,请参考timer.h里的定义.
// 返回: 成功返回 SUCCESS, 错误返回 FAIL.
// 版本: V1.0, 2012-10-22
//========================================================================
u8	GPIO_Inilize(u8 GPIO, GPIO_InitTypeDef *GPIOx)
{
	if(GPIO > GPIO_P7)				return FAIL;	//错误
	if(GPIOx->Mode > GPIO_OUT_PP)	return FAIL;	//错误
	if(GPIO == GPIO_P0)
	{
		if(GPIOx->Mode == GPIO_PullUp)		P0M1 &= ~GPIOx->Pin,	P0M0 &= ~GPIOx->Pin;	 //上拉准双向口
		if(GPIOx->Mode == GPIO_HighZ)			P0M1 |=  GPIOx->Pin,	P0M0 &= ~GPIOx->Pin;	 //浮空输入
		if(GPIOx->Mode == GPIO_OUT_OD)		P0M1 |=  GPIOx->Pin,	P0M0 |=  GPIOx->Pin;	 //开漏输出
		if(GPIOx->Mode == GPIO_OUT_PP)		P0M1 &= ~GPIOx->Pin,	P0M0 |=  GPIOx->Pin;	 //推挽输出
	}
	if(GPIO == GPIO_P1)
	{
		if(GPIOx->Mode == GPIO_PullUp)		P1M1 &= ~GPIOx->Pin,	P1M0 &= ~GPIOx->Pin;	 //上拉准双向口
		if(GPIOx->Mode == GPIO_HighZ)			P1M1 |=  GPIOx->Pin,	P1M0 &= ~GPIOx->Pin;	 //浮空输入
		if(GPIOx->Mode == GPIO_OUT_OD)		P1M1 |=  GPIOx->Pin,	P1M0 |=  GPIOx->Pin;	 //开漏输出
		if(GPIOx->Mode == GPIO_OUT_PP)		P1M1 &= ~GPIOx->Pin,	P1M0 |=  GPIOx->Pin;	 //推挽输出
	}
	if(GPIO == GPIO_P2)
	{
		if(GPIOx->Mode == GPIO_PullUp)		P2M1 &= ~GPIOx->Pin,	P2M0 &= ~GPIOx->Pin;	 //上拉准双向口
		if(GPIOx->Mode == GPIO_HighZ)			P2M1 |=  GPIOx->Pin,	P2M0 &= ~GPIOx->Pin;	 //浮空输入
		if(GPIOx->Mode == GPIO_OUT_OD)		P2M1 |=  GPIOx->Pin,	P2M0 |=  GPIOx->Pin;	 //开漏输出
		if(GPIOx->Mode == GPIO_OUT_PP)		P2M1 &= ~GPIOx->Pin,	P2M0 |=  GPIOx->Pin;	 //推挽输出
	}
	if(GPIO == GPIO_P3)
	{
		if(GPIOx->Mode == GPIO_PullUp)		P3M1 &= ~GPIOx->Pin,	P3M0 &= ~GPIOx->Pin;	 //上拉准双向口
		if(GPIOx->Mode == GPIO_HighZ)			P3M1 |=  GPIOx->Pin,	P3M0 &= ~GPIOx->Pin;	 //浮空输入
		if(GPIOx->Mode == GPIO_OUT_OD)		P3M1 |=  GPIOx->Pin,	P3M0 |=  GPIOx->Pin;	 //开漏输出
		if(GPIOx->Mode == GPIO_OUT_PP)		P3M1 &= ~GPIOx->Pin,	P3M0 |=  GPIOx->Pin;	 //推挽输出
	}
	if(GPIO == GPIO_P4)
	{
		if(GPIOx->Mode == GPIO_PullUp)		P4M1 &= ~GPIOx->Pin,	P4M0 &= ~GPIOx->Pin;	 //上拉准双向口
		if(GPIOx->Mode == GPIO_HighZ)			P4M1 |=  GPIOx->Pin,	P4M0 &= ~GPIOx->Pin;	 //浮空输入
		if(GPIOx->Mode == GPIO_OUT_OD)		P4M1 |=  GPIOx->Pin,	P4M0 |=  GPIOx->Pin;	 //开漏输出
		if(GPIOx->Mode == GPIO_OUT_PP)		P4M1 &= ~GPIOx->Pin,	P4M0 |=  GPIOx->Pin;	 //推挽输出
	}
	if(GPIO == GPIO_P5)
	{
		if(GPIOx->Mode == GPIO_PullUp)		P5M1 &= ~GPIOx->Pin,	P5M0 &= ~GPIOx->Pin;	 //上拉准双向口
		if(GPIOx->Mode == GPIO_HighZ)			P5M1 |=  GPIOx->Pin,	P5M0 &= ~GPIOx->Pin;	 //浮空输入
		if(GPIOx->Mode == GPIO_OUT_OD)		P5M1 |=  GPIOx->Pin,	P5M0 |=  GPIOx->Pin;	 //开漏输出
		if(GPIOx->Mode == GPIO_OUT_PP)		P5M1 &= ~GPIOx->Pin,	P5M0 |=  GPIOx->Pin;	 //推挽输出
	}
	if(GPIO == GPIO_P6)
	{
		if(GPIOx->Mode == GPIO_PullUp)		P6M1 &= ~GPIOx->Pin,	P6M0 &= ~GPIOx->Pin;	 //上拉准双向口
		if(GPIOx->Mode == GPIO_HighZ)			P6M1 |=  GPIOx->Pin,	P6M0 &= ~GPIOx->Pin;	 //浮空输入
		if(GPIOx->Mode == GPIO_OUT_OD)		P6M1 |=  GPIOx->Pin,	P6M0 |=  GPIOx->Pin;	 //开漏输出
		if(GPIOx->Mode == GPIO_OUT_PP)		P6M1 &= ~GPIOx->Pin,	P6M0 |=  GPIOx->Pin;	 //推挽输出
	}
	if(GPIO == GPIO_P7)
	{
		if(GPIOx->Mode == GPIO_PullUp)		P7M1 &= ~GPIOx->Pin,	P7M0 &= ~GPIOx->Pin;	 //上拉准双向口
		if(GPIOx->Mode == GPIO_HighZ)			P7M1 |=  GPIOx->Pin,	P7M0 &= ~GPIOx->Pin;	 //浮空输入
		if(GPIOx->Mode == GPIO_OUT_OD)		P7M1 |=  GPIOx->Pin,	P7M0 |=  GPIOx->Pin;	 //开漏输出
		if(GPIOx->Mode == GPIO_OUT_PP)		P7M1 &= ~GPIOx->Pin,	P7M0 |=  GPIOx->Pin;	 //推挽输出
	}
	return SUCCESS;	//成功
}

注意STC32G_GPIO.h中包含了头文件config.h及自定义的数据类型符号,需从Independent_Programme文件夹下的子目录下将下面两个头文件Copy到library文件夹下,否则编译会报错。

下面是我自己写的库函数,头文件如下:

/*STC32G_GPIO.h
  Designed by Bill Liu
  Version 0.0 
  Modified last by Bill Liu on 06/23/2022
*/

#ifndef	__STC32G_GPIO_H
#define	__STC32G_GPIO_H
#include <STC32G.h>
#include "config.h"
#include "mtype.h"

//***************************************
typedef enum
{
  PIN0 = 0x01, 
  PIN1 = 0x02, 
  PIN2 = 0x04, 
  PIN3 = 0x08, 
  PIN4 = 0x10, 
  PIN5 = 0x20, 
  PIN6 = 0x40, 
  PIN7 = 0x80  
}IOPORT_PIN;

//***************************************
typedef enum
{
  SCT_P0 = 0x01, //P0 port
  SCT_P1 = 0x02, //P1 port
  SCT_P2 = 0x04, //P2 port
  SCT_P3 = 0x08, //P3 port
  SCT_P4 = 0x10, //P4 port
  SCT_P5 = 0x20, //P5 port
  SCT_P6 = 0x40, //P6 port
  SCT_P7 = 0x80  //P7 port
}STC32G_IOPORT;

//***************************************
typedef enum
{
  BI_IO = 0, //bidirectional I/O
	PP_OUT,    //push_pull out
	HI_IN,     //high impendence in
	ODR_OUT     //open drain out
}STC32G_IOMODE;

/****************************************
Function: STC32G_P0PinsInit(ui8 pins, STC32G_IOMODE mode, BOOL pullUpEnable, BOOL pullDownEnable,BOOL drvEn,BOOL speedHi,BOOL dIEnable, BOOL sTEnable);
Return value: void
pins: pins to init,
mode: configure work mode
pullUpEnable: 0-pullup disable, 1-pull up enable
pullDownEnable: 0-pull down disable,1-pull down enable
drvEn: 1-drive ability enhance enable, 0-drive ability enhance disable
speedHi; 1- I/O speed high enable, 0- I/O speed high disable
dIEnable: 1- digital singal input enable, 0- digital singal input enable
sTEnable: 1-schmitt trigger enable, 0-schmitt trigger disable
Example:
STC32G_P0PinsInit(PIN0|PIN3, BI_IO, 0, 0,0,1,1,1);
****************************************/
void STC32G_P0PinsInit(ui8 pins, STC32G_IOMODE mode, BOOL pullUpEnable, BOOL pullDownEnable,BOOL drvEn,BOOL speedHi,BOOL dIEnable, BOOL sTEnable);

/****************************************
Function: STC32G_P1PinsInit(ui8 pins, STC32G_IOMODE mode, BOOL pullUpEnable, BOOL pullDownEnable,BOOL drvEn,BOOL speedHi,BOOL dIEnable, BOOL sTEnable);
Return value: void
pins: pins to init,
mode: configure work mode
pullUpEnable: 0-pullup disable, 1-pull up enable
pullDownEnable: 0-pull down disable,1-pull down enable
drvEn: 1-drive ability enhance enable, 0-drive ability enhance disable
speedHi; 1- I/O speed high enable, 0- I/O speed high disable
dIEnable: 1- digital singal input enable, 0- digital singal input enable
sTEnable: 1-schmitt trigger enable, 0-schmitt trigger disable
Example:
STC32G_P1PinsInit(PIN0|PIN3, BI_IO, 0, 0,0,1,1,1);
****************************************/
void STC32G_P1PinsInit(ui8 pins, STC32G_IOMODE mode, BOOL pullUpEnable, BOOL pullDownEnable,BOOL drvEn,BOOL speedHi,BOOL dIEnable, BOOL sTEnable);

/****************************************
Function: STC32G_P2PinsInit(ui8 pins, STC32G_IOMODE mode, BOOL pullUpEnable, BOOL pullDownEnable,BOOL drvEn,BOOL speedHi,BOOL dIEnable, BOOL sTEnable);
Return value: void
pins: pins to init,
mode: configure work mode
pullUpEnable: 0-pullup disable, 1-pull up enable
pullDownEnable: 0-pull down disable,1-pull down enable
drvEn: 1-drive ability enhance enable, 0-drive ability enhance disable
speedHi; 1- I/O speed high enable, 0- I/O speed high disable
dIEnable: 1- digital singal input enable, 0- digital singal input enable
sTEnable: 1-schmitt trigger enable, 0-schmitt trigger disable
Example:
STC32G_P2PinsInit(PIN0|PIN3, BI_IO, 0, 0,0,1,1,1);
****************************************/
void STC32G_P2PinsInit(ui8 pins, STC32G_IOMODE mode, BOOL pullUpEnable, BOOL pullDownEnable,BOOL drvEn,BOOL speedHi,BOOL dIEnable, BOOL sTEnable);

/****************************************
Function: STC32G_P3PinsInit(ui8 pins, STC32G_IOMODE mode, BOOL pullUpEnable, BOOL pullDownEnable,BOOL drvEn,BOOL speedHi,BOOL dIEnable, BOOL sTEnable);
Return value: void
pins: pins to init,
mode: configure work mode
pullUpEnable: 0-pullup disable, 1-pull up enable
pullDownEnable: 0-pull down disable,1-pull down enable
drvEn: 1-drive ability enhance enable, 0-drive ability enhance disable
speedHi; 1- I/O speed high enable, 0- I/O speed high disable
dIEnable: 1- digital singal input enable, 0- digital singal input enable
sTEnable: 1-schmitt trigger enable, 0-schmitt trigger disable
Example:
STC32G_P3PinsInit(PIN0|PIN3, BI_IO, 0, 0,0,1,1,1);
****************************************/
void STC32G_P3PinsInit(ui8 pins, STC32G_IOMODE mode, BOOL pullUpEnable, BOOL pullDownEnable,BOOL drvEn,BOOL speedHi,BOOL dIEnable, BOOL sTEnable);

/****************************************
Function: STC32G_P4PinsInit(ui8 pins, STC32G_IOMODE mode, BOOL pullUpEnable, BOOL pullDownEnable,BOOL drvEn,BOOL speedHi,BOOL dIEnable, BOOL sTEnable);
Return value: void
pins: pins to init,
mode: configure work mode
pullUpEnable: 0-pullup disable, 1-pull up enable
pullDownEnable: 0-pull down disable,1-pull down enable
drvEn: 1-drive ability enhance enable, 0-drive ability enhance disable
speedHi; 1- I/O speed high enable, 0- I/O speed high disable
dIEnable: 1- digital singal input enable, 0- digital singal input enable
sTEnable: 1-schmitt trigger enable, 0-schmitt trigger disable
Example:
STC32G_P4PinsInit(PIN0|PIN3, BI_IO, 0, 0,0,1,1,1);
****************************************/
void STC32G_P4PinsInit(ui8 pins, STC32G_IOMODE mode, BOOL pullUpEnable, BOOL pullDownEnable,BOOL drvEn,BOOL speedHi,BOOL dIEnable, BOOL sTEnable);

/****************************************
Function: STC32G_P5PinsInit(ui8 pins, STC32G_IOMODE mode, BOOL pullUpEnable, BOOL pullDownEnable,BOOL drvEn,BOOL speedHi,BOOL dIEnable, BOOL sTEnable);
Return value: void
pins: pins to init,
mode: configure work mode
pullUpEnable: 0-pullup disable, 1-pull up enable
pullDownEnable: 0-pull down disable,1-pull down enable
drvEn: 1-drive ability enhance enable, 0-drive ability enhance disable
speedHi; 1- I/O speed high enable, 0- I/O speed high disable
dIEnable: 1- digital singal input enable, 0- digital singal input enable
sTEnable: 1-schmitt trigger enable, 0-schmitt trigger disable
Example:
STC32G_P5PinsInit(PIN0|PIN3, BI_IO, 0, 0,0,1,1,1);
****************************************/
void STC32G_P5PinsInit(ui8 pins, STC32G_IOMODE mode, BOOL pullUpEnable, BOOL pullDownEnable,BOOL drvEn,BOOL speedHi,BOOL dIEnable, BOOL sTEnable);

/****************************************
Function: STC32G_P6PinsInit(ui8 pins, STC32G_IOMODE mode, BOOL pullUpEnable, BOOL pullDownEnable,BOOL drvEn,BOOL speedHi,BOOL dIEnable, BOOL sTEnable);
Return value: void
pins: pins to init,
mode: configure work mode
pullUpEnable: 0-pullup disable, 1-pull up enable
pullDownEnable: 0-pull down disable,1-pull down enable
drvEn: 1-drive ability enhance enable, 0-drive ability enhance disable
speedHi; 1- I/O speed high enable, 0- I/O speed high disable
dIEnable: 1- digital singal input enable, 0- digital singal input enable
sTEnable: 1-schmitt trigger enable, 0-schmitt trigger disable
Example:
STC32G_P6PinsInit(PIN0|PIN3, BI_IO, 0, 0,0,1,1,1);
****************************************/
void STC32G_P6PinsInit(ui8 pins, STC32G_IOMODE mode, BOOL pullUpEnable, BOOL pullDownEnable,BOOL drvEn,BOOL speedHi,BOOL dIEnable, BOOL sTEnable);

/****************************************
Function: STC32G_P7PinsInit(ui8 pins, STC32G_IOMODE mode, BOOL pullUpEnable, BOOL pullDownEnable,BOOL drvEn,BOOL speedHi,BOOL dIEnable, BOOL sTEnable);
Return value: void
pins: pins to init,
mode: configure work mode
pullUpEnable: 0-pullup disable, 1-pull up enable
pullDownEnable: 0-pull down disable,1-pull down enable
drvEn: 1-drive ability enhance enable, 0-drive ability enhance disable
speedHi; 1- I/O speed high enable, 0- I/O speed high disable
dIEnable: 1- digital singal input enable, 0- digital singal input enable
sTEnable: 1-schmitt trigger enable, 0-schmitt trigger disable
Example:
STC32G_P7PinsInit(PIN0|PIN3, BI_IO, 0, 0,0,1,1,1);
****************************************/
void STC32G_P7PinsInit(ui8 pins, STC32G_IOMODE mode, BOOL pullUpEnable, BOOL pullDownEnable,BOOL drvEn,BOOL speedHi,BOOL dIEnable, BOOL sTEnable);

/****************************************
Function: STC32G_PortsPinsInit(ui8 ports,ui8 pins, STC32G_IOMODE mode, BOOL pullUpEnable, BOOL pullDownEnable,BOOL drvEn,BOOL speedHi,BOOL dIEnable, BOOL sTEnable);
Return value: void
ports: ports to init
pins: pins to init,
mode: configure work mode
pullUpEnable: 0-pullup disable, 1-pull up enable
pullDownEnable: 0-pull down disable,1-pull down enable
drvEn: 1-drive ability enhance enable, 0-drive ability enhance disable
speedHi; 1- I/O speed high enable, 0- I/O speed high disable
dIEnable: 1- digital singal input enable, 0- digital singal input enable
sTEnable: 1-schmitt trigger enable, 0-schmitt trigger disable
Example:
STC32G_PortsPinsInit(SCT_P0|SCT_P1|SCT_P6,PIN0|PIN3, BI_IO, 0, 0,0,1,1,1);
****************************************/
void STC32G_PortsPinsInit(ui8 ports,ui8 pins, STC32G_IOMODE mode, BOOL pullUpEnable, BOOL pullDownEnable,BOOL drvEn,BOOL speedHi,BOOL dIEnable, BOOL sTEnable);

#endif

库函数源代码如下:

/*STC32G_GPIO.h
  Designed by Bill Liu
  Version 0.0 
  Modified last by Bill Liu on 06/23/2022
*/

#include	"STC32G_GPIO.h"

//*********************************************************************************************************************************************/
void STC32G_P0PinsInit(ui8 pins, STC32G_IOMODE mode, BOOL pullUpEnable, BOOL pullDownEnable,BOOL drvEn,BOOL speedHi,BOOL dIEnable, BOOL sTEnable)
{
	SysInit();
	switch(mode)
	{
		case BI_IO:
			P0M1 &= ~pins;
			P0M0 &= ~pins;
			break;
		case PP_OUT:
			P0M1 &= ~pins;
			P0M0 |= pins;
			break;
		case HI_IN:
			P0M1 |= pins;
			P0M0 &= pins;
			break;
		case ODR_OUT:
			P0M1 |= pins;
			P0M0 |= pins;
			break;
	}
	if(pullUpEnable)
		P0PU |= pins;
	else
		P0PU &= ~pins;
	if(pullDownEnable)
		P0DR |= pins;
	else
		P0DR &= ~pins;
	if(drvEn)
		P0DR &= ~pins;
	else
		P0DR |= pins;
	if(speedHi)
		P0SR &= ~pins;
	else
		P0SR |= pins;
	if(dIEnable)
		P0IE |= pins;
	else
		P0IE &= ~pins;
	if(sTEnable)
		P0NCS &= ~pins;
	else
		P0NCS |= pins;
}
//End of STC32G_P0PinsInit(ui8 pins, STC32G_IOMODE mode, BOOL pullUpEnable, BOOL pullDownEnable,BOOL drvEn,BOOL speedHi,BOOL dIEnable, BOOL sTEnable)

//*********************************************************************************************************************************************/
void STC32G_P1PinsInit(ui8 pins, STC32G_IOMODE mode, BOOL pullUpEnable, BOOL pullDownEnable,BOOL drvEn,BOOL speedHi,BOOL dIEnable, BOOL sTEnable)
{
	SysInit();
	switch(mode)
	{
		case BI_IO:
			P1M1 &= ~pins;
			P1M0 &= ~pins;
			break;
		case PP_OUT:
			P1M1 &= ~pins;
			P1M0 |= pins;
			break;
		case HI_IN:
			P1M1 |= pins;
			P1M0 &= pins;
			break;
		case ODR_OUT:
			P1M1 |= pins;
			P1M0 |= pins;
			break;
	}
	if(pullUpEnable)
		P1PU |= pins;
	else
		P1PU &= ~pins;
	if(pullDownEnable)
		P1DR |= pins;
	else
		P1DR &= ~pins;
	if(drvEn)
		P1DR &= ~pins;
	else
		P1DR |= pins;
	if(speedHi)
		P1SR &= ~pins;
	else
		P1SR |= pins;
	if(dIEnable)
		P1IE |= pins;
	else
		P1IE &= ~pins;
	if(sTEnable)
		P1NCS &= ~pins;
	else
		P1NCS |= pins;
}
//End of STC32G_P1PinsInit(ui8 pins, STC32G_IOMODE mode, BOOL pullUpEnable, BOOL pullDownEnable,BOOL drvEn,BOOL speedHi,BOOL dIEnable, BOOL sTEnable)

//**********************************************************************************************************************************************/
void STC32G_P2PinsInit(ui8 pins, STC32G_IOMODE mode, BOOL pullUpEnable, BOOL pullDownEnable,BOOL drvEn,BOOL speedHi,BOOL dIEnable, BOOL sTEnable)
{
	SysInit();
	switch(mode)
	{
		case BI_IO:
			P2M1 &= ~pins;
			P2M0 &= ~pins;
			break;
		case PP_OUT:
			P2M1 &= ~pins;
			P2M0 |= pins;
			break;
		case HI_IN:
			P2M1 |= pins;
			P2M0 &= pins;
			break;
		case ODR_OUT:
			P2M1 |= pins;
			P2M0 |= pins;
			break;
	}
	if(pullUpEnable)
		P2PU |= pins;
	else
		P2PU &= ~pins;
	if(pullDownEnable)
		P2DR |= pins;
	else
		P2DR &= ~pins;
	if(drvEn)
		P2DR &= ~pins;
	else
		P2DR |= pins;
	if(speedHi)
		P2SR &= ~pins;
	else
		P2SR |= pins;
	if(dIEnable)
		P2IE |= pins;
	else
		P2IE &= ~pins;
	if(sTEnable)
		P2NCS &= ~pins;
	else
		P2NCS |= pins;
}
//End of STC32G_P2PinsInit(ui8 pins, STC32G_IOMODE mode, BOOL pullUpEnable, BOOL pullDownEnable,BOOL drvEn,BOOL speedHi,BOOL dIEnable, BOOL sTEnable)

//*********************************************************************************************************************************************/
void STC32G_P3PinsInit(ui8 pins, STC32G_IOMODE mode, BOOL pullUpEnable, BOOL pullDownEnable,BOOL drvEn,BOOL speedHi,BOOL dIEnable, BOOL sTEnable)
{
	SysInit();
	switch(mode)
	{
		case BI_IO:
			P3M1 &= ~pins;
			P3M0 &= ~pins;
			break;
		case PP_OUT:
			P3M1 &= ~pins;
			P3M0 |= pins;
			break;
		case HI_IN:
			P3M1 |= pins;
			P3M0 &= pins;
			break;
		case ODR_OUT:
			P3M1 |= pins;
			P3M0 |= pins;
			break;
	}
	if(pullUpEnable)
		P3PU |= pins;
	else
		P3PU &= ~pins;
	if(pullDownEnable)
		P3DR |= pins;
	else
		P3DR &= ~pins;
	if(drvEn)
		P3DR &= ~pins;
	else
		P3DR |= pins;
	if(speedHi)
		P3SR &= ~pins;
	else
		P3SR |= pins;
	if(dIEnable)
		P3IE |= pins;
	else
		P3IE &= ~pins;
	if(sTEnable)
		P3NCS &= ~pins;
	else
		P3NCS |= pins;
}
//End of STC32G_P3PinsInit(ui8 pins, STC32G_IOMODE mode, BOOL pullUpEnable, BOOL pullDownEnable,BOOL drvEn,BOOL speedHi,BOOL dIEnable, BOOL sTEnable)

//**********************************************************************************************************************************************/
void STC32G_P4PinsInit(ui8 pins, STC32G_IOMODE mode, BOOL pullUpEnable, BOOL pullDownEnable,BOOL drvEn,BOOL speedHi,BOOL dIEnable, BOOL sTEnable)
{
	SysInit();
	switch(mode)
	{
		case BI_IO:
			P4M1 &= ~pins;
			P4M0 &= ~pins;
			break;
		case PP_OUT:
			P4M1 &= ~pins;
			P4M0 |= pins;
			break;
		case HI_IN:
			P4M1 |= pins;
			P4M0 &= pins;
			break;
		case ODR_OUT:
			P4M1 |= pins;
			P4M0 |= pins;
			break;
	}
	if(pullUpEnable)
		P4PU |= pins;
	else
		P4PU &= ~pins;
	if(pullDownEnable)
		P4DR |= pins;
	else
		P4DR &= ~pins;
	if(drvEn)
		P4DR &= ~pins;
	else
		P4DR |= pins;
	if(speedHi)
		P4SR &= ~pins;
	else
		P4SR |= pins;
	if(dIEnable)
		P4IE |= pins;
	else
		P4IE &= ~pins;
	if(sTEnable)
		P4NCS &= ~pins;
	else
		P4NCS |= pins;
}
//End of STC32G_P4PinsInit(ui8 pins, STC32G_IOMODE mode, BOOL pullUpEnable, BOOL pullDownEnable,BOOL drvEn,BOOL speedHi,BOOL dIEnable, BOOL sTEnable)

//**********************************************************************************************************************************************/
void STC32G_P5PinsInit(ui8 pins, STC32G_IOMODE mode, BOOL pullUpEnable, BOOL pullDownEnable,BOOL drvEn,BOOL speedHi,BOOL dIEnable, BOOL sTEnable)
{
	SysInit();
	switch(mode)
	{
		case BI_IO:
			P5M1 &= ~pins;
			P5M0 &= ~pins;
			break;
		case PP_OUT:
			P5M1 &= ~pins;
			P5M0 |= pins;
			break;
		case HI_IN:
			P5M1 |= pins;
			P5M0 &= pins;
			break;
		case ODR_OUT:
			P5M1 |= pins;
			P5M0 |= pins;
			break;
	}
	if(pullUpEnable)
		P5PU |= pins;
	else
		P5PU &= ~pins;
	if(pullDownEnable)
		P5DR |= pins;
	else
		P5DR &= ~pins;
	if(drvEn)
		P5DR &= ~pins;
	else
		P5DR |= pins;
	if(speedHi)
		P5SR &= ~pins;
	else
		P5SR |= pins;
	if(dIEnable)
		P5IE |= pins;
	else
		P5IE &= ~pins;
	if(sTEnable)
		P5NCS &= ~pins;
	else
		P5NCS |= pins;
}
//End of STC32G_P5PinsInit(ui8 pins, STC32G_IOMODE mode, BOOL pullUpEnable, BOOL pullDownEnable,BOOL drvEn,BOOL speedHi,BOOL dIEnable, BOOL sTEnable)

//**********************************************************************************************************************************************/
void STC32G_P6PinsInit(ui8 pins, STC32G_IOMODE mode, BOOL pullUpEnable, BOOL pullDownEnable,BOOL drvEn,BOOL speedHi,BOOL dIEnable, BOOL sTEnable)
{
	SysInit();
	switch(mode)
	{
		case BI_IO:
			P6M1 &= ~pins;
			P6M0 &= ~pins;
			break;
		case PP_OUT:
			P6M1 &= ~pins;
			P6M0 |= pins;
			break;
		case HI_IN:
			P6M1 |= pins;
			P6M0 &= pins;
			break;
		case ODR_OUT:
			P6M1 |= pins;
			P6M0 |= pins;
			break;
	}
	if(pullUpEnable)
		P6PU |= pins;
	else
		P6PU &= ~pins;
	if(pullDownEnable)
		P6DR |= pins;
	else
		P6DR &= ~pins;
	if(drvEn)
		P6DR &= ~pins;
	else
		P6DR |= pins;
	if(speedHi)
		P6SR &= ~pins;
	else
		P6SR |= pins;
	if(dIEnable)
		P6IE |= pins;
	else
		P6IE &= ~pins;
	if(sTEnable)
		P6NCS &= ~pins;
	else
		P6NCS |= pins;
}
//End of STC32G_P6PinsInit(ui8 pins, STC32G_IOMODE mode, BOOL pullUpEnable, BOOL pullDownEnable,BOOL drvEn,BOOL speedHi,BOOL dIEnable, BOOL sTEnable)

//**********************************************************************************************************************************************/
void STC32G_P7PinsInit(ui8 pins, STC32G_IOMODE mode, BOOL pullUpEnable, BOOL pullDownEnable,BOOL drvEn,BOOL speedHi,BOOL dIEnable, BOOL sTEnable)
{
	SysInit();
	switch(mode)
	{
		case BI_IO:
			P7M1 &= ~pins;
			P7M0 &= ~pins;
			break;
		case PP_OUT:
			P7M1 &= ~pins;
			P7M0 |= pins;
			break;
		case HI_IN:
			P7M1 |= pins;
			P7M0 &= pins;
			break;
		case ODR_OUT:
			P7M1 |= pins;
			P7M0 |= pins;
			break;
	}
	if(pullUpEnable)
		P7PU |= pins;
	else
		P7PU &= ~pins;
	if(pullDownEnable)
		P7DR |= pins;
	else
		P7DR &= ~pins;
	if(drvEn)
		P7DR &= ~pins;
	else
		P7DR |= pins;
	if(speedHi)
		P7SR &= ~pins;
	else
		P7SR |= pins;
	if(dIEnable)
		P7IE |= pins;
	else
		P7IE &= ~pins;
	if(sTEnable)
		P7NCS &= ~pins;
	else
		P7NCS |= pins;
}
//End of STC32G_P7PinsInit(ui8 pins, STC32G_IOMODE mode, BOOL pullUpEnable, BOOL pullDownEnable,BOOL drvEn,BOOL speedHi,BOOL dIEnable, BOOL sTEnable)

/***************************************************************************************************************************************************/
void STC32G_PortsPinsInit(ui8 ports,ui8 pins, STC32G_IOMODE mode, BOOL pullUpEnable, BOOL pullDownEnable,BOOL drvEn,BOOL speedHi,BOOL dIEnable, BOOL sTEnable)
{
	ui8 i;
	for(i = 0; i < 8; i++)
	{
		if(ports & (1 << i))
		{
			switch(i)
			{
				case 0:
					STC32G_P0PinsInit(pins, mode, pullUpEnable, pullDownEnable,drvEn,speedHi,dIEnable, sTEnable);
					break;
				case 1:
					STC32G_P1PinsInit(pins, mode, pullUpEnable, pullDownEnable,drvEn,speedHi,dIEnable, sTEnable);
					break;
				case 2:
					STC32G_P2PinsInit(pins, mode, pullUpEnable, pullDownEnable,drvEn,speedHi,dIEnable, sTEnable);
					break;
				case 3:
					STC32G_P3PinsInit(pins, mode, pullUpEnable, pullDownEnable,drvEn,speedHi,dIEnable, sTEnable);
					break;
				case 4:
					STC32G_P4PinsInit(pins, mode, pullUpEnable, pullDownEnable,drvEn,speedHi,dIEnable, sTEnable);
					break;
				case 5:
					STC32G_P5PinsInit(pins, mode, pullUpEnable, pullDownEnable,drvEn,speedHi,dIEnable, sTEnable);
					break;
				case 6:
					STC32G_P6PinsInit(pins, mode, pullUpEnable, pullDownEnable,drvEn,speedHi,dIEnable, sTEnable);
					break;
				case 7:
					STC32G_P7PinsInit(pins, mode, pullUpEnable, pullDownEnable,drvEn,speedHi,dIEnable, sTEnable);
					break;
			}
		}
	}
}
//End of STC32G_PortsPinsInit(ui8 ports,ui8 pins, STC32G_IOMODE mode, BOOL pullUpEnable, BOOL pullDownEnable,BOOL drvEn,BOOL speedHi,BOOL dIEnable, BOOL sTEnable)

我也将config.h 做了修改,修改后的代码如下:

/*STC32G_GPIO.h
  Designed by Bill Liu
  Version 0.0 
  Modified last by Bill Liu on 06/23/2022
*/
#ifndef __CONFIG_H__
#define __CONFIG_H__

#define FOSC                    35000000UL
//#define FOSC                    24000000UL
//#define FOSC                    27000000UL
//#define FOSC                    30000000UL
//#define FOSC                    33177600UL



//********************************************************
void SysInit(); //init System speed  fastest


#endif

config.c的源代码如下:

/*config.c
  Designed by Bill Liu
  Version 0.0 
  Modified last by Bill Liu on 06/22/2022
*/

#include <STC32G.h>
#include "config.h"

//********************************************************
void SysInit() //init System speed  fastest
{
	EAXFR = 1;		//visit XFR enable
	CKCON = 0x00; //set outer data bus speed fastest
	WTST = 0x00;  //CPU wait 0 to run program
}
//End of SysInit()

5. I/O配置实例 下面将用我自己写的库函数来演示验证I/O配置。我将以下面这块板来做验证

?这块板用白线隔开将板分成了三个区域,左边为电源区,中间为模拟采样及MCU区域,右边为逻辑输入输出区。再看一下单片机的型号:

?先编译下面的空程序,将其下载到单片机中,看能否下在成功。

/*main.c
  Designed by Bill Liu
  Version 0.0 
  Modified last by Bill Liu on 06/22/2022
*/

#include "main.h"
#include "config.h"

void main()
{
	
}
//End of main()

?下载成功,如下:

?测一下I/O口的输出电压,随意选一个I/O口测试(实际测试的是P73),测试结果如下。

视乎输出了低电平,应该是既不输出高电平也不输出低电平(没电流流入,也没电流流出), 看它能否驱动Led,下面是一个能够正常点亮的LED:

先将接电源端不变,另一端将其 接在与P73连接的排针上,看LED是否任被点亮,结果如下:

LED未被点亮?说明确实没有电流输入。SCT32G上电复位时I/O口处于高阻输入状态,一方面可以增强抗干扰能力,另一方面可以避免该单片机控制的电路载单片机上电复位时误动作。

在main()函数中插入如下代码:

void main()
{
	STC32G_P7PinsInit(PIN3, BI_IO, 0, 0, 0, 0, 0, 0);
	P73 = 0;
	while(1);
}
//End of main()

编译下载,如果库函数没有问题,LED应该亮起,结果如下:

说明库函数没有错误。将main()函数中的代码修改如下:

void main()
{
	STC32G_P7PinsInit(PIN3, BI_IO, 0, 0, 0, 0, 0, 0);
	P73 = 1;
	while(1);
}
//End of main()

?编译下载到单片机,先来测试下双向I/O弱上拉的正向驱动能力。结果如下:

LED仅发出微弱的亮光,说明I/O正向驱动能力弱。现将上拉电阻使能,即库函数的第三个参数设为1,修改main()函数中的代码如下:

void main()
{
	STC32G_P7PinsInit(PIN3, BI_IO, 1, 0, 0, 0, 0, 0);
	P73 = 1;
	while(1);
}
//End of main()

编译下载后的结果如下:

?LED变亮,说明正向驱动能力有所增强。现将驱动电流增大,将下面红框中的变量设为1,看下结果如和变化:

void main()
{
	STC32G_P7PinsInit(PIN3, BI_IO, 0, 0, 1, 0, 0, 0);
	P73 = 1;
	while(1);
}
//End of main()

?编译下载,结果如下:

感觉与双向I/O弱上拉的正向驱动能力差不多。再看一下,打开下拉电阻时双向I/O弱上拉的正向驱动能力。修改main()函数代码如下:

void main()
{
	STC32G_P7PinsInit(PIN3, BI_IO, 0, 1, 0, 0, 0, 0);
	P73 = 1;
	while(1);
}
//End of main()

编译下载到单片机的结果如下:

?感觉LED亮度变化并不明显。

STC32G的 I/O 较STC15? 的I/O新增新的功能与寄存器,从上面的测试可以看出,I/O的负向驱动能力较强(可承受较大的灌电流),正向驱动能力弱,如果要使I/O口有较大的正向驱动能力,需将其设置为推挽模式,如果设置为双向I/O,需使能上拉电阻才能使其正向驱动能力有所提升,使能PxDR对提供双向I/O的正向驱动能力并不明显,可能仅是对提高I/O的电平转换速度有用。

下面再测试一下I/O的读写,将main()函数的代码修改如下:

void main()
{
	STC32G_P7PinsInit(PIN3, BI_IO, 1, 0, 1, 0, 1, 0);
	P73 = 1;
	STC32G_P0PinsInit(0xFF, BI_IO, 0, 0, 0, 0, 1, 1); //init to bidirectional I/O, pull up disable, pull down disable , dive ability enhance disable, 
	//high speed disable, digital singal input enable, schmitt trigger enable
	STC32G_P2PinsInit(0xFF, BI_IO, 0, 0, 0, 0, 1, 1);
    P0 = 0xA0;
    P2 = 0xFF;
	while(1)
	{
		
		if(P2==P0)
			P73 = 1;
		else
			P73 = 0;
	}
}
//End of main()

编译下载后的结果如下:

?LED为P73正向驱动,LED亮,说明P2、P0正常读取数据。现将将main()函数的代码修改如下:

void main()
{
	STC32G_P7PinsInit(PIN3, BI_IO, 1, 0, 1, 0, 1, 0);
	P73 = 1;
	STC32G_P0PinsInit(0xFF, BI_IO, 0, 0, 0, 0, 1, 1); //init to bidirectional I/O, pull up disable, pull down disable , dive ability enhance disable, 
	//high speed disable, digital singal input enable, schmitt trigger enable
	STC32G_P2PinsInit(0xFF, BI_IO, 0, 0, 0, 0, 1, 0);
    P0 = 0xA0;
    P2 = 0xFF;
	while(1)
	{
		
		if(P2==P0)
			P73 = 1;
		else
			P73 = 0;
	}
}
//End of main()

?编译下载结果如下:

?说明即使不使能施密特触发器,依然可以读取数据。

  嵌入式 最新文章
基于高精度单片机开发红外测温仪方案
89C51单片机与DAC0832
基于51单片机宠物自动投料喂食器控制系统仿
《痞子衡嵌入式半月刊》 第 68 期
多思计组实验实验七 简单模型机实验
CSC7720
启明智显分享| ESP32学习笔记参考--PWM(脉冲
STM32初探
STM32 总结
【STM32】CubeMX例程四---定时器中断(附工
上一篇文章      下一篇文章      查看所有文章
加:2022-07-04 23:07:47  更:2022-07-04 23:08:32 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年12日历 -2024/12/29 8:10:15-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码
数据统计