[Top][All Lists]

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

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

From: Kevin Cozens
Subject: Re: [avr-gcc-list] Using PORTB with variables, PORTB = var
Date: Sat, 22 Apr 2006 12:40:20 -0400
User-agent: Mozilla Thunderbird 1.0.7-1.1.fc4 (X11/20050929)

address@hidden wrote:
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 (:

It has nothing to do with scope. It has more to do with your being new to C (and previously programming in assembler?) if I understand properly what you are trying to do based on your code examples.

Originally you stated that you weren't getting the behaviour you expected but never stated what you did expect. Your code examples lead me to think you have 8 LED's connected to port B of an AVR. You want to turn the first LED on then off, followed by the second, the third, etc. After the eighth one, you want to start at the first one again.

Assuming that is what you want to do, you are almost there.

The first issue with your code is that var is a 16-bit value which you are outputting to an 8-bit port. var should be declared as an 8-bit value. The second issue is with your use of the C operator <<. This is equivalent to a shift operation in assembly language. It is NOT the same as a rotate operation you may have used in assembly language.

The << in C is more like a simple arithmetic(?) shift you find in some microcontrollers. The bits shift left until they reach the MSB then they fall off the MSB after that. A rotate instruction that you find in some microcontrollers will take the value in the MSB and place it in the LSB while shifting all other bits left.

A simple fix to the program is to declare var as an 8-bit value. Then, in the change() function, add an if statement after the bit shift

if (var == 0)
    var = 1;

It won't be the most elegant way of doing things but it should give you the effect you want.



http://www.interlog.com/~kcozens/ |"What are we going to do today, Borg?"
Owner of Elecraft K2 #2172        |"Same thing we always do, Pinkutus:
                                  |  Try to assimilate the world!"
#include <disclaimer/favourite>   |              -Pinkutus & the Borg

reply via email to

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