program ds18b20

' déclaration des variables
dim i, oct1, oct2, tempe, diz, uni, deci as byte

' procédure/fonction qui affiche les chiffres
sub procedure affiche_chiffre(dim x as byte)
if x=0 then
           PortB.0 = 0
           PortB.1 = 0
           PortB.2 = 0
           PortB.3 = 0
           PortB.4 = 0
           PortB.5 = 0
           PortB.6 = 1
           exit
else
if x=1 then
           PortB.0 = 1
           PortB.1 = 0
           PortB.2 = 0
           PortB.3 = 1
           PortB.4 = 1
           PortB.5 = 1
           PortB.6 = 1
           exit
else
if x=2 then
           PortB.0 = 0
           PortB.1 = 0
           PortB.2 = 1
           PortB.3 = 0
           PortB.4 = 0
           PortB.5 = 1
           PortB.6 = 0
           exit
else
if x=3 then
           PortB.0 = 0
           PortB.1 = 0
           PortB.2 = 0
           PortB.3 = 0
           PortB.4 = 1
           PortB.5 = 1
           PortB.6 = 0
           exit
else
if x=4 then
           PortB.0 = 1
           PortB.1 = 0
           PortB.2 = 0
           PortB.3 = 1
           PortB.4 = 1
           PortB.5 = 0
           PortB.6 = 0
           exit
else
if x=5 then
           PortB.0 = 0
           PortB.1 = 1
           PortB.2 = 0
           PortB.3 = 0
           PortB.4 = 1
           PortB.5 = 0
           PortB.6 = 0
           exit
else
if x=6 then
           PortB.0 = 0
           PortB.1 = 1
           PortB.2 = 0
           PortB.3 = 0
           PortB.4 = 0
           PortB.5 = 0
           PortB.6 = 0
           exit
else
if x=7 then
           PortB.0 = 0
           PortB.1 = 0
           PortB.2 = 0
           PortB.3 = 1
           PortB.4 = 1
           PortB.5 = 1
           PortB.6 = 1
           exit
else
if x=8 then
           PortB.0 = 0
           PortB.1 = 0
           PortB.2 = 0
           PortB.3 = 0
           PortB.4 = 0
           PortB.5 = 0
           PortB.6 = 0
           exit
else
if x=9 then
           PortB.0 = 0
           PortB.1 = 0
           PortB.2 = 0
           PortB.3 = 0
           PortB.4 = 1
           PortB.5 = 0
           PortB.6 = 0
           exit
end if
end if
end if
end if
end if
end if
end if
end if
end if
end if
end sub

main:
' initialisation pic et sonde
     TRISA = 1                      ' Ports A en entrées
     TRISB = 0                      ' Ports B en sorties
     PortB = 255                    ' eteint tout
     ow_reset(PORTA, 4)             ' onewire reset
     ow_write(PORTA, 4, $CC)        ' command to DS1820
     ow_write(PORTA, 4, $4E)        ' demande envoi 3 bits de config. ($4E)
     ow_write(PORTA, 4, $65)        ' envoi TH : alarme Température Haute @ 101°C
     ow_write(PORTA, 4, $81)        ' envoi TL : alarme Température Basse @ -1°C
     ow_write(PORTA, 4, $0)         ' envoi résolution t. sur 9 bits (
     ow_reset(PORTA, 4)             ' onewire reset
     ow_write(PORTA, 4, $CC)        ' command to DS1820
     ow_write(PORTA, 4, $48)        ' copy scratchpad ($48)

' début programme en boucle de lecture
' et d'affichage de la température
temperature:
     ow_reset(PORTA, 4)             ' onewire reset
     ow_write(PORTA, 4, $CC)        ' command to DS1820
     ow_write(PORTA, 4, $44)        ' demande convertissage température ($44)

     ' attente fin conversion
     'while  ow_read(PORTA, 4)= 0
     '      i = 0
     'wend
     delay_us(100)
    
     i = ow_reset(PORTA, 4)         ' onewire reset
     ow_write(PORTA, 4, $CC)        ' command to DS1820
     ow_write(PORTA, 4, $BE)        ' demande lecture température ($BE)
     oct1 = ow_read(PORTA, 4)       ' lit 1 er octet du scratchpad
     oct2 = ow_read(PORTA, 4)       ' lit 2 éme octet du scratchpad
     ' init. vars
     tempe = 0
     deci = 0
     diz = 0
     uni = 0
     ' décodage 2 octets reçus
     ' et conversion en 1 octet
     ' de température "tempe"
     if oct1.4 = 1 then
          tempe = tempe + 1
     end if
     if oct1.5 = 1 then
          tempe = tempe + 2
     end if
     if oct1.6 = 1 then
          tempe = tempe + 4
     end if
     if oct1.7 = 1 then
          tempe = tempe + 8
     end if
     if oct2.0 = 1 then
          tempe = tempe + 16
     end if
     if oct2.1 = 1 then
          tempe = tempe + 32
     end if
     if oct2.2 = 1 then
          tempe = tempe + 64
     end if
     if oct1.3 = 1 then            ' regarde etat décimale
          deci = 1
     end if
     if oct2.7 = 1 then            ' regarde si négatif
         diz = 0
         uni = 0
     else
         if tempe < 10 then
            diz = 0
            uni = tempe
          else
              if tempe >= 100 then
                 diz = 9
                 uni = 9
                 deci = 1
              else
                  diz = tempe div 10            ' extraction dizaine de degrés
                  uni = tempe mod 10            ' extaction unité de degrés
             end if
        end if
     end if
    
     if deci = 1 then              ' affichage du point si décimale
         PortB.7 = 0
     else
         PortB.7 = 1
     end if
     PortA.3 = 0                   ' début affichage de la température
     PortA.2 = 1
     affiche_chiffre(diz)          ' affiche   dizaine
     delay_ms(2)
     PortA.3 = 1
     PortA.2 = 0
     affiche_chiffre(uni)          ' affiche   unité
     delay_ms(2)
     goto temperature

end.