DS1820 - Help I'm rusty !
OK, just received some extra goodies for my easyPIC5 board, one of which is a DS1820 temp sensor. I've used Bruce's example as a starting point and edited it to work with the 2 x 16 LCD on the EasyPIC5 board, but I don't get anything displayed on the screen.
Its been a while since I did any programming, so I'm a tad rusty, Can someone give me some direction. Here are the main points:
16F887 running with 20Mhz
DS1820 output set to RA5
LCD on portB as per the user manual
Code:
TRISA=%11111111 ' set PORTA as all input
TRISB=%00000000 ' set PORTB as all output
DEFINE OSC 20
DEFINE LCD_DREG PORTB ' LCD data port
DEFINE LCD_DBIT 0 ' LCD data starting bit 0 or 4
DEFINE LCD_RSREG PORTB ' LCD register select port
DEFINE LCD_RSBIT 4 ' LCD register select bit
DEFINE LCD_EREG PORTB ' LCD enable port
DEFINE LCD_EBIT 5 ' LCD enable bit
DEFINE LCD_BITS 4 ' LCD bus size 4 or 8
DEFINE LCD_LINES 2 ' Number lines on LCD
DEFINE LCD_COMMANDUS 2000 ' Command delay time in us
DEFINE LCD_DATAUS 50 ' Data delay time in us
Comm_Pin VAR PortA.5 ' One-wire Data-Pin "DQ" on PortC.0
Busy VAR BIT ' Busy Status-Bit
TempC VAR WORD ' Temp in deg C
TempF VAR WORD ' Temp in deg F
Float VAR WORD ' Holds remainder for + temp C display
Cold_Bit VAR R_Temp.Bit11 ' Sign-Bit for +/- Temp. 1 = Below 0 deg C
Real_Cold CON 1 ' Define Real_Cold = 1
Deg CON 223 ' Data to display Deg ° symbol
Sign VAR BYTE ' +/- sign for temp display
Dummy VAR BYTE ' Dummy for Div32
Start_Convert:
OWOUT Comm_Pin, 1, [$CC, $44] ' Skip ROM search & do temp conversion
Wait_Up:
OWIN Comm_Pin, 4, [Busy] ' Read busy-bit
IF Busy = 0 THEN Wait_Up ' Still busy..?, Wait_Up..!
OWOUT Comm_Pin, 1, [$CC, $BE] ' Skip ROM search & read scratchpad memory
OWIN Comm_Pin, 2, [R_Temp.Lowbyte, R_Temp.Highbyte] ' Read two bytes / end comms
GOSUB Convert_Temp
GOTO Start_Convert
Convert_Temp: ' +32.0 to +257 F
IF Cold_Bit = Real_Cold THEN Yikes ' If Cold_Bit = 1, it's below "0" deg C
Sign = "+"
Dummy = 625 * R_Temp ' Multiply to load internal registers with 32-bit value
TempC = DIV32 10 ' Use Div32 value to calculate precise deg C
Dummy = 1125 * R_Temp
TempF = DIV32 100
IF TempF >6795 THEN ' Over 99.5 deg F..?
TempF = TempF + 3200
lcdout $FE, 1," TempF = ",Sign,DEC TempF DIG 4,_
DEC TempF DIG 3,DEC TempF DIG 2,".",DEC2 TempF,Deg,"F "
ELSE
TempF = TempF + 3200
LCDOUT $FE,$C0, " TempF = ",Sign,DEC TempF DIG 3,_
DEC TempF DIG 2,".",DEC2 TempF,Deg,"F "
ENDIF
TempC = (R_Temp & $0FF0) >> 4 ' Mask middle 8-bits, shift into lower byte
Float = ((R_Temp.Lowbyte & $0F) * 625) ' Lower 4-bits of result * 625
RETURN
Yikes: ' Display full range -C to -F conversion
Sign = "-" ' Display - symbol for negative temp
Dummy = 625 * ~R_Temp+1 ' Multiply to load internal registers with 32-bit value
TempC = DIV32 10 ' Use Div32 value to calculate precise deg C
TempF = ~R_Temp / 16 ' Begin conversion from -C to deg +/-F
IF TempF >=18 THEN ' Check for -degrees F "-18 C = -0.4 F"
TempF = ((((TempF + 50) * 9) /5) -122) ' -C to -F below -17 deg C
lcdout $FE, 1," TempF = ",Sign, DEC TempF,Deg,"F "
LCDOUT $FE,$C0, " TempC = ",Sign,DEC TempC DIG 4,_
DEC TempC DIG 3,".",DEC3 TempC,Deg,"C "
ELSE ' Else result = +deg F
TempF = ((((-TempF + 50) * 9) /5) -58) ' -C to +F below 32.0 deg F to -17 deg C
lcdout $FE, 1, " TempF = ","+",DEC TempF,Deg,"F "
LCDOUT $FE,$C0 , " TempC = ",Sign,DEC TempC DIG 4,_
DEC TempC DIG 3,".",DEC3 TempC,Deg,"C "
ENDIF
RETURN
END
There are a few compiling errors,
Cold_Bit VAR R_Temp.Bit11 gives "a bad data type"
OWIN Comm_Pin, 2, [R_Temp.Lowbyte, R_Temp.Highbyte] gives "expected [" and "expected ]" even though the exist
and half a dozen "bad expressions"
The example was for a serial LCD, where as mine is a traditional parallel one in 4 bit mode, so I've tried to convert the example, but its not displaying anything, even though it was cut and pasted from a bit of code I wrote a year ago which worked (just changed portC to port B for the use on the development board)
I'm sure its missing something really obvious. Should I still configure the chip, or use the settings in the Flash2 programmer software.
TIA
Now we're getting somewhere
Ok, with a bit of head scratching I've managed to get the LCD to display some text, and then added an edited section of Bruce's code to get the DEC value of TEMPf displayed on the screen
Code:
ANSEL = 0
ANSELH = 0
DEFINE OSC 20
DEFINE LCD_DREG PORTB
DEFINE LCD_DBIT 0
DEFINE LCD_RSREG PORTB
DEFINE LCD_RSBIT 4
DEFINE LCD_EREG PORTB
DEFINE LCD_EBIT 5
DEFINE LCD_BITS 4
DEFINE LCD_LINES 2
DEFINE LCD_COMMANDUS 1000
DEFINE LCD_DATAUS 100
Comm_Pin VAR PortA.5 ' One-wire Data-Pin "DQ" on PortA.5
Busy VAR BIT ' Busy Status-Bit
R_Temp VAR WORD ' RAW Temperature readings
TempC VAR WORD ' Temp in deg C
TempF VAR WORD ' Temp in deg F
Float VAR WORD ' Holds remainder for + temp C display
Cold_Bit VAR R_Temp.Bit11' Sign-Bit for +/- Temp. 1 = Below 0 deg C
Real_Cold CON 1 ' Define Real_Cold = 1
Sign VAR BYTE ' +/- sign for temp display
Dummy VAR BYTE ' Dummy for Div32
Start_Convert:
OWOUT Comm_Pin, 1, [$CC, $44] ' Skip ROM search & do temp conversion
Wait_Up:
OWIN Comm_Pin, 4, [Busy] ' Read busy-bit
IF Busy = 0 THEN Wait_Up ' Still busy..?, Wait_Up..!
OWOUT Comm_Pin, 1, [$CC, $BE] ' Skip ROM search & read scratchpad memory
OWIN Comm_Pin, 2, [R_Temp.Lowbyte, R_Temp.Highbyte] ' Read two bytes / end comms
GOSUB Convert_Temp
GOTO Start_Convert
Convert_Temp: ' +32.0 to +257 F
Sign = "+"
Dummy = 625 * R_Temp ' Multiply to load internal registers with 32-bit value
TempC = DIV32 10 ' Use Div32 value to calculate precise deg C
Dummy = 1125 * R_Temp
TempF = DIV32 100
IF TempF >6795 THEN ' Over 99.5 deg F..?
TempF = TempF + 3200
lcdout $FE,1," Temp F = ",Sign,DEC TempF
ELSE
TempF = TempF + 3200
LCDOUT $FE,1, " Temp F = ",Sign,DEC TempF
ENDIF
TempC = (R_Temp & $0FF0) >> 4 ' Mask middle 8-bits, shift into lower byte
Float = ((R_Temp.Lowbyte & $0F) * 625) ' Lower 4-bits of result * 625
RETURN
The code compiles and loads OK, and I get the value (3199) shown on the screen, but seem to have a few niggles I can't workout what settings I need to change.
1) - if viewed at an angle, the display seems to be scanning (ie its similar to the effect when filming a TV and you get a black bar slowley moving over the picture)
2) - holding the DS1820 between fingers I would expect the value to change, but it doesn't
Any guidance would help