Friday, December 23, 2011

PSoC3 and the TMP141 - Part 3

So if you've been following these few posts(Part 1,Part 2) on the TMP141 sensor,you'll know I had some problems with read/write ops,and then fixed those too.
Heres a picture of my working setup,before I go further:

Back to the topic.With 16bit reads fixed,I went on to parity calculations and the write functions.

Now,the transmission on the SensePath Bus uses EP(Even Parity),i.e there is an Even Parity bit sent by the master to the slave during write operations,and vice versa during reads.There are many ways to do Parity computations,the one I implemented takes 2 machine cycles only(but is specific to C51 systems.It can be implemented in a similar way on others too,I think.)
Say we want to calculate the parity of a byte of data,all we need to do is load it into the Accumulator Register and then check the P flag.What does that mean in code? Well,just one line:
uint8 parity=(ACC=DataByte,P);               ....Cool,eh?
After implementing the required parity calculations in this way,and confirming that implementation with success in reads,both 8 and 16bit,I moved onto the write functions.

The 8 and 16bit write functions aren't too complex either.One must remember that data is transmitted MSB first(irrespective of who is transmitting it,slave(s) or master(s) ).So you shift the data out,MSB first,bit by bit.
Similar EP calculations are involved in write transactions also,which include the device address,register address,R/W# flag,and data payload,8 or 16bit,as the case may be.

With the basic structure now in place,the setup and config is just 3 lines of code.You need to enable the sensor by setting the 'ENAB' bit in register 0x05(Device Control Register).Even 'EN_S' bit in register 0x0A(Temp. Control Register) needs to be set to start conversions.Lastly,set the conversion rate by writing the appropriate values to register 0x20(Conversion Rate).After wrapping these lines between curly brackets,TMP141_Start() is what I called this part.Returning the OR-ed the return values on all the 3 write operations helps the caller track the errors(if any) during the data transmission.

Reading Temperature is what comes next,where we need wait for SF1 bit in Register 0x04(Status) to become '1',indicating completion of a conversion.Once that is set,we swoop in and read off the temperature in 16 bit glory from register 0x09,and process those to get a commonly understandable temperature value in degrees Celsius.

So looking that,if we got 0x1B80,that'd come to a pleasant 27.5°C.Try it.
Note that negative temperature values are in 2's complement,so do the needful if you're expecting those.

I wrote some demo code comprising of an ID check and then continuous polling and display of temperature.This demo code uses functions from TMP141.h,which can easily be ported to other platforms as well.

This code was tested on the CY8CKIT-030,and was written in PSoC Creator 2.0.
Heres what the top design view looks like:
You can find the code in my 4shared account,linked here.
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.
Drop a line in the comments for any clarifications.

No comments:

Post a Comment