avr-gcc-list
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [avr-gcc-list] Using PORTB with variables, PORTB = var


From: unauthorized
Subject: Re: [avr-gcc-list] Using PORTB with variables, PORTB = var
Date: Sat, 22 Apr 2006 19:29:10 +1000

On Sat, 22 Apr 2006 08:30:33 +0200
Lars Noschinski <address@hidden> wrote:

> * address@hidden <address@hidden> [2006-04-22 08:09]:
> >  How can I use the PORTB constant with variables? eg:
> >
> >while (1) {
> >  var <<= 1;
> >  PORTB = var
> >}
> 
> This will work. But after seven iterations (if var is (u)int8_t), var
> will be 0.
> 
> >That is what I'm trying to acheive.  I'd like to be able to do it with 
> >variables
> >like I do above, but it doesn't seem to work.  Once I replace "var" with an
> >actual number, it works as expected.
> 
> The problem lies probably elsewhere.


OK.  This is some brief code...  This works as expected.

int main() {
  DDRB = 255;
  while (1) {
    int var=1;
    while (var != 0) {
      PORTB = var;
      var <<= 1;
    }
  }
  return 0;
}


This example doesn't, and I don't know why... I know it's specific to C, so 
that's why I'm asking here...

int var 1;

void blink()
{
 PORTB ^= var; // LED on
 sleep(50);
 PORTB = 255; // LEDs off
 sleep(50);
}

void change() {
  var <<= 1;
}

int main() {
  DDRB = 255;
  PORTB=255;
  while (1) {
    blink();
    change();
  }
  return 0;
}


This doesn't work as expected...  not sure why.  I would like to keep the 
functions if possible, I'm thinking it has something to do with scope perhaps?  
I am new to C by the way, if it isn't evident already (:

Thanks,

Chris-




reply via email to

[Prev in Thread] Current Thread [Next in Thread]