Created
March 8, 2012 13:43
-
-
Save YuuichiAkagawa/2001034 to your computer and use it in GitHub Desktop.
avrpindef.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//***************************************************************************** | |
// | |
// avrpindef.h - AVR pin definition template | |
// | |
// Copyright(C) 2012 Yuuichi Akagawa | |
// | |
/* This code was inspired by the Konstantin Chizhov's AVR port templates */ | |
#ifndef _avrpindef_h_ | |
#define _avrpindef_h_ | |
#include <avr/io.h> | |
// this class represents one pin in a IO port. | |
// It is fully static. | |
//Port definitions for AtTiny, AtMega families. | |
#define DECLARE_PORT(CLASS_NAME, PORT_NAME, DDR_NAME, PIN_NAME) \ | |
class CLASS_NAME{\ | |
public:\ | |
static uint8_t PinRead() {return PIN_NAME;}\ | |
static uint8_t Read() {return PORT_NAME;}\ | |
static void Set(uint8_t value) {PORT_NAME |= value;}\ | |
static void Clear(uint8_t value) {PORT_NAME &= ~value;}\ | |
static void Write(uint8_t value) {PORT_NAME = value;}\ | |
static void DirSet(uint8_t value) {DDR_NAME |= value;}\ | |
static void DirClear(uint8_t value){DDR_NAME &= ~value;}\ | |
static void DirWrite(uint8_t value){DDR_NAME = value;}\ | |
}; | |
#define DECLARE_PORT_PINS(PORT_TYPE_NAME, PIN_NAME_PREFIX) \ | |
typedef TPin<PORT_TYPE_NAME, 0> PIN_NAME_PREFIX ## 0;\ | |
typedef TPin<PORT_TYPE_NAME, 1> PIN_NAME_PREFIX ## 1;\ | |
typedef TPin<PORT_TYPE_NAME, 2> PIN_NAME_PREFIX ## 2;\ | |
typedef TPin<PORT_TYPE_NAME, 3> PIN_NAME_PREFIX ## 3;\ | |
typedef TPin<PORT_TYPE_NAME, 4> PIN_NAME_PREFIX ## 4;\ | |
typedef TPin<PORT_TYPE_NAME, 5> PIN_NAME_PREFIX ## 5;\ | |
typedef TPin<PORT_TYPE_NAME, 6> PIN_NAME_PREFIX ## 6;\ | |
typedef TPin<PORT_TYPE_NAME, 7> PIN_NAME_PREFIX ## 7; | |
// this class represents one pin in a IO port. | |
template<typename PORT, uint8_t PIN> | |
class TPin | |
{ | |
public: | |
static void Set() | |
{ | |
PORT::Set(1 << PIN); | |
} | |
static void Set(uint8_t val) | |
{ | |
if(val) | |
Set(); | |
else | |
Clear(); | |
} | |
static void Clear() | |
{ | |
PORT::Clear(1 << PIN); | |
} | |
static void SetDir(uint8_t val) | |
{ | |
if(val) | |
SetDirWrite(); | |
else | |
SetDirRead(); | |
} | |
static void SetDirRead() | |
{ | |
PORT::DirClear(1 << PIN); | |
} | |
static void SetDirWrite() | |
{ | |
PORT::DirSet(1 << PIN); | |
} | |
static uint8_t IsSet() | |
{ | |
return PORT::PinRead() & (uint8_t)(1 << PIN); | |
} | |
}; //class TPin... | |
#ifdef PORTA | |
DECLARE_PORT(Porta, PORTA, DDRA, PINA) | |
DECLARE_PORT_PINS(Porta, Pa) | |
#endif | |
#ifdef PORTB | |
DECLARE_PORT(Portb, PORTB, DDRB, PINB) | |
DECLARE_PORT_PINS(Portb, Pb) | |
#endif | |
#ifdef PORTC | |
DECLARE_PORT(Portc, PORTC, DDRC, PINC) | |
DECLARE_PORT_PINS(Portc, Pc) | |
#endif | |
#ifdef PORTD | |
DECLARE_PORT(Portd, PORTD, DDRD, PIND) | |
DECLARE_PORT_PINS(Portd, Pd) | |
#endif | |
#ifdef PORTE | |
DECLARE_PORT(Porte, PORTE, DDRE, PINE) | |
DECLARE_PORT_PINS(Porte, Pe) | |
#endif | |
#ifdef PORTF | |
DECLARE_PORT(Portf, PORTF, DDRF, PINF) | |
DECLARE_PORT_PINS(Portf, Pf) | |
#endif | |
#ifdef PORTG | |
DECLARE_PORT(Portg, PORTG, DDRG, PING) | |
DECLARE_PORT_PINS(Portg, Pg) | |
#endif | |
#ifdef PORTH | |
DECLARE_PORT(Porth, PORTH, DDRH, PINH) | |
DECLARE_PORT_PINS(Porth, Ph) | |
#endif | |
#ifdef PORTJ | |
DECLARE_PORT(Portj, PORTJ, DDRJ, PINJ) | |
DECLARE_PORT_PINS(Portj, Pj) | |
#endif | |
#ifdef PORTK | |
DECLARE_PORT(Portk, PORTK, DDRK, PINK) | |
DECLARE_PORT_PINS(Portk, Pk) | |
#endif | |
#ifdef PORTL | |
DECLARE_PORT(Portl, PORTL, DDRL, PINL) | |
DECLARE_PORT_PINS(Portl, Pl) | |
#endif | |
#ifdef PORTQ | |
DECLARE_PORT(Portq, PORTQ, DDRQ, PINQ) | |
DECLARE_PORT_PINS(Portq, Pq) | |
#endif | |
#ifdef PORTR | |
DECLARE_PORT(Portr, PORTR, DDRR, PINR) | |
DECLARE_PORT_PINS(Portr, Pr) | |
#endif | |
#endif //_avrpindef_h_ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment