View previous topic :: View next topic |
Author |
Message |
Brindan
Joined: 08 Feb 2014 Posts: 1
|
If condition not working properly |
Posted: Sat Aug 02, 2014 12:09 pm |
|
|
I have written a simple code but if condition in it, is not working properly. I am actually trying to make a 4x4 keypad driver but this is just initial testing. Can anyone point out the problem for me?
Code: | #include <18F4520.h>
#fuses NOWDT,PUT,BROWNOUT,NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
#include <lcd.c>
int a=0,b=0,c=0,d=0,e=0,f=0,g=0,h=0;
void main()
{
lcd_init();
setup_oscillator(OSC_4MHZ);
lcd_putc("Ready...");
a=input(pin_b0);
b=input(pin_b1);
c=input(pin_b2);
d=input(pin_b3);
e=input(pin_b4);
f=input(pin_b5);
g=input(pin_b6);
h=input(pin_b7);
while(1)
{
output_high(pin_b6);
if (f==1 && a==1)
output_high(pin_a1);
lcd_putc("a");
}
} |
|
|
|
dyeatman
Joined: 06 Sep 2003 Posts: 1934 Location: Norman, OK
|
|
Posted: Sat Aug 02, 2014 12:15 pm |
|
|
What CCS C Version are you using? _________________ Google and Forum Search are some of your best tools!!!! |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19549
|
|
Posted: Sat Aug 02, 2014 12:46 pm |
|
|
The obvious problem is that a, and f are read outside the loop. Unless they are both high at the moment the chip is powered up, the 'if' will never execute. |
|
|
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
Posted: Sat Aug 02, 2014 7:20 pm |
|
|
also this:
what you really have is:
Code: |
if (f==1 && a==1) output_high(pin_a1);
// following line always executed
lcd_putc("a");
|
after you fix MR. T's observation,
because without { } -the IF statement is a ONE instruction branch...
thus
no matter what the IF test result is -
the LCD will output "a" in a very tight loop........... |
|
|
gpsmikey
Joined: 16 Nov 2010 Posts: 588 Location: Kirkland, WA
|
|
Posted: Sun Aug 03, 2014 11:58 pm |
|
|
Unfortunately, I'm betting he is familiar with Python - which does not use the {} but instead uses indent levels to determine code blocks. I have been playing with Python a bit and that indent level thing is driving me nuts (short drive) - I wish they did use brackets - less error prone !!
mikey _________________ mikey
-- you can't have too many gadgets or too much disk space !
old engineering saying: 1+1 = 3 for sufficiently large values of 1 or small values of 3 |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19549
|
|
Posted: Mon Aug 04, 2014 2:09 am |
|
|
Even in Python the behaviour would be exactly as Asmboy comments.
The other thing probably being missed is just how 'quick/slow' things are.
By the time the message 'ready' actually appears on the display, the scan of the input pins will have long since been passed. It typically takes up to perhaps 1/4 second for the LCD to actually display the message sent to it. At 4Mhz, reading the pins will take about 32uSec....
I suspect he is possibly thinking of 'input', more like 'getc', and expecting the code to wait in some way for an event on the pins, rather than just reading them. |
|
|
|