Hi,
I am having an issue that i cant seem to narrow down. Thought it worth posting in case anyone else has a similar issue.
I am using PIC32MX575F256L
I have several sensors on I2C bus, using software I2C. So far i have an RTCC and Pressure sensor working perfectly.
A few days ago i started coding in a temperature sensor. This is where things get odd.
I tell the sensor to take a sample, wait and then receive the data into MSB and LSB. These are stored in a string array.
I then create a 16 bit value from the MSB and LSB.
This data is all output to a terminal so i can see what data is being read and then it tells me the temperature value following conversions
This is something that i am familiar with an have done many times before.
So, the weird part.
Randomly, I read the MSB from the array as, lets say 0x6C, but it gets shifted into the 16bit variable as 0xFF. Meaning that my 16bit value is incorrect.
**So far i have tried replacing the sensor, twice. So i dont think its faulty.
**The data is read first and stored. Then data is manipulated.
**Most of the time it works as expected. Randomly the 16bit value is corrupted.
Any ideas on anything i can do to get to the bottom of this?
Is there any reason where a value in an array could be misread?
Software I2C odd behaviour
-
- Posts: 12
- http://meble-kuchenne.info.pl
- Joined: Sun Jun 20, 2021 11:55 am
- Has thanked: 2 times
- Been thanked: 1 time
-
- Matrix Staff
- Posts: 1939
- Joined: Mon Dec 07, 2020 10:06 am
- Has thanked: 506 times
- Been thanked: 688 times
Re: Software I2C odd behaviour
Hello
Without seeing your project file or knowing which temperature sensor I'm really just guessing but does the sensor have a data ready flag you can check before reading the data registers or something as simple as a maximum sample frequency that your exceeding. It could be your reading the data when it's not ready.
An easy fix to bury the problem would be to check the most significant byte when it comes in and if its 255 then you know the data is probably not correct so discard it and use the previous correct value. This might fix the problem but the values that you do read may still not be 100% correct if there are other error conditions.
Without seeing your project file or knowing which temperature sensor I'm really just guessing but does the sensor have a data ready flag you can check before reading the data registers or something as simple as a maximum sample frequency that your exceeding. It could be your reading the data when it's not ready.
An easy fix to bury the problem would be to check the most significant byte when it comes in and if its 255 then you know the data is probably not correct so discard it and use the previous correct value. This might fix the problem but the values that you do read may still not be 100% correct if there are other error conditions.
Regards Ben Rowland - MatrixTSL
Flowcode Online Code Viewer (Beta) - Flowcode Product Page - Flowcode Help Wiki - My YouTube Channel
Flowcode Online Code Viewer (Beta) - Flowcode Product Page - Flowcode Help Wiki - My YouTube Channel
Re: Software I2C odd behaviour
Hi Ben
I have attached my flowcode file for you to take a look at. Its very much a work in progress so its a bit messy.
The sensor is the SI7020.
You will find the area where data is gathered in the SI7020_GET_DATA macro.
Unfortunatly there isnt a data ready flag, or at least not that i have seen. You have to just wait for the conversion to complete. Datasheet says 10ms where i am allowing 100ms
The sensor was where i started looking but after 2 other sensors didnt work properly i looked elsewhere. Its worth mentioning that i have this code running on an STM32 with the same sensor and it works perfectly. Now im using the PIC32 its showing weaknesses.
From the data that is being output, it seems that i am getting the data from the sensor properly. I can output the MSB and LSB on the terminal.
But those MSB and LSB bytes combined get corrupted somehow.
This is what i cannot figure out.
After reading the sensor, i will end up with MSB and LSB. These are now fixed in the array. I output these to terminal and get e.g 0x6C and 0x88
Combined to a 16bit number thats 0x6C88. But instead when i read the 16bit value its randomly changing to 0xFF88.
So what im trying to get across, it would appear that the data is correct before being shifted but not afterwards!!
Also since you have my file, can you compile it and tell me what all those warnings are for relating to strings. Despite the warnings, the program works as expected.
I have attached my flowcode file for you to take a look at. Its very much a work in progress so its a bit messy.
The sensor is the SI7020.
You will find the area where data is gathered in the SI7020_GET_DATA macro.
Unfortunatly there isnt a data ready flag, or at least not that i have seen. You have to just wait for the conversion to complete. Datasheet says 10ms where i am allowing 100ms
The sensor was where i started looking but after 2 other sensors didnt work properly i looked elsewhere. Its worth mentioning that i have this code running on an STM32 with the same sensor and it works perfectly. Now im using the PIC32 its showing weaknesses.
From the data that is being output, it seems that i am getting the data from the sensor properly. I can output the MSB and LSB on the terminal.
But those MSB and LSB bytes combined get corrupted somehow.
This is what i cannot figure out.
After reading the sensor, i will end up with MSB and LSB. These are now fixed in the array. I output these to terminal and get e.g 0x6C and 0x88
Combined to a 16bit number thats 0x6C88. But instead when i read the 16bit value its randomly changing to 0xFF88.
So what im trying to get across, it would appear that the data is correct before being shifted but not afterwards!!
Also since you have my file, can you compile it and tell me what all those warnings are for relating to strings. Despite the warnings, the program works as expected.
- Attachments
-
- Q10 Main Development v0.1.fcfx
- (238.48 KiB) Downloaded 520 times