' ********************************************************************** ' * File: gmbi2c.bas - Rel. 1.1 con Bascom AVR IDE e LIB 1.11.7.4 * ' * Schede: GMB HR84 + GMM AM08 * ' * * ' * GRIFO(R) via Dell'Artigiano 8/6 40016 S. Giorgio di Piano (BO) * ' * Tel. +39 051 892052 Fax. +39 051 893661 * ' * http://www.grifo.com http://www.grifo.it * ' * sales@grifo.it tech@grifo.it grifo@grifo.it * ' * by Graziano Gaiba del 21.01.04 * ' ********************************************************************** ' ' ' Questo demo permette di comunicare con disposivi I2C BUS collegati a CN3. ' In accordo con il Mini Modulo utilizzato, e' possibile leggere e scrivere ' byte ad un qualsiasi slave address ed address inseriti da console. ' In particolare, in lettura il byte ricevuto viene visualizzato, mentre in ' caso di scrittura il dato inserito viene spedito. ' ' 21.01.04 - Rel 1.1 By Graziano Gaiba per GMM AM08 ' ' Il file compilato non supera i 2048 bytes, quindi puo' essere ' compilato con il demo gratuito di BASCOM AVR. ' ' ' !!!!!!!!!!!!!!!!!!!!!!!!!!! IMPORTANTE !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ' Nel menu Options | Compiler | Chip, impostare: ' ' Chip: M8 ' HW Stack: almeno 64 ' Soft Stack: almeno 32 ' Framesize: almeno 64 ' '****************** Direttive del compilatore ************************** ' $regfile = "m8def.dat" $crystal = 7372800 $baud = 19200 ' '****************** Dichiarazione delle costanti *********************** ' Const Bel = 7 Const Cret = 13 ' Time out in caso una operazione TWI non riceva risposta Const Ee_timeout = 50000 ' '****************** Dichiarazione delle variabili ********************** ' ' '****************** Dichiarazione delle procedure ********************** ' Declare Sub Clrscr() ' Pulisce lo schermo Declare Sub Errore() ' Messaggio di errore Declare Function Leggi_char() As Byte ' Legge un carattere (sospensiva) Declare Sub Demo_twi() ' Invia un comando all'interfaccia TWI, attende che completi e restituisce lo stato Declare Function Twi_cmd(byval Cmd As Byte) As Byte ' '************************* Programma main ****************************** ' Main: Do Portb.5 = 1 ' Spegne il LED Call Clrscr() Call Demo_twi() Loop Do Loop End ' '************************ Fine del programma *************************** ' ' '**************************** Procedure ******************************** ' ' ********************** Trasmette 25 Line Feed ************************ ' Effettua la funzione di clear screen per una generica console. ' Parametri: ' Ingresso : nulla ' Uscita : nulla ' ************************************************************************ ' Sub Clrscr Local I As Byte For I = 1 To 25 Print Next I End Sub ' ' ' Stampa il messaggio "Errore: " ' Sub Errore() Print "Errore: "; End Sub ' ' ' Legge un carattere dalla seriale in maniera sospensiva, lo stampa e lo ' converte in maiuscolo. ' Function Leggi_char() As Byte Local C As Byte C = Waitkey() Print Chr(c) C = C And &HDF ' Converte un carattere ASCII in maiuscolo Leggi_char = C End Function ' ' ' Scrive il comando passato nel parametro cmd nel registro di conrollo TWI e ' attende il completamento dell'operazione impostata per poi leggere lo stato ' dello stesso controllore e restituirlo. ' Function Twi_cmd(byval Cmd As Byte) As Byte Local Time_out As Word , I As Byte Twcr = Cmd ' Fornisce comando passato Time_out = 0 ' Contatore timeout Do Incr Time_out ' Incrementa contatore Loop Until Twcr.twint = 1 Or Time_out = Ee_timeout ' Legge stato attuale e mantiene bit significativi I = Twsr I = I And &HF8 Twi_cmd = I End Function ' ' ' Dimostrativo dell'utilizzo dell'interfaccia seriale a due fili (TWI). ' Sub Demo_twi() Local S_addr As Word , Time_out As Word Local Dato As Byte , Char As Byte Ddrc = Ddrc And &HCF Portc = Portc Or &H30 ' Attiva pull-up Twsr = Twsr And &HFC ' Prescaler tace Twbr = 72 ' Clock rate 46080 Hz Time_out = 0 ' Time-out per stop Print "Demo TWI" Print Print "Azioni:" Print Do Print "A) Scrivere" Print "B) Leggere" Print "C) Fine" Print Print "Scelta: "; Char = Leggi_char() If Char <> "C" Then Print Inputhex "Slave address e indirizzo in hex (Es. A804): " , S_addr Select Case Char Case "A" : Inputhex "Dato in hex (Es. 01): " , Dato Print If Twi_cmd(&Ha4) = &H08 Then ' Start Twdr = High(s_addr) ' Scrive SLA+W If Twi_cmd(&H84) = &H18 Then Twdr = Low(s_addr) ' Scrive indirizzo If Twi_cmd(&H84) = &H28 Then Twdr = Dato ' Scrive dato If Twi_cmd(&H84) = &H28 Then Twcr = &H94 ' Stop ' Attende che lo stop Do ' sia completato o che Incr Time_out ' scada il Time Out Loop Until Twcr.twsto = 0 Or Time_out = Ee_timeout Else Call Errore() Print "dato" End If Else Call Errore() Print "address" End If Else Call Errore() Print "SLA+W" End If Else Call Errore() Print "start" End If Case "B" : If Twi_cmd(&Ha4) = &H08 Then ' Start Twdr = High(s_addr) ' Scrive SLA+W If Twi_cmd(&H84) = &H18 Then Twdr = Low(s_addr) ' Scrive indirizzo If Twi_cmd(&H84) = &H28 Then If Twi_cmd(&Ha4) = &H10 Then ' Ripete start Twdr = High(s_addr) Or 1 ' Scrive SLA+R If Twi_cmd(&H84) = &H40 Then ' Usare Twi_cmd(&HC4) per effettuare piu' letture consecutive ' Intercettare il codice &H50 se la lettura effettuata non ' e' l'ultima della sequenza, altrimenti usare il codice ' qui sotto. If Twi_cmd(&H84) = &H58 Then ' Legge il dato Dato = Twdr Twcr = &H94 ' Stop ' Attende che lo stop Do ' sia completato o che Incr Time_out ' scada il Time Out Loop Until Twcr.twsto = 0 Or Time_out = Ee_timeout Print "Dato letto: " ; Hex(dato) Print Else Call Errore() Print "dato" End If Else Call Errore() Print "SLA+R" End If Else Call Errore() Print "start" End If Else Call Errore() Print "address" End If Else Call Errore() Print "SLA+W" End If Else Call Errore() Print "start" End If End Select Twcr.twen = 0 ' Spegne la periferica TWI End If Loop Until Char = "C" End Sub