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 support@ccsinfo.com

Logical OR checking

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







Logical OR checking
PostPosted: Sun Dec 01, 2002 10:41 pm     Reply with quote

I am using RS232 on a PIC, to receive Modbus data from a PC.
I save the data into an array. (Data_in[])
So, to read a EEPROM register on the PIC, the string will have a board address, a command code (Read Register in this case), and a register address.
So far, so good.
Now, I need to check the register address.
If it is register 10,20,30,40.....D0,E0 or F0 I have to get an analog reading from the ADC, then reply.

Now the question.....
Is there a better way than

if(data_in[3]==10||data_in[3]==20||data_in[3]==30||data_in[3]==40||data_in[3]==50......||data_in[3]==F0)
{
}

This DOES work, but I get tired of typing.....

Thanks
Cam
___________________________
This message was ported from CCS's old forum
Original Post ID: 9668
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

Re: Logical OR checking
PostPosted: Sun Dec 01, 2002 11:34 pm     Reply with quote

:=If it is register 10,20,30,40.....D0,E0 or F0 I have to get an analog reading from the ADC, then reply.
---------------------------------------------
It looks like you want to take an action if the upper 4 bits
are non-zero. The following code will do it:

if(data_in[3] & 0xF0)
{
// Do something here
}

----------------
One other note:
I'm assuming that even though your sample code shows the
numbers as decimal, that you really mean for them to be
hexadecimal, because your sequence has D0, E0, F0 in it.
So if you did use a chain of logical OR tests, you would
need to use the "0x" prefix in front of your numbers.
___________________________
This message was ported from CCS's old forum
Original Post ID: 9669
john lee
Guest







Re: Logical OR checking
PostPosted: Mon Dec 02, 2002 3:33 am     Reply with quote

:=I am using RS232 on a PIC, to receive Modbus data from a PC.
:=I save the data into an array. (Data_in[])
:=So, to read a EEPROM register on the PIC, the string will have a board address, a command code (Read Register in this case), and a register address.
:=So far, so good.
:=Now, I need to check the register address.
:=If it is register 10,20,30,40.....D0,E0 or F0 I have to get an analog reading from the ADC, then reply.
:=
:=Now the question.....
:=Is there a better way than
:=
:=if(data_in[3]==10||data_in[3]==20||data_in[3]==30||data_in[3]==40||data_in[3]==50......||data_in[3]==F0)
:= {
:= }
:=
:=This DOES work, but I get tired of typing.....
:=

Hi, As you appear to be testing for increments of 10 decimal, another little trick is: If(data_in[3]\%10==0)(do_something);
Regards,
John Lee.
:=Thanks
:=Cam
___________________________
This message was ported from CCS's old forum
Original Post ID: 9673
cbruce
Guest







Re: Logical OR checking
PostPosted: Mon Dec 02, 2002 8:31 am     Reply with quote

I guess I was tired of typing.
I did the code on a diferent computer, and re-typed it here to post.
The numbers are in Hex. It actually reads 0x10, 0x20....0xF0.

I'll use the masking ideas you posted. It looks like it will work.

Thanks again
Cam
___________________________
This message was ported from CCS's old forum
Original Post ID: 9684
john lee
Guest







Re: Logical OR checking
PostPosted: Mon Dec 02, 2002 5:23 pm     Reply with quote

:=:=I am using RS232 on a PIC, to receive Modbus data from a PC.
:=:=I save the data into an array. (Data_in[])
:=:=So, to read a EEPROM register on the PIC, the string will have a board address, a command code (Read Register in this case), and a register address.
:=:=So far, so good.
:=:=Now, I need to check the register address.
:=:=If it is register 10,20,30,40.....D0,E0 or F0 I have to get an analog reading from the ADC, then reply.
:=:=
:=:=Now the question.....
:=:=Is there a better way than
:=:=
:=:=if(data_in[3]==10||data_in[3]==20||data_in[3]==30||data_in[3]==40||data_in[3]==50......||data_in[3]==F0)
:=:= {
:=:= }
:=:=
:=:=This DOES work, but I get tired of typing.....
:=:=
:=
:=Hi, As you appear to be testing for increments of 10 decimal, another little trick is: If(data_in[3]\%10==0)(do_something);
:=Regards,
:=John Lee.
:=:=Thanks
:=:=Cam

OK. So i got the number base wrong!!!
Try Modulus 16????
(still works)
Gnite.
Stigga.
___________________________
This message was ported from CCS's old forum
Original Post ID: 9707
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