dimanche 23 mai 2010

Programme de test

Faire fonctionner un composant basé sur le protocole 1-Wire sur un picBasic 3B n'était pas vraiment évident... la littérature et les publications à ce sujet ne courent pas le web..
Un grand merci à Jacobée JP pour son aide précieuse concernant l'aide qu'il m'a fourni...
Grâce à ces bases, le picBasic dialogue en mode I2C avec un DS2482-800 qui pilote les composant 1-Wire.
J'ai essayé de refaire un programme de base  le plus simple possible et de le commenter afin que d'autres puissent s'en servir si nécessaire.
NB : attention aux retours à la ligne de lignes longues

Dans un autre article, je décrirait mon programme de gestion qui est bien plus compliqué.... ;-)





'--------------------------------------------------------------------------------------
' ---MESURE DE TEMPERATURE SUR PIC BASIC 3B / BUS 1-WIRE ---------
' ----COPYLEFT CHRISTIAN TRILLAUD trillaud@gmail.com -------------------
'-------------------------------------------------------------------------------------


'################################ constantes ########################################

'----------------------- PIC --------------
CONST DEVICE = 3B                    'pic utilisé

'----------------------- I2C --------------
Const SCL = 11                        'port I2C
Const SDA = 12                        'port I2C

'----------- DS2482 adresses de canaux d'acquisition --------------
Const CH0 = &HF0                    'chanel P0 - pin 14
Const CH1 = &HE1                    'chanel P1 - pin 15
Const CH2 = &HD2                    'chanel P2 - pin 16
Const CH3 = &HC3                    'chanel P3 - pin 1
Const CH4 = &HB4                    'chanel P4 - pin 12
Const CH5 = &HA5                    'chanel P5 - pin 11
Const CH6 = &H96                    'chanel P6 - pin 10
Const CH7 = &H87                    'chanel P7 - pin 9


'################################ variables ########################################

'--- mesures de temperature
dim T1 as byte
dim T2 as byte
dim T3 as byte
dim T4 as byte
dim T5 as byte
dim T6 as byte
dim T7 as byte
dim T8 as byte

'--- signe des mesures de températures
dim S1 as byte
dim S2 as byte
dim S3 as byte
dim S4 as byte
dim S5 as byte
dim S6 as byte
dim S7 as byte
dim S8 as byte

'--- Variables 1-Wire
Dim CH as byte                         'Channel lecture : num de l'entrée capteur considérée
Dim KL as byte                         'Octet de poids faible DS18B20
Dim KH as byte                         'Octet de poids fort DS18B20
Dim DA as byte                        'variable de commande du DS18B20
Dim BY as byte                        'test busy
Dim Sig as byte                        'signe
Dim Tf as byte                         'valeur décimale
Dim TL as byte                        '4 bits poids faible
Dim TH as byte                        '3 bits poids fort
Dim TT as byte                        'temperature






'############ PROGRAMME PRINCIPAL ################
'suppression du curseur de l'affichage
CRSOFF

'boucle principale
DEBUT:

        'mesure des températures
        GOSUB MESURES

        'affichage des temperatures
        GOSUB affichTEMP

GOTO DEBUT




'############# SUBROUTINE ###################

'----------------------------- mesure des temperatures ------------------------------------
MESURES:   
        CH = CH0
        GOSUB LECTURE
        T1 = TT
        S1 = Sig

        CH = CH1
        GOSUB LECTURE
        T2 = TT
        S2 = Sig

        CH = CH2
        GOSUB LECTURE
        T3 = TT
        S3 = Sig

        CH = CH3
        GOSUB LECTURE
        T4 = TT
        S4 = Sig

        CH = CH4
        GOSUB LECTURE
        T5 = TT
        S5 = Sig

        CH = CH5
        GOSUB LECTURE
        T6 = TT
        S6 = Sig

        CH = CH6
        GOSUB LECTURE
        T7 = TT
        S7 = Sig

        CH = CH7
        GOSUB LECTURE
        T8 = TT
        S8 = Sig
RETURN

'-------------------------- LECTURE D'UN CAPTEUR  -------------
LECTURE:               
Channel:
        Gosub  I2C_START
            Shiftout SCL,SDA,2,&H30        'AD,0 adresse I2C (DS2482) ecrire
            Shiftout SCL,SDA,2,&HC3        'CHSL    Pointer sur Channel Selection Register               
            Shiftout SCL,SDA,2,CH        'CHANNEL choisi   
        Gosub  I2C_STOP   
        Gosub  OneWReset
        Gosub  OWWread
CONVER:                            'ROUTINE DE CONVERSION DU RESULTAT
        DA=&HCC : Gosub  OWWrite        'Commande skip ROM  (DS18B20)               
        DA=&H44 : Gosub  OWWrite        'Commande convertion               
        Gosub BUSY                'Routine de test ligne1-wire                   
        Gosub OWWread                                           
        Sig=KH AND &H80                'signe (0 : positif, 1 : négatif)
        Tf=KL AND &H0F                 'Octet L partie decimale = 4bits poids faible (non utilisé)
        TL=KL AND &HF0 : TL=TL/16        'Octet L Decale de 4 >>               
        TH=KH AND &H07 : TH = TH*16        'Octet H 3 bits poids faible
        TT=TL+TH                'TT resultat temperature sur 7 bits
        if Sig = 1 then    TT=&H80-TT        'temperature negative

RETURN

                                                                   
'-----------------------'ROUTINE ECRITURE DS2482-800 1-WIRE (varIN : SCL, SDA, DA) ---------------------
OWWrite:
        Gosub  I2C_START
            Shiftout SCL,SDA,2,&H30         'AD,0 adresse I2C (DS2482)ecrire           
            Shiftout SCL,SDA,2,&HA5        'command 1WWB 1Wire write byte
            Shiftout SCL,SDA,2,DA        'Envoie variable
        Gosub  I2C_STOP
RETURN

'----------------------- ROUTINE LECTURE DS2482-800 1-WIRE (varIN : SCL, SDA, DA, varOUT : KL, KH) ------------
OWWread:
        DA=&HCC : Gosub  OWWrite        'Commande skip ROM > DS18B20
        DA=&HBE : Gosub  OWWrite        'Commande Lecture > DS18B20                   
        Gosub  I2C_START
            Shiftout SCL,SDA,2,&H30        'AD,0 Adresse Ecriture DS2482               
            Shiftout SCL,SDA,2,&H96        '1WRB Lecture byte > 1-wire               
        Gosub  I2C_START
            Shiftout SCL,SDA,2,&H30        'AD,0 Adresse Ecriture DS2482
            Shiftout SCL,SDA,2,&HE1        'SRP    Set Read Pointer    (E1)               
            Shiftout SCL,SDA,2,&HE1        'E1h    Pointer sur Read Data Regiser           
        Gosub  I2C_START
            Shiftout SCL,SDA,2,&H31        'AD,1 adresse lecture (DS2482)               
            KL=Shiftin (SCL,SDA,1)        'Byte > KL                       
        Gosub  I2C_START               
            Shiftout SCL,SDA,2,&H30        'AD,0 Adresse Ecriture DS2482               
            Shiftout SCL,SDA,2,&H96        '1WRB Lecture byte > 1-wire               
        Gosub  I2C_START
            Shiftout SCL,SDA,2,&H30        'AD,0
            Shiftout SCL,SDA,2,&HE1        'SRP    Set Read Pointer                   
            Shiftout SCL,SDA,2,&HE1        'E1h    Pointer sur Read Data Regiser           
        Gosub  I2C_START
            Shiftout SCL,SDA,2,&H31        'AD,1 adresse lecture(DS2482)               
            KH=Shiftin (SCL,SDA,1)        'Byte > KH                       
        Gosub  I2C_STOP
        Gosub BUSY                '*** INDISPENSABLE ***   
Return
                   
'-------------------- RESET DS2482-800 1-WIRE ------------------
OneWReset:
        Gosub  I2C_START                                           
            Shiftout SCL,SDA,2,&H30         'AD,0 adresse I2C (DS2482)ecrire               
            Shiftout SCL,SDA,2,&HB4        '1WRS  RESET Device Reset F0h
        Gosub  I2C_STOP
RETURN

'------------------------ TEST LIGNE 1-WIRE (varIN : SCL, SDA) ----
BUSY:           
        Gosub  I2C_START   
            Shiftout SCL,SDA,2,&H30
            Shiftout SCL,SDA,2,&HB4        '0= LSB d'abord                   
            delay 10
        Gosub  I2C_START
            Shiftout SCL,SDA,2,&H31        'Adresse (DS2482) Lecture               
RE:            BY = Shiftin (SCL,SDA,1)    'Test 2482
            BY = BY AND &b00000001               
            IF BY = 1 THEN             'Test ligne <> 0
                CLS:Locate 0,0 : Print "WAIT BY=",HEX(BY,2,1): GOTO RE
            END IF                   
        gosub I2C_STOP
RETURN


'------------------------ START I2C (varIN : SCL, SDA) ----
I2C_START:   
        Out SCL,1
        Out SDA,1
        Out SDA,0
RETURN

'------------------------ STOP I2C (varIN : SCL, SDA) ----
I2C_STOP:
        Out SDA,0
        Out SCL,1
        Out SDA,1
RETURN



'------------------- affichage des températures (afficheur 4 x 20) ----
affichTEMP:

    LOCATE 0,0
    print "T1 "
    'signe
    if S1 = 1
        then PRINT "-"
    else
        print " "
    end if
    print dec(T1,3,1)

    LOCATE 10,0
    print "T5 "
    'signe
    if S5 = 1
        then PRINT "-"
    else
        print " "
    end if
    print dec(T5,3,1)

    LOCATE 0,1
    print "T2 "
    'signe
    if S2 = 1
        then PRINT "-"
    else
        print " "
    end if
    print dec(T2,3,1)

    LOCATE 10,1
    print "T6 "
    'signe
    if S6 = 1
        then PRINT "-"
    else
        print " "
    end if
    print dec(T6,3,1)

    LOCATE 0,2
    'signe
    if S3 = 1
        then PRINT "-"
    else
        print " "
    end if
    print dec(T3,3,1)

    LOCATE 10,2
    print "T7 "
    'signe
    if S7 = 1
        then PRINT "-"
    else
        print " "
    end if
    print dec(T7,3,1)

    LOCATE 0,3
    print "T4 "
    'signe
    if S4 = 1
        then PRINT "-"
    else
        print " "
    end if
    print dec(T4,3,1)

    LOCATE 10,3
    print "T8 "
    'signe
    if S8 = 1
        then PRINT "-"
    else
        print " "
    end if
    print dec(T8,3,1)


RETURN

Aucun commentaire:

Enregistrer un commentaire