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 support@ccsinfo.com

problem with input() command....condition always TRUE

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



Joined: 27 Apr 2011
Posts: 19

View user's profile Send private message

problem with input() command....condition always TRUE
PostPosted: Wed Apr 27, 2011 7:12 am     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Apr 27, 2011 8:07 am     Reply with quote

Change this:

while(1) //loopforever
{


To this:

while(1) { // loop forever
_________________
David
Ttelmah



Joined: 11 Mar 2010
Posts: 19446

View user's profile Send private message

PostPosted: Wed Apr 27, 2011 8:27 am     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Apr 27, 2011 9:24 am     Reply with quote

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.
Shocked Shocked Shocked Shocked Shocked Rolling Eyes Rolling Eyes Rolling Eyes

It works now well....

And thanks for the do while loop suggestion also.

Regards
Haseeb
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