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

📄 funio.c

📁 dsp2407的函数库
💻 C
字号:
/****************************************************************
*	funio.c						See funlib.h for instuctions	*
*	Dean Tsai                  	about using this file.          *
*	10/12/02                                                    *
*																*
*	Reference: TI Reference book Chapter 5.						*
*                                                               *
*  	There are 47 digital IOs total.  IOPA, IOPB, IOPC, IOPD, 	*
*	IOPE each has 8, and IOPF has 7.							*
*																*
*	The functions here support all but IOPD1 >> 7.  Therefore, 	*
*	without modifying the functions, you should be able to use 	*
*	40 IOs.  If you wish to use IOPD 1 >> 7, you can easily 	*
*	modify the function.  However, be sure to test IOPD's before*
*	you use them.												*
*																*
*	For a given one of these supported pins, use the function 	*
*	initIO to set the function for that pin to be digital i/o,  *
*	and also to select the data direction for that pin to be 	*
*	either input or output.  IOinit takes as arguments the port,*
*	channel, and data direction.  For the port argument use one *
*	of the macros IOPA, IOPB, IOPC, IOPD, IOPE, or IOPF.  For 	*
*	the direction argument use one of the macros INPUT or 		*
*	OUTPUT.  These are defined in the header file.				*
*																*
*	initIO returns -1 if you pass an invalid port or channel.	*
*																*
*	Example: to configure IOPB7 as an input pin:				*
*	status = initIO(IOPB, 7, INPUT);							*
*																*
*	Example: to configure IOPA0 as an ouptut pin:				*
*	status = initIO(IOPA, 0, OUTPUT);                           *
*		             ^    ^										*
*				  letter number									*
*																*
*	To read the value of a digital i/o pin, first use initIO to *
*	configure that pin for input, and then call getIO.  This 	*
*	function returns 1 when the pin is high and 0 when the pin 	*
*	is low.														*
*																*
*	Example: to get the value of pin IOPA0:						*
*	val = getIO(IOPA, 0);										*
*																*
*	To set a digital i/o pin, first use IOinit to configure 	*
*	that pin for output, and then call setIO with the desired 	*
*	value, 1 or 0.												*
*																*
*	Example: to set pin IOPA0 high:								*
*	status = setIO(IOPA, 0, 1);									*
*																*
*	For the direction bit in PxDATDIR:  0=input, 1=output.		*
****************************************************************/

#include        "f2407_c.h"             /* Defines register names and addresses */
#include		<math.h>                /* Enables the DSP to do certain math functions */
#include		"funlib.h"    
    

/* initIO:  Set up one of the digital I/O pins for either input 
			or output.  */
    
int initIO(int port, int ch, int direction){
  switch(port){

  case IOPA:
    /* check valid channel for Port A */
    if(ch<0 || ch>7)
      return -1;
    /* select i/o function */
    *MCRA &= ~(1<<ch);
    /* set data direction */
    if(direction==OUTPUT)
      *PADATDIR |= (1<<(ch+8));
    else
      *PADATDIR &= ~(1<<(ch+8));
    break;

  case IOPB:
    /* check valid channel for Port B */
    if(ch<0 || ch>7)
      return -1;
    /* select i/o function */
    *MCRA &= ~(1<<(ch+8));
    /* set data direction */
    if(direction==OUTPUT)
      *PBDATDIR |= (1<<(ch+8));
    else
      *PBDATDIR &= ~(1<<(ch+8));
    break;

  case IOPC:
    /* check valid channel for Port C */
    if(ch<0 || ch>7)
      return -1;
    /* select i/o function */
    *MCRB &= ~(1<<ch);
    /* set data direction */
    if(direction==OUTPUT)
      *PCDATDIR |= (1<<(ch+8));
    else
      *PCDATDIR &= ~(1<<(ch+8));
    break;        
    
  case IOPD:
    /* check valid channel for Port D */
    if(ch<0 || ch>0)
      return -1;
    /* select i/o function */
     *MCRB &= ~(1<<(ch+8));
    /* set data direction */
    if(direction==OUTPUT)
      *PDDATDIR |= (1<<(ch+8));
    else
      *PDDATDIR &= ~(1<<(ch+8));
    break;
  
  case IOPE:
    /* check valid channel for Port E */
    if(ch<0 || ch>7)
      return -1;
    /* select i/o function */
    *MCRC &= ~(1<<ch);
    /* set data direction */
    if(direction==OUTPUT)
      *PEDATDIR |= (1<<(ch+8));
    else
      *PEDATDIR &= ~(1<<(ch+8));
    break;
 
  case IOPF:
    /* check valid channel for Port F */
    if(ch<0 || ch>6)
      return -1;
    /* select i/o function */
    *MCRC &= ~(1<<(ch+8));
    /* set data direction */
    if(direction==OUTPUT)
      *PFDATDIR |= (1<<(ch+8));
    else
      *PFDATDIR &= ~(1<<(ch+8));
    break;
                                     
  default:
    return -1; /* invalid Port */
  }

  return 0;
}

/* getIO:  Read the value of a digital I/O pin that has been 
			configured for input. */

int getIO(int port, int ch){
  switch(port){

  case IOPA:
    /* check valid channel for Port A */
    if(ch<0 || ch>7)
      return -1;
    return (*PADATDIR>>ch & 0x0001);
    break;

  case IOPB:
    /* check valid channel for Port B */
    if(ch<0 || ch>7)
      return -1;
    return (*PBDATDIR>>ch & 0x0001);
    break;

  case IOPC:
    /* check valid channel for Port C */
    if(ch<0 || ch>7)
      return -1;
    return (*PCDATDIR>>ch & 0x0001);
    break;
  
  case IOPD:
    /* check valid channel for Port D */
    if(ch<0 || ch>0)
      return -1;
    return (*PDDATDIR>>ch & 0x0001);
    break;

  case IOPE:
    /* check valid channel for Port E */
    if(ch<0 || ch>7)
      return -1;
    return (*PEDATDIR>>ch & 0x0001);
    break;

  case IOPF:
    /* check valid channel for Port F */
    if(ch<0 || ch>6)
      return -1;
    return (*PFDATDIR>>ch & 0x0001);
    break;
     
  default:
    return -1; /* invalid Port */
  }

  return 0;
}

/* setIO:  Write the value of a digital I/O pin that has been
			configured for output. */
			
int setIO(int port, int ch, int val){
  switch(port){

  case IOPA:
    /* check valid channel for Port A */
    if(ch<0 || ch>7)
      return -1;
    if(val==0)
      *PADATDIR &= ~(1<<ch);
    else
      *PADATDIR |= (1<<ch);
    break;

  case IOPB:
    /* check valid channel for Port B */
    if(ch<0 || ch>7)
      return -1;
    if(val==0)
      *PBDATDIR &= ~(1<<ch);
    else
      *PBDATDIR |= (1<<ch);
    break;

  case IOPC:
    /* check valid channel for Port C */
    if(ch<0 || ch>7)
      return -1;
    if(val==0)
      *PCDATDIR &= ~(1<<ch);
    else
      *PCDATDIR |= (1<<ch);
    break;
    
  case IOPD:
    /* check valid channel for Port D */
    if(ch<0 || ch>0)
      return -1;
    if(val==0)
      *PDDATDIR &= ~(1<<ch);
    else
      *PDDATDIR |= (1<<ch);
    break;

  case IOPE:
    /* check valid channel for Port E */
    if(ch<0 || ch>7)                 
      return -1;
    if(val==0)
      *PEDATDIR &= ~(1<<ch);
    else
      *PEDATDIR |= (1<<ch);
    break;

  case IOPF:
    /* check valid channel for Port F */
    if(ch<0 || ch>6)
      return -1;
    if(val==0)
      *PFDATDIR &= ~(1<<ch);
    else
      *PFDATDIR |= (1<<ch);
    break;
 
  default:
    return -1; /* invalid Port */
  }

  return 0;
}

⌨️ 快捷键说明

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