Hi all,
I i'm working on a project that requires a DS18B20 Temp sensor from maxim.
I can read the and display the temperature but when it comes over 32.76 degrees, it shows negative numbers.
I know what the problem is, and it is because im using integers but how im do i fix this.
does somebody have any ideas.
Regards Jordy
Anybody have ideas how to fix this
Moderator: Benj
- Jordy101091
- Posts: 519
- Joined: Sat Jan 08, 2011 4:02 pm
- Location: The Netherlands
- Has thanked: 25 times
- Been thanked: 188 times
- Contact:
Anybody have ideas how to fix this
- Attachments
-
- Flowcode1.fcf
- (15.5 KiB) Downloaded 232 times
the will to learn, should not be stopped by any price
-
- Posts: 258
- Joined: Wed Nov 29, 2006 6:32 pm
- Location: Finland
- Has thanked: 7 times
- Been thanked: 36 times
- Contact:
Re: Anybody have ideas how to fix this
Hi.
This is just from memory, but try like this.
Start conversion
Delay 750
OO read device(device number)
Ds1820 get temp(device number)
The get temp macro givers the scratchpad bytes 0 and 1 and if the device is 18b20, the value is 16bit signed integer, so the 4msb bits is -/+ sign so the Fxxxh is negative temp , and rest 12bit is the temp value 0,0625 deg per bit...
And the FFFFh is -0,0625,the 0001h +0,0625, so the negative value starts at FFFFh, and every -0,0625 subtract one bit..
Mika
This is just from memory, but try like this.
Start conversion
Delay 750
OO read device(device number)
Ds1820 get temp(device number)
The get temp macro givers the scratchpad bytes 0 and 1 and if the device is 18b20, the value is 16bit signed integer, so the 4msb bits is -/+ sign so the Fxxxh is negative temp , and rest 12bit is the temp value 0,0625 deg per bit...
And the FFFFh is -0,0625,the 0001h +0,0625, so the negative value starts at FFFFh, and every -0,0625 subtract one bit..
Mika
- JonnyW
- Posts: 1230
- Joined: Fri Oct 29, 2010 9:13 am
- Location: Matrix Multimedia Ltd
- Has thanked: 63 times
- Been thanked: 290 times
- Contact:
Re: Anybody have ideas how to fix this
Hi Jordy.
The number you describe, 32.76 looks like the upper limit to an 'int' - 32768 is hex 8000, which is the signed lowest value available. Im afraid I dont have access to Flowcode v4 at the moment but if you ammend your code from:
To:
Then you can do a string manipulation:
And hopefully get the result you want. This should have a range 2 times higher than the current range, though 'temp_calc_low' will need left-padding with zeros if its less than 100 (3 digits):
Like I say, I dont have access to FCv4 right now so all this is off the top of my head, but hopefully it should work OK (barring any typos!).
Hope this makes enough sense,
Jonny
The number you describe, 32.76 looks like the upper limit to an 'int' - 32768 is hex 8000, which is the signed lowest value available. Im afraid I dont have access to Flowcode v4 at the moment but if you ammend your code from:
Code: Select all
temp_calculated = (onewire_temp_lowbyte << 8) + onewire_temp_highbyte
Code: Select all
temp_calculated = (onewire_temp_lowbyte << 7) + (onewire_temp_highbyte >> 1)
onewire_temp_lowint = (temp_calculated % 500)
onewire_temp_highint = (temp_calculated / 500)
Code: Select all
temp_calc_high = ToString$(onewire_temp_highint)
temp_calc_low = ToString$(onewire_temp_lowint)
Code: Select all
temp_calc_low = "000" + temp_calc_low
temp_calc_low = Right$(temp_calc_low, 3)
Hope this makes enough sense,
Jonny
- Jordy101091
- Posts: 519
- Joined: Sat Jan 08, 2011 4:02 pm
- Location: The Netherlands
- Has thanked: 25 times
- Been thanked: 188 times
- Contact:
Re: Anybody have ideas how to fix this
Hi JonnyW
I have found the problem with the temperature reading. I have fixed and it works perfectly.
The problem was that I have put an integer value in a byte variable.
Know a want to go a step further in my project and make a menu with some settings, my goal is to navigate trough the menu and
alter the different settings with a rotary encoder.
I have been playing a round with this thing, but I can get it to work proparly.
here are some pics of my scope output ad counter-clock-wise(1) and clock-wise(2) rotation(one-click)
(1)
(http://imageshack.us/photo/my-images/513/img0022uu.jpg)
(2)
(http://imageshack.us/photo/my-images/46/img0023ev.jpg)
When going trough the menu I need to turn one-click at a time otherwise I see 2 poiting arrows or more.
Also in the video you can see the value of 189 it wont increase or decrease I dont know why it is.
http://youtu.be/uL4lr8ROW_0?hd=1
I hope you want and can help me with this,
Regards,
Jordy
I have found the problem with the temperature reading. I have fixed and it works perfectly.
The problem was that I have put an integer value in a byte variable.
Know a want to go a step further in my project and make a menu with some settings, my goal is to navigate trough the menu and
alter the different settings with a rotary encoder.
I have been playing a round with this thing, but I can get it to work proparly.
here are some pics of my scope output ad counter-clock-wise(1) and clock-wise(2) rotation(one-click)
(1)
(http://imageshack.us/photo/my-images/513/img0022uu.jpg)
(2)
(http://imageshack.us/photo/my-images/46/img0023ev.jpg)
When going trough the menu I need to turn one-click at a time otherwise I see 2 poiting arrows or more.
Also in the video you can see the value of 189 it wont increase or decrease I dont know why it is.
http://youtu.be/uL4lr8ROW_0?hd=1
I hope you want and can help me with this,
Regards,
Jordy
- Attachments
-
- Flowcode1.fcf
- (46.62 KiB) Downloaded 273 times
the will to learn, should not be stopped by any price
- JonnyW
- Posts: 1230
- Joined: Fri Oct 29, 2010 9:13 am
- Location: Matrix Multimedia Ltd
- Has thanked: 63 times
- Been thanked: 290 times
- Contact:
Re: Anybody have ideas how to fix this
Hi Jordy. I'm not an embedded engineer so cant help with your oscilloscope problem, but I'll ask someone here (probably Ben or Sean) to take a look.
One point, you mentioned your arrows sometimes display twice - I cant simulate or test your program as you are using a custom component for the LCD but you have a point in your code that either increases or decreases 'count'. Yet the code to hide the previous menu item (writing " " to the LCD) always assumes you are moving down the menu. Is this right?
Also, the code 'encoder_A = encoder_A_old' - is this checking for debounce or should this be 'encoder_A != encoder_A_old'?
Anyway, on a check for 'encoder_A_old' this is never being assigned a value in Time_Date_Settings. This might be your problem. You could also do with initialising some of your variables on startup too - 'rtc_set_hour' is never initialised so 189 is probably whatever value was in RAM on startup, and never changes.
Cheers, lemme know if this fixes anything,
Jonny
One point, you mentioned your arrows sometimes display twice - I cant simulate or test your program as you are using a custom component for the LCD but you have a point in your code that either increases or decreases 'count'. Yet the code to hide the previous menu item (writing " " to the LCD) always assumes you are moving down the menu. Is this right?
Also, the code 'encoder_A = encoder_A_old' - is this checking for debounce or should this be 'encoder_A != encoder_A_old'?
Anyway, on a check for 'encoder_A_old' this is never being assigned a value in Time_Date_Settings. This might be your problem. You could also do with initialising some of your variables on startup too - 'rtc_set_hour' is never initialised so 189 is probably whatever value was in RAM on startup, and never changes.
Cheers, lemme know if this fixes anything,
Jonny