CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Program problem

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
STIX



Joined: 17 Jun 2006
Posts: 12

View user's profile Send private message

Program problem
PostPosted: Sat Jun 17, 2006 4:10 pm     Reply with quote

I'm having problem with this simple program below. When I try to compare the value in the array "d" to see if it is negative, the red LED does not come on. However, the green LED does come on for a positive value. So I don't know what's wrong with the code, can anyone help me out? Thanks.

Code:
#include <16f877a.h>
#device  *=16
#include <math.h>
#fuses HS,NOWDT,NOPUT,NOPROTECT,NODEBUG,BROWNOUT,NOLVP,NOCPD,NOWRT
#byte PORTB    = 0x06
#byte TRISB    = 0x86
#bit  redLED   = PORTB.4
#bit  greenLED = PORTB.7
#bit  TRISB4   = TRISB.4
#bit  TRISB7   = TRISB.7

float const d[4] = {-1, 1, 1, -1};

void main (void)
{
   int k;
   TRISB4 = 0;
   TRISB7 = 0;
   for (k = 0; k <= 3; k++) {
      if (d[k] < 0)
         turn on red LED
      if (d[k] > 0)
         turn on green LED
   }
}
Eugeneo



Joined: 30 Aug 2005
Posts: 155
Location: Calgary, AB

View user's profile Send private message

Re: Program problem
PostPosted: Sat Jun 17, 2006 4:25 pm     Reply with quote

Have you tried using

if ((float)d[k] < 0)
SherpaDoug



Joined: 07 Sep 2003
Posts: 1640
Location: Cape Cod Mass USA

View user's profile Send private message

PostPosted: Sat Jun 17, 2006 4:53 pm     Reply with quote

The pseudo-code you posted should work. In the real code make sure you never assign d[k] to a simple int as ints default to unsigned so -1 will become 255. Its a mistake we have all made zillions of times.
_________________
The search for better is endless. Instead simply find very good and get the job done.
STIX



Joined: 17 Jun 2006
Posts: 12

View user's profile Send private message

PostPosted: Sat Jun 17, 2006 5:18 pm     Reply with quote

I did this:

if ((float)d[k] < 0) and it worked.

However, I don't understand what the problem was before. In my declaration, I made "float const d[4] = {-1, 1, 1, -1}", so why do I have to cast "d" as a float again?

And why did:

if (d[k] > 0) work in the first place then?
epideath



Joined: 07 Jun 2006
Posts: 47

View user's profile Send private message

PostPosted: Sat Jun 17, 2006 10:51 pm     Reply with quote

I believe it didn't work becasue you were using a const of 0 as in
Code:
d[k] < 0;

so it defaulted to an integer value. As another poster had suggested this would then default to -1 which would be 255. Possibly if you had used
Code:
d[k] < 0.0;

so it forces it to use float then it may have worked.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sat Jun 17, 2006 11:17 pm     Reply with quote

It works with PCM vs. 3.249. I took your program and added some
code to turn into a full test program. It displays the following output:
Quote:
Red LED On

Green LED On

Green LED On

Red LED On


Code:
#include <16f877a.h>
#fuses HS,NOWDT,NOPUT,NOPROTECT,NODEBUG,BROWNOUT,NOLVP,NOCPD,NOWRT
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)

float const d[4] = {-1, 1, 1, -1};

void main (void)
{
int k;

for(k = 0; k <= 3; k++)
   {
    if(d[k] < 0)
       printf("Red LED On\n\r");
    if (d[k] > 0)
        printf("Green LED On\n\r");
   }
}
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group