View previous topic :: View next topic |
Author |
Message |
STIX
Joined: 17 Jun 2006 Posts: 12
|
Program problem |
Posted: Sat Jun 17, 2006 4:10 pm |
|
|
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
}
} |
|
|
![](templates/subSilver/images/spacer.gif) |
Eugeneo
Joined: 30 Aug 2005 Posts: 155 Location: Calgary, AB
|
Re: Program problem |
Posted: Sat Jun 17, 2006 4:25 pm |
|
|
Have you tried using
if ((float)d[k] < 0) |
|
![](templates/subSilver/images/spacer.gif) |
SherpaDoug
Joined: 07 Sep 2003 Posts: 1640 Location: Cape Cod Mass USA
|
|
Posted: Sat Jun 17, 2006 4:53 pm |
|
|
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. |
|
![](templates/subSilver/images/spacer.gif) |
STIX
Joined: 17 Jun 2006 Posts: 12
|
|
Posted: Sat Jun 17, 2006 5:18 pm |
|
|
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? |
|
![](templates/subSilver/images/spacer.gif) |
epideath
Joined: 07 Jun 2006 Posts: 47
|
|
Posted: Sat Jun 17, 2006 10:51 pm |
|
|
I believe it didn't work becasue you were using a const of 0 as in
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
so it forces it to use float then it may have worked. |
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sat Jun 17, 2006 11:17 pm |
|
|
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");
}
} |
|
|
![](templates/subSilver/images/spacer.gif) |
|