' ********************************************************************** ' ** Program: gmbtmpe.BAS - Version : 1.1 - 14 June 2005 ** ' ** Compiler : Bascom AVR IDE - LIB 1.11.7.4 ** ' ** Board : GMB HR168 and GMM AM32 ** ' ** Society: grifo(r) ITALIAN TECHNOLOGY ** ' ** Via Dell' Artigiano 8/6 40016 San Giorgio di Piano (BO) ** ' ** Tel.+39 051 892 052 Fax +39 051 893 661 ** ' ** http://www.grifo.com http://www.grifo.it ** ' ** Written by: Graziano Gaiba ** ' ********************************************************************** ' ' 14.06.05 - Rel 1.1 By Graziano Gaiba ' This demo allows to manage three timers that act on relays outputs ' of CN3 and CN4. ' In detail, it is possible to input from console the action to perform ' and the time of activation for each of the three timers; two timers ' allow to input a combination for output relays, the third timer opens ' all the output relays.' ' '****************** Compiler directives ************************** ' $regfile = "m32def.dat" $crystal = 7372800 $baud = 19200 ' '****************** Constants declatation *********************** ' Const Cret = 13 ' Codice di ritorno di carrello Const Nl = 10 ' Codice nuova linea Const Clrscr = 12 ' codice di clear screen Const Bell = 7 ' codice di Bell ' Time out in case a TWI command receives no response Const Ee_timeout = 50000 ' TWI reading options Const Twi_ack = 1 Const Twi_nack = 0 ' ' '********************* Variables declaration ************************* ' ' Generic use Dim Sr As String * 1 ' String Generic use Dim V As Byte ' Byte Generic use Dim A As Eram Byte At 1023 ' Management of activity 1 Dim M1 As Byte ' Minute of activity 1 Dim A1 As Bit ' Enable activity 1 Dim Valore1 As Byte ' Outputs status for activity 1 ' Management of activity 2 Dim M2 As Byte ' Minute of activity 2 Dim A2 As Bit ' Enable activity 2 Dim Valore2 As Byte ' Outputs status for activity 2 ' Management of output deactivation Dim M_d As Byte ' Minute of outputs deactivation Dim A_d As Bit ' Enable outputs deactivation ' Used by demo RTC Dim S As Byte ' seconds and generic use Dim M As Byte ' minutes and generic use Dim Ho As Byte ' hours Dim D As Byte ' day and generic use Dim We As Byte ' week Dim Mnt As Byte ' month Dim Y As Byte ' year and generic use Dim Wm As Byte ' week and month Dim Yd As Byte ' year and day ' '****************** Procedures declaration ********************** ' Declare Sub Rtc() ' Demo RTC management ' Send a command to TWI interface, wait for it to complete and returns status Declare Function Twi_cmd(byval Cmd As Byte) As Byte Declare Sub Twiinit(byval Clockrate As Byte) ' Initialize TWI hardware interface Declare Function Twistart() As Byte ' Generate a start by TWI hardware interface Declare Function Twiwbyte(byval Dato As Byte) As Byte ' Send a byte by TWI hardware interface Declare Function Twirbyte(byval Assert_ack As Byte) As Byte ' Read a byte from TWI hardware interface Declare Function Twistop() As Byte ' Generate a stop by TWI hardware interface Declare Sub Check_ready() Declare Sub Init() Declare Sub Output_orario() ' Print time 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 Get_rtc() Declare Sub Sn_input() ' Asks Y or N Declare Sub Tempo_input() ' Asks minutes Declare Sub Valore_input() ' Asks a 8 bit value Declare Sub Set_relays_out(byval V As Byte) ' Sets outputs ' '************************* Programma main ****************************** ' Main: Call Init() ' Initialize module Call Set_relays_out(0) Print Chr(clrscr); ' Clears the screen Print "Demo 1.1 for GMM AM32 + GMB HR168" Print Call Check_ready() Print Chr(clrscr); ' Clear screen Print "Demo timed activities" Print Print "This program sets the outputs as selected by the user." Do ' Show main menu Print A1 = 0 ' Disables activities A2 = 0 A_d = 0 Print "Set with value 1 (Y/N)?"; Call Sn_input() If Sr = "Y" Then Call Tempo_input() ' Returns BCD value M1 = M Call Valore_input() Valore1 = S A1 = 1 Else Print End If Print "Set with value 2 (Y/N)?"; Call Sn_input() If Sr = "Y" Then Call Tempo_input() ' Returns BCD value M2 = M Call Valore_input() Valore2 = S A2 = 1 Else Print End If Print "Disable outputs (Y/N)?"; Call Sn_input() If Sr = "Y" Then Call Tempo_input() ' Returns BCD value M_d = M A_d = 1 Else Print End If Print "Clock" Call Tempo_input() ' Returns BCD value Print ' Seconds input by the user S = M ' 0=sunday..6=saturday We = 6 ' Day D = 31 ' Month Mnt = 12 ' 0=2005..3=2008 Y = 1 ' Hour Ho = 23 ' Minute M = 0 Call Set_rtc(s , M , Ho , D , We , Mnt , Y) ' Set RTC Print "Press key..." Do Call Output_orario() ' Manages timed activities If A1 = 1 Then If S = M1 Then Call Set_relays_out(valore1) End If End If If A2 = 1 Then If S = M2 Then Call Set_relays_out(valore2) End If End If If A_d = 1 Then If S = M_d Then Call Set_relays_out(&H00) End If End If M = Inkey() Loop Until M <> 0 Loop End ' '*************************** Program End ****************************** ' ' '**************************** Procedures ******************************* ' ' ' '****************************************************************************** '* Demo specific procedures * '****************************************************************************** ' ' ' Initializes demo ' Sub Init() ' Initializes directionality of ports that drive optocouped inputs and relay ' outputs: ' IN1-1 - Porta.0 ' IN2-1 - Porta.1 ' IN3-1 - Portd.2 ' IN4-1 - Portd.3 ' IN5-1 - Portb.0 ' IN6-1 - Portb.1 ' IN7-1 - Porta.2 ' IN8-1 - Porta.3 Ddra.0 = 0 Ddra.1 = 0 Ddrd.2 = 0 Ddrd.3 = 0 Ddrb.0 = 0 Ddrb.1 = 0 Ddra.2 = 0 Ddra.3 = 0 ' IN1-2 - Porta.4 ' IN2-2 - Porta.5 ' IN3-2 - Portc.2 ' IN4-2 - Portc.3 ' IN5-2 - Portc.4 ' IN6-2 - Portc.5 ' IN7-2 - Portc.6 ' IN8-2 - Portc.7 Ddra.4 = 0 Ddra.5 = 0 Ddrc.2 = 0 Ddrc.3 = 0 Ddrc.4 = 0 Ddrc.5 = 0 Ddrc.6 = 0 Ddrc.7 = 0 ' OUT A1 - Portd.6 ' OUT A2 - Portb.4 ' OUT B1 - Portb.2 ' OUT B2 - Portb.3 ' OUT C1 - Portb.5 ' OUT C2 - Portb.6 ' OUT D1 - Portb.7 ' OUT D2 - Porta.6 (by default, that is when J10 is connected in 3-4) Ddrd.6 = 1 Ddrb.4 = 1 Ddrb.2 = 1 Ddrb.3 = 1 Ddrb.5 = 1 Ddrb.6 = 1 Ddrb.7 = 1 Ddra.6 = 1 End Sub ' '****************************************************************************** '* I2C BUS hardware management procedures * '****************************************************************************** ' ' ' ' Writes command passed in parameter cmd into control register TWI and waits ' for the completion of operation, then reads the status of TWI interface and ' returns it. ' Function Twi_cmd(byval Cmd As Byte) As Byte Local Time_out As Word , I As Byte Twcr = Cmd ' Writes the command Time_out = 0 ' Timeout counter Do Incr Time_out ' Increment counter Loop Until Twcr.twint = 1 Or Time_out = Ee_timeout ' Reads status and masks not significant bits I = Twsr I = I And &HF8 Twi_cmd = I End Function ' ' ' Initializes hardware TWI interface ' Sub Twiinit(byval Clockrate As Byte) Twsr = Twsr And &HFC ' No prescaler Twbr = Clockrate ' Set clock rate End Sub ' ' ' Send start for hardware TWI interface ' Function Twistart() As Byte Twistart = Twi_cmd(&Ha4) ' Start End Function ' ' ' Send byte to hardware TWI interface ' Function Twiwbyte(byval Dato As Byte) As Byte Twdr = Dato ' Write data Twiwbyte = Twi_cmd(&H84) End Function ' ' ' Read byte from hardware TWI interface ' Function Twirbyte(byval Assert_ack As Byte) As Byte If Assert_ack = 1 Then Twirbyte = Twi_cmd(&Hc4) Else Twirbyte = Twi_cmd(&H84) End If End Function ' ' ' Send stop on hardware TWI interface. ' Returns 0 if there are no errors, a value different from 0 in case of ' timeout. ' Function Twistop() As Byte Local Time_out As Word Twcr = &H94 ' Stop ' Waits for the stop completer or timeout Time_out = 0 Do Incr 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 ' '****************************************************************************** '* RTC management * '****************************************************************************** ' ' ' Read the time stored in the RTC ' Sub Get_rtc() If Twistart() = &H08 Then ' start sequence If Twiwbyte(&Ha0) = &H18 Then ' point address to write in PCF8583 If Twiwbyte(2) = &H28 Then ' point register 2 If Twistart() = &H10 Then ' repeats start sequence If Twiwbyte(&Ha1) = &H40 Then ' point address to read If Twirbyte(twi_ack) = &H50 Then S = Twdr ' read seconds reg. 2 If Twirbyte(twi_ack) = &H50 Then M = Twdr ' read minutes reg. 3 If Twirbyte(twi_ack) = &H50 Then Ho = Twdr ' read hours reg. 4 If Twirbyte(twi_ack) = &H50 Then Yd = Twdr ' read year and day reg. 5 If Twirbyte(twi_nack) = &H58 Then Wm = Twdr ' read week and month reg. 6 If Twistop() = 1 Then ' stop sequence Print "get_rtc: Timeout during stop." End If Else Print "get_rtc: Error reading register 6." End If Else Print "get_rtc: Error reading register 5." End If Else Print "get_rtc: Error reading register 4." End If Else Print "get_rtc: Error reading register 3." End If Else Print "get_rtc: Error reading register 2." End If Else Print "get_rtc: Error on slave address while reading." End If Else Print "get_rtc: Error during second start." End If Else Print "get_rtc: Error on address of register 2." End If Else Print "get_rtc: Error on slave address." End If Else Print "get_rtc: Error on start." End If Twcr.twen = 0 ' Disables TWI We = Wm ' store value read Y = Yd ' store value read Y = Y And 192 ' mask bits not concerning the year We = We And 224 ' mask bits not concerning the week Wm = Wm And &H01F ' mask 3 high bits Yd = Yd And &H03F ' mask 2 high bits End Sub ' ' ' Set RTC time ' 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) S = Makebcd(s) ' seconds M = Makebcd(m) ' minutes Ho = Makebcd(ho) ' hours D = Makebcd(d) ' day Mnt = Makebcd(mnt) ' week Y = Y * 64 ' shift 2 bits 6 times Yd = Y Or D ' create value for year and day register We = We * 32 ' shift 3 bits 5 times Wm = We Or Mnt ' create value for week and month register If Twistart() = &H08 Then ' start sequence If Twiwbyte(&Ha0) = &H18 Then ' point address to write to PCF8583 If Twiwbyte(0) = &H28 Then ' point register 0 If Twiwbyte(0) = &H28 Then ' read high bits If Twistop() = 1 Then ' stop sequence Print "set_rtc: Timeout on stop." End If Else Print "set_rtc: Error on reading high bit." End If Else Print "set_rtc: Error on addressing register 0." End If Else Print "set_rtc: Error on slave address." End If Else Print "set_rtc: Error on start." End If ' If Twistart() = &H08 Then ' start sequence If Twiwbyte(&Ha0) = &H18 Then ' point to address to write PCF8583 If Twiwbyte(2) = &H28 Then ' point register 2 If Twiwbyte(s) = &H28 Then ' read seconds, reg. 2 If Twiwbyte(m) = &H28 Then ' read minutes, reg. 3 If Twiwbyte(ho) = &H28 Then ' read hours, reg. 4 If Twiwbyte(yd) = &H28 Then ' read year and day, reg. 5 If Twiwbyte(wm) = &H28 Then ' read week and month, reg. 6 If Twistop() = 1 Then ' stop sequence Print "set_rtc: Timeout on stop." End If Else Print "set_rtc: Error writing register 6." End If Else Print "set_rtc: Error writing register 5." End If Else Print "set_rtc: Error writing register 4." End If Else Print "set_rtc: Error writing register 3." End If Else Print "set_rtc: Error writing register 2." End If Else Print "set_rtc: Error writing register 2." End If Else Print "set_rtc: Error on slave address." End If Else Print "set_rtc: Error on start." End If Twcr.twen = 0 ' Disable TWI End Sub ' ' ' Used in the RTC demo to show continuously the time until a ' Key is pressed ' Sub Output_orario() Call Get_rtc() ' Read RTC Select Case We ' Weekday Case 0 : Print "Sunday "; Case 32 : Print "Monday "; Case 64 : Print "Tuesday "; Case 96 : Print "Wednesday "; Case 128 : Print "Thursday "; Case 160 : Print "Friday "; Case 192 : Print "Saturday "; Case Else : Print "Error !!! "; End Select Print " " ; Bcd(yd) ; ' Day Print "/" ; Bcd(wm) ; "/"; ' Month Select Case Y ' Year Case 0 : Print "2005"; Case 64 : Print "2006"; Case 128 : Print "2007"; Case 192 : Print "2008"; Case Else : Print "Err."; End Select Print " - " ; Bcd(ho) ; ' Hour Print ":" ; Bcd(m) ; ":" ; Bcd(s); ' Minutes and seconds Print " " ; Chr(13); ' Carriage return End Sub ' '****************************************************************************** '* Procedures to manage I/O signals of GMB HR168 * '****************************************************************************** ' ' ' . ' ' Set relays outputs ' If Bit value is 1, corresponding relay is closed, otherwise it is open ' V.0 controls RL1 ' V.1 controls RL2 ' V.2 controls RL3 ' V.3 controls RL4 ' V.4 controls RL5 ' V.5 controls RL6 ' V.6 controls RL7 ' V.7 controls RL8 Sub Set_relays_out(byval V As Byte) Portd.6 = Not V.0 Portb.4 = Not V.1 Portb.2 = Not V.2 Portb.3 = Not V.3 Portb.5 = Not V.4 Portb.6 = Not V.5 Portb.7 = Not V.6 Porta.6 = Not V.7 End Sub ' '****************************************************************************** '* Generic procedures * '****************************************************************************** ' ' ' Asks seconds ' Sub Tempo_input Print "Seconds:"; Input M M = Makebcd(m) End Sub ' ' ' Asks for a 4 bit value ' Sub Valore_input Inputhex "Value (00-FF):" , S End Sub ' ' ' Asks Y or N ' Sub Sn_input Do Sr = Waitkey() Sr = Ucase(sr) Loop Until Sr = "Y" Or Sr = "N" Print Sr End Sub ' ' Check for hardware ready ' Sub Check_ready() Test: If A = &H55 Then Goto L2 Else Goto Test End If L2: End Sub