' ********************************************************************** ' ** Program name: RTCSEG.BAS - Version : 1.2 - 05 June 2000 ** ' ** Compiler : BASCOM 8051 DEMO, (IDE V.2.0.0.0, LIB V.2.0) ** ' ** Board : K51-AVR ** ' ** Firm: 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 ** ' ** sales@grifo.it tech@grifo.it grifo@grifo.it ** ' ** ** ' ** Written by: Adriano Pedrielli ** ' ********************************************************************** ' ' This program allows to show the RTC or Real Time Clock on IC7 (PCF8583) ' to the four 7 segments displays: ' To set the RTC values keys T2 and T3 are used, in detail key T2 ' increments the hours and T3 increments the minutes. ' Whenever one of the two keys is pressed the seconds are reset. ' Key T1 switches between visualization of seconds and hours. ' Whenever a key is pressed, an acoustic signal is emitted. ' Date and eventual alarm are not managed: ' ' Before compiling set in menu Option/Compiler/Misc: Byte End 5F. ' '*********************************************************************** ' '********************* Compiler Directives ***************************** ' ' $regfile = "REG51.DAT" ' 8xc51 registers list '$regfile = "89c2051.DAT" ' 89c2051 registers list $romstart = &H0 ' code starting address $crystal = 11059200 ' CPU clock frequence Config Debounce = 50 ' Time to wait before considering ' a key as pressed ' E.g.: 10= 10ms, 50= 50ms ' ' *************** List PINs for use with 89c1051/2051/4051 ********************* 'Config Sda = P1.7 ' Pin 19 DATA signal for I2CBUS 'Config Scl = P1.6 ' Pin 18 Clock signal for I2CBUS 'T1 Alias P1.5 ' Pin 17 connected to key T1 'T2 Alias P1.4 ' Pin 16 connected to key T2 'T3 Alias P1.3 ' Pin 15 connected to key T3 'Buz Alias P3.5 ' Pin 9 connected to Buzzer ' ' *************************** List PINs for use with 8xc51 ********************* ' Buz Alias P3.5 ' Pin 15 connected to buzzer Config Sda = P3.6 ' Pin 16 DATA signal for I2CBUS Config Scl = P3.7 ' Pin 17 Clock signal for I2CBUS T1 Alias P1.0 ' Pin 1 connected to key T1 T2 Alias P1.1 ' Pin 2 connected to key T2 T3 Alias P1.2 ' Pin 3 connected to key T3 ' '********************* Constants declaration *************************** ' Dim Saa1064 As Const &H38 ' Slave address SAA1064 Dim Wsaa1064 As Const &H70 ' Slave address SAA1064 in Write Dim Rsaa1064 As Const &H71 ' Slave address SAA1064 in Read ' ******************** List addresses of Saa1064 *********************** Dim Ctb As Const 0 ' Address Control byte Dim Dig1 As Const 1 ' Address Digit 1 Dim Dig2 As Const 2 ' Address Digit 2 Dim Dig3 As Const 3 ' Address Digit 3 Dim Dig4 As Const 4 ' Address Digit 4 ' '*********************************************************************** ' Dim Rtc As Const &H50 ' Slave address RTC PCF8583 Dim Wrtc As Const &HA0 ' Slave address RTC PCF8583 in Write Dim Rrtc As Const &HA1 ' Slave address RTC PCF8583 in Read ' '*********************** Variables declaration ************************* ' Dim Valore As Byte ' value to write to or read from I2CBUS etc. Dim Sm As Byte ' stores value of seconds Dim S As Byte ' seconds Dim M As Byte ' minutes Dim Ho As Byte ' hours Dim Wm As Byte ' week and month Dim Yd As Byte ' year and day Dim Dig As Byte ' value of a digit Dim Cifd As Byte ' figure of decines Dim Cifu As Byte ' figure of units Dim N1 As Byte ' BCD number for DY1 and DY2 Dim N2 As Byte ' BCD number for DY3 and DY4 Dim Vsec As Bit ' indicates whether seconds are shown Dim Xbit As Bit ' indicates the status of the decimal point ' '********************** Procedure declarations ************************* ' Declare Sub Iniz ' Peripherals initialization Declare Sub Cifre (valore As Byte , Cifd As Byte , Cifu As Byte ) ' converts a bcd number in two figures Declare Sub Digit (dig As Byte ) ' Converts a number in range 0-9 in 7 segments Declare Sub Vis_2num (n1 As Byte , N2 As Byte) ' shows two BCD numbers Declare Sub Settime (ho As Byte , M As Byte , S As Byte) ' clock programming Declare Sub Gettime ' clock visualization ' '*************************** Main program ****************************** ' Main: ' Delay for signals settling Waitms 1 Call Iniz ' initializations Do Call Gettime ' visualize RTC Debounce T1 , 0 , Vis_sec , Sub ' check for T1 pressed Debounce T2 , 0 , Inc_ore , Sub ' check for T2 pressed Debounce T3 , 0 , Inc_min , Sub ' check for T3 pressed Loop ' loop forever ' ' ***************** Secondi visualization management ********************* Vis_sec: ' inverts the status of the bit, ' so each key pressure enables or ' disables the visualization of ' the seconds Buz = 0 ' turn on the buzzer Waitms 50 ' delay Vsec = Not Vsec Buz = 1 ' turn off the buzzer Return ' ' *************** Increment hours everytime key T2 is pressed **************** Inc_ore: Call Cifre(ho , Cifd , Cifu) ' convert hours from BCD to Decimal If Valore < 23 Then ' increment only if lower than 23 Incr Valore ' increment hours Else Valore = 0 ' reset if hours is 23 End If Ho = Valore ' store in the variable hours Call Cifre(m , Cifd , Cifu) ' convert minutes from BCD to Decimal M = Valore ' store in the variable minutes Goto Pr_rtc ' ' *************** Increment minutes everytime key T3 is pressed *************** Inc_min: Call Cifre(ho , Cifd , Cifu) ' convert hours from BCD to Decimal Ho = Valore ' store in the variable hours Call Cifre(m , Cifd , Cifu) ' convert minutes from BCD to Decimal If Valore < 59 Then ' increment only if lower than 59 Incr Valore ' increment Minutes Else Valore = 0 ' reset if minutes is 59 End If M = Valore ' store in the variable minutes Pr_rtc: If Vsec = 0 Then ' seconds are not visualized Buz = 0 ' turn on the buzzer Waitms 50 ' delay Call Settime(ho , M , 0) ' program RTC, resetting seconds Buz = 1 ' turn on the buzzer End If Return End ' '**************************** Program end ****************************** ' ' '**************************** Procedures ******************************* ' ' ********************** Peripherals initialization ******************** ' This procedure performs all the system initializations. ' Parameters: ' Input : nothing ' Output : nothing ' ************************************************************************ ' Sub Iniz ' Peripherals initialization Vsec = 0 ' do not visualize seconds Yd = 0 ' reset register year and day Wm = 0 ' reset register week and month Do I2creceive Rsaa1064 , Valore ' Read the status register Loop Until Valore = 0 ' Wait for SAA1064 to turn on I2cstart ' Start sequence for I2CBUS I2cwbyte Wsaa1064 ' Communicate the Slave address I2cwbyte Ctb ' Point to control register I2cwbyte &B00100111 ' bit0 =1 dynamic mode ' bit1 =1 digit 1+3 not blanked ' bit2 =1 digit 2+4 not blanked ' bit3 =0 no test segment ' bit4 =0 no 3mA segment current ' bit5 =1 6mA segment current ' bit6 =0 no 12mA segment current ' bit7 =0 indifferent I2cwbyte 0 ' sets DY1 off I2cwbyte 0 ' sets DY2 off I2cwbyte 0 ' sets DY3 off I2cwbyte 0 ' sets DY4 off I2cstop End Sub ' ' *********** Converts a figure in range from 0 to 9 in 7 segments ***** ' This procedure converts a figure in range from 0 to 9 in 7 segments format, if ' value is greater than 9 the display is off. ' Parameters: ' Input : dig as byte, value from 0 to 9 ' Output : dig as byte, value in 7 segments format. ' ************************************************************************ ' Sub Digit (dig As Byte ) ' Converts a number in range 0-9 in 7 segments If Dig < 10 Then ' number is lower than 10 Dig = Lookup(dig , Tab_7seg) ' read value in table Else Dig = 0 ' if = 10 or greater reset End If End Sub ' ' ******************* Converts a BCD number in two figures *************** ' This procedure converts a BCD number from 0to 153 (DEC 0-99) in two ' separated figures and returns the decimal value, so the BCD can be ' shown. ' If the number is greater than 153 the two figures are assigned the value ' of ten that means 7 segments display off. ' Parameters: ' Input : Valore as byte, value from 0 to 153 BCD ( Decimal 0..99) ' Output : Cifd as byte, decines figure ' Cifu as byte, units figure ' Valore as byte, contains the decimal value of the BCD number ' ************************************************************************ ' Sub Cifre (valore As Byte , Cifd As Byte , Cifu As Byte ) ' Converts a BCD number in two figures If Valore < 154 Then ' if is lower than 154 BCD( 100 in DEC) Cifd = Valore / 16 ' calculate decines figure Cifu = Cifd * 16 Cifu = Valore - Cifu ' calculate units figure Valore = Cifd * 10 Valore = Valore + Cifu ' calculate decimal value Else Cifd = 10 ' turn off decines figure Cifu = 10 ' turn off units figure Valore = 0 ' reset decimale value End If End Sub ' ' **************** Visualizes the hour stored in the RTC *********************** ' Parameters: ' Input : nothing ' Output : nothing ' ************************************************************************ ' Sub Gettime I2cstart ' Start sequence for I2CBUS I2cwbyte Wrtc ' point address to write to PCF8583 I2cwbyte 2 ' point to register 2 I2cstart ' repeat start sequence for I2CBUS I2cwbyte Rrtc ' point address to read I2crbyte S , Ack ' read seconds register 2 I2crbyte M , Ack ' read minutes register 3 I2crbyte Ho , Ack ' read hours register 4 I2crbyte Yd , Ack ' read year and day register 5 I2crbyte Wm , Nack ' write week and month register 6 I2cstop ' Stop sequence for I2CBUS If Vsec = 0 Then ' do not visualize seconds Call Vis_2num(ho , M) ' visualize hours and minutes Else Call Vis_2num(154 , S) ' turn off first 2 figures End If ' visualize 2 figures of seconds End Sub ' ' *********************** Visualizes 2 BCD numbers ************************ ' This procedure allows to visualize two BCD numbers that in decimal are ' made of two figures. E.g.: BCD 153= Dec. 99 ' As time goes, each variation of seconds turns on or off the decimal point ' of DY2. ' ' Parameters: ' Input : N1 as byte, value first number ' N2 as byte, value second number ' Uscita : nulla ' ************************************************************************ ' Sub Vis_2num (n1 As Byte , N2 As Byte) ' visualize 2 BCD numbers I2cstart ' Start sequence for I2CBUS I2cwbyte Wsaa1064 ' Communicate the Slave address I2cwbyte Dig1 ' point to display 1 (DY1) Call Cifre(n1 , Cifd , Cifu) ' convert numbero in 2 figures If Cifd = 0 Then ' if figure = 0 reset it Cifd = 10 End If Call Digit(cifd) ' convert decines figure in 7 segments I2cwbyte Dig ' output to first display Call Digit(cifu) ' convert units figure in 7 segments If Sm <> S Then ' if seconds changed CPL {XBIT} ' turn on decimal point Sm = S ' store seconds End If If Xbit = 1 Then ' if flag is on Dig = Dig Or 128 ' turn on decimal poinit End If ' normallly it is not visualized I2cwbyte Dig Call Cifre(n2 , Cifd , Cifu) ' convert number 2 in 2 figures Call Digit(cifd) ' convert decines figure in 7 segments I2cwbyte Dig ' output to third display Call Digit(cifu) ' convert units figure in 7 segments I2cwbyte Dig ' output to fourth display I2cstop ' Stop sequence for I2CBUS End Sub ' ' *********************** RTC PCF8583 programming ************************** ' This procedure sets the hour in the RTC. ' Values of variables are converted into BCD. ' Date and alarm are not managed. ' Parameters. ' Input : Ho as byte, value for hours ' M as byte, value for minutes ' S as byte, value for seconds ' Output : nothing ' ************************************************************************ ' Sub Settime(ho As Byte , M As Byte , S As Byte) S = Makebcd(s) ' seconds M = Makebcd(m) ' minutes Ho = Makebcd(ho) ' hours I2cstart ' Start sequence for I2CBUS I2cwbyte Wrtc ' point address to write to PCF8583 I2cwbyte 0 ' selects control register in register 0 I2cwbyte 0 ' starts high bits read I2cstop ' Stop sequence for I2CBUS ' I2cstart ' Start sequence for I2CBUS I2cwbyte Wrtc ' point address to read from PCF8583 I2cwbyte 2 ' point to register 2 I2cwbyte S ' writes seconds, register 2 I2cwbyte M ' writes minutes, register 3 I2cwbyte Ho ' writes hours, register 4 I2cstop ' Stop sequence for I2CBUS End Sub ' ' ************* Conversion table for 7 segments figure in the range from 0 to 15 ******* Tab_7seg: ' num. 0 1 2 3 4 5 6 7 8 9 Data &H3F , &H06 , &H5B , &H4F , &H66 , &H6D , &H7D , &H07 , &H7F , &H6F ' ************************************************************************************** '