» Advertenties

Zo 20 Mei 2012, 07:50

C code -
  1. //*****************************************************************************
  2. //
  3. // File Name : g.h
  4. // Title : global usefull define and typedef
  5. // Author : Smoerijf.be (Orinal by Pascal Stang - edit by smoerijf)
  6. // Target MCU : Atmel AVR Series
  7. //
  8. //*****************************************************************************
  9. //@{
  10.  
  11. #ifndef GLOBAL_H
  12. #define GLOBAL_H
  13.  
  14. #include <avr/io.h>
  15.  
  16.  
  17. // Code compatibility to new AVR-libc
  18. // outb(), inb(), inw(), outw(), BV(), sbi(), cbi(), sei(), cli()
  19. #ifndef outb
  20.   #define   outb(addr, data)   addr = (data)
  21. #endif
  22. #ifndef inb
  23.   #define   inb(addr)       (addr)
  24. #endif
  25. #ifndef outw
  26.   #define   outw(addr, data)   addr = (data)
  27. #endif
  28. #ifndef inw
  29.   #define   inw(addr)       (addr)
  30. #endif
  31. #ifndef BV
  32.   #define BV(bit)       (1<<(bit))
  33. #endif
  34. #ifndef cbi
  35.   #define cbi(reg,bit)   reg &= ~(BV(bit))
  36. #endif
  37. #ifndef sbi
  38.   #define sbi(reg,bit)   reg |= (BV(bit))
  39. #endif
  40. #ifndef cli
  41.   #define cli()       __asm__ __volatile__ ("cli" ::)
  42. #endif
  43. #ifndef sei
  44.   #define sei()       __asm__ __volatile__ ("sei" ::)
  45. #endif
  46.  
  47. // support for individual port pin naming in the mega128
  48. // see port128.h for details
  49. #ifdef __AVR_ATmega128__
  50.   // not currently necessary due to inclusion
  51.   // of these defines in newest AVR-GCC
  52.   // do a quick test to see if include is needed
  53.   #ifndef PD0
  54.     #include "port128.h"
  55.   #endif
  56. #endif
  57.  
  58. // use this for packed structures
  59. // (this is seldom necessary on an 8-bit architecture like AVR,
  60. // but can assist in code portability to AVR)
  61. #define GNUC_PACKED __attribute__((packed))
  62.  
  63. // port address helpers
  64. #define DDR(x) (*(&x - 1)) // address of data direction register of port x
  65. #define PIN(x) (*(&x - 2)) // address of input register of port x
  66.  
  67. // MIN/MAX/ABS macros
  68. #define MIN(a,b)       ((a<b)?(a):(b))
  69. #define MAX(a,b)       ((a>b)?(a):(b))
  70. #define ABS(x)         ((x>0)?(x):(-x))
  71.  
  72. // constants
  73. #define PI     3.14159265359
  74.  
  75. //! read bit in
  76. unsigned char rbi(unsigned char addr, unsigned char bit)
  77. {
  78.   return ((addr & BV(bit)) > 0);
  79. }
  80.  
  81. //! toggle bit
  82. #define tbi(port, bit) ((rbi(port, bit)) ? (cbi(port, bit)) : (sbi(port, bit)))
  83.  
  84.  
  85.  
  86. #ifndef WIN32
  87.   // true/false defines
  88.   #define FALSE   0
  89.   #define TRUE   -1
  90. #endif
  91.  
  92. // datatype definitions macros
  93. typedef unsigned char u08;
  94. typedef signed char s08;
  95. typedef unsigned short u16;
  96. typedef signed short s16;
  97. typedef unsigned long u32;
  98. typedef signed long s32;
  99. typedef unsigned long long u64;
  100. typedef signed long long s64;
  101.  
  102. // maximum value that can be held
  103. // by unsigned data types (8,16,32bits)
  104. #define MAX_U08   255
  105. #define MAX_U16   65535
  106. #define MAX_U32   4294967295
  107.  
  108. // maximum values that can be held
  109. // by signed data types (8,16,32bits)
  110. #define MIN_S08   -128
  111. #define MAX_S08   127
  112. #define MIN_S16   -32768
  113. #define MAX_S16   32767
  114. #define MIN_S32   -2147483648
  115. #define MAX_S32   2147483647
  116.  
  117. #ifndef WIN32
  118.   // more type redefinitions
  119.   typedef unsigned char BOOL;
  120.   typedef unsigned char   BYTE;
  121.   typedef unsigned int   WORD;
  122.   typedef unsigned long   DWORD;
  123.  
  124.   typedef unsigned char   UCHAR;
  125.   typedef unsigned int   UINT;
  126.   typedef unsigned short USHORT;
  127.   typedef unsigned long   ULONG;
  128.  
  129.   typedef char       CHAR;
  130.   typedef int         INT;
  131.   typedef long       LONG;
  132. #endif
  133.  
  134.  
  135. #define CYCLES_PER_US ((F_CPU+500000)/1000000)   // cpu cycles per microsecond
  136.  
  137. #endif
Laatste wijziging: Wo 8 April 2009, 20:38