' ********************************************************************** ' ** Program: gmbtmpe.BAS - Version : 1.1 - 20 June 2005 ** ' ** Compiler : Bascom AVR IDE - LIB 1.11.7.4 ** ' ** Board : GMB HR168 and GMM AM128 ** ' ** 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 ** ' ********************************************************************** ' ' 20.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.' ' ' ' !!!!!!!!!!!!!!!!!!!!!!!!!!! IMPORTANT !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ' In menu Options | Compiler | Chip, set: ' ' ' Chip: M128 ' HW Stack: at least 64 ' Soft Stack: at least 32 ' Framesize: at least 64 ' ' $regfile = "m128def.dat" $crystal = 7372800 $baud = 19200 ' '****************** Constants declatation *********************** ' Const Cret = 13 ' Carriage return Const Nl = 10 ' New line Const Clrscr = 12 ' Clear screen Const Bell = 7 ' 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 ' 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(twi_data As Byte , 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 Function Check_ready_2() As Byte 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 AM128 + GMB HR168" Print Call Check_ready() 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 = 0 ' 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 ****************************** ' ' ' '****************************************************************************** '* Demo specific procedures * '****************************************************************************** ' ' ' Initializes demo ' Sub Init() ' Initializes directionality of ports that drive optocouped inputs and relay ' outputs: ' 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 (by Default , that is When J10 is connected 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 ' Initialize hardware I2C BUS interface at about 72 kHz Call Twiinit(72) 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(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 ' ' ' 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(s , Twi_ack) = &H50 Then ' read seconds reg. 2 If Twirbyte(m , Twi_ack) = &H50 Then ' read minutes reg. 3 If Twirbyte(ho , Twi_ack) = &H50 Then ' read hours reg. 4 If Twirbyte(yd , Twi_ack) = &H50 Then ' read year and day reg. 5 If Twirbyte(wm , Twi_nack) = &H58 Then ' 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 ' 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 (by default, that is J10 in 3-4) ' 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) 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 ' '****************************************************************************** '* 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 ' ' ' Supporting function for Check_ready(). ' Function Check_ready_2() As Byte Local Time_out As Word , I As Byte Time_out = 0 ' Timeout counter Do Incr Time_out ' Increment counter Loop Until Twcr.twint = 1 Or Time_out = Ee_timeout I = Twsr I = I And &HF8 Check_ready_2 = I End Function ' ' ' Check for card ready ' Sub Check_ready() Local Check As Byte , Test As Byte Do ' Loop to check for card ready 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