2012年8月19日 星期日

AtmegaXXX系列 I/O port 位元操作

1)AtmegaXXX I/o port中不能用  PINA.0 or PORTA.2 這樣的定址法操作

2)在IAR C  compiler 中改用如下方式:

  * An example showing the SFR_B() macro call, the expanded result and usage of this result:
  *
  * SFR_B_R(0x1F,   AVR) Expands to:
   __io union {
              unsigned char AVR;                 // The sfrb as 1 byte
              struct {                                         // The sfrb as 8 bits
                      unsigned char AVR_Bit0:1,
                                    AVR_Bit1:1,
                                    AVR_Bit2:1,
                                    AVR_Bit3:1,
                                    AVR_Bit4:1,
                                    AVR_Bit5:1,
                                    AVR_Bit6:1,
                                    AVR_Bit7:1;
                     };
             } @ 0x1F;
  Examples of how to use the expanded result:
 a) 用   AVR |= (1<<5);           //設定AVR暫存器bit5=1  b)用   AVR_Bit5 = 1;             //設定AVR暫存器bit5=1 
* An example showing the SFR_B_N() macro call,  the expanded result and usage of this result:
  SFR_B_N(0x25,  TCCR2, FOC2, WGM20, COM21, COM20, WGM21, CS22, CS21, CS20)
   Expands to:
   __io union {
               unsigned char TCCR2;
               struct {
                       unsigned char TCCR2_Bit0:1,
                                     TCCR2_Bit1:1,
                                     TCCR2_Bit2:1,
                                     TCCR2_Bit3:1,
                                     TCCR2_Bit4:1,
                                     TCCR2_Bit5:1,
                                     TCCR2_Bit6:1,
                                     TCCR2_Bit7:1;
                      };
               struct {
                       unsigned char TCCR2_CS20:1,
                                     TCCR2_CS21:1,
                                     TCCR2_CS22:1,
                                     TCCR2_WGM21:1,
                                     TCCR2_COM20:1,
                                     TCCR2_COM21:1,
                                     TCCR2_WGM20:1,
                                     TCCR2_FOC2:1;
                      };
              } @ 0x25;

  Examples of how to use the expanded result:
  a) 用   TCCR2 |= (1<<5);                     //設定TCCR2暫存器bit5=1
 b)or  if ENABLE_BIT_DEFINITIONS is defined   (首先打開在workspace 中的option)      然後用  TCCR2 |= (1 << COM21)   //設定TCCR2暫存器bit5=1
b)用  TCCR2_Bit5 = 1;                          //設定TCCR2暫存器bit5=1
d)用  TCCR2_COM21 = 1;                   //設定TCCR2暫存器bit5=1

3) 在AVR GCC compiler (GNU C compiler)中 改用如下方式 :

typedef struct{ uint8_t b0:1;
uint8_t bit1:1;
uint8_t bit2:1;
uint8_t bit3:1;
uint8_t bit4:1;
uint8_t bit5:1;
uint8_t bit6:1;
uint8_t bit7:1; } bits;
 
// define all the ports of your microcontroller
#define APORT (* (volatile bits *) &PORTA)
#define APIN (* (volatile bits *) &PINA)
#define ADDR (* (volatile bits *) &DDRA)

然後可以用 APORT.bit0=1; APORT.bit2=0;

4) AVR GCC compiler  中 A better way and faster is to use this:









// add this defines
#define SETBIT(ADDRESS,BIT) (ADDRESS |= ( 1 << BIT )) 
#define CLEARBIT(ADDRESS,BIT) (ADDRESS &= ~( 1 << BIT )) 
#define FLIPBIT(ADDRESS,BIT) (ADDRESS ^= ( 1 << BIT )) 
#define CHECKBIT(ADDRESS,BIT) (ADDRESS & ( 1 << BIT ))
#define WRITEBIT(RADDRESS,RBIT,WADDRESS,WBIT) \
(CHECKBIT(RADDRESS,RBIT) ? SETBIT(WADDRESS,WBIT) : CLEARBIT(WADDRESS,WBIT))// and then you can use
SETBIT(PORTX,2);  // set bit2 of portx (set to 1)
SETBIT(PORTY,0);  // set bit0 of porty
 
CLEARBIT(PORTX,5); // clear bit5 of portx (set to 0)
 
FLIPBIT(PORTX,2);  // invert bit2 of portX, if it was 1 it becomes 0, and if 0 becomes 1
                    // example for portx 0x10101010 the result is 0x10101110
                    
if (CHECKBIT(PORTX,4)) {} else {} // result is true if bit4 of portx is 1, false if it is 0
if (!CHECKBIT(PORTX,4)) {} else {} // result is true if bit4 of portx is 0, false if it is 1
 
 
WRITEBIT(PORTX,0,PORTY,2);   // copy the bit0 of portx to bit2 of porty

5) AVR GCC compiler  中使用在 sfr_defs.h 定義好的巨集,用法如下:
if(bit_is_set(PINC,0))   //this can check if a bit is 1
if(bit_is_clear(PINC,0))  // this can check if a bit is 0

http://www.edaboard.com/thread166246.html

沒有留言:

張貼留言

注意:只有此網誌的成員可以留言。