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

Logical AND operator problem

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
indiansiva555@gmail.com



Joined: 23 Jun 2012
Posts: 22

View user's profile Send private message

Logical AND operator problem
PostPosted: Sat Nov 24, 2012 3:38 am     Reply with quote

Pls help me. I am a beginner. The following code blinks LED even if one of the input is high. Whats wrong? The code or the circuit?

Code:

#include <16F877A.h>
#device adc=8

#FUSES NOWDT
#FUSES HS
#FUSES NOPUT
#FUSES NOPROTECT
#FUSES NODEBUG
#FUSES NOBROWNOUT
#FUSES NOLVP
#FUSES NOCPD
#FUSES NOWRT

#use delay(clock=20000000)
#define LED PIN_B4

void main()
{
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_CLOCK_DIV_2);
setup_psp(PSP_DISABLED);
setup_spi(SPI_SS_DISABLED);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);

//Example blinking LED program
while(true)
  {
   if(input(PIN_B0) && (input(PIN_B1))
     {
      output_high(LED);
      delay_ms(1000);
      output_low(LED);
      delay_ms(1000);
     }
  }

}
Gabriel



Joined: 03 Aug 2009
Posts: 1067
Location: Panama

View user's profile Send private message

PostPosted: Sat Nov 24, 2012 7:24 am     Reply with quote

Code:
if(1==input(PIN_B0) && (1==input(PIN_B1))


try the above.
you were not comparing anything in your "if".

make sure your button circuit is ok... use pull ups and switch to ground.

... the above statment should make it STOP blinking when you press the button...

Code:
if(0==input(PIN_B0) && (0==input(PIN_B1))


that should make it blink when you press the button..

reduce your delays to 500 ms... (so that it blinks once per second..purely esthetics (spelling?))


G.[/code]
_________________
CCS PCM 5.078 & CCS PCH 5.093
Ttelmah



Joined: 11 Mar 2010
Posts: 19504

View user's profile Send private message

PostPosted: Sat Nov 24, 2012 8:39 am     Reply with quote

This:
Code:

   if(input(PIN_B0) && input(PIN_B1))

Is perfectly legitimate (with the minute change I have done - study the brackets.....).
The 'input' statement, already returns a logical TRUE/FALSE value, so there is no need to test this against zero or one. If does not have to have a compare.

However, as posted the code won't compile, and this is always a 'bad sign'. Implies what is being posted, is not what is actually being tested (the number of opening brackets must match the number of closing brackets in the statement.....).

So, most likely problems:
1) What is being tried has a similar basic syntax error.
2) The voltages on the pins don't actually go high/low properly.

Best Wishes
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sat Nov 24, 2012 3:01 pm     Reply with quote

He PM'ed me and I told him this, but he didn't do it:
Quote:

Post this question in the main forum and give a detailed description of
the external circuits on pins B0 and B1.
ERICOO



Joined: 13 Jun 2011
Posts: 14
Location: NIGERIA

View user's profile Send private message

PostPosted: Sun Nov 25, 2012 2:03 am     Reply with quote

Hello Gabriel, depending on the way the press button is connected to your mcu the outcome of your code will be different. For instance on the PICDEM 2 PLUS demo board the input is pull up, in that case the led will always blink with your code, but if you use something like:
Code:
 if(!input(PIN_B0) && !input(PIN_B1))

then the led would blink only when you press the two push buttons simultaneously.
bkamen



Joined: 07 Jan 2004
Posts: 1615
Location: Central Illinois, USA

View user's profile Send private message

PostPosted: Sun Nov 25, 2012 2:10 am     Reply with quote

Ttelmah wrote:
This:
Code:

   if(input(PIN_B0) && input(PIN_B1))

Is perfectly legitimate (with the minute change I have done - study the brackets.....).
The 'input' statement, already returns a logical TRUE/FALSE value, so there is no need to test this against zero or one. If does not have to have a compare.


I second Ttelmah's assertion/assessment.

-Ben
_________________
Dazed and confused? I don't think so. Just "plain lost" will do. :D
indiansiva555@gmail.com



Joined: 23 Jun 2012
Posts: 22

View user's profile Send private message

PostPosted: Sun Nov 25, 2012 3:00 am     Reply with quote

My Circuit is simple, I have used pull down resistors for those inputs. I have checked with a multimeter and whenever the switches are closed, the pins receives a digital high input, but the MCU has no response to the signals. It simply blinks the LED even if one of the inputs is high.
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Sun Nov 25, 2012 7:10 am     Reply with quote

Then I'd say we have to go back to one of the first remarks in this thread: the code as posted has an error and doesn't compile. From this we know that the program as posted is not the same as you are using for testing. That's a big offence in this forum as we have often spent time trying to fix a problem that was in a part of the code not posted.

Then a few (minor) remarks on your code:
- Your code contains a lot of functionality for hardware modules that you are not using in this test. The code can be made a lot smaller.
- Disabling SPI requires the parameter FALSE, not SPI_SS_DISABLED. This was an error in an old version of the CCS Wizard.
- Very nice that you made a define for the LED input pin, this makes code easier to read. Too bad that you didn't use the same good practice for the two input pins.
- The C language is case sensitive. By default the CCS compiler doesn't care about upper / lower case but in my opinion this makes you a lazy programmer. Program code is easier to read when upper and lower cases are used in a consistent manner. For this add the '#case' command at the start of your program.

Then a generic comment on debugging. When you can't find the error try to continue making the program simpler by removing functionality until you get something that is working as expected. The last change you made to get it working is most likely the culprit. From here start extending your program again in small functional steps that are all tested to work.

For example, you keep testing with two switches. What happens when you use just one switch? And when working correctly, what happens when you test with only the other switch?

Here is a test program for just one switch. Try this for functionality:
Code:
#include <16F877A.h>
#FUSES HS
#FUSES NOWDT
#FUSES NOLVP

#case

#use delay(clock=20MHz)

#define LED       PIN_B4
#define SWITCH1   PIN_B0
#define SWITCH2   PIN_B1

void main()
{
   setup_adc_ports(NO_ANALOGS);
   setup_psp(PSP_DISABLED);
   setup_spi(FALSE);
   setup_comparator(NC_NC_NC_NC);
   
   //Example blinking LED program
   while(TRUE)
   {
      if (input(SWITCH1))
      {
         output_high(LED);
         delay_ms(1000);
         output_low(LED);
         delay_ms(1000);
      }
   }
}
Also, study my changes to the original program and try to understand why I did so. If you have questions, don't hesitate to ask.
temtronic



Joined: 01 Jul 2010
Posts: 9225
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Sun Nov 25, 2012 8:16 am     Reply with quote

another problem might be switch 'bounce' ?
indiansiva555@gmail.com



Joined: 23 Jun 2012
Posts: 22

View user's profile Send private message

PostPosted: Fri Dec 14, 2012 10:53 am     Reply with quote

Sorry for the delayed reply as I was sick for the past two weeks.
I thank Mr. Ckielstra for his coding. I copied and used it in my compiler. There was no error and the code was compiled. But the same problem for me. Without any input the LED blinks. I have used proper pull-down resistor, but the PIN B0 receives some potential of about 0.1V. Will this create any problem or what else could be the reason?
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Fri Dec 14, 2012 1:58 pm     Reply with quote

What is your compiler version number?
You can find this number (x.yyy) from "Start/PIC-C/Compiler Version" or from the top of the list file (*.lst).
indiansiva555@gmail.com



Joined: 23 Jun 2012
Posts: 22

View user's profile Send private message

PostPosted: Sat Dec 15, 2012 8:59 am     Reply with quote

PIC C Compiler CCS PCWHD v4.068

if (!input(SWITCH1))
{
output_high(LED);
delay_ms(1000);
output_low(LED);
delay_ms(1000);
}

With the above codes the hardware works properly (with the use of "!")
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