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

2006-04-22 Thread Abdul Malik Khan
Hi,

what is the type and size of var? 

What error you are geting?
On 4/22/06, [EMAIL PROTECTED] [EMAIL PROTECTED]
 wrote:Hiyas,How can I use the PORTB constant with variables? eg:
while (1) {var = 1;PORTB = var}That is what I'm trying to acheive.I'd like to be able to do it with variableslike I do above, but it doesn't seem to work.Once I replace var with an
actual number, it works as expected.Can someone help me out here?URL? reference?Thanks,Chris-___AVR-GCC-list mailing list
AVR-GCC-list@nongnu.orghttp://lists.nongnu.org/mailman/listinfo/avr-gcc-list-- Abdul Malik Khan

___
AVR-GCC-list mailing list
AVR-GCC-list@nongnu.org
http://lists.nongnu.org/mailman/listinfo/avr-gcc-list


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

2006-04-22 Thread Lars Noschinski

* [EMAIL PROTECTED] [EMAIL PROTECTED] [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.


___
AVR-GCC-list mailing list
AVR-GCC-list@nongnu.org
http://lists.nongnu.org/mailman/listinfo/avr-gcc-list


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

2006-04-22 Thread unauthorized
On Sat, 22 Apr 2006 08:30:33 +0200
Lars Noschinski [EMAIL PROTECTED] wrote:

 * [EMAIL PROTECTED] [EMAIL PROTECTED] [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-


___
AVR-GCC-list mailing list
AVR-GCC-list@nongnu.org
http://lists.nongnu.org/mailman/listinfo/avr-gcc-list


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

2006-04-22 Thread Matthew MacClary
On Sat, Apr 22, 2006 at 07:29:10PM +1000, [EMAIL PROTECTED] wrote:
 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...

When asking a question like this, it would be useful for you to
describe exactly what you are seeing and what you expected to
see. Here is my interpretation of what you are probably seeing: in
less than half a second, a lit LED shifts across your output port and
then nothing else ever happens again.

 I would like to keep the functions if possible, I'm thinking it has
 something to do with scope perhaps?

You can do what you seem to try to be doing using functions. The
only scope problem I see is using a globally scoped variable
unnecessarily...

 I am new to C by the way, if it isn't evident already (:

Great, you can learn the right way to do things from the start
without fighting bad habits!
Here is how I would implement your program. I apologize in advance
since I don't have a board available to try the code on. I hope that
the main ideas are apparent, even if I didn't squash all the bugs.

-Matt


void blink(volatile uint8_t * port, uint8_t mask) {

  *port = ~ mask; // LEDs on
  sleep(500); // milliseconds
  *port = 255;// LEDs off
}

void change(uint8_t * var) {
  *var = 1;
  if (0 == *var) {
*var = 1;
  }
}

int main() {
  uint8_t led_mask = 1;

  DDRB = 255;  // PORTB as outputs
  PORTB= 255;  // LEDs off

  while (1) {
blink(PORTB, led_mask);
change(led_mask);
  }
  return 0;
}

/* Changes

1) Don't use global variables (a common exception to this rule is to
   communicate with an interrupt routine). [In my team you would lose
   a hand for doing this.]

2) Don't assign a 16 bit signed integer value to an 8 bit unsigned
   location (the port). [This would cost you your other hand ;-) ]

3) Don't write non-reentrant functions, that depend on and modify
   global data.

4) Don't blink the LEDs faster than you can see them.

5) Don't keep shifting a variable the same way forever, since it will
   end up all zeros.

6) Do keep programming AVRs in C!!

*/


___
AVR-GCC-list mailing list
AVR-GCC-list@nongnu.org
http://lists.nongnu.org/mailman/listinfo/avr-gcc-list


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

2006-04-22 Thread Steve Meliza
 I am new to C by the way, if it isn't evident already (:

You might want to do some reading then.

Try
http://users.rcn.com/rneswold/avr/avr-tools.pdf

and
http://download.savannah.gnu.org/releases/avr-libc/avr-libc-user-manual-1.4.4.pdf

Also good are many of the documents from Atmel:
http://www.atmel.com/dyn/resources/Prod_documents/avr_3_04.pdf
http://www.atmel.com/dyn/products/app_notes.asp?family_id=607

Feel free to continue to ask questions as needed, but make sure to be 
descriptive as Matt (hi Matt!) described.

-Steve


___
AVR-GCC-list mailing list
AVR-GCC-list@nongnu.org
http://lists.nongnu.org/mailman/listinfo/avr-gcc-list


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

2006-04-22 Thread Kevin Cozens

[EMAIL PROTECTED] 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.


--
Cheers!

Kevin.

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


___
AVR-GCC-list mailing list
AVR-GCC-list@nongnu.org
http://lists.nongnu.org/mailman/listinfo/avr-gcc-list


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

2006-04-22 Thread Ned Konz


On Apr 22, 2006, at 2:29 AM, [EMAIL PROTECTED] wrote:


On Sat, 22 Apr 2006 08:30:33 +0200
Lars Noschinski [EMAIL PROTECTED] wrote:

* [EMAIL PROTECTED] [EMAIL PROTECTED]  
[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;


// the problem is that int is both signed and 16-bits wide; you want
// an unsigned 8-bit value. And I assume you meant to have an = sign  
in there.

// Better to use:
unsigned short var = 1;

void blink()
{
 PORTB ^= var; // LED on
// assuming you have an LED per pin and the low 8 bits of var are not  
zero, that'd work.

 sleep(50);
 PORTB = 255; // LEDs off
 sleep(50);
}

void change() {
  var = 1;
// the way you have it written, this would toggle each of the pins of  
PORTB once.

// is that what you're getting?
// The problem is that when var gets to 128, it next becomes 256,  
which is all
// 0s in the low 8 bits, so it won't affect your LEDs. The sequence  
would be:

// 1, 2, 4, 8, 16, 32, 64, 128,as intended
// 256, 512, 1024, 2048, 4096, 8192, 16384, -32768,// which won't  
work

// 0, 0, 0, ...   // which clearly won't work
// better to do this (assuming var is 8 bits):
var = 1;
if (var == 0) var = 1;

}

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


This doesn't work as expected...  not sure why.


It's always better to say *what it does do*, and *what you expected*,  
rather than doesn't work as expected if you want people to help you.


I would like to keep the functions if possible, I'm thinking it has  
something to do with scope perhaps?


It's hard to tell why you would like to keep the functions.
You're probably in a better position to say why g.
--
Ned Konz
MetaMagix embedded consulting
[EMAIL PROTECTED]




___
AVR-GCC-list mailing list
AVR-GCC-list@nongnu.org
http://lists.nongnu.org/mailman/listinfo/avr-gcc-list


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

2006-04-22 Thread Ned Konz


On Apr 22, 2006, at 9:20 AM, Matthew MacClary wrote:


Here is how I would implement your program. I apologize in advance
since I don't have a board available to try the code on. I hope that
the main ideas are apparent, even if I didn't squash all the bugs.


Here's a version of Matthew's code that will compile correctly (had  
to change the names of sleep() etc.);
using avr-gcc 4.1.0 it compiles to 142 bytes with the compilation  
options given below.


/*
avr-gcc -g -mmcu=atmega8 -fwhole-program -Os -o xx2c.elf xx2.c
avr-objdump -S xx2c.elf  xx2c.lst
*/

/* default 1MHz internal oscillator */
#define F_CPU 100

#include avr/io.h
#include util/delay.h

inline void
blink(volatile uint8_t * const port, uint8_t mask)
{
*port = ~mask;  // LEDs on
_delay_ms(500); // milliseconds
*port = 255;// LEDs off
_delay_ms(500); // milliseconds
}

inline void
change(uint8_t * const var)
{
*var = 1;
if (! *var)
*var = 1;
}

int
main()
{
uint8_t led_mask = 1;

DDRB = 255; // PORTB as outputs
PORTB = 255;// LEDs off

while (1)
{
blink(PORTB, led_mask);
change(led_mask);
}
return 0;
}

--
Ned Konz
MetaMagix embedded consulting
[EMAIL PROTECTED]




___
AVR-GCC-list mailing list
AVR-GCC-list@nongnu.org
http://lists.nongnu.org/mailman/listinfo/avr-gcc-list


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

2006-04-22 Thread Ben Jackson
 void change() {
   var = 1;
 }

That's a shift, not a rotation, so var is going to become 0.

   while (1) {
 int var=1;
 while (var != 0) {
   PORTB = var;
   var = 1;
 }
   }

Also, 'int var' is going to be 16 bits wide, so in this version which
rotates the bit it's going to spend half its time in high bits and
PORTB is going to be 0.



___
AVR-GCC-list mailing list
AVR-GCC-list@nongnu.org
http://lists.nongnu.org/mailman/listinfo/avr-gcc-list


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

2006-04-21 Thread unauthorized
Hiyas,

  How can I use the PORTB constant with variables? eg:

while (1) {
  var = 1;
  PORTB = var
}

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.

Can someone help me out here?  URL? reference?

Thanks,

Chris- 


___
AVR-GCC-list mailing list
AVR-GCC-list@nongnu.org
http://lists.nongnu.org/mailman/listinfo/avr-gcc-list