Code: Select all
program TEMPRETURE
' Set TEMP_RESOLUTION to the corresponding resolution of your DS18x20 sensoras
' 18S20as 9 (default setting can be 9,10,11,or 12)
' 18B20as 12
const TEMP_RESOLUTION as byte = 12
dim
j as byte
temp as word
text as string[8] ' "000.0000"
sub procedure Read_Temperature()
Ow_Reset(PORTF,6) ' Onewire reset signal
Ow_Write(PORTF,6,0xCC) ' Issue command SKIP_ROM
Ow_Write(PORTF,6,0x44) ' Issue command CONVERT_T
Delay_us(120)
Ow_Reset(PORTF,6)
Ow_Write(PORTF,6,0xCC) ' Issue command SKIP_ROM
Ow_Write(PORTF,6,0xBE) ' Issue command READ_SCRATCHPAD
Delay_ms(400)
j = OW_Read(PORTF,6) ' Get temperature LSB
temp = OW_Read(PORTF,6) ' Get temperature MSB
temp = temp << 8
temp = temp + j ' Form the 2byte variable
end sub
sub procedure Write_Temp(dim temp2write as word)
const
RES_SHIFT as byte = TEMP_RESOLUTION - 8
dim
temp_whole as byte
temp_fraction as word
if ((temp and 0x8000) > 0) then
text[0] = "-"
temp2write = (not temp2write) + 1
end if
temp_whole = temp2write >> RES_SHIFT
if (temp_whole div 100) then
text[0] = temp_whole div 100 + 48
end if
text[1] =(temp_whole div 10) mod 10 + 48
text[2] = temp_whole mod 10 + 48
temp_fraction = temp2write << (4-RES_SHIFT)
temp_fraction = temp_fraction and 0x000F
temp_fraction = temp_fraction * 625
text[4] = temp_fraction div 1000 + 48
text[5] = (temp_fraction div 100) mod 10 + 48
text[6] = (temp_fraction div 10) mod 10 + 48
text[7] = temp_fraction mod 10 + 48
Lcd_Out(2,1,text)
UART1_Write_Text(text)
end sub
' ----Main----
main:
text = "000.0000"
ADPCFG = $FFFF
Lcd_Init_EasydsPIC4()
Lcd_Cmd(LCD_CLEAR)
Lcd_Cmd(LCD_CURSOR_OFF)
Lcd_Out(1,1,"Temperature:")
UART1_INIT(9600)
'--- main loop
while TRUE
Read_Temperature()
Write_Temp(temp)
Delay_ms(1000)
wend
end.
When I built the project, under Messages there is:
35:5 H-11 Hint: Variable 'temp_fraction (Write_Temp)' has been eliminated by optimizer
Which is: temp_fraction as word on line 35.
I suspect that as it's been eliminated by the optimizer that it's causing the decimal point to be in the wrong place.
How to I prevent the Optimizer from taking such action??
Fritzables