Getting Started

(lily) #1

Chapter 10: C Structures


define OCF2A 1

r structures. First we get the address of the Port B
registers from io169.h which also has defines for each bit.


PORTB _SFR_IO8(0x05)

#defin
defin


ction has:

lup on

And E which in binary is 1110,
so since PB etting PORTB equal to 00001110 << 0,
rememberi tor we know that we actually aren’t
doing anything, or rather we are left shifting 15 zero times, which is doing
not g r story, we are
setting POR l-ups on port B pins 2, 3,
and 4. And



#define TOV2 0


Next we write a function that causes our code to wait for the timer2 compare flag,
OCF2A, which is bit one of the Timer/Counter2 Interrupt Flag Register:


while ( !(TIFR2 && (1<<OCF2A)) );// wait for timer2 compareflag


So this usage will do the same as a bit-field, but with greater efficiency.


Let’s look at another example where we assign Port B to a ‘bit-field structure’
without using bit-fields o


/ Port B /
#define PINB _SFR_IO8(0x03)
#define DDRB _SFR_IO8(0x04)
#define


e PB7 7


e PB6 6


#define PB5 5
#define PB4 4
#define PB3 3
#define PB2 2
#define PB1 1
#define PB0 0


In the Butterfly software main.c file, the initialization fun


PORTB = (15<<PB0); // Enable pul

we ask, what’s with the 15? Well, 15 in hex is 0x
0 == 0, what we are doing is s
ng that ‘<<’ is the left-shift opera

hin if I’ve ever seen nothing, which I haven’t but... back to ou
TB pins 1, 2, and 3 to 1 thus enabling the pul
that ends this discussion.
Free download pdf