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

If condition not working properly

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



Joined: 08 Feb 2014
Posts: 1

View user's profile Send private message

If condition not working properly
PostPosted: Sat Aug 02, 2014 12:09 pm     Reply with quote

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: 1933
Location: Norman, OK

View user's profile Send private message

PostPosted: Sat Aug 02, 2014 12:15 pm     Reply with quote

What CCS C Version are you using?
_________________
Google and Forum Search are some of your best tools!!!!
Ttelmah



Joined: 11 Mar 2010
Posts: 19496

View user's profile Send private message

PostPosted: Sat Aug 02, 2014 12:46 pm     Reply with quote

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

View user's profile Send private message AIM Address

PostPosted: Sat Aug 02, 2014 7:20 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Sun Aug 03, 2014 11:58 pm     Reply with quote

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: 19496

View user's profile Send private message

PostPosted: Mon Aug 04, 2014 2:09 am     Reply with quote

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.
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