$bf is the register address for clock burst mode read I believe.
Do a search for $bf in the ds1302 data sheet.
steve
$bf is the register address for clock burst mode read I believe.
Do a search for $bf in the ds1302 data sheet.
steve
Yes had just checked data sheet just after I posted !
$bf is for burst mode :-)
Thanks to all who replied .. this is making a whole lot more sense now :-)
Kind regards
Dennis
@ Steve ...
Do you have a snippet for the write in burst mode so one can set the clock ?
Kind regards
Dennis
Sure, this should do it:
steveCode:settime: ' Subroutine to write time to RTC RST = 1 ' Ready for transfer Shiftout IO, SCLK, LSBFIRST, [$8e, 0] ' Enable write RST = 0 ' Reset RTC RST = 1 ' Ready for transfer ' Write all 8 RTC registers in burst mode Shiftout IO, SCLK, LSBFIRST, [$be, rtcsec, rtcmin, rtchr, rtcdate, rtcmonth, rtcday, rtcyear, 0] rst = 0 Return
@Steve
My code before you replied ...
As you can see I was totally off the mark with the $bf , $be setting !Code:setclk: high rst ' Ready for transfer RTC pin must go high Shiftout ds_data, ds_clk, LSBFIRST, [$bf] ' send burst mode control $bf to read all 8 RTC registers in burst mode Shiftout ds_data, ds_clk, LSBFIRST, [rtcsec, rtcmin, rtchr, rtcdate, rtcmonth, rtcday, rtcyear, rtccontrol] send the data low rst ' Reset RTC reset pin
Ok but I have read in a lot of posts regarding the DS1302 and BCD conversion ..does this still apply ?
Kind regards
Dennis
Last edited by Dennis; - 22nd February 2010 at 21:03.
Hi all
I have been battling for over a week now trying to get the DS1302 clock chip working.
I think I finally have the format and code sorted , only problem is that my clock never ticks :-( in other words the time never changes!
Please could somebody either take a look at my code or advise me what I should check next.
I'm sure it is really something small or silly that I have overlooked.
Kind regards
Dennis
Code:'device 18F4520 'DS1302 test ' ' 'Ocsillator selections here OSCCON = $70 'Int CLK 8MHz OSCTUNE.6 = 1 'PLL 4x ADCON1= %00001111 '$0F = disable A/D converter cmcon = 7 INTCON2.7 = 0 'switch pull-ups ON 'END of oscillator selections 'timer/oscillator defines DEFINE OSC 32 '4x 8MHz 'END of timer/oscillator defines ' ' 'Port IO directions and presets for port pins begin here 'TRISX = %76543210 << tris bit order numbering 'TRISA = %11111111 'All pins are outputs '// Define port pins as inputs and outputs ... TRISA = %00000000 'example only - TRISB = %00001111 ;Make B4-B7 outputs, B0-B3 inputs, TRISB = %11111111 'for 4x4 keypad all input TRISC = %10000000 TRISD = %00000000 TRISE.0 = 0 TRISE.1 = 0 TRISE.2 = 0 'End of Port IO directions and presets for port pins begin here ' ' ' 'LCD defines begin here DEFINE LCD_BITS 4 'defines the number of data interface lines (4 or 8) DEFINE LCD_DREG PORTD 'defines the port where data lines are connected to DEFINE LCD_DBIT 4 'defines the position of data lines for 4-bit interface (0 or 4) DEFINE LCD_RSREG PORTD 'defines the port where RS line is connected to DEFINE LCD_RSBIT 2 'defines the pin where RS line is connected to DEFINE LCD_EREG PORTD 'defines the port where E line is connected to DEFINE LCD_EBIT 3 'defines the pin where E line is connected 'DEFINE LCD_RWREG 0 'defines the port where R/W line is connected to (set to 0 if not used) 'DEFINE LCD_RWBIT 0 'defines the pin where R/W line is connected to (set to 0 if not used) DEFINE LCD_COMMANDUS 2000 'defines the delay after LCDOUT statement DEFINE LCD_DATAUS 200 'delay in micro seconds 'END of LCD DEFINES ' 'includes begin here INCLUDE "modedefs.bas" 'end of includes ' 'HSER defines DEFINE HSER_RCSTA 90h ' Enable serial port & continuous receive DEFINE HSER_TXSTA 20h ' Enable transmit, BRGH = 0 DEFINE HSER_SPBRG 207 ' 2400 Baud @ 32MHz, 0.17% DEFINE HSER_CLROERR 1 ' Clear overflow automatically 'HSER defines begin here '-------------------------- Clock Variables --------------------------------- 'SCLK var portC.3 ' DS1302 Clock Pin 7 'IO var portC.4 ' DS1302 Data Pin 6 'rst var portD.1 ' DS1302 Reset Pin 5 'pins RST var PORTA.0 'DS1302 PIN 5 IO var PORTA.1 'DS1302 PIN 6 SCLK var PORTA.2 'DS1302 PIN 7 'variables rtcyear var byte rtcday var byte rtcmonth var byte rtcdate var byte rtchr var byte rtcmin var byte rtcsec var byte rtccontrol var byte Main: Low RST ' Reset DS1302 RTC Low SCLK 'pull SCLK line LOW ' clock Set 16:45:00 04/14/08 rtcyear = $08 rtcday = $06 rtcmonth = $04 rtcdate = $14 rtchr = $16 rtcmin = $45 rtcsec = $00 Gosub SetTime 'fetch the time Goto loopy 'goto loopy loopy: Gosub gettime ' go fetch the time Lcdout $fe, 1 'display date on top row on 16x2 lcd lcdout hex2 rtcmonth, "/", hex2 rtcdate, "/" , hex2 rtcyear lcdout $fe,$c0 'display time output on bottom line 16x2 lcd lcdout hex2 rtchr, ":", hex2 rtcmin, ":", hex2 rtcsec Pause 200 ' pause 0.2 Goto loopy 'return to main loop SetTime: 'this module sets the time using shiftout RST = 1 ' bring the DS1302 RST line high and now ready for transfer Shiftout IO, SCLK, LSBFIRST, [$8e, 0] ' Enable write RST = 0 ' Reset DS1302 RTC RST = 1 ' bring reset line high and now ready for transfer Shiftout IO, SCLK, LSBFIRST, [$be, rtcsec, rtcmin, rtchr, rtcdate, rtcmonth, rtcday, rtcyear, 0] ' Write all 8 RTC registers in burst mode RST = 0 ' reset DS1302 RTC Return 'return control to calling program GetTime: ' Subroutine to read time from RTC RST = 1 ' reset line high now ready for transfer Shiftout IO, SCLK, LSBFIRST, [$bf] ' Read all 8 DS1302 RTC registers in burst mode using shiftout/shiftin Shiftin IO, SCLK, LSBPRE, [rtcsec, rtcmin, rtchr, rtcdate, rtcmonth, rtcday, rtcyear, rtccontrol] RST = 0 ' Reset RTC Return 'return control to calling module End
Last edited by Dennis; - 24th February 2010 at 21:32.
Hmmm. I don't see any code to initialize the RTC. Add something like this to your code, so it runs once before you enter the main loop....
You'll have to look in the data sheet to set your preferred trickle charge rates and such if you are going to use the charger.
Code:'initialize the RTC and set the trickle charge rate Low SCLK low rst ' Reset RTC high rst ' RTC Ready for transfer Shiftout IO, SCLK, LSBFIRST, [$8e, 0] ' Enable write low rst high rst Shiftout IO, SCLK, LSBFIRST, [$90, %10101011] ' Set charger on and to "2 diodes, 8Kohm" Low RST ' Reset RTC
steve
If you use a push button to increment the minutes (or hours, etc) you will run into a problem because of the BCD.
This subroutine increments the minutes by 1 every time it's called.
It does the BCD to decimal conversion, increments the minutes, then converts the new number back to BCD before setting the time.
Code:increminute: 'subroutine to do BCD to decimal conversion, increment and convert back to BCD mathtemp =(rtcmin>>4)*10+(rtcmin & $0F) 'convert BCD minutes into Decimal minutes mathtemp=mathtemp+1 'increment by 1 If mathtemp>59 then 'If minutes exceeds 59 mathtemp=0 'reset to 0 endif rtcmin=((mathtemp DIG 1)<<4)+mathtemp DIG 0 'convert Decimal back to BCD rstcount = 0 gosub settime return
steve
@Steve
Thanks for the trickle charger tip :-) now that table makes sense too :-) YAY
I added the code below (originally exactly as you gave it to me) see the updates, I haven't added all the possible options but I will so any line could be un-commented for any selection of the desired charger setup and will post the updates tomorrow sometime.
Sadly the clock is still not working. I still just get a static display.Code:init: 'initialize the RTC and set the trickle charge rate Low SCLK low rst ' Reset RTC high rst ' RTC Ready for transfer Shiftout IO, SCLK, LSBFIRST, [$8e, 0] ' Enable write low rst high rst 'Shiftout IO, SCLK, LSBFIRST, [$90, %10101011] ' Set charger on and to "2 diodes, 8Kohm" 'Shiftout IO, SCLK, LSBFIRST, [$90, %01011100] ' inititial power state Shiftout IO, SCLK, LSBFIRST, [$90, %00000000] ' disabled Low RST ' Reset RTC return
I have no buttons assigned to the clock as yet,so I didn't add any increment sub-routine..should I ?
I was hoping to just preset the time and it would start ticking away.
If I did add an increment sub-routine , would I need to add one for secs,mins,hours and day month and year ? Or just for mins and hours?
Kind regards
Dennis
Are there any other things I could try ?
Kind regards
Dennis
Last edited by Dennis; - 25th February 2010 at 00:45.
Bookmarks