Directly setting IO pins high or low

See 7495 Shift Register for an example of how these can be used.

DirectIO.h

/* Project: Direct digital pin access for Arduino UNO.
 * File: DirectIO.h
 * Description: Manipulate digital IO pins directly. Depending
 *     method used can be much faster than digitalWrite().
 *
 * Copyright (C) 2014 Marc Symonds
 * All rights reserved.
 *
 * This software may be used and redistributed, with or without
 * modification, as long as it is understood that this software
 * is provided as-is without any explicit or implied warranties
 * of merchantability or fitness of purpose.
 */

#ifndef DirectIO_h
#define DirectIO_h

#include "Arduino.h"

/* These are about 4 times faster than digitalWrite(); */

void DIO_SetPin4(boolean);
void DIO_SetPin7(boolean);
void DIO_SetPin8(boolean);
void DIO_SetPin9(boolean);
void DIO_SetPin10(boolean);
void DIO_SetPin11(boolean);
void DIO_SetPin12(boolean);
void DIO_SetPin13(boolean);

void DIO_SetPinNull(boolean);

void DIO_SetPin(int pin, boolean state);

typedef void (*DIO_SetPinFPtr)(boolean);

DIO_SetPinFPtr DIO_GetSetPinFunction(byte);


/* These are about 30 times faster than using digitalWrite(); */

static inline void PIN4H() { PORTD = PORTD | B00010000; }
static inline void PIN4L() { PORTD = PORTD & B11101111; }

static inline void PIN7H() { PORTD = PORTD | B10000000; }
static inline void PIN7L() { PORTD = PORTD & B01111111; }

static inline void PIN8H() { PORTB = PORTB | B00000001; }
static inline void PIN8L() { PORTB = PORTB & B11111110; }

static inline void PIN9H() { PORTB = PORTB | B00000010; }
static inline void PIN9L() { PORTB = PORTB & B11111101; }

static inline void PIN10H() { PORTB = PORTB | B00000100; }
static inline void PIN10L() { PORTB = PORTB & B11111011; }

static inline void PIN11H() { PORTB = PORTB | B00001000; }
static inline void PIN11L() { PORTB = PORTB & B11110111; }

static inline void PIN12H() { PORTB = PORTB | B00010000; }
static inline void PIN12L() { PORTB = PORTB & B11101111; }

static inline void PIN13H() { PORTB = PORTB | B00100000; }
static inline void PIN13L() { PORTB = PORTB & B11011111; }


/* The inline functions above are as fast as the code below. */

#define PIN13Hi PORTB = PORTB | B00100000
#define PIN13Li PORTB = PORTB & B11011111

#define PIN13Ha asm("SBI 5, 5\n")
#define PIN13La asm("CBI 5, 5\n")

#endif

DirectIO.cpp

/* Project: Direct digital pin access for Arduino UNO.
 * File: DirectIO.cpp
 * Description: Manipulate digital IO pins directly. Depending
 *     method used can be much faster than digitalWrite().
 *
 * Copyright (C) 2014 Marc Symonds
 * All rights reserved.
 *
 * This software may be used and redistributed, with or without
 * modification, as long as it is understood that this software
 * is provided as-is without any explicit or implied warranties
 * of merchantability or fitness of purpose.
 */

#include "Arduino.h"
#include "DirectIO.h"

/*
  Direct digital port manipulation using the PORT variables.
  About 4 times quicker than using digitalWrite().
*/

/* 
  Returns a pointer to the function that can turn set the specified port high or low.

    DIO_SetPinFPtr func;

    func = DIO_getSetPinFunction(4);
    func(true); // Set high.
    func(low); // Set low.

  You could, of course, just as easily call the DIO_set4 fuction directly.
*/

DIO_SetPinFPtr DIO_GetSetPinFunction(byte pin)
{
  switch(pin)
  {
    case 4:
      return DIO_SetPin4;
      break;
      
    case 7:
      return DIO_SetPin7;
      break;
      
    case 8:
      return DIO_SetPin8;
      break;
      
    case 9:
      return DIO_SetPin9;
      break;
      
    case 10:
      return DIO_SetPin10;
      break;
      
    case 11:
      return DIO_SetPin11;
      break;
      
    case 12:
      return DIO_SetPin12;
      break;

    case 13:
      return DIO_SetPin13;
  }

  return DIO_SetPinNull;
}

void DIO_SetPinNull(boolean state)
{
  // Do nothing
}

void DIO_SetPin4(boolean state)
{
  if (state)
    PIN4H();
  else
    PIN4L();
}

void DIO_SetPin7(boolean state)
{
  if (state)
    PIN7H();
  else
    PIN7L();
}

void DIO_SetPin8(boolean state)
{
  if (state)
    PIN8H();
  else
    PIN8L();
}

void DIO_SetPin9(boolean state)
{
  if (state)
    PIN9H();
  else
    PIN9L();
}

void DIO_SetPin10(boolean state)
{
  if (state)
    PIN10H();
  else
    PIN10L();
}

void DIO_SetPin11(boolean state)
{
  if (state)
    PIN11H();
  else
    PIN11L();
}

void DIO_SetPin12(boolean state)
{
  if (state)
    PIN12H();
  else
    PIN12L();
}

void DIO_SetPin13(boolean state)
{
  if (state)
    PIN13H();
  else
    PIN13L();
}

void DIO_SetPin(int pin, boolean state)
{
  if (state)
  {
    switch(pin)
    {
      case 4:
        PIN4H();
        break;
			 
      case 7:
        PIN7H();
        break;
		     
      case 8:
        PIN8H();
        break;
		     
      case 9:
        PIN9H();
        break;
		     
      case 10:
        PIN10H();
        break;
		     
      case 11:
        PIN11H();
        break;
		     
      case 12:
        PIN12H();
        break;
		     
      case 13:
        PIN13H();
        break;
    }
  }
  else
  {
    switch(pin)
    {
      case 4:
        PIN4L();
        break;
			 
      case 7:
        PIN7L();
        break;
		     
      case 8:
        PIN8L();
        break;
		     
      case 9:
        PIN9L();
        break;
		     
      case 10:
        PIN10L();
        break;
		     
      case 11:
        PIN11L();
        break;
		     
      case 12:
        PIN12L();
        break;
		     
      case 13:
        PIN13L();
        break;
    }
 }	    
}

keywords.txt

ShiftReg_7495	KEYWORD1

DIO_SetPin4	KEYWORD2
DIO_SetPin7	KEYWORD2
DIO_SetPin8	KEYWORD2
DIO_SetPin9	KEYWORD2
DIO_SetPin10	KEYWORD2
DIO_SetPin11	KEYWORD2
DIO_SetPin12	KEYWORD2
DIO_SetPin13	KEYWORD2

DIO_SetPin	KEYWORD2

PIN4L	KEYWORD2
PIN4H	KEYWORD2

PIN7L	KEYWORD2
PIN7H	KEYWORD2

PIN8L	KEYWORD2
PIN8H	KEYWORD2

PIN9L	KEYWORD2
PIN9H	KEYWORD2

PIN10L	KEYWORD2
PIN10H	KEYWORD2

PIN11L	KEYWORD2
PIN11H	KEYWORD2

PIN12L	KEYWORD2
PIN12H	KEYWORD2

PIN13L	KEYWORD2
PIN13H	KEYWORD2

Leave a Reply

Your email address will not be published. Required fields are marked *

Comments Protected by WP-SpamShield Spam Blocker