![](templates/subSilver/images/CCSLogo.jpg) |
![CCS C Software and Maintenance Offers](templates/subSilver/images/forumAd6.jpg) |
View previous topic :: View next topic |
Author |
Message |
emaxxenon
Joined: 21 Jan 2020 Posts: 42
|
Convert 2 8-bits data to 16-bit data [Solved] |
Posted: Tue Jan 28, 2020 5:04 am |
|
|
Hello,
I have 2 8-bits data.
I have to combine these two and make 16 bits.
Data sent to me:
Sensor1 = A1 + A2*2 + A3*4 + A4*8 + A5*16 + A6*32 + A7*64 + A8*128
Sensor2 = B1 + B2*2 + B3*4 + B4*8 + B5*16 + B6*32 + B7*64 + B8*128
(A=1 or 0)
(B=1 or 0)
The data i see:
Sensor1 max value = 255
Sensor2 max value = 255
I'm trying to make 16 bits.
( AAAAAAAA BBBBBBBB ) = max value 65535
I need to combine the data.
is there any auxiliary code to do this?
Thank you.
EDIT:
SENSOR =make16(Sensor2, Sensor1);
I solved my problem. I couldn't find this code. sorry. |
|
![](templates/subSilver/images/spacer.gif) |
temtronic
Joined: 01 Jul 2010 Posts: 9177 Location: Greensville,Ontario
|
|
Posted: Tue Jan 28, 2020 5:39 am |
|
|
Try this CCS function...
Code: |
//CCS example from the manual
long x;
int hi,lo;
x = make16(hi,lo);
// this should work for you
unsigned int16 combined;
unsigned int8 Sensor1, Sensor2;
result=(Sensor1,Sensor2); // make 16bit from 2 8 bit values
|
If you're using MPLAB, pressing F11, opens the CCS manual while your project is open.
Jay |
|
![](templates/subSilver/images/spacer.gif) |
temtronic
Joined: 01 Jul 2010 Posts: 9177 Location: Greensville,Ontario
|
|
Posted: Tue Jan 28, 2020 8:26 am |
|
|
This
Quote: | SENSOR =make16(Sensor2, Sensor1); |
Gives you BBBBBBBBAAAAAAAA
It needs to ne
Code: | SENSOR =make16(Sensor1, Sensor2); |
To give you AAAAAAAABBBBBBBB
which is what you originally posted.
Quote: | I'm trying to make 16 bits.
( AAAAAAAA BBBBBBBB ) = max value 65535 |
Jay |
|
![](templates/subSilver/images/spacer.gif) |
emaxxenon
Joined: 21 Jan 2020 Posts: 42
|
|
Posted: Tue Jan 28, 2020 8:41 am |
|
|
Thank you.
Another question;
Slave:
Code: | if(state >= 0x80)
{
i2c_write(A);
i2c_wrie(B);
} |
Master:
Code: | i2c_start();
i2c_write(slave);
A=i2c_read(0);
B=i2c_read(0); |
Answer: S 13 A 04 N FF N p
But must be: 04 and 08 (FF false)
Can I send one after another?
No problem if i do it separately.
Master:
Code: |
i2c_start();
i2c_write(slave);
i2c_write(1);
i2c_stop();
i2c_start();
i2c_write(slave);
A=i2c_read(0);
i2c_stop();
i2c_start();
i2c_write(slave);
i2c_write(2);
i2c_stop();
i2c_start();
i2c_write(slave);
B=i2c_read(0);
i2c_stop();
|
Slave:
Code: |
if(state >= 0x80)
{
if(master==1)
i2c_write(A);
if(master==2)
i2c_wrie(B);
} |
it works like this.
Can't I send it together? |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19381
|
|
Posted: Tue Jan 28, 2020 8:58 am |
|
|
emaxxenon wrote: | A=i2c_read(0);
B=i2c_read(0); |
Only the _last_ transaction want 0.
The 0 is actually a flag to say to the master 'this is the end of the
transaction'. Is needed on the last byte transfered but not before. |
|
![](templates/subSilver/images/spacer.gif) |
emaxxenon
Joined: 21 Jan 2020 Posts: 42
|
|
Posted: Tue Jan 28, 2020 11:35 pm |
|
|
Ttelmah wrote: | emaxxenon wrote: | A=i2c_read(0);
B=i2c_read(0); |
Only the _last_ transaction want 0.
The 0 is actually a flag to say to the master 'this is the end of the
transaction'. Is needed on the last byte transferred but not before. |
Master:
Code: | i2c_start();
i2c_write(slave);
A=i2c_read();
B=i2c_read(0); |
Slave:
Code: | if(state >= 0x80)
{
i2c_write(A);
i2c_write(B);
} |
is it true this way?
or is the structure below more accurate?
Slave:
Code: | if(state >= 0x80)
{
if(state==0x80)
i2c_write(A);
if(state==0x81)
i2c_write(B);
} |
|
|
![](templates/subSilver/images/spacer.gif) |
allenhuffman
Joined: 17 Jun 2019 Posts: 540 Location: Des Moines, Iowa, USA
|
|
Posted: Wed Jan 29, 2020 1:40 pm |
|
|
The CCS compiler has some neat things that generate some nice assembly. I was curious about the difference of doing this in C versus using the CCS statement. Nice results. It's less than half the lines to use the make16() call.
Code: | .................... value = (a << 8) | b;
0041A: MOV.B CA0,W0L
0041C: MOV.B W0L,B
0041E: CLR.B W5
00420: MOV.B CA1,W0L
00422: CLR.B 1
00424: MOV.B W0L,CA2
00426: CLR.B CA3
00428: MOV W5,W0
0042A: IOR CA2
....................
.................... value = a * 256 + b;
0042C: MOV.B CA0,W0L
0042E: CLR.B 1
00430: MOV.B W0L,W0L
00432: MOV.B W0L,B
00434: CLR.B W5
00436: MOV CA0,W4
00438: LSR W4,#8,W4
0043A: ADD W5,W4,W0
0043C: MOV W0,CA2
....................
.................... value = make16 (a, b);
0043E: MOV.B CA1,W0L
00440: MOV.B W0L,CA2
00442: MOV.B CA0,W0L
00444: MOV.B W0L,CA3 |
Nicely done, CCS. _________________ Allen C. Huffman, Sub-Etha Software (est. 1990) http://www.subethasoftware.com
Embedded C, Arduino, MSP430, ESP8266/32, BASIC Stamp and PIC24 programmer.
http://www.whywouldyouwanttodothat.com ? |
|
![](templates/subSilver/images/spacer.gif) |
temtronic
Joined: 01 Jul 2010 Posts: 9177 Location: Greensville,Ontario
|
|
Posted: Wed Jan 29, 2020 1:55 pm |
|
|
Yes, saves a lot of space AND x2 or more faster ! |
|
![](templates/subSilver/images/spacer.gif) |
allenhuffman
Joined: 17 Jun 2019 Posts: 540 Location: Des Moines, Iowa, USA
|
|
Posted: Wed Jan 29, 2020 1:57 pm |
|
|
temtronic wrote: | Yes, saves a lot of space AND x2 or more faster ! |
It makes me wonder what other goodies are in there that I should be using.
A coworker suggested using a union, so I added a third test case that seems to be the best way to do it in generic C. Something like...
Code: | .................... union
.................... {
.................... struct {
.................... unsigned int8 a;
.................... unsigned int8 b;
.................... } Bytes;
.................... unsigned int16 value;
.................... } value2;
....................
.................... value2.Bytes.a = a;
0043E: MOV.B CA0,W0L
00440: MOV.B W0L,CA4
.................... value2.Bytes.b = b;
00442: MOV.B CA1,W0L
00444: MOV.B W0L,CA5 |
...but hopefully with better structure/union/variable names ;-) _________________ Allen C. Huffman, Sub-Etha Software (est. 1990) http://www.subethasoftware.com
Embedded C, Arduino, MSP430, ESP8266/32, BASIC Stamp and PIC24 programmer.
http://www.whywouldyouwanttodothat.com ? |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19381
|
|
Posted: Wed Jan 29, 2020 2:31 pm |
|
|
Yes, I like unions for things like this. Have campaigned to encourage
people to use these in the past. It is 'generic C', and gives efficient code.
And (of course) the same union can be used 'both ways' both to combine
and to split data.
You can be even more ingenious with these. For example, if you are passing
a float to a function that then internally wants to use the bytes or words
from this, you can declare the function with the union, directly pass the
float, and in the function directly access the bytes, without having to use
any intermediate variable. The compiler is smart enough to realise that
the union can accept the float, and doesn't complain. |
|
![](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
|