pin_macros.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*----------------------------------------------------------------------------------------------
  2. * pin_macros.h (AVR/avr-gcc)
  3. *----------------------------------------------------------------------------------------------
  4. *
  5. * Author: Oleksandr Redchuk aka ReAl (real@real.kiev.ua)
  6. *
  7. * Description: port bits access macros for AVR uC family (avr-gcc compiler)
  8. *
  9. * Based on macros by Ascold Volkov, Andy Mozzevilov, Aleksey Musin
  10. *----------------------------------------------------------------------------------------------
  11. */
  12. /*
  13. examples
  14. #define Pin1 A,2,H
  15. #define Pin2 B,3,L
  16. ^ ^ ^
  17. port | |
  18. bit \active level
  19. void main(void)
  20. {
  21. DRIVER(Pin1,OUT); //as output
  22. DRIVER(Pin2,IN); //as input
  23. DRIVER(Pin2,PULLUP); // with pullup
  24. ON(Pin1);
  25. if (ACTIVE(Pin2)) OFF(Pin1);
  26. if (LATCH(Pin1)) { //if active level presents on Pin1 for drive something
  27. //do something
  28. }
  29. }
  30. */
  31. #ifndef PIN_MACROS_H
  32. #define PIN_MACROS_H
  33. // assume chip has PORT_TOGGLE_BY_PIN_WRITE feature by default (all new AVR chips)
  34. // check only old-style chips
  35. #if defined(__AVR_AT94K__) \
  36. || defined(__AVR_AT43USB320__) || defined(__AVR_AT43USB355__) \
  37. || defined(__AVR_AT76C711__) || defined(__AVR_AT86RF401__) \
  38. || defined(__AVR_AT90C8534__) || defined(__AVR_AT90S4434__) \
  39. || defined(__AVR_AT90S1200__) || defined(__AVR_AT90S2343__) \
  40. || defined(__AVR_AT90S2333__) || defined(__AVR_AT90S2323__) \
  41. || defined(__AVR_AT90S2313__) || defined(__AVR_AT90S4433__) \
  42. || defined(__AVR_AT90S4414__) \
  43. || defined(__AVR_AT90S8515__) || defined(__AVR_AT90S8535__) \
  44. || defined(__AVR_ATmega8515__) || defined(__AVR_ATmega8535__) \
  45. || defined(__AVR_ATmega103__) \
  46. || defined(__AVR_ATmega64__) || defined(__AVR_ATmega128__) \
  47. || defined(__AVR_ATmega8__) || defined(__AVR_ATmega16__) \
  48. || defined(__AVR_ATmega32__) \
  49. || defined(__AVR_ATtiny11__) || defined(__AVR_ATtiny12__) \
  50. || defined(__AVR_ATtiny15__) || defined(__AVR_ATtiny28__) \
  51. || defined(__AVR_ATtiny22__) || defined(__AVR_ATtiny26__) \
  52. || defined(__AVR_ATmega161__) || defined(__AVR_ATmega162__) \
  53. || defined(__AVR_ATmega163__) || defined(__AVR_ATmega323__) \
  54. || defined(__AVR_ATmega8HVA__) || defined(__AVR_ATmega16HVA__)
  55. # define PORT_TOGGLE_BY_PIN_WRITE 0
  56. #else
  57. # define PORT_TOGGLE_BY_PIN_WRITE 1
  58. #endif
  59. #define PM_BITNUM(port,bit,val) (bit)
  60. #define BITNUM(x) PM_BITNUM(x)
  61. #define BITMASK(x) (1<<PM_BITNUM(x))
  62. #define PM_SETOUT(port,bit) (DDR##port |= (1<<(bit)))
  63. #define PM_SETIN(port,bit) (DDR##port &= ~(1<<(bit)))
  64. #define PM_SETPULLUP(port,bit) (PORT##port |= (1<<(bit)))
  65. #define PM_SETHIZ(port,bit) (PORT##port &= ~(1<<(bit)))
  66. #define PM_DRIVER(port,bit,val,mode) PM_SET##mode(port,bit)
  67. /* dmode = OUT or IN, PULLUP or HIZ */
  68. #define DRIVER(x,mode) PM_DRIVER(x,mode)
  69. #define PM_SETL(port,bit,dummy) (PORT##port &= ~(1<<(bit)))
  70. #define PM_SETH(port,bit,dummy) (PORT##port |= (1<<(bit)))
  71. #define PM_SET(port,bit,val) PM_SET##val(port,bit,dummy)
  72. #define ON(x) PM_SET(x)
  73. #define SET(x) PM_SETH(x)
  74. #define CLR(x) PM_SETL(x)
  75. #define PM_CLRL(port,bit,dummy) PM_SETH(port,bit,dummy)
  76. #define PM_CLRH(port,bit,dummy) PM_SETL(port,bit,dummy)
  77. #define PM_CLR(port,bit,val) PM_CLR##val(port,bit,dummy)
  78. #define OFF(x) PM_CLR(x)
  79. #define PM_PINH(port,bit,dummy) (PIN##port & (1<<(bit)))
  80. #define PM_PINL(port,bit,dummy) !PM_PINH(port,bit,dummy)
  81. #define PM_PIN(port,bit,val) PM_PIN##val(port,bit,dummy)
  82. #define ACTIVE(x) PM_PIN(x)
  83. #define PIN_H(x) PM_PINH(x)
  84. #define PIN_L(x) PM_PINL(x)
  85. #define PM_LATCHH(port,bit,dummy) (PORT##port & (1<<(bit)))
  86. #define PM_LATCHL(port,bit,dummy) !PM_LATCHH(port,bit,dummy)
  87. #define PM_LATCH(port,bit,val) PM_LATCH##val(port,bit,dummy)
  88. #define LATCH(x) PM_LATCH(x)
  89. #define LATCH_H(x) PM_LATCHH(x)
  90. #define LATCH_L(x) PM_LATCHL(x)
  91. #if PORT_TOGGLE_BY_PIN_WRITE
  92. # define PM_CPL(port,bit,val) (PIN##port |= (1 << (bit)))
  93. #else
  94. # define PM_CPL(port,bit,val) (PORT##port ^= (1 << (bit)))
  95. #endif
  96. #define CPL(x) PM_CPL(x)
  97. #define TOGGLE(x) PM_CPL(x)
  98. #endif // PIN_MACROS_H