' ********************************************************************** ' * File: k51ppi.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 permette di acquisire lo stato delle otto linee digitali TTL e lo ' visualizza sui display a 7 segmenti della K51-AVR, accanto alla scritta 'in'. ' ' ' !!!!!!!!!!!!!!!!!!!!!!!!!!! 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 ' Valore letto dal PCF8574 Dim Dato As Byte ' Usate dalla cifre Dim Cifh As Byte ' cifra 4 bit alti in HEX Dim Cifl As Byte ' cifra 4 bit bassi in HEX ' '****************** 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 Declare Sub Cifre(byval Valore As Byte ) ' converte un numero in 2 cifre HEX Declare Function Digit(byval Dig As Byte ) As Byte ' Converte un numero da 0-F in 7 segmenti Declare Sub Vis_num(byval N1 As Byte) ' visualizza un byte in HEX ' '************************* Programma main ****************************** ' Main: Print Chr(12) Print "Demo input di 8 linee TTL del PCF8574." Print Call Check_ready() Print "I display a 7 segmenti mostrano il valore letto." Call Iniz() ' inizializzazioni Do I2cstatus = Twistart() ' sequenza di Start per I2CBUS I2cstatus = Twiwbyte(rpcf8574) I2cstatus = Twirbyte(dato , Twi_nack) ' leggo il registro di stato I2cstatus = Twistop() 'I2creceive Rpcf8574 , Dato ' leggo lo stato di 8 ingressi del PCF8574 Call Vis_num(dato) ' visualizzo il byte in HEX 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(48) ' scrive DY1 "I" I2cstatus = Twiwbyte(84) ' scrive DY2 "n" I2cstatus = Twiwbyte(0) ' scrive DY3 off I2cstatus = Twiwbyte(0) ' scrive DY4 off I2cstatus = Twistop() End Sub ' ' ' ***************** Converte un numero da 0 a F in 7 segmenti *********** ' Questa procedura converte una cifra da 0 a F nel formato 7 segmenti, se ' il valore e maggiore di F il display risulta spento. ' Parametri: ' Ingresso : dig as byte, valore da 0 a F ' Uscita : byte, valore in formato 7 segmenti. ' ************************************************************************ ' Function Digit(byval Dig As Byte ) As Byte ' Converte un numero da 0-F in 7 segmenti Local Valore As Byte If Dig < 16 Then ' il numero risulta minore di 16 Valore = Lookup(dig , Tab_7seg) ' leggo in tabella il valore Else Valore = 0 ' se = 10 o superiore azzero End If Digit = Valore End Function ' ' ' ***************** Converte 1 byte in due cifre HEX ******************** ' Questa procedura converte 1 byte da 0 a 255 in due cifre esadecimali a ' 4 bit. ' Parametri: ' Ingresso : Valore as byte, valore da 0 a 255 ' Uscita : Cifh as byte, cifra del nibble alto ' Cifl as byte, cifra del nibble basso ' ************************************************************************ ' Sub Cifre(byval Valore As Byte ) ' converte un numero in due cifre HEX Cifh = Valore And &HF0 ' prelevo i 4 bit alti Cifh = Cifh / 16 ' sposto in basso i 4 bit alti Cifl = Valore And &H0F ' prelevo i 4 bit bassi End Sub ' ' ' *********************** Visualizza un byte in HEX ********************** ' Questa procedura permette di visualizzare un byte nel formato esadecimale. ' sui display DY3 e DY4. Es: 255= FFH, 32= 20H ecc. ' Parametri: ' Ingresso : N1 as byte, numero da visualizzare in HEX ' Uscita : nulla ' ************************************************************************ ' Sub Vis_num(byval N1 As Byte) ' visualizza un byte in HEX Local Valore As Byte I2cstatus = Twistart() ' sequenza di START I2cstatus = Twiwbyte(wsaa1064) ' slave address I2cstatus = Twiwbyte(dig3) ' punto al diplay 3 (DY3) Call Cifre(n1) ' converto il numero1 in 2 cifre Valore = Digit(cifh) ' converto la cifra decine in 7 seg. I2cstatus = Twiwbyte(valore) ' scrivo il display DY3 Valore = Digit(cifl) ' converto la cifra unita in 7 seg. I2cstatus = Twiwbyte(valore) ' scrivo il display DY4 I2cstatus = Twistop() ' sequenza di stop 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 ' ' ************* Tabella conversione per cifra a 7 segmenti da 0 a 15 ******* Tab_7seg: ' num. 0 1 2 3 4 5 6 7 8 9 Data &H3F , &H06 , &H5B , &H4F , &H66 , &H6D , &H7D , &H07 , &H7F , &H6F ' num. A B C D E F Data &H77 , &H7C , &H39 , &H5E , &H79 , &H71 ' ************************************************************************** '