AVR revisited (The basics)

I am revisiting AVR C for various reasons and I have decided to write it down in my notebook (my blog) everything worth remembering.

First things first, I don't like working on Windows when I code and I am generally working on my Mac when I am in the zone. I decided to use Xcode. I haven't had a lot of good experiences with it so I was a bit apprehensive. But I found this cool AVR templates for Xcode.

Now for a simple blink example. This is important since it will document the various initial code that might be relevant to future code.

\\simple blink
#include <stdio.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>

void initIO(void) {  
    DDRB |= (1<<DDB5);

int main(void) {  

    while (1) {
        PORTB |= (1<<PORTB5);
        PORTB &= ~(1<<PORTB5);
    return 0; // never reached

First line for setting the data direction registers.

DDRB |= (1<<DDB5);  

DDRB is defined int he 328p definition file. So is DDB5. Now this brings up an important question on why << operator works the way it does.

The thing is DDB5 is defined as 5 in the definition file. So when we do a

0x01 << 5  

what it essentially means is the value 0b00000001 shifted 5 times to the left. This yields 0b00100000. This value is passed to DDRB or rather ORd so that any other previous directions can be maintained. I should mention that it's an 8 bit word length uController.

Similar to DDB5, PORTB5 and PINB5 are also defined as 5.

The next important thing to note is to set direction we use DDBX and to actually pass value to the PORT we use PORTX. PINX is used to read values but that'll be later.