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
Bookmarks