Sabahan
- 30th May 2014, 10:04
I had try to use a DS1307 on my recent project to keep track of the time,but seems the device is not working,it keep give me value for all return,below is my detup and my code
MCU : PIC16F877A
RTC : DS1307 with 32.768 KHz Crystal
Crystal : 4Mhz
others : Bat termial of DS1307 is grounded
Pull up resistor is connected to SDA and SCL
the code
'************************************************* ***************
'* Name : UNTITLED.BAS *
'* Author : [select VIEW...EDITOR OPTIONS] *
'* Notice : Copyright (c) 2014 [select VIEW...EDITOR OPTIONS] *
'* : All Rights Reserved *
'* Date : 29/05/2014 *
'* Version : 1.0 *
'* Notes : *
'* : *
'************************************************* ***************
' Name : RTCXU.pbp
' Compiler : PICBASIC PRO Compiler 2.6
' Assembler : MPASM
' Target PIC : PIC16F877A or similar type
' Hardware :
' Oscillator : 4MHz external
' Keywords : I2CREAD, I2CWRITE, LCDOUT
' Description : PICBASIC PRO program for an LCD clock program
' using the Dallas DS1307 I2C RTC.
'
' Define LOADER_USED to allow use of the boot loader.
' This will not affect normal program operation.
' Define LOADER_USED 1
' RESET_ORG can be set to move the BASIC program out of the way
' of any boot loader running from location 0, such as the
' Microchip USB boot loader
'Define RESET_ORG 800h
TRISD = %00000000
TRISC = %00000000
Define OSC 4 ' Core is running at 4MHz
' Define LCD pins
DEFINE LCD_DREG PORTD
DEFINE LCD_DBIT 4
DEFINE LCD_RSREG PORTD
DEFINE LCD_RSBIT 1
DEFINE LCD_EREG PORTD
DEFINE LCD_EBIT 0
DEFINE LCD_BITS 4
DEFINE LCD_LINES 2
' Alias pins
SDA Var PORTC.0
SCL Var PORTC.1
' Allocate variables
RTCYear Var Byte
RTCMonth Var Byte
RTCDate Var Byte
RTCDay Var Byte
RTCHour Var Byte
RTCMin Var Byte
RTCSec Var Byte
RTCCtrl Var Byte
ADCON1 = 15 ' PORTA and E digital
Low PORTE.2 ' LCD R/W low = write
Pause 100 ' Wait for LCD to startup
' Set initial time to 8:00:00AM 06/21/05
RTCYear = $05
RTCMonth = $06
RTCDate = $21
RTCDay = $02
RTCHour = $08
RTCMin = 0
RTCSec = 0
RTCCtrl = 0
Gosub settime ' Set the time
Goto mainloop ' Skip over subroutines
' Subroutine to write time to RTC
settime:
I2CWrite SDA, SCL, $D0, $00, [RTCSec, RTCMin, RTCHour, RTCDay, RTCDate, RTCMonth, RTCYear, RTCCtrl]
Return
' Subroutine to read time from RTC
gettime:
I2CRead SDA, SCL, $D0, $00, [RTCSec, RTCMin, RTCHour, RTCDay, RTCDate, RTCMonth, RTCYear, RTCCtrl]
Return
' Main program loop - in this case, it only updates the LCD with the time
mainloop:
Gosub gettime ' Read the time from the RTC
' Display time on LCD
Lcdout $fe, 1, hex2 RTCMonth, "/", hex2 RTCDate, "/" , hex2 RTCYear
lcdout $fe,$c0,hex2 RTCHour, ":", hex2 RTCMin, ":", hex2 RTCSec
Pause 500 ' Do it about 2 times a second
Goto mainloop ' Do it forever
End
Can anyone please pointing out what have I done wrong?
MCU : PIC16F877A
RTC : DS1307 with 32.768 KHz Crystal
Crystal : 4Mhz
others : Bat termial of DS1307 is grounded
Pull up resistor is connected to SDA and SCL
the code
'************************************************* ***************
'* Name : UNTITLED.BAS *
'* Author : [select VIEW...EDITOR OPTIONS] *
'* Notice : Copyright (c) 2014 [select VIEW...EDITOR OPTIONS] *
'* : All Rights Reserved *
'* Date : 29/05/2014 *
'* Version : 1.0 *
'* Notes : *
'* : *
'************************************************* ***************
' Name : RTCXU.pbp
' Compiler : PICBASIC PRO Compiler 2.6
' Assembler : MPASM
' Target PIC : PIC16F877A or similar type
' Hardware :
' Oscillator : 4MHz external
' Keywords : I2CREAD, I2CWRITE, LCDOUT
' Description : PICBASIC PRO program for an LCD clock program
' using the Dallas DS1307 I2C RTC.
'
' Define LOADER_USED to allow use of the boot loader.
' This will not affect normal program operation.
' Define LOADER_USED 1
' RESET_ORG can be set to move the BASIC program out of the way
' of any boot loader running from location 0, such as the
' Microchip USB boot loader
'Define RESET_ORG 800h
TRISD = %00000000
TRISC = %00000000
Define OSC 4 ' Core is running at 4MHz
' Define LCD pins
DEFINE LCD_DREG PORTD
DEFINE LCD_DBIT 4
DEFINE LCD_RSREG PORTD
DEFINE LCD_RSBIT 1
DEFINE LCD_EREG PORTD
DEFINE LCD_EBIT 0
DEFINE LCD_BITS 4
DEFINE LCD_LINES 2
' Alias pins
SDA Var PORTC.0
SCL Var PORTC.1
' Allocate variables
RTCYear Var Byte
RTCMonth Var Byte
RTCDate Var Byte
RTCDay Var Byte
RTCHour Var Byte
RTCMin Var Byte
RTCSec Var Byte
RTCCtrl Var Byte
ADCON1 = 15 ' PORTA and E digital
Low PORTE.2 ' LCD R/W low = write
Pause 100 ' Wait for LCD to startup
' Set initial time to 8:00:00AM 06/21/05
RTCYear = $05
RTCMonth = $06
RTCDate = $21
RTCDay = $02
RTCHour = $08
RTCMin = 0
RTCSec = 0
RTCCtrl = 0
Gosub settime ' Set the time
Goto mainloop ' Skip over subroutines
' Subroutine to write time to RTC
settime:
I2CWrite SDA, SCL, $D0, $00, [RTCSec, RTCMin, RTCHour, RTCDay, RTCDate, RTCMonth, RTCYear, RTCCtrl]
Return
' Subroutine to read time from RTC
gettime:
I2CRead SDA, SCL, $D0, $00, [RTCSec, RTCMin, RTCHour, RTCDay, RTCDate, RTCMonth, RTCYear, RTCCtrl]
Return
' Main program loop - in this case, it only updates the LCD with the time
mainloop:
Gosub gettime ' Read the time from the RTC
' Display time on LCD
Lcdout $fe, 1, hex2 RTCMonth, "/", hex2 RTCDate, "/" , hex2 RTCYear
lcdout $fe,$c0,hex2 RTCHour, ":", hex2 RTCMin, ":", hex2 RTCSec
Pause 500 ' Do it about 2 times a second
Goto mainloop ' Do it forever
End
Can anyone please pointing out what have I done wrong?