I2C Write Data Format Problems Hex/Int

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
lock attach
Attachments are accessible only for community members.
ToVa_285016
Level 5
Level 5
100 replies posted 50 replies posted 50 questions asked

 Hi,

   

See the attached project.  

   

What I am trying to do is read data, then send it to a 0-5V DAC chip (MCP4725).  I have no problem reading the data, but when it goes from my data buffer to be written to the I2C port, something bad happens, because the DAC does not respond correctly.  See the picture I captured in the zip file.  The value of the data is, correctly, 0xA1, or 161.  If I manually retype '0xA1' in the bracket instead of putting the variable, it works like it is supposed to.  Do I need to 'cast' the hex as an integer somehow, or inform the compiler what type of data it is?

   

I know, this is probably extremely basic C programming stuff, but please bear with me.

   

Thank you,

   

Tom

0 Likes
42 Replies
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

Another question is - how do I 'poll' the pointer to find out where it is in the buffer?

   

 

   

Just make the pointer global, volatile, and poll its value ? Of course

   

if thats being updated in an ISR your test would be >= some address ?

   

Don't test for equality as you may miss one or more address's

   

 

   

Regards, Dana,

0 Likes
DaMc_1553781
Level 1
Level 1
First like given

bring an old thread back to life! I'm just wondering, if I was to pass a 16 bit value from an ADC, would I simply split the data and pass data[0] = 0x40, data[1] = first 8 bits, data[3] = last 8 bits?

   

 

   

D

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

Because your two devices (PSoC and I2C-slave) might have different endianess you have to consider the order to send your bytes.

   

When the endianess is the same, a simple type cast from (int16*) to (char*)  of the data argument would be ok. If not you will have to separate high and low byte by masking and shifting as

   

highbyte is (char)(((int16) Data >> 😎 & 0xff00)

   

and lowbyte is (char)((int16)Data & 0x00ff)

   

 

   

Bob

0 Likes