' ********************************************************************** ' * File gmb_rtc.bas - Rel. 1.1 con Bascom 8051 DEMO IDE e LIB 2.0.6.0 * ' * 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 * ' * sales@grifo.it tech@grifo.it grifo@grifo.it * ' * by Graziano Gaiba del 16.04.03 * ' ********************************************************************** ' ' 16.04.03 - Rel 1.1 By Graziano Gaiba ' Demo program for GMB HR84 block module. ' Works with CAN GM1, CAN GM2 and GMM 5115. ' This program shows how to use RTC. ' ' Size of compiled file is not over 2048 bytes, so it can be compiled ' using free demo of BASCOM 8051. ' '****************** Compiler directives ************************** ' ' Set to 80h internal memory limit by: ' Options/Compiler/Misc $regfile = "grifo_mm.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 ' Read I2C Bus Const W = 0 ' Write I2C Bus ' Board where the demo is running Const Can_gm1 = "1" Const Can_gm2 = "2" Const Gmm_5115 = "3" ' '****************** Variables declarations ********************** ' ' Generic use Dim V As Byte , T As Byte ' Byte Generic use Dim S0 As Bit ' BitGeneric use Dim Cntintr As Byte ' Interrupts counter ' Usaed 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() ' Prints time data 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 Rtc_int() ' Shows interrupt periodic of RTC ' '************************* Main program ****************************** ' 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.1 for GMB HR84 ds 100203" Print Print "Which Mini Modulo grifo(r) is the demo running on:" Print "1) CAN GM1" Print "2) CAN GM2" Print "3) GMM 5115" Print Print "SELECT: "; Do S = Inkey() ' Stand by a key Loop Until S <> 0 If S = "3" Then Print "RTC not present." Do Loop End If ' Manages RTC (component PCF 8583 on board) in I2C bus Do ' Show main menu Print Chr(clrscr); ' Clear screen Print "Demo RTC" Print Print "Clock set as 31/12/2003 23:59.55" ' 0=sunday..6=saturday We = 6 ' Day D = 31 ' Month Mnt = 12 ' 0=2002..3=2005 Y = 1 ' Hours Ho = 23 ' Minutes M = 59 ' Seconds S = 55 Call Settime S , M , Ho , D , We , Mnt , Y ' Set RTC time and date Print "Press a key for demo interrupt" Call Output_orario() Call Rtc_int() ' RTC periodic interrupt Loop End ' '************************ Program End *************************** ' ' '**************************** Procedures ******************************** ' ' Shows RTC time and date continuiusly until a key is pressed Sub Output_orario() Do Call Gettime() ' Reads RTC If We = 0 Then Print "Sun "; ' Day of week If We = 32 Then Print "Mon "; If We = 64 Then Print "Tue "; If We = 66 Then Print "Wed "; If We = 128 Then Print "The "; If We = 160 Then Print "Fri "; If We = 192 Then Print "Sat "; Print " " ; Bcd(yd) ; ' Day Print "/" ; Bcd(wm) ; "/"; ' Month Print "200" ; If Y = 0 Then Print "2"; ' Year If Y = 64 Then Print "3"; If Y = 128 Then Print "4"; If Y = 192 Then Print "5"; Print " Ore " ; Bcd(ho) ; ' Hours Print ":" ; Bcd(m) ; ":" ; Bcd(s); ' Minutes and seconds Print " " ; Chr(13); ' Carriage return Waitms 200 ' delay S = Inkey Loop Until S <> 0 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 ' Questa procedura programma l'ora contenuta nell'RTC. ' I valori delle variabili prima di mandarli all'RTC vengono convertiti in BCD 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 ' Demo periodic interrupt of RTC Sub Rtc_int() Print Chr(clrscr); ' Clear screen Print "Interrupt" Print Print "Count transictions H->L of pin P2.5" S0 = P2.5 Cntintr = 0 Do If S0 = 1 And P2.5 = 0 Then Incr Cntintr S0 = P2.5 Print Cntintr ; Chr(cret); Waitms 200 S = Inkey Loop Until S <> 0 End Sub