Sunteți pe pagina 1din 4

Reloj en tiempo real con pic 16f628a

Device = 16F628A Xtal = 4 All_Digital = True PortB_Pullups = True ' Setup the LCD LCD_DTPin = PORTB.0 LCD_RSPin = PORTB.4 LCD_ENPin = PORTB.5 LCD_Interface = 4 LCD_Lines = 2 LCD_Type = 0 ' Define I2C bus ports SDA_Pin = PORTA.0 'DS1307 SDA pin SCL_Pin =PORTA.1 'DS1307 SCL pin Dim Temp1 As Byte Dim Temp2 As Byte Dim TempVal As Byte Dim Dim Dim Dim Dim Dim Dim Dim Dim Secs As Byte Mins As Byte Hrs As Byte day As Byte Date As Byte Month As Byte Year As Byte Ctrl As Byte actualizado As Bit

Dim Secs_last As Byte EData 0 EWrite 0,[actualizado] If actualizado =0 Then inicio 'Initialize LCD DelayMS 100 Cls ' Set initial DS1307 time / Date Secs = 0 Mins = 30 Hrs = 12 day = 1 Date = 30 Month = 11 Year = 6 ' Set seconds ' Set minutes ' Set hours ' Set day of week value ' Day of month value ' Month value ' Year value

Ctrl = 0 this example)

' Set the control byte (leave as 0 in

' The DS1307 works with data in BCD format, so convert BIN to BCD TempVal=Secs GoSub BIN_TO_BCD Secs=TempVal TempVal=Mins GoSub BIN_TO_BCD Mins=TempVal TempVal=Hrs GoSub BIN_TO_BCD Hrs=TempVal TempVal=day GoSub BIN_TO_BCD day=TempVal TempVal=Date GoSub BIN_TO_BCD Date=TempVal TempVal=Month GoSub BIN_TO_BCD Month=TempVal TempVal=Year GoSub BIN_TO_BCD Year=TempVal BStart ' The datasheet specifies the first byte is 1101000x where x is read(1) or write(0). ' The second byte tells the DS 1307 where to start reading, 0 is at the start. ' The Ctrl byte contains advanced features, read the datasheet for more info BusOut 11010000, 0, [Secs, Mins, Hrs, day, Date, Month, Year, Ctrl] 'Write initial values for time / Date BStop DelayMS 20 inicio: BStart ' The datasheet specifies the first byte is 1101000x where x is read(1) or write(0). ' The second byte tells the DS 1307 where to start reading, 0 is at the start.

BusIn 11010001, 0, [Secs, Mins, Hrs, day, Date, Month, Year, Ctrl] BStop ' The DS1307 sends it data in BCD, therefore it must be changed to ' BIN so that it can be easily used (eg, print onto an LCD) TempVal=Secs GoSub BCD_TO_BIN Secs=TempVal TempVal=Mins GoSub BCD_TO_BIN Mins=TempVal TempVal=Hrs GoSub BCD_TO_BIN Hrs=TempVal TempVal=Date GoSub BCD_TO_BIN Date=TempVal TempVal=Month GoSub BCD_TO_BIN Month=TempVal TempVal=Year GoSub BCD_TO_BIN Year=TempVal

If Secs - Secs_last = 0 Then GoTo inicio update in Secs, display time and Date

'If there is

' The Dec2 modifier makes sure that each value will have 2 characters, eg 1 becomes 01 Print At 1,1,"Hora: ",Dec2 Hrs, ":", Dec2 Mins,":", Dec2 Secs Print At 2,1,"Fecha: ", Dec2 Date, "-", Dec2 Month, "-", Dec2 Year Secs_last = Secs GoTo inicio BCD_TO_BIN: into BIN Temp1 = $0F & TempVal bits Temp1 = Temp2 = 2 BCD value Temp2 = TempVal Dig Temp1, 0 TempVal >> 4 Dig Temp2, 0 = Temp2 * 10 + Temp1 ' Shift down four to read ' Convert the BCD values ' Clear off the top four

Return BIN_TO_BCD: Temp1 = Dig TempVal, 0 THE FIRST NIBBLE Temp2 = Dig TempVal, 1 THE FIRST NIBBLE Temp2 = Temp2 << 4 2ND NIBBLE TempVal = Temp1 ^ Temp2 THE WHOLE BCD NUMBER Return End ' GET THE DEC DIGIT FOR ' GET THE DEC DIGIT FOR ' MOVE NUMBER OVER TO ' XOR THEM TOGTHER TO MAKE

Diagrama del circuito en proteus