![](templates/subSilver/images/CCSLogo.jpg) |
![CCS C Software and Maintenance Offers](templates/subSilver/images/forumAd6.jpg) |
View previous topic :: View next topic |
Author |
Message |
cbruce Guest
|
Logical OR checking |
Posted: Sun Dec 01, 2002 10:41 pm |
|
|
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 |
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
Re: Logical OR checking |
Posted: Sun Dec 01, 2002 11:34 pm |
|
|
:=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 |
|
![](templates/subSilver/images/spacer.gif) |
john lee Guest
|
Re: Logical OR checking |
Posted: Mon Dec 02, 2002 3:33 am |
|
|
:=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 |
|
![](templates/subSilver/images/spacer.gif) |
cbruce Guest
|
Re: Logical OR checking |
Posted: Mon Dec 02, 2002 8:31 am |
|
|
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 |
|
![](templates/subSilver/images/spacer.gif) |
john lee Guest
|
Re: Logical OR checking |
Posted: Mon Dec 02, 2002 5:23 pm |
|
|
:=:=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 |
|
![](templates/subSilver/images/spacer.gif) |
|
|
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
|