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

why this method did not work " if(a||b||c||d)

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



Joined: 08 Apr 2009
Posts: 100
Location: Chennai, India

View user's profile Send private message

why this method did not work " if(a||b||c||d)
PostPosted: Mon Jul 13, 2009 12:28 am     Reply with quote

hi every one

I programmed a logic which uses 4 input pins Surprised
Code:

int a, b, c, d;

a=input(pin_b1);
b=input(pin_b2);
c=input(pin_b3);
d=input(pin_b4);

if (a||b||c||d) output_high(pin_a1);

This is the code I programmed but this does not work.

When I changed the above code like this it works:
Code:
if (a==1||b==1||c==1||d==1) output_high(pin_a1);

It works....... I don't know why it did not work... did I make any mistake or any compiler issue?

But this also works:
Code:
  if (a&&b&&c&&d) output_high(pin_a1);

Summary: AND logic works but OR logic don't work.

Anyone reveal me please.

Arrow
Guest








PostPosted: Mon Jul 13, 2009 1:12 am     Reply with quote

Hello,

Compiling your code under PCH 4.093 for the PIC18F86J16 produces the following:

Code:

....................    int a, b, c, d;
.................... 
....................    a=input(pin_b1);
00022:  BSF    F93.1
00024:  CLRF   06
00026:  BTFSC  F81.1
00028:  INCF   06,F
....................    b=input(pin_b2);
0002A:  BSF    F93.2
0002C:  CLRF   07
0002E:  BTFSC  F81.2
00030:  INCF   07,F
....................    c=input(pin_b3);
00032:  BSF    F93.3
00034:  CLRF   08
00036:  BTFSC  F81.3
00038:  INCF   08,F
....................    d=input(pin_b4);
0003A:  BSF    F93.4
0003C:  CLRF   09
0003E:  BTFSC  F81.4
00040:  INCF   09,F
.................... 
....................    if (a||b||c||d)
00042:  MOVF   06,F
00044:  BNZ   0052
00046:  MOVF   07,F
00048:  BNZ   0052
0004A:  MOVF   08,F
0004C:  BNZ   0052
0004E:  MOVF   09,F
00050:  BZ    0056
....................       output_high(pin_a1);
00052:  BCF    F92.1
00054:  BSF    F89.1
.................... 
....................    if (a&&b&&c&&d)
00056:  MOVF   06,F
00058:  BZ    006A
0005A:  MOVF   07,F
0005C:  BZ    006A
0005E:  MOVF   08,F
00060:  BZ    006A
00062:  MOVF   09,F
00064:  BZ    006A
....................       output_high(pin_a1);
00066:  BCF    F92.1
00068:  BSF    F89.1


I haven't tried running it, but I don't see anything wrong with the assembly code. Are you using the latest compiler version? I have seen problems with logic similar to yours in older versions (but not for quite some time). When you say it doesn't work, what are you actually observing?

/Ross.
mutthunaveen



Joined: 08 Apr 2009
Posts: 100
Location: Chennai, India

View user's profile Send private message

thanks bro
PostPosted: Mon Jul 13, 2009 10:24 pm     Reply with quote

ill check my compiler version and ill update to u

actually i coded for 4 pins in the port B with tris function

a=input(pin_b1);
b=input(pin_b2);
c=input(pin_b3);
d=input(pin_b4);

when i used a logic as if(a||b||c||d) { output_high(pin_a1); }

but if i enable these 4 pins to 1, the a1 pin is not going to 1

i changed the code to if(a==1||b==1||c==1||d==1) {output_high(pin_a1); }

then when i enable the put pins then the output pin a1 is going high.....
so here it is working Laughing
RossJ



Joined: 25 Aug 2004
Posts: 66

View user's profile Send private message

PostPosted: Mon Jul 13, 2009 10:35 pm     Reply with quote

I see no reason why your two if statements shoul behave any differently to each other. The only way to confirm if it is a compiler issue is to look at the generated assembly code (in the .LST file). Can you post the 'relevant' part of the .LST file as I did previously.

/Ross.
mutthunaveen



Joined: 08 Apr 2009
Posts: 100
Location: Chennai, India

View user's profile Send private message

PostPosted: Tue Jul 14, 2009 1:14 am     Reply with quote

my compiler is installed in other PC.... ill go home evening.

ill update the ASM code today evening........

thanks for ur interest
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Tue Jul 14, 2009 1:22 am     Reply with quote

Quote:
but if i enable these 4 pins to 1, the a1 pin is not going to 1

What does "enable to 1" exactly mean?
mutthunaveen



Joined: 08 Apr 2009
Posts: 100
Location: Chennai, India

View user's profile Send private message

PostPosted: Tue Jul 14, 2009 2:28 am     Reply with quote

FvM wrote:
Quote:
but if i enable these 4 pins to 1, the a1 pin is not going to 1

What does "enable to 1" exactly mean?


enable 1 means... i made particular pin to "high" +4.5V

so if i give high for those pin i configured the pin a1 must be high
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Tue Jul 14, 2009 7:12 am     Reply with quote

Quote:
so if i give high for those pin i configured the pin a1 must be high
I agree. In this case, can you post a complete application, that produces the problem, including #fuses and processor type selection, and tell the actual compiler version.
mutthunaveen



Joined: 08 Apr 2009
Posts: 100
Location: Chennai, India

View user's profile Send private message

PostPosted: Tue Jul 14, 2009 9:08 am     Reply with quote

So this is my entire code for program
Code:

#include <16F88.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=14000000)
#use fast_io(b)
#use fast_io(a)


void main(){

while(1){

int a, b, c, d;

set_tris_a(0x00);
set_tris_b(0x0F);

a=input(pin_b0);
b=input(pin_b1);
c=input(pin_b2);
d=input(pin_b3);

if (a||b||c||d) output_high(pin_a1);
if (a&&b&&c&&d) output_high(pin_a2);
}

}


The ASM code is
Code:

CCS PCM C Compiler, Version 4.023, 25097               14-Jul-09 20:23

               Filename: C:\Program Files\PICC\Examples\EX_GLCD.lst

               ROM used: 60 words (1%)
                         Largest free fragment is 2048
               RAM used: 6 (2%) at main() level
                         10 (3%) worst case
               Stack:    0 locations

*
0000:  MOVLW  00
0001:  MOVWF  0A
0002:  GOTO   004
0003:  NOP
.................... #include <16F88.h>
.................... //////// Standard Header file for the PIC16F88 device ////////////////
.................... #device PIC16F88
.................... #list
.................... 
.................... #fuses HS,NOWDT,NOPROTECT,NOLVP
.................... #use delay(clock=14000000)
.................... #use fast_io(b)
.................... #use fast_io(a)
.................... 
.................... 
.................... void main(){
0004:  CLRF   04
0005:  BCF    03.7
0006:  MOVLW  1F
0007:  ANDWF  03,F
0008:  BSF    03.5
0009:  BCF    1F.4
000A:  BCF    1F.5
000B:  MOVF   1B,W
000C:  ANDLW  80
000D:  MOVWF  1B
000E:  MOVLW  07
000F:  MOVWF  1C
.................... 
.................... while(1){
.................... 
.................... int a, b, c, d;
.................... 
....................       set_tris_a(0x00);
0010:  MOVLW  00
0011:  MOVWF  05
....................       set_tris_b(0x0F);
0012:  MOVLW  0F
0013:  MOVWF  06
.................... 
.................... a=input(pin_b0); 
0014:  BCF    03.5
0015:  CLRF   21
0016:  BTFSC  06.0
0017:  INCF   21,F
.................... b=input(pin_b1); 
0018:  CLRF   22
0019:  BTFSC  06.1
001A:  INCF   22,F
.................... c=input(pin_b2); 
001B:  CLRF   23
001C:  BTFSC  06.2
001D:  INCF   23,F
.................... d=input(pin_b3); 
001E:  CLRF   24
001F:  BTFSC  06.3
0020:  INCF   24,F
.................... 
.................... if (a||b||c||d) output_high(pin_a1);
0021:  MOVF   21,F
0022:  BTFSS  03.2
0023:  GOTO   02C
0024:  MOVF   22,F
0025:  BTFSS  03.2
0026:  GOTO   02C
0027:  MOVF   23,F
0028:  BTFSS  03.2
0029:  GOTO   02C
002A:  MOVF   24,F
002B:  BTFSS  03.2
002C:  BSF    05.1
.................... if (a&&b&&c&&d) output_high(pin_a2);
002D:  MOVF   21,F
002E:  BTFSC  03.2
002F:  GOTO   039
0030:  MOVF   22,F
0031:  BTFSC  03.2
0032:  GOTO   039
0033:  MOVF   23,F
0034:  BTFSC  03.2
0035:  GOTO   039
0036:  MOVF   24,F
0037:  BTFSS  03.2
0038:  BSF    05.2
.................... 
.................... 
.................... }
0039:  BSF    03.5
003A:  GOTO   010
.................... }
003B:  SLEEP

Configuration Fuses:
   Word  1: 3F62   HS NOWDT PUT MCLR BROWNOUT NOLVP NOCPD NOWRT NODEBUG CCPB0 NOPROTECT
   Word  2: 3FFF   FCMEN IESO
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Jul 14, 2009 1:39 pm     Reply with quote

1. How are you testing this program ? Are you using a simulator ?
If so, which one ? Or is it a hardware board ? If so, have you ever
made this board do anything, such as blinking an LED ?


2. Describe the external circuits on pins B0 to B3.


3. Is this frequency value accurate ? 14 MHz is an unusual frequency:
Quote:
#use delay(clock=14000000)
bkamen



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

View user's profile Send private message

PostPosted: Tue Jul 14, 2009 1:59 pm     Reply with quote

I think it's a little easier to some extent than this..

|| is a COMPARATIVE 'OR' requiring a complete expression output OR'd with another. ANY of those expressions being true controls the conditional.

where | is a logical 'OR' and the result of the OR controls the conditional.

If you have several single bit values (easier to work with), you can LOGICALLY 'OR' them together and that would work.


So

If A = 1 and B = 0

A | B == 1

So you COULD write (and I've done this)

Code:

 if (a|b) { // A|B is non-zero
     do something
 }


or

Code:

 if ( ! (a|b) ) { // A|B is zero
     do something
 }


OR (no pun)

Code:

 if ( (a|b) == 0 ) { // A|B is zero
     do something
 }


Cheers,

Ben
_________________
Dazed and confused? I don't think so. Just "plain lost" will do. :D
mutthunaveen



Joined: 08 Apr 2009
Posts: 100
Location: Chennai, India

View user's profile Send private message

PostPosted: Wed Jul 15, 2009 2:21 am     Reply with quote

So

If A = 1 and B = 0

A | B == 1

thanks bro Laughing
I'll try this today.........evening

and IO am using a PIC simulator oshonsofts latest trial version.
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Wed Jul 15, 2009 7:54 am     Reply with quote

I just want to report, that the example application is operating correctly with PCM V4,093 and MPLAB SIM, thus I don't expect that there is a real issue with the code. I'm under the impression, that the shown V4.023 code is also correct, particularly the questioned "if (a||b ...)" line.

As a general remark, if rather outdated CCS C versions or third party simulators are involved with any assumed bugs, I would like this clearly mentioned from the start.
bkamen



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

View user's profile Send private message

PostPosted: Wed Jul 15, 2009 8:30 am     Reply with quote

FvM wrote:
I just want to report, that the example application is operating correctly with PCM V4,093 and MPLAB SIM, thus I don't expect that there is a real issue with the code. I'm under the impression, that the shown V4.023 code is also correct, particularly the questioned "if (a||b ...)" line.



I would expect A||B to work - but the user must understand that it really means

A != 0 (positive integer)
B != 0 (positive integer)

A or B could be > 1. If this is ok, then A||B is ok.

(Maybe some would say it's bad form? I tend to spell out my stuff so it's not cryptic to the next potential coder.)

Anyway - I had forgotten to mention that, so cheers.

-Ben
_________________
Dazed and confused? I don't think so. Just "plain lost" will do. :D
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