Programme DS1820

20 mar Programme DS1820

Voici un programme en mikroBasic permettant de lire et afficher la température via un DS1820
program onewire

dim i, j1, j2 as byte
text as string[6]
tmp_sign as byte

someText as string[10]
main:

text = "Temp:"
ADCON1 = 255 ' configure RA5 pin as digital I/O
PORTA = 255 ' initialize porte to 255
PORTB = 0 ' initialize portb to 255
TRISA = 255 ' designate porte as input
TRISB = 0 ' designate portb as output
Lcd_Config(PORTB, 3, 2, 1, 0, PORTB, 4, 6, 5)
Lcd_Cmd(LCD_CURSOR_OFF)
Lcd_Out(1, 1, text)
Lcd_Chr(2, 12, 223) ' 'degree' character
Lcd_Chr(2, 13, "C")

while TRUE
ow_reset(PORTA, 5) ' onewire reset signal
ow_write(PORTA, 5, $CC) ' issue command to DS1820
ow_write(PORTA, 5, $44) ' issue command to DS1820
Delay_us(120)
i = ow_reset(PORTA, 5)
ow_write(PORTA, 5, $CC) ' issue command to DS1820
ow_write(PORTA, 5, $BE) ' issue command to DS1820

j1 = ow_read(PORTA, 5) ' get result
j2 = ow_read(PORTA, 5) ' get result (assuming the temperature is positive)

if j2 = $FF then
tmp_sign = "-" ' temperature sign
j1= j1 or $FF ' complement of two
j1= j1 + $01
else
tmp_sign = "+"
end if
j2 = (j1 and $01) * 5 ' Get decimal value
j1 = j1 >> 1 ' Get temp value

ByteToStr(j1, text) ' whole number
Lcd_Chr(2, 7, tmp_sign)
Lcd_Chr(2, 8, text[1])
Lcd_Chr(2, 9, text[2])
Lcd_Chr(2, 10, 46) ''.'

ByteToStr(j2, text) ' decimal
Lcd_Chr(2, 11, text[2])
someText = tmp_sign." ".text[1].".".text[2]."°C"
Delay_ms(500)
wend 'endless loop
end.

Be Sociable, Share!
1Comment

Post A Comment