⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 io.c

📁 ST7很实用的例子
💻 C
字号:
/*****************************************************************************
IO子程序(以下子程序参考或采用ST公司MCD Application Team提供的相关子程序)
******************************************************************************/

#include "ST7lib_config.h"                            /* 芯片选择 */
#include "IO_hr.h"                              			/* IO寄存器位定义 */
#include "IO.h"             													/* IO函数原型定义 */

/***************************端口寄存器地址 ******************************/
@tiny static unsigned char * const PortAddr[]={&PADR,&PBDR};


/*-----------------------------------------------------------------------------
子程序名称   :  IO_Init
输入         :  无
输出         :  无	       
描述         :  初始化所有的端口寄存器。
注释         :  在做IO操作时必须先运行该程序。
-----------------------------------------------------------------------------*/ 
void IO_Init (void)
{
    PADDR = 0x00 ;
    PBDDR = 0x00 ;
    PAOR  = 0x00 ;  
    PBOR  = 0x00 ;   
}                                                        
                                                        
/*-----------------------------------------------------------------------------
子程序名称   : IO_Input
输入         : 输入端口模式、端口名称、端口引脚号
输出         : 无	       
描述         : 配置所选的端口的引脚为输入模式
注释         : 无
-----------------------------------------------------------------------------*/
void IO_Input (IO_Input_Mode Input_Val, IO_Port Port_Val1, IO_Pin Pin_Val1)
{                  
    volatile unsigned char *PortDDR, *PortOR;

    PortDDR = (PortAddr[Port_Val1]+1) ;
    
    *PortDDR &= (unsigned char)(~(unsigned char)( Pin_Val1)) ;
    
    PortOR = (PortAddr[Port_Val1]+2);
    
    if (Input_Val == IO_FLOATING )
    {                                     /* 配置为浮空(float)输入模式 */
        *PortOR &= (unsigned char)(~(unsigned char)( Pin_Val1 )) ;
    }
    else         
    {                                    /* 配置为上拉(pull-up)输入模式 */ 
        *PortOR |= (unsigned char)(Pin_Val1) ;           
    }    

} 

/*-----------------------------------------------------------------------------
子程序名称   : IO_Output
输入         : 输出端口模式、端口名称、端口引脚号 
输出         : 无	       
描述         : 配置所选的端口的引脚为输出模式 
注释         : 无
-----------------------------------------------------------------------------*/
void IO_Output (IO_Output_Mode Output_Val,IO_Port Port_Val2, IO_Pin Pin_Val2)
{                   

    volatile unsigned char *PortDDR, *PortOR;
    
    PortDDR = PortAddr[Port_Val2]+1;
    *PortDDR |= (unsigned char)Pin_Val2 ;  /* 端口配置为输出模式 */    
    PortOR = PortAddr[Port_Val2]+2;
    if (Output_Val == IO_OPEN_DRAIN )
    {                                /* 配置为open-drain输出模式 */
        *PortOR &= (unsigned char)(~(unsigned char)( Pin_Val2 )) ;
    }
    else
    {                                 /* 配置为push-pull输出模式 */
        *PortOR |= (unsigned char)(Pin_Val2 ) ;           
    }    
		
}    
 
/*-----------------------------------------------------------------------------
子程序名称   : IO_Read
输入         : 端口名称
输出         : 返回所选端口数据寄存器的状态
描述         : 读端口并给用户返回值
注释         : 无
-----------------------------------------------------------------------------*/
unsigned char IO_Read (IO_Port Read_Val)
{   
    volatile unsigned char *PortDR;

    PortDR = PortAddr[Read_Val];
    return *PortDR;
}

/*-----------------------------------------------------------------------------
子程序名称   : IO_Write
输入         : 端口名称、端口引脚号、要写的数据(高/低)
输出         : 无
描述         : 把数据写入端口寄存器
注释         : 无
-----------------------------------------------------------------------------*/
void IO_Write (IO_Port Port_Val3,IO_Pin Pin_Val3, IO_Write_Data Data_Val)
{
    volatile unsigned char *PortDR;
    
    PortDR = PortAddr[Port_Val3];

    if (Data_Val == IO_DATA_HIGH )
    {                            /* 在所指定的引脚写入逻辑高 */
        *PortDR |= (unsigned char)Pin_Val3 ; 
    } 
    else if (Data_Val == IO_DATA_TOGGLE )
    {                                
        *PortDR ^= (unsigned char)(Pin_Val3) ;  /* 切换所选的端口引脚 */                                   
    }
    else
    {                             /* 在所指定的引脚写入逻辑低 */
        *PortDR &= (unsigned char)(~(unsigned char)(Pin_Val3)) ;
    }
}  

/*-----------------------------------------------------------------------------
子程序名称   : IO_ByteWrite
输入         : 端口名称、要写入的数据字节
输出         : 无
描述         : 写数据到端口寄存器
注释         : 无
-----------------------------------------------------------------------------*/
void IO_ByteWrite (IO_Port Port_Val4,unsigned char IO_ByteData)
{   
    volatile unsigned char *PortDR;

    PortDR = PortAddr[Port_Val4];
    *PortDR = IO_ByteData;
	

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -