' ********************************************************************** ' * File: gmbrtc.bas - Rel. 1.1 con Bascom AVR IDE e LIB 1.11.7.4 * ' * Schede: GMB HR168 + GMM AM128 * ' * 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 16.06.05 * ' ********************************************************************** ' ' 16.06.05 - Rel 1.1 By Graziano Gaiba ' Questo demo mostra la gestione dell'RTC a bordo della GMM AM128. ' ' ' 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 Cret = 13 ' Codice di ritorno di carrello Const Nl = 10 ' Codice nuova linea Const Clrscr = 12 ' codice di clear screen Const Bel = 7 ' codice di Bell ' ' ' Time out in caso una operazione TWI non riceva risposta ' Const Ee_timeout = 50000 ' Operazioni di lettura TWI Const Twi_ack = 1 Const Twi_nack = 0 ' '****************** Dichiarazione delle variabili ********************** ' ' Uso generico Dim Sr As String * 1 ' Stringa di uso generico Dim V As Byte ' Byte di uso generico ' Usate dal demo RTC Dim S As Byte ' secondi e uso generico Dim M As Byte ' minuti e uso generico Dim Ho As Byte ' ore Dim D As Byte ' giorno e uso generico Dim We As Byte ' settimana Dim Mnt As Byte ' mese Dim Y As Byte ' anno e uso generico Dim Wm As Byte ' settimana pił mese Dim Yd As Byte ' anno pił giorno ' '****************** Dichiarazione delle procedure ********************** ' Declare Sub Rtc() ' Demo gestione RTC ' 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 Init() Declare Sub Output_orario() ' Stampa dati orario Declare Sub Get_rtc() ' Legge l'orario su RTC Declare Sub Set_rtc(byval S As Byte , Byval M As Byte , Byval Ho As Byte , Byval D As Byte , Byval We As Byte , Byval Mnt As Byte , Byval Y As Byte) Declare Sub Set_relays_out(byval V As Byte) ' Imposta lo stato dei rele' ' '************************* Programma main ****************************** ' Main: Call Init() ' Inzializza il modulo Call Twiinit(72) Call Set_relays_out(0) Print Chr(clrscr); ' Pulisce lo schermo Print "Demo 1.1 per GMM AM128 + GMB HR168" Print Call Check_ready() ' Verifica scheda pronta Do ' Visualizzo il menu principale Print Chr(clrscr); ' Cancello lo schermo Print "Demo RTC" Print Print "Orologio impostato a 31/12/2005 23:59.55" ' 0=domenica..6=sabato We = 6 ' Giorno D = 31 ' Mese Mnt = 12 ' 0=2005..3=2008 Y = 0 ' Ore Ho = 23 ' Minuti M = 59 ' Secondi S = 55 Call Set_rtc(s , M , Ho , D , We , Mnt , Y ) ' Imposta RTC Do Call Output_orario() Waitms 200 Loop Loop End ' '************************ Fine del programma *************************** ' ' '****************************************************************************** '* Procedure di gestione specifiche del demo * '****************************************************************************** ' ' ' ' Inizializza il demo ' Sub Init() ' Inizializza la direzionalita' dei port che pilotano gli ingressi optoisolati ' e le uscite a rele': ' IN1-1 - Portb.7 ' IN2-1 - Portb.6 ' IN3-1 - Porte.4 ' IN4-1 - Porte.5 ' IN5-1 - Portd.7 ' IN6-1 - Porte.6 ' IN7-1 - Portb.4 ' IN8-1 - Portb.0 Ddrb.7 = 0 Ddrb.6 = 0 Ddre.4 = 0 Ddre.5 = 0 Ddrd.7 = 0 Ddre.6 = 0 Ddrb.4 = 0 Ddrb.0 = 0 ' IN1-2 - Portc.0 ' IN2-2 - Portc.1 ' IN3-2 - Portc.2 ' IN4-2 - Portc.3 ' IN5-2 - Portc.4 ' IN6-2 - Portc.5 ' IN7-2 - Portc.6 ' IN8-2 - Portc.7 Ddrc = 0 ' OUT A1 - Portf.4 ' OUT A2 - Portf.5 ' OUT B1 - Portf.6 ' OUT B2 - Portf.7 ' OUT C1 - Portb.3 ' OUT C2 - Portb.2 ' OUT D1 - Portb.1 ' OUT D2 - Porte.2 (di default, ovvero con J10 in 3-4) Ddrf.4 = 1 Ddrf.5 = 1 Ddrf.6 = 1 Ddrf.7 = 1 Ddrb.3 = 1 Ddrb.2 = 1 Ddrb.1 = 1 Ddre.2 = 1 End Sub ' ' ' Funzione di supporto della Check_ready(). ' Function Check_ready_2() As Byte Local Time_out As Word , I As Byte Time_out = 0 ' Contatore timeout Do Incr Time_out ' Incrementa contatore Loop Until Twcr.twint = 1 Or Time_out = Ee_timeout I = Twsr I = I And &HF8 Check_ready_2 = I 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 ' '****************************************************************************** '* 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 ' '****************************************************************************** '* Gestione dell'RTC * '****************************************************************************** ' ' ' ' Questa procedura legge l'ora contenuta nell'RTC ' Sub Get_rtc() If Twistart() = &H08 Then ' sequenza start If Twiwbyte(&Ha0) = &H18 Then ' punta indirizzo per scrive su PCF8583 If Twiwbyte(2) = &H28 Then ' punta nel registro 2 If Twistart() = &H10 Then ' ripete la sequenza di start If Twiwbyte(&Ha1) = &H40 Then ' punta indirizzo per leggere If Twirbyte(s , Twi_ack) = &H50 Then ' lettura secondi reg. 2 If Twirbyte(m , Twi_ack) = &H50 Then ' lettura minuti reg. 3 If Twirbyte(ho , Twi_ack) = &H50 Then ' lettura ore reg. 4 If Twirbyte(yd , Twi_ack) = &H50 Then ' lettura anno e giorno reg. 5 If Twirbyte(wm , Twi_nack) = &H58 Then ' lettura settimana e mese reg. 6 If Twistop() = 1 Then ' sequenza stop Print Chr(bel) ; "get_rtc: Timeout su stop." End If Else Print Chr(bel) ; "get_rtc: Errore su lettura registro 6." End If Else Print Chr(bel) ; "get_rtc: Errore su lettura registro 5." End If Else Print Chr(bel) ; "get_rtc: Errore su lettura registro 4." End If Else Print Chr(bel) ; "get_rtc: Errore su lettura registro 3." End If Else Print Chr(bel) ; "get_rtc: Errore su lettura registro 2." End If Else Print Chr(bel) ; "get_rtc: Errore su slave address in lettura." End If Else Print Chr(bel) ; "get_rtc: Errore su start ripetuto." End If Else Print Chr(bel) ; "get_rtc: Errore su indirizzo registro 2." End If Else Print Chr(bel) ; "get_rtc: Errore su slave address." End If Else Print Chr(bel) ; "get_rtc: Errore su start." End If Twcr.twen = 0 ' Spegne la periferica TWI We = Wm ' memorizzo il valore letto Y = Yd ' memorizzo il valore letto Y = Y And 192 ' elimino i bit che non riguardano anno We = We And 224 ' elimino i bit che non riguardano la settimana Wm = Wm And &H01F ' elimino 3 bit alti Yd = Yd And &H03F ' elimino i 2 bit alti End Sub ' ' ' Imposta l'ora dell'RTC ' Sub Set_rtc(s As Byte , M As Byte , Ho As Byte , D As Byte , We As Byte , Mnt As Byte , Y As Byte) S = Makebcd(s) ' secondi M = Makebcd(m) ' minuti Ho = Makebcd(ho) ' ore D = Makebcd(d) ' giorno Mnt = Makebcd(mnt) ' mese Y = Y * 64 ' sposto i primi 2 bit di 6 posti Yd = Y Or D ' compongono il registro anno e giorno We = We * 32 ' sposto i primi 3 bit di 5 posti Wm = We Or Mnt ' compongo il registro settimana e mese If Twistart() = &H08 Then ' sequenza start If Twiwbyte(&Ha0) = &H18 Then ' punta indirizzo per scrive su PCF8583 If Twiwbyte(0) = &H28 Then ' punta nel registro 0 If Twiwbyte(0) = &H28 Then ' attivo la lettura dei bit alti If Twistop() = 1 Then ' sequenza stop Print "set_rtc: Timeout su stop." End If Else Print "set_rtc: Errore su attivazione lettura bit alti." End If Else Print "set_rtc: Errore su indirizzo registro 0." End If Else Print "set_rtc: Errore su slave address." End If Else Print "set_rtc: Errore su start." End If ' If Twistart() = &H08 Then ' sequenza start If Twiwbyte(&Ha0) = &H18 Then ' punta indirizzo per scrive su PCF8583 If Twiwbyte(2) = &H28 Then ' punta nel registro 2 If Twiwbyte(s) = &H28 Then ' scrive secondi, reg. 2 If Twiwbyte(m) = &H28 Then ' scrive minuti, reg. 3 If Twiwbyte(ho) = &H28 Then ' scrive ore, reg. 4 If Twiwbyte(yd) = &H28 Then ' scrive anno e giorno, reg. 5 If Twiwbyte(wm) = &H28 Then ' scrive settimana e mese, reg. 6 If Twistop() = 1 Then ' sequenza stop Print "set_rtc: Timeout su stop." End If Else Print "set_rtc: Errore su scrittura registro 6." End If Else Print "set_rtc: Errore su scrittura registro 5." End If Else Print "set_rtc: Errore su scrittura registro 4." End If Else Print "set_rtc: Errore su scrittura registro 3." End If Else Print "set_rtc: Errore su scrittura registro 2." End If Else Print "set_rtc: Errore su indirizzo registro 2." End If Else Print "set_rtc: Errore su slave address." End If Else Print "set_rtc: Errore su start." End If Twcr.twen = 0 ' Spegne la periferica TWI End Sub ' ' ' Mostra l'ora letta dall'RTC Sub Output_orario() Call Get_rtc() ' Legge RTC Select Case We ' Giorno della settimana Case 0 : Print "Domenica "; Case 32 : Print "Lunedi' "; Case 64 : Print "Martedi' "; Case 96 : Print "Mercoledi' "; Case 128 : Print "Giovedi' "; Case 160 : Print "Venerdi' "; Case 192 : Print "Sabato "; Case Else : Print "Errore !! "; End Select Print " " ; Bcd(yd) ; ' Giorno Print "/" ; Bcd(wm) ; "/"; ' Mese Select Case Y ' Anno Case 0 : Print "2005"; Case 64 : Print "2006"; Case 128 : Print "2007"; Case 192 : Print "2008"; Case Else : Print "Err."; End Select Print " Ore " ; Bcd(ho) ; ' Ore Print ":" ; Bcd(m) ; ":" ; Bcd(s); ' Minuti e secondi Print " " ; Chr(13); ' A capo End Sub ' ' '****************************************************************************** '* Procedure di gestione degli I/O della GMB HR168 * '****************************************************************************** ' ' ' ' Imposta le uscite a rele' ' Se il Bit e' 1 il corrispondente rele' viene chiuso, altrimenti viene aperto ' OUT A1 - Portf.4 ' OUT A2 - Portf.5 ' OUT B1 - Portf.6 ' OUT B2 - Portf.7 ' OUT C1 - Portb.3 ' OUT C2 - Portb.2 ' OUT D1 - Portb.1 ' OUT D2 - Porte.2 (di default, ovvero con J10 in 3-4) ' ' V.0 comanda RL1 OUT A1 ' V.1 comanda RL2 OUT A2 ' V.2 comanda RL3 OUT B1 ' V.3 comanda RL4 OUT B2 ' V.4 comanda RL5 OUT C1 ' V.5 comanda RL6 OUT C2 ' V.6 comanda RL7 OUT D1 ' V.7 comanda RL8 OUT D2 Sub Set_relays_out(byval V As Byte) Portf.4 = Not V.0 Portf.5 = Not V.1 Portf.6 = Not V.2 Portf.7 = Not V.3 Portb.3 = Not V.4 Portb.2 = Not V.5 Portb.1 = Not V.6 Porte.2 = Not V.7 End Sub