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

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

[avr-gcc-list] If statement with Negative number problems


From: Dale Seaburg
Subject: [avr-gcc-list] If statement with Negative number problems
Date: Sat, 26 May 2001 14:38:49 -0500

I never got any response from my last posting of this problem.  I'll
repost with a smaller program that I'm running on an at90s8515 (STK200)
as my testbed.  My original problem was on an atmega103L.

Basically, when I test a variable for a value less than zero, I get an
incorrect (it appears to *me* to be incorrect) result.  The program is
below, and is a snippet of the much larger program that has the
problem.  The LEDs on the STK200 show a 4 as the result, rather than a 1
as would be expected.

As I had stated in my previous post, the code that I'm porting to the
AVR family is from a PIC 16F876 and does compile correctly using the
Hi-Tech compiler.  And, it does handle the negative number comparison
correctly.

What am I doing wrong to get this results?  Is this a bug in the
compiler?  I'm using the latest Linux stuff from Gunner Henne's website.

Anyone's help would be appreciated with an explanation of the results.  

Thanks very much,

Dale Seaburg

--------------------------------------------
// Test negative number problem with *if* statement 

#include <iom103.h>
#include <ctype.h>
#include <math.h>
#include <pgmspace.h>
#include <eeprom.h>
#include <string-avr.h>
#include <interrupt.h>
#include <sig-avr.h>
#include <wdt.h>


// *** GLOBAL VARIABLES **

volatile signed char val;                               // Used in interrupt 
service routine

// Main Function

main (void) 
{
        // disable interrupts for initialization
        cli();

        // Initialize PORTS

        outp(0xFF, DDRB);                       // All outputs for debug
        outp(0xFF, PORTB);                      // turn off DEBUG LEDs for now.

        outp(0x00, DDRD);                       // Switch inputs
        outp(0xFF, PORTD);                      // Enable Pullups

        // ****** MAIN EVENT LOOP ******

        {
                val = -1;

                if (val < 0)
                {
                        outp(~1, PORTB);
                }
                else
                {
                        if (val == 0)
                        {
                                outp(~2, PORTB);
                        }
                        else
                        {
                                if (val > 0)
                                {
                                        outp(~3, PORTB);
                                }
                                else
                                {
                                        outp(~4, PORTB);
                                }
                        }
                }
        }
}
--------------------------------------------



reply via email to

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