CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Feb 2008
    Posts
    1

    Help: Explaining Code

    Hi, I'm new here. I am having problems reading this code, to understand what each function does as I do not have much experience with this computer programming. I hope to get an idea of this code as a base, to use it as a LED blinker to allow me to show that it can signal time.

    The following code is suppose to light an LED after being switched on for 10 seconds. We are using an Atmega88 AVR Chip that will be programed.

    // AVR IO memory space
    #include <avr/io.h>

    // Macro defn to select bit n, assuming 0 <= n <= 7
    #define BIT(n) (1 << (n)) //what does this do, I am unfamiliar with this call

    // Delay function
    void delay (unsigned char time)
    {
    unsigned char t1, t2;
    unsigned int count = 0;
    for (t1 = time; t1 > 0; --t1){ //this will be the timer countdown
    for (t2 = 255; t2 > 0; --t2){ //why do we count down from 255 here?
    count += 1;
    }
    }
    return;
    }

    // Main function
    int main (void)
    {
    unsigned char LEDbit;
    DDRD = 0xFF; // set all PortD to output I assume this is the output of the AVR port that will be used to light the LED
    LEDbit = 0;

    while(1){ // forever looped
    LEDbit += 1;
    if (LEDbit > 7) LEDbit = 0;
    // turn on LED # by writing 0 to its MCU pin
    PORTD = ~BIT(LEDbit); //This is the function that I am getting confused, PORTD is probably an output variable from the io.h, but what does the ~BIT(LEDbit) do, and why the new for the LEDbit counter?
    // delay by (#) seconds
    delay(10);
    }
    return 0;
    }

    Thanks for the help, much appreciated

  2. #2
    Join Date
    Nov 2006
    Location
    Barcelona - Catalonia
    Posts
    364

    Re: Help: Explaining Code

    Hi,

    Please, post your code between code tags. Look here.
    The operator <<, is a left shift operator:
    The << operator shifts its first operand left by a number of bits given by its second operand
    So,
    Code:
    BIT(0) = 1 << 0 = 1;
    BIT(1) = 1 << 1 = 10 (binary) = 2 (decimal)
    BIT(2) = 1 << 2 = 100 (binary) = 4 (decimal)
    ...
    In other words, left << operator adds n 0's to the right, in binary base. And this is same than multiply first operant (1, in your code) by 2^n in decimal base.

    On the other hand, there's a rigth shift operator: >>, that makes the reverse operation: Divides first operator by 2^n.

    The instruction:
    Code:
    PORTD = ~BIT(LEDbit) = ~(1 << (LEDbit));
    sets all bits of PORTD to 1, except the bit LEDbit. I guess it should switch led state, but I'm not sure.

    I think delay function doesn't wait the amount of time in seconds, unless
    Code:
    for (t2 = 255; t2 > 0; --t2){
       count += 1;
    }
    takes 1 second exactly in that chip, but it makes 'a delay'.
    t2 = 255 is the highest integer for a unsigned char.

    AlbertGM
    Please, correct me. I'm just learning.... and sorry for my english :-)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured