View previous topic :: View next topic |
Author |
Message |
haseeb
Joined: 27 Apr 2011 Posts: 19
|
problem with input() command....condition always TRUE |
Posted: Wed Apr 27, 2011 7:12 am |
|
|
Hi
Im using PIC18F2620 running at 4MHz internal OSC and programming in CCS. I have a simple program which simply changes the output state of a port pin based on the input state of another pin. This is not happening and the program simply responds to the ELSE condition only. I have tested the pin outs on a scope and everything is fine. But the software only sees the ELSE condition. Please can someone look at my code below and let me know what is wrong. The compiler keeps giving me warning that condition always TRUE just above the if statement. What does this mean and could this be the problem?
Haseeb
Code: |
#include <18F2620.h>
#FUSES NOWDT // No Watch Dog Timer
#FUSES BROWNOUT // Brown out reset
#FUSES intrc_io // Use internal OSC
#FUSES NOPROTECT // Code not protected from reading
#use delay(clock=4000000) // Use 4Mhz Resonator
void main (void)
{
set_tris_B(0b00000111); //set pin I/O directions
while(1) //loopforever
{
if(!input(pin_B0)) // If pin B0 is LOW
output_low (pin_b3); //latch B3 LOW
else //pin B0 is HIGH
output_high(pin_b3); //latch B3 HIGH
delay_ms(1000); //delay for 1sec
}// end of while loop
}//end of main()
|
|
|
|
drh
Joined: 12 Jul 2004 Posts: 192 Location: Hemet, California USA
|
|
Posted: Wed Apr 27, 2011 8:07 am |
|
|
Change this:
while(1) //loopforever
{
To this:
while(1) { // loop forever _________________ David |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19538
|
|
Posted: Wed Apr 27, 2011 8:27 am |
|
|
Are you _sure_ the pin is reaching the required voltage to trigger the 'true' condition?.
The condition always true thing is just that the main loop does execute forever, and the compiler is warning you, in case this is not what you want.
You can avaoid this with:
Code: |
#include <18F2620.h>
#FUSES NOWDT // No Watch Dog Timer
#FUSES BROWNOUT // Brown out reset
#FUSES intrc_io // Use internal OSC
#FUSES NOPROTECT // Code not protected from reading
#FUSES NOPBADEN //Set pins on port B as digital
#use delay(clock=4000000) // Use 4Mhz Resonator
void main (void)
{
//set_tris_B(0b00000111); //set pin I/O directions Not needed
setup_adc_ports(NO_ANALOGS); //Alternative way of ensuring pins
//are set as digital.
do //loopforever
{
if(!input(pin_B0)) // If pin B0 is LOW
output_low (pin_b3); //latch B3 LOW
else //pin B0 is HIGH
output_high(pin_b3); //latch B3 HIGH
delay_ms(1000); //delay for 1sec
} while(TRUE); // end of while loop
}//end of main()
|
One critical change. On some chips particular pins may wake up set as _analog_ inputs, You need to ensure they are set to digital before input functions will work. On your chip, this can be changed with a fuse NOPBADEN. Currently, the pin you are using is set as an analog input.....
Best Wishes |
|
|
haseeb
Joined: 27 Apr 2011 Posts: 19
|
|
Posted: Wed Apr 27, 2011 9:24 am |
|
|
Ttelmah...
much much appreciated. Thanks so much...i did not realize
that the pin i have been slaving over all day was not even set to digital.
It works now well....
And thanks for the do while loop suggestion also.
Regards
Haseeb |
|
|
|