StepperII
Dual Axis Stepper Controller

CBaseio.h

00001 
00002 #pragma once
00003 #include "bitops.h"
00004 
00005 #define DECLARE_BASE_IO_CLASS(port, bit)                                                                                        \
00006         BASE_IO(PORT     ## port,                               \
00007                         DDR      ## port,                               \
00008                         PIN      ## port,                               \
00009                          bit,           \
00010                         BASE_IO_ ## port ## bit)                // Class name is BASE_IO_PB
00011                                                                                         // where P = A,B,C... B = 0,1,..
00012 // The USB process threw me a curve.  Bit names are different!
00013 // Just going to use the raw number.  i.e. PB7 -> 7
00014 //                      P        ## port ## bit,                
00015 
00016 //---------------------------------------------------------------------------
00018 /*
00019         Private Bit Operations:
00020                 void setPORT(void)                              PORTx high
00021                 void clearPORT(void)                    PORTx low
00022                 void togglePORT(void)                   Toggle PORTx
00023 
00024                 void setDDR(void)                               DDRx high
00025                 void clearDDR(void)                             DDRx low
00026 
00027                 unsigned char readPIN(void)             Return PINx
00028                 unsigned char readPIN_al(void)  Return inverted PINx
00029 
00030         Protected Operations:
00031                 void initOut()                                  Port out, data low
00032                 void initOut_al()                               Port out, data high (active low)
00033                 void initIn()                                   Port in
00034                 void initPullup()                               Port in, pullup on
00035                 void hiZ(void)                                  Port in, pullup off                                             
00036         
00037                 void on(void)                                   Data high
00038                 void off(void)                                  Data low
00039                 unsigned char read(void)                Return data in
00040         
00041                 void on_al(void)                                Data low
00042                 void off_al(void)                               Data high
00043                 unsigned char read_al(void)             Return inverted data in
00044                 
00045                 void toggle(void)                               Toggle data
00046                 unsigned char toggle(char x)    Toggle and return data
00047                 unsigned char toggle_al(char x) Toggle and return inverted data
00048 */
00049                 
00050 //---------------------------------------------------------------------------
00052 
00053 #define BASE_IO(PORTx, DDRx, PINx, BIT, BASE_NAME)                                                      \
00054 class BASE_NAME                                                 /* Unique class name                    */      \
00055 {                                                                                                                                                       \
00056 private:        /* All bit operations                                                                           */      \
00057         void setPORT(void)                                                                                                              \
00058                 { BIT_SET(PORTx, BIT);          }       /* Set output high                              */      \
00059         void clearPORT(void)                                                                                                    \
00060                 { BIT_CLEAR(PORTx, BIT);        }       /* Set output low                               */      \
00061         void togglePORT(void)                                                                                                   \
00062                 { BIT_TOGGLE(PORTx, BIT);       }       /* Toggles the output bit               */      \
00063         void togglePORT_MX(void)                                                                                                \
00064                 { BIT_TOGGLE_MX(PINx, BIT); }   /* Write 1 to PIN to toggle             */      \
00065         void setDDR(void)                                                                                                               \
00066                 { BIT_SET(DDRx, BIT);           }       /* Set pin direction to out     */      \
00067         void clearDDR(void)                                                                                                             \
00068                 { BIT_CLEAR(DDRx, BIT);         }       /* Set pin direction to in              */      \
00069         unsigned char readPIN(void)                                                                                             \
00070         {                                                                                                                                               \
00071                 unsigned char retVal;                                                                                           \
00072                 retVal = BIT_READ(PINx, BIT);                                                                           \
00073                 return retVal;                                  /* Creates minimum code                 */      \
00074         }                                                                                                                                               \
00075         unsigned char readPIN_al(void)                                                                                  \
00076         {                                                                                                                                               \
00077                 unsigned char retVal;                                                                                           \
00078                 retVal = BIT_READ_AL(PINx, BIT);                                                                        \
00079                 return retVal;                                  /* Creates minimum code                 */      \
00080         }                                                                                                                                               \
00081 protected:                                                                                                                                      \
00082         BASE_NAME()                                                     /* Constructor                                  */      \
00083                 {                                                       }                                                                               \
00084         void initOut()                                                                                                                  \
00085                 { clearPORT(); setDDR();        }       /* Set pin direction to output  */      \
00086         void initOut_al()                                                                                                               \
00087                 { setPORT(); setDDR();          }       /* Set pin direction to output  */      \
00088         void initIn()                                                                                                                   \
00089                 { clearDDR();                           }       /* Set pin direction to input   */      \
00090         void initPullup()                                                                                                               \
00091                 { clearDDR(); setPORT();        }       /* Set pin to input     w/pullup        */      \
00092         void hiZ(void)                                                                                                                  \
00093                 { clearDDR(); clearPORT();      }       /* Input with pullup off                */      \
00094         /* Active high methods for on(), off(), and read()                                      */      \
00095         void on(void)                                                                                                                   \
00096                 { setPORT();                            }       /* Set output "active high"             */      \
00097         void off(void)                                                                                                                  \
00098                 { clearPORT();                          }       /* Set output "inactive low"    */      \
00099         unsigned char read(void)                        /* Return "active high" read    */      \
00100                 { return readPIN();                     }                                                                               \
00101         /* Active low methods for on(), off(), and read()                                       */      \
00102         void on_al(void)                                                                                                                \
00103                 { clearPORT();                          }       /* Set output "active low"              */      \
00104         void off_al(void)                                                                                                               \
00105                 { setPORT();                            }       /* Set output "inactive high"   */      \
00106         unsigned char read_al(void)                     /* Return "active low" read             */      \
00107                 { return readPIN_al();          }                                                                               \
00108         void toggle(void)                                                                                                               \
00109                 { togglePORT();                         }       /* Toggles the output bit               */      \
00110         unsigned char toggle(char x)                                                                                    \
00111         {                                                                                                                                               \
00112                 toggle();                                                                                                                       \
00113                 return read();                                                                                                          \
00114         }                                                                                                                                               \
00115         unsigned char toggle_al(char x)                                                                                 \
00116         {                                                                                                                                               \
00117                 toggle();                                                                                                                       \
00118                 return read_al();                                                                                                       \
00119         }                                                                                                                                               \
00120         void toggleMX(void)                                                                                                             \
00121                 { togglePORT_MX();                      }       /* Toggles the output bit               */      \
00122 };
00123 
00124 //---------------------------------------------------------------------------
00125 // End of File
00126 //---------------------------------------------------------------------------
 All Classes Files Functions Variables Defines