View previous topic :: View next topic |
Author |
Message |
indiansiva555@gmail.com
Joined: 23 Jun 2012 Posts: 22
|
Logical AND operator problem |
Posted: Sat Nov 24, 2012 3:38 am |
|
|
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
|
|
Posted: Sat Nov 24, 2012 7:24 am |
|
|
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: 19513
|
|
Posted: Sat Nov 24, 2012 8:39 am |
|
|
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
|
|
Posted: Sat Nov 24, 2012 3:01 pm |
|
|
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
|
|
Posted: Sun Nov 25, 2012 2:03 am |
|
|
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
|
|
Posted: Sun Nov 25, 2012 2:10 am |
|
|
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
|
|
Posted: Sun Nov 25, 2012 3:00 am |
|
|
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
|
|
Posted: Sun Nov 25, 2012 7:10 am |
|
|
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: 9226 Location: Greensville,Ontario
|
|
Posted: Sun Nov 25, 2012 8:16 am |
|
|
another problem might be switch 'bounce' ? |
|
|
indiansiva555@gmail.com
Joined: 23 Jun 2012 Posts: 22
|
|
Posted: Fri Dec 14, 2012 10:53 am |
|
|
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
|
|
Posted: Fri Dec 14, 2012 1:58 pm |
|
|
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
|
|
Posted: Sat Dec 15, 2012 8:59 am |
|
|
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 "!") |
|
|
|