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

📄 liftoperationmonitor.c.txt

📁 这是《嵌入式linux-硬件、软件与接口》一书对应的所有linux方面实例的源代码
💻 TXT
字号:
/*
 * liftoperationmonitor v0.1 9/25/01
 * www.embeddedlinuxinterfacing.com
 *
 * The original location of this code is
 * http://www.embeddedlinuxinterfacing.com/chapters/07/liftoperationmonitor.c
 *
 *
 * Copyright (C) 2001 by Craig Hollabaugh
 *
 * This program is free software; you can redistribute it and/or modify 
 * it under the terms of the GNU Library General Public License as 
 * published by the Free Software Foundation; either version 2 of the 
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but 
 * WITHOUT ANY WARRANTY; without even the implied warranty of 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public 
 * License along with this program; if not, write to the 
 * Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

/* liftoperationmonitor
 * liftoperationmonitor uses inb and outb to control an interface
 * circuit connected the PC parallel printer port. The port's 
 * control port controls the input/output operation on the 
 * interface circuit's data bus.
 */

/*
remember to compile with -O2 for proper inb/outb macro expansion
gcc -O2 -o liftoperationmonitor liftoperationmonitor.c
*/


#include <asm/io.h>

#define SPPDATAPORT    0x378
#define SPPSTATUSPORT  (SPPDATAPORT + 1)
#define SPPCONTROLPORT (SPPDATAPORT + 2)

/* these are the control port bit defs */
#define OUTPUTENABLE 0x02
#define OUTPUTLATCH  0x04
#define INPUTENABLE  0x08
#define SPPPORTREAD  0x20

int main(void)
{
  unsigned char v,i;

/* get permission from the OS to use the 
 * data, status and control ports*/
  if (ioperm(SPPDATAPORT, 3, 1))
  {
    perror("ioperm");
    exit(1);
  }

/* this asserts three items.
 * 1. SSPPORTREAD, this configures the bidirectional data port as input
 * 2. INPUTENABLE, this enables the input buffer to drive the data bus
 * 3. OUTPUTENABLE, enable the output latch, driving the output rack
 *                  if you don't assert this, the output modules will 
 *                  turn off
 */
  outb(SPPPORTREAD |  INPUTENABLE | OUTPUTENABLE,SPPCONTROLPORT); 

/* The input buffer is now driving the bus, so do a read */
  v = inb(SPPSTATUSPORT);

/* Deassert SPPORTREAD and INPUTENABLE. 
 * Use OUTPUTENABLE to keep output latch enabled 
 */
  outb(OUTPUTENABLE  ,SPPCONTROLPORT);

/* loop through the bits in v and output 8 0s or 1s, like 01000001
 * MSB, D7 is output first
 */
  for (i = 0; i < 8; i++)
  {
    if ( v & 0x80 )
      putchar('1');
    else
      putchar('0');
      
    v <<= 1; /* shift bits */
  } 

/* let OS know we're done with the port */
  if (ioperm(SPPDATAPORT, 3, 0))
  {
    perror("ioperm");
    exit(1);
  }
}

⌨️ 快捷键说明

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