' ********************************************************************** ' ** Program : gmbtmpe.bas - Version : 1.2 ** ' ** Compiler : BASCOM 8051 DEMO, (IDE and LIB V.2.0.11.0) ** ' ** Board : GMB HR1684 with GMM ACZero ** ' ** Corporate: 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 02.02.2005 ** ' ********************************************************************** ' ' If GMB HR168 is provided with optional Real Time Clock, 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. ' ' Rel. 1.1 03.03.04 - By Graziano Gaiba ' ' Rel. 1.2 02.02.05 - By Graziano Gaiba ' ' Modified to work with GMM ACZero ' ' ' !!!!!!!!!!!!!!!!!!!!!!! ATTENTION !!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ' In menu Options/Compiler/Misc set: ' - Byte End(hex) at least A0 ' ' ' The file compiled is smaller than 2048 bytes, so it can be compiled ' with free demo version of BASCOM 8051. ' '****************** Compiler directives ************************** ' $regfile = "8951cc03.dat" $romstart = &H0 ' start address of code for FLASH $ramstart = &H0 ' start address of external RAM $ramsize = &H100 ' 256 bytes of external RAM $crystal = 14745600 ' Microcontroller clock $baud = 19200 ' RS-232 baud rate '$large ' 16 bit addressing for jumps ' (Not for demo version) $map ' Generates address map Config Sda = P2.1 ' I2C serial DATA Config Scl = P2.0 ' I2C serial CLK ' '****************** Constants declatation *********************** ' Const Cret = 13 ' Carriage return Const Nl = 10 ' New line Const Clrscr = 12 ' Clear screen Const Bell = 7 ' Bell Const R = 1 ' value to read in I2C Bus Const W = 0 ' value to write to I2C Bus ' '********************* 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 ' Output value at 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 ' Output value at activity 2 ' Management outputs disable Dim M_d As Byte ' Minute outputs disable Dim A_d As Bit ' Enable outputs disable ' 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 Declare Sub Output_orario() ' Print time Declare Sub Settime(s As Byte , M As Byte , Ho As Byte , D As Byte , We As Byte , Mnt As Byte , Y As Byte) Declare Sub Gettime() Declare Sub Sn_input() ' Aks S or N Declare Sub Tempo_input() ' Asks seconds Declare Sub Valore_input() ' Asks for a 4 bit value Declare Sub Set_relays_out(v As Byte) ' Sets outputs ' '************************* Programma main ****************************** ' Main: ' Inizializes demo Disable Interrupts ' Disables interrupts Auxr = &H0C ' Selects Eram on external data area Eecon = &H00 ' Disables Eeprom of Micro Adcf = Adcf And &HF9 ' Sets P1.1 and P1.2 as I/O P3 = &HFF ' Sets port 3 as input Do Print Chr(clrscr); ' Clear screen Print "Demo 1.2 for GMB HR168 ds 110104" Print Print "Is optional RTC installed (Y/N)? " Do S = Inkey() ' Stand by a key Loop Until S <> 0 S = S Or &H20 If S = "n" Then Print "NO RTC" Do Loop End If Do ' Show main menu Print Chr(clrscr); ' Clear screen Print "Demo timed activities" Print Print "This program sets the outputs as selected by the user." 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=2004..3=2007 Y = 1 ' Hour Ho = 23 ' Minute M = 0 Call Settime 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 ******************************* ' ' ' Asks seconds ' Sub Tempo_input Print "Seconds:"; Input M M = Makebcd(m) End Sub ' ' ' Asks for a 4 bit value ' Sub Valore_input Inputhex "Valore (00-FF):" , S End Sub ' ' ' Shows RTC time ' Sub Output_orario() Call Gettime() ' Read RTC ' Hours, minutes , seconds and carriage return Print Bcd(ho) ; ":" ; Bcd(m) ; "." ; Bcd(s) ; Chr(13); Waitms 200 ' delay End Sub ' ' ' Get RTC time and date ' Sub Gettime I2cstart ' start sequence I2cwbyte &HA0 ' slave address of PCF8583 I2cwbyte 2 ' registr 2 I2cstart ' start sequence again I2cwbyte &HA1 ' slave address for reading I2crbyte S , Ack ' read seconds in reg. 2 I2crbyte M , Ack ' read minutes in reg. 3 I2crbyte Ho , Ack ' read hours in reg. 4 I2crbyte Yd , Ack ' read year and day in reg. 5 I2crbyte Wm , Nack ' read week and month in reg. 6 I2cstop ' stop sequence We = Wm ' stores read value Y = Yd ' stores read value Y = Y And 192 ' masks bit not regarding the year We = We And 224 ' masks bit not regarding the week Wm = Wm And &H01F ' masks 7 most significant bits Yd = Yd And &H03F ' masks 2 most significant bits End Sub ' ' ' This procedure set RTC time and date. ' Values are converted to BCD before being sent to RTC ' Sub Settime(s As Byte , M As Byte , Ho As Byte , D As Byte , We As Byte , Mnt As Byte , Y As Byte) S = Makebcd(s) ' seconds M = Makebcd(m) ' minutes Ho = Makebcd(ho) ' hours D = Makebcd(d) ' day Mnt = Makebcd(mnt) ' month Y = Y * 64 ' shift 2 LSB 6 places left Yd = Y Or D ' Value for year-and-day register We = We * 32 ' shift 3 LSB 5 places left Wm = We Or Mnt ' Value for week-and-month register I2cstart ' start sequence I2cwbyte &HA0 ' slave address for writing I2cwbyte 0 ' selects control register in reg. 0 I2cwbyte 0 ' reads its most significant bits I2cstop ' stop sequence ' I2cstart ' start sequence I2cwbyte &HA0 ' slave address for writing I2cwbyte 2 ' addresses register 2 I2cwbyte S ' writes seconds, reg. 2 I2cwbyte M ' writes minutes, reg. 3 I2cwbyte Ho ' writes hours, reg. 4 I2cwbyte Yd ' writes year and day, reg. 5 I2cwbyte Wm ' writes week and month, reg. 6 I2cstop ' stop sequence 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 ' ' ' Set relays outputs ' If Bit is 1, corresponding relay is closed, otherwise it is opened ' V.0 commands RL1 ' V.1 commands RL2 ' V.2 commands RL3 ' V.3 commands RL4 ' V.4 commands RL5 ' V.5 commands RL6 ' V.6 commands RL7 ' V.7 commands RL8 Sub Set_relays_out(v As Byte) P1.4 = Not V.0 P1.5 = Not V.1 P1.6 = Not V.2 P1.7 = Not V.3 P4.0 = Not V.4 P4.1 = Not V.5 P2.2 = Not V.6 P1.3 = Not V.7 End Sub