' ********************************************************************** ' * File: k51ppo.bas - Rel. 1.1 con Bascom AVR IDE e LIB 1.11.7.4 * ' * Schede: GMB HR168 + GMM AM128 + K51-AVR * ' * 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 * ' * by Graziano Gaiba del 17.06.05 * ' ********************************************************************** ' ' 17.06.05 - Rel 1.1 By Graziano Gaiba ' Questo demo permette di pilotare una periferica I2C BUS a bordo della K51-AVR, ' PCF8574, attraverso il modulo mini-BLOCK GMB HR168 ed un mini modulo grifo(r). ' Il PCF874 e' un I/O expander che permette di pilotare 8 linee digitali TTL sia ' in ingresso che in uscita, tutte le operazioni di lettura e scrittura ' avvengono tramite l'interfaccia seriale sincrona I2C BUS. ' Il demo attiva alternativamente una linea alla volta, creando il classico ' scorrimento di un bit a destra e a sinistra, e scrive 'out' sui display a 7 ' segmenti della K51-AVR per indicare il funzionamento del demo. ' ' ' !!!!!!!!!!!!!!!!!!!!!!!!!!! IMPORTANTE !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ' Nel menu Options | Compiler | Chip, impostare: ' ' ' Chip: M128 ' HW Stack: almeno 64 ' Soft Stack: almeno 32 ' Framesize: almeno 64 ' ' '****************** Direttive del compilatore ************************** ' $regfile = "m128def.dat" $crystal = 7372800 $baud = 19200 ' '****************** Dichiarazione delle costanti *********************** ' Const Ee_timeout = 50000 ' Operazioni di lettura TWI Const Twi_ack = 1 Const Twi_nack = 0 Const Saa1064 = &H38 ' Slave address SAA1064 Const Wsaa1064 = &H70 ' Slave address SAA1064 in Write Const Rsaa1064 = &H71 ' Slave address SAA1064 in Read ' ***************** Elenco indirizzi per Saa1064 *********************** Const Ctb = 0 ' Ind. Control byte Const Dig1 = 1 ' Ind. Digit 1 Const Dig2 = 2 ' Ind. Digit 2 Const Dig3 = 3 ' Ind. Digit 3 Const Dig4 = 4 ' Ind. Digit 4 ' ********************************************************************** ' Commentare la dichiarazione del componente non usato ' 'Const Pcf8574 = &H3C ' Slave address PCF8574A 'Const Wpcf8574 = &H78 ' Slave address PCF8574A in Write 'Const Rpcf8574 = &H79 ' Slave address PCF8574A in Read Const Pcf8574 = &H24 ' Slave address PCF8574P Const Wpcf8574 = &H48 ' Slave address PCF8574P in Write Const Rpcf8574 = &H49 ' Slave address PCF8574P in Read ' '****************** Dichiarazione delle variabili ********************** ' 'Uso generico Dim I2cstatus As Byte Dim X As Byte Dim Pout As Byte ' valore per le linee di uscita Dim Dato As Byte ' valore di conteggio ' '****************** Dichiarazione delle procedure ********************** ' ' Invia un comando all'interfaccia TWI, attende che completi e restituisce lo stato Declare Function Twi_cmd(byval Cmd As Byte) As Byte ' Esegue un comando TWI Declare Sub Twiinit(byval Clockrate As Byte) ' Inizializza l'interfaccia TWI hardware Declare Function Twistart() As Byte ' Invia lo start per l'interfaccia TWI hardware Declare Function Twiwbyte(byval Dato As Byte) As Byte ' Invia un byte sull'interfaccia TWI hardware Declare Function Twirbyte(twi_data As Byte , Byval Assert_ack As Byte) As Byte ' Legge un byte dall'interfaccia TWI hardware Declare Function Twistop() As Byte ' Invia lo stop su interfaccia TWI hardware. Declare Sub Check_ready() Declare Function Check_ready_2() As Byte Declare Sub Iniz() ' Inizializzazione periferiche ' '************************* Programma main ****************************** ' Main: Print Chr(12) Print "Demo di otto uscite TTL del PCF8574" Print Call Check_ready() Print "Sulle otto uscite TTL scorre un bit da destra a sinistra e vicecersa." Call Iniz() ' inizializzazioni Do Dato = 1 ' valore di partenza For X = 1 To 7 ' inizializzo il ciclo Pout = Not Dato ' complemento in valore I2cstatus = Twistart() I2cstatus = Twiwbyte(wpcf8574) I2cstatus = Twiwbyte(pout) I2cstatus = Twistop() ' I2csend Wpcf8574 , Pout ' setto le uscite Rotate Dato , Left ' shift dei bit a sinistra Waitms 255 ' ritardo Next X For X = 1 To 7 ' inizializzo il ciclo Pout = Not Dato ' complemento in valore I2cstatus = Twistart() I2cstatus = Twiwbyte(wpcf8574) I2cstatus = Twiwbyte(pout) I2cstatus = Twistop() ' I2csend Wpcf8574 , Pout ' setto le uscite Rotate Dato , Right ' shift dei bit a destra Waitms 255 ' ritardo Next X Loop End ' '************************ Fine del programma *************************** ' ' '**************************** Procedure ******************************** ' ' ******************* Inizializzazione delle periferiche ***************** ' Questa procedura esegue tutte le inizializzazioni del sistema. ' Parametri: ' Ingresso : nulla ' Uscita : nulla ' ************************************************************************ ' ' Sub Iniz ' Inizializzazione periferiche Local Valore As Byte ' Inizializza l'interfaccia I2C BUS a circa 72 kHz Call Twiinit(72) Do I2cstatus = Twistart() ' sequenza di Start per I2CBUS I2cstatus = Twiwbyte(rsaa1064) I2cstatus = Twirbyte(valore , Twi_nack) ' leggo il registro di stato I2cstatus = Twistop() 'I2creceive Rsaa1064 , Valore Loop Until Valore = 0 ' attendo accensione SAA1064 I2cstatus = Twistart() ' sequenza di Start per I2CBUS I2cstatus = Twiwbyte(wsaa1064) ' comunico lo Slave address I2cstatus = Twiwbyte(ctb) ' Punto al registro di controllo I2cstatus = Twiwbyte(&B00100111) ' bit0 =1 dynamic mode ' bit1 =1 digit 1+3 not blanked ' bit2 =1 digit 2+4 not blanked ' bit3 =0 no test segment ' bit4 =0 no 3mA segment current ' bit5 =1 6mA segment current ' bit6 =0 no 12mA segment current ' bit7 =0 indifferente I2cstatus = Twiwbyte(92) ' scrive DY1 "o" I2cstatus = Twiwbyte(28) ' scrive DY2 "u" I2cstatus = Twiwbyte(120) ' scrive DY3 "t" I2cstatus = Twiwbyte(0) ' scrive DY4 off I2cstatus = Twistop() End Sub ' '****************************************************************************** '* Procedure di gestione dell'I2C BUS hardware * '****************************************************************************** ' ' ' ' 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 ' ' ' Inizializza l'interfaccia TWI hardware ' Sub Twiinit(byval Clockrate As Byte) Twsr = Twsr And &HFC ' Prescaler tace Twbr = Clockrate ' Imposta clock rate End Sub ' ' ' Invia lo start per l'interfaccia TWI hardware ' Function Twistart() As Byte Twistart = Twi_cmd(&Ha4) ' Start End Function ' ' ' Invia un byte sull'interfaccia TWI hardware ' Function Twiwbyte(byval Dato As Byte) As Byte Twdr = Dato ' Scrive dato Twiwbyte = Twi_cmd(&H84) End Function ' ' ' Legge un byte dall'interfaccia TWI hardware ' Function Twirbyte(twi_data As Byte , Byval Assert_ack As Byte) As Byte If Assert_ack = 1 Then Twirbyte = Twi_cmd(&Hc4) Else Twirbyte = Twi_cmd(&H84) End If Twi_data = Twdr End Function ' ' ' Invia lo stop su interfaccia TWI hardware. ' Restituisce 0 se non ci sono errori, diverso da 0 in caso di ' timeout. ' Function Twistop() As Byte Local Time_out As Word Twcr = &H94 ' Stop ' Attende che lo stop Time_out = 0 Do ' sia completato o che Incr Time_out ' scada il Time Out Loop Until Twcr.twsto = 0 Or Time_out = Ee_timeout If Time_out = Ee_timeout Then Twistop = 1 Else Twistop = 0 End If End Function ' '****************************************************************************** '* Procedure di uso generico * '****************************************************************************** ' ' ' ' Funzione di supporto della Check_ready(). ' Function Check_ready_2() As Byte Local Time_out As Word , I_n As Byte Time_out = 0 ' Contatore timeout Do Incr Time_out ' Incrementa contatore Loop Until Twcr.twint = 1 Or Time_out = Ee_timeout I_n = Twsr I_n = I_n And &HF8 Check_ready_2 = I_n End Function ' ' ' Controlla che la scheda sia pronta ' Sub Check_ready() Local Check As Byte , Test As Byte Do ' Ciclo attesa scheda pronta Twsr = Twsr And &HFC Check = Twdr Or &HF4 Test.4 = Not Check.3 Twbr = 72 Twcr = &HA4 Check = Check_ready_2() Test = Check Or Twcr If Check = &H09 Then Test = &H67 Else Test.1 = Not Test.1 End If Twdr = &HA0 Twcr = &H84 Check = Check_ready_2() Twcr = &H94 Waitms 27 Loop Until Check = &H18 Or Test <> 1 Twcr.twen = 0 End Sub