本帖最后由 正版长小强 于 2024-1-2 08:24 编辑
本次更新增加了对 HC89F0541 的端口 配置支持为了自动适配 所使用的 芯片 在.H 文件中 有 芯片头文件 包含检查 ,目前我接触到2款芯片,已经做适配 。
依赖的#include "x_include.h" 文件,主要功能是芯片头文件包含 ,IO 功能分配 , 这样就可与 全局配置IO 比较清晰,防止冲突 。
/****************************************code .C************************************************************/
/**
* ************************************************************************************
* X_GPIO
* ************************************************************************************
* @Examle Version V1.1.0.0
* [url=home.php?mod=space&uid=1102]@demo[/url] Version V1.1.0.0
* @Date 2023.11.01
* ************************************************************************************
* 实现了端口配置 : char Pin_Mode_Set(unsigned char Pin_BCD,enum Pin_Mode Mode)
* 实现了外设端口映射 : char PinRemapConfig(unsigned char Pin_BCD,enum Pin_Remap Remap)
* 实现端口输入状态获取 :char Pin_ReadInputDataBit(unsigned char Pin_BCD)
* ************************************************************************************
**/
#include "x_gpio.h"
/*
功能:引脚功能配置
参数:Pin_BCD 引脚
参数:Mode 模式
返回:0 暂未启用,计划做端口范围异常控制
*/
char Pin_Mode_Set(unsigned char Pin_BCD,enum Pin_Mode Mode)
{
#if defined (__HC89F0322_H__) || defined(__HC89F0411A_H__)
xdata unsigned char Mode_Reset[] = {0XF0,0x0F};
#endif
xdata unsigned char Reg_add = 0X00;
xdata strChar Set_Pin;
Set_Pin.byte = Pin_BCD;
#if defined (__HC89F0322_H__) || defined(__HC89F0411A_H__)
Reg_add = Set_Pin.half.low * 8 + Set_Pin.half.high / 2;
*(&P0M0 + Reg_add ) = (*(&P0M0 + Reg_add )) & Mode_Reset[ Set_Pin.half.high % 2] | ( Mode << (4 * (Set_Pin.half.high % 2)) );
#endif
#if defined (__HC89F0541_H__)
Reg_add = Set_Pin.half.low * 8 + Set_Pin.half.high ;
*(&P0M0 + Reg_add ) = Mode;
#endif
return 0;
}
/*
功能:外设功能引脚映射
参数:Pin_BCD 引脚
参数:Remap 外设
返回:0 暂未启用,计划做端口范围异常控制
*/
char Pin_RemapConfig(unsigned char Pin_BCD,enum Pin_Remap Remap)
{
*(&P0M0 + Remap ) = Pin_BCD ;
return 0;
}
/*
功能:输入状态获取
参数:Pin_BCD 引脚
返回:0 down 1 up
*/
bit Pin_ReadInputDataBit(unsigned char Pin_BCD)
{
bit status = 0;
xdata strChar Set_Pin;
Set_Pin.byte = Pin_BCD;
switch(Set_Pin.half.low)
{
#ifdef GPIO_P0
case 0x00: status = (( P0 >> Set_Pin.half.high ) & 0x01) ; break;
#endif
#ifdef GPIO_P1
case 0x01: status = (( P1 >> Set_Pin.half.high ) & 0x01) ; break;
#endif
#ifdef GPIO_P2
case 0x02: status = (( P2 >> Set_Pin.half.high ) & 0x01) ; break;
#endif
#ifdef GPIO_P3
case 0x03: status = (( P3 >> Set_Pin.half.high ) & 0x01) ; break;
#endif
default: break;
}
return status;
}
/*
功能:输出状态控制
参数:Pin_BCD 引脚
参数:status 0 down 1 up
返回:无
*/
void Pin_SetOutputDataBit(unsigned char Pin_BCD, unsigned char status)
{
xdata strChar Set_Pin;
Set_Pin.byte = Pin_BCD;
switch(Set_Pin.half.low)
{
#ifdef GPIO_P0
case 0x00: P0 &= ~(1 << Set_Pin.half.high);P0 |= (status << Set_Pin.half.high); break;
#endif
#ifdef GPIO_P1
case 0x01: P1 &= ~(1 << Set_Pin.half.high);P1 |= (status << Set_Pin.half.high); break;
#endif
#ifdef GPIO_P2
case 0x02: P2 &= ~(1 << Set_Pin.half.high);P2 |= (status << Set_Pin.half.high); break;
#endif
#ifdef GPIO_P3
case 0x03: P3 &= ~(1 << Set_Pin.half.high);P3 |= (status << Set_Pin.half.high); break;
#endif
default: break;
}
}
/****************************************code .H************************************************************/
/**
* ************************************************************************************
* X_GPIO
* ************************************************************************************
* @Examle Version V1.1.0.0
* @Demo Version V1.1.0.0
* @Date 2023.11.01
* ************************************************************************************
* ************************************************************************************
**/
#ifndef __X_GPIO_H__
#define __X_GPIO_H__
#include "x_include.h"
#if defined(__HC89F0411A_H__) || defined (__HC89F0322_H__) || defined (__HC89F0541_H__)
#define GPIO_P0
#endif
#if defined (__HC89F0322_H__) || defined (__HC89F0541_H__)
#define GPIO_P1
#endif
#if defined(__HC89F0411A_H__) || defined (__HC89F0322_H__) || defined (__HC89F0541_H__)
#define GPIO_P2
#endif
#if defined (__HC89F0541_H__)
#define GPIO_P3
#endif
#if defined (__HC89F0322_H__) || defined(__HC89F0411A_H__)
/*端口模式*/
enum Pin_Mode
{ /*输入模式*/
GPIO_Mode_IN =(unsigned char)0X00,
GPIO_Mode_IPD =(unsigned char)0X01,
GPIO_Mode_IPU =(unsigned char)0X02,
GPIO_Mode_IN_FLOATING =(unsigned char)0X03,
GPIO_Mode_IN_SMT =(unsigned char)0X04, /*施密特*/
GPIO_Mode_IPD_SMT =(unsigned char)0X05, /*施密特*/
GPIO_Mode_IPU_SMT =(unsigned char)0X06, /*施密特*/
/*输出模式*/
GPIO_Mode_OUT_PP =(unsigned char)0X08,
GPIO_Mode_OUT_OD =(unsigned char)0X09,
GPIO_Mode_OUT_OD_PU =(unsigned char)0X0A,
};
#endif
#if defined (__HC89F0541_H__)
#define GPIO_PIN_ENABLE_OUT (unsigned char)0x80
#define GPIO_PIN_ENABLE_IN (unsigned char)0x40
#define GPIO_PIN_ENABLE_UP (unsigned char)0x20
#define GPIO_PIN_ENABLE_DOWN (unsigned char)0x10
#define GPIO_PIN_ENABLE_SMT (unsigned char)0x80
#define GPIO_PIN_ENABLE_OD (unsigned char)0x40
#define GPIO_PIN_ENABLE_20MA (unsigned char)0x00
#define GPIO_PIN_ENABLE_10MA (unsigned char)0x01
#define GPIO_PIN_ENABLE_7MA (unsigned char)0x02
#define GPIO_PIN_ENABLE_4MA (unsigned char)0x03
/*端口模式*/
enum Pin_Mode
{ /*输入模式*/
GPIO_Mode_IN =(unsigned char)(GPIO_PIN_ENABLE_IN),
GPIO_Mode_IPD =(unsigned char)(GPIO_PIN_ENABLE_IN | GPIO_PIN_ENABLE_DOWN),
GPIO_Mode_IPU =(unsigned char)(GPIO_PIN_ENABLE_IN | GPIO_PIN_ENABLE_UP),
GPIO_Mode_IN_FLOATING =(unsigned char)(GPIO_PIN_ENABLE_IN ),
GPIO_Mode_IN_SMT =(unsigned char)(GPIO_PIN_ENABLE_IN | GPIO_PIN_ENABLE_SMT), /*施密特*/
GPIO_Mode_IPD_SMT =(unsigned char)(GPIO_PIN_ENABLE_IN | GPIO_PIN_ENABLE_SMT | GPIO_PIN_ENABLE_DOWN), /*施密特*/
GPIO_Mode_IPU_SMT =(unsigned char)(GPIO_PIN_ENABLE_IN | GPIO_PIN_ENABLE_SMT | GPIO_PIN_ENABLE_UP), /*施密特*/
/*输出模式*/
GPIO_Mode_OUT_PP =(unsigned char)(GPIO_PIN_ENABLE_OUT),
GPIO_Mode_OUT_OD =(unsigned char)(GPIO_PIN_ENABLE_OUT | GPIO_PIN_ENABLE_OD),
GPIO_Mode_OUT_OD_PU =(unsigned char)(GPIO_PIN_ENABLE_OUT | GPIO_PIN_ENABLE_OD | GPIO_PIN_ENABLE_UP),
GPIO_Mode_OUT_PP_20MA =(unsigned char)(GPIO_PIN_ENABLE_OUT | GPIO_PIN_ENABLE_20MA),
GPIO_Mode_OUT_PP_10MA =(unsigned char)(GPIO_PIN_ENABLE_OUT | GPIO_PIN_ENABLE_10MA),
GPIO_Mode_OUT_PP_7MA =(unsigned char)(GPIO_PIN_ENABLE_OUT | GPIO_PIN_ENABLE_7MA),
GPIO_Mode_OUT_PP_4MA =(unsigned char)(GPIO_PIN_ENABLE_OUT | GPIO_PIN_ENABLE_4MA),
GPIO_Mode_OUT_OD_70MA =(unsigned char)(GPIO_PIN_ENABLE_OUT | GPIO_PIN_ENABLE_OD | GPIO_PIN_ENABLE_20MA),
GPIO_Mode_OUT_OD_28MA =(unsigned char)(GPIO_PIN_ENABLE_OUT | GPIO_PIN_ENABLE_OD | GPIO_PIN_ENABLE_10MA),
GPIO_Mode_OUT_OD_14MA =(unsigned char)(GPIO_PIN_ENABLE_OUT | GPIO_PIN_ENABLE_OD | GPIO_PIN_ENABLE_7MA),
GPIO_Mode_OUT_OD_7MA =(unsigned char)(GPIO_PIN_ENABLE_OUT | GPIO_PIN_ENABLE_OD | GPIO_PIN_ENABLE_4MA),
GPIO_Mode_OUT_OD_PU_70MA =(unsigned char)(GPIO_PIN_ENABLE_OUT | GPIO_PIN_ENABLE_OD | GPIO_PIN_ENABLE_UP | GPIO_PIN_ENABLE_20MA),
GPIO_Mode_OUT_OD_PU_28MA =(unsigned char)(GPIO_PIN_ENABLE_OUT | GPIO_PIN_ENABLE_OD | GPIO_PIN_ENABLE_UP | GPIO_PIN_ENABLE_10MA),
GPIO_Mode_OUT_OD_PU_14MA =(unsigned char)(GPIO_PIN_ENABLE_OUT | GPIO_PIN_ENABLE_OD | GPIO_PIN_ENABLE_UP | GPIO_PIN_ENABLE_7MA),
GPIO_Mode_OUT_OD_PU_7MA =(unsigned char)(GPIO_PIN_ENABLE_OUT | GPIO_PIN_ENABLE_OD | GPIO_PIN_ENABLE_UP | GPIO_PIN_ENABLE_4MA),
};
#endif
/*外设重映射 MAP*/
enum Pin_Remap
{ /*MAP*/
Pin_Remap_T0 =(unsigned char)0X80,
Pin_Remap_T1 =(unsigned char)0X81,
Pin_Remap_T3 =(unsigned char)0X83,
Pin_Remap_T4 =(unsigned char)0X84,
Pin_Remap_T5 =(unsigned char)0X85,
Pin_Remap_CLKO_MAP =(unsigned char)0X8F,
Pin_Remap_PWM0 =(unsigned char)0X90,
Pin_Remap_PWM01 =(unsigned char)0X91,
Pin_Remap_PWM1 =(unsigned char)0X94,
Pin_Remap_PWM11 =(unsigned char)0X95,
Pin_Remap_PWM2 =(unsigned char)0X98,
Pin_Remap_PWM21 =(unsigned char)0X99,
Pin_Remap_PWM3 =(unsigned char)0X9C,
Pin_Remap_TXD =(unsigned char)0XA0,
Pin_Remap_RXD =(unsigned char)0XA1,
Pin_Remap_SCL =(unsigned char)0XA2,
Pin_Remap_SDA =(unsigned char)0XA3,
Pin_Remap_SS =(unsigned char)0XA4,
Pin_Remap_SCK =(unsigned char)0XA5,
Pin_Remap_MOSI =(unsigned char)0XA6,
Pin_Remap_MISO =(unsigned char)0XA7,
Pin_Remap_TXD2 =(unsigned char)0XA8,
Pin_Remap_RXD2 =(unsigned char)0XA9,
};
/*
功能: 将Pin_BCD端口配置成指定模式Pin_Mode
例如:将P2.1 配置成推挽输出 Pin_Mode_Set(0X21,GPIO_Mode_OUT_PP);
*/
extern char Pin_Mode_Set(unsigned char Pin_BCD,enum Pin_Mode Mode);
/*
功能:外设功能引脚映射
例如:将P2.1 配置成TXD PinRemapConfig(0X21,Pin_Remap_TXD_MAP);
*/
extern char Pin_RemapConfig(unsigned char Pin_BCD,enum Pin_Remap Remap);
/*高速模式直接操作IO
功能:获取端口状态
例如:将P0.4 配置成TXD Pin_ReadInputDataBit(0X04);
*/
extern bit Pin_ReadInputDataBit(unsigned char Pin_BCD);
/*高速模式直接操作IO
功能:控制端口状态
例如:将P0.4 配置成高电平 Pin_SetOutputDataBit(0X04,1);
*/
extern void Pin_SetOutputDataBit(unsigned char Pin_BCD, unsigned char status);
#endif
|
@21小跑堂
@芯圣电子官方QQ@flybluefox@flybluefox
@芯圣电子官方QQ