Akses bit seperti variabel, PORTB.1=0 di AVR GCC

Untuk mengakses bit di IO mikrokontroler AVR dengan compiler AVR GCC biasanya dengan menggunakan perintah PORTB |= (1<<0); atau langsung PORTB=0b00000001; bagaimana jika ingin langsung PORTB.0=1; ? di AVR studio tidak bisa langsung dengan perintah ini, memangnya di codevision ???.

Pake ini saja, bikin strukt dulu :

struct bits {
uint8_t b0:1;
uint8_t b1:1;
uint8_t b2:1;
uint8_t b3:1;
uint8_t b4:1;
uint8_t b5:1;
uint8_t b6:1;
uint8_t b7:1;
} __attribute__((__packed__));

terus definisikan, misal dengan makro SBIT

#define SBIT_(port,pin) ((*(volatile struct bits*)&port).b##pin)
#define    SBIT(x,y)    SBIT_(x,y)

definisikan IO yang akan diakses

#define SWITCH1            SBIT( PINB, 0 )
#define SWITCH1_PULLUP     SBIT( PORTB, 0 )

#define LED1            SBIT( PORTB, 1 )
#define LED1_DDR        SBIT( DDRB, 1 )

coba langsung di fungsi main

void main()
{
LED1_DDR=1;
while(1)
{
if(SWITCH1==0)
{
LED1=0;
_delay_ms(500);
LED1=1;
_delay_ms(500);
}
}
}

~ by den-wam on May 15, 2012.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: