⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 2.txt

📁 基于pic的DS1302串行实时时钟程序
💻 TXT
📖 第 1 页 / 共 3 页
字号:

;       specs
;       baud rate       parity  data bits       stop bits               
;       2400            n       8               2
;       data
;       year    month   day     hour    minute second   index   break value
;       yyyy    mm      dd      hh      mm      ss      iiiiii  kkkkd       
;
;////   16f84   pin     In/Out  function       
;////   ra0     17      I       reed rla 4 closure for HOLD
;////   ra1     18      O       rtc     /rst    pin5
;////   ra2     1       O       rtc     clk     pin7
;////   ra3     2       I/O     rtc     dq data pin6
;////   ra4     3       O       serial out rs232 0-5vdc (or up to 14v)
;////
;////   rb0     6       I/O     interrupt of D.U. of mc14433 adc
;////   rb1     7       I       BCD data from mc14433   Q0      pin20
;////   rb2     8       I       BCD                     Q1 &p;    pin21
;////   rb3     9       I       BCD                     Q2 &p;    pin22
;////   rb4     10      I       BCD most sig.           Q3      pin23
;////   rb5     11      I       normally HI switches    advance time
;////   rb6     12      I               "       "       retard time
;////   rb7     13      spare
;////                                            
;//////////////////////////////////////////////////////////////////////////

	include <p16f84.inc>
eerd            equ     0               ;eeprom eecon1,0 read bit
eewr            equ     1               ;eeprom eecon1,1 write bit

;porta
rr4             equ     0               ;reed relay4 closure.   LO=closed
_rst            equ     1               ;rtc rst 
clk             equ     2               ;rtc clk
dataq           equ     3               ;rtc (I/O)
tx              equ     4               ;rs232 tx line
#define         REED4   porta,rr4       ;INPUT ex m/b reed relay4. close=LO
#define         CLKRST  porta,_rst      ;rtc rst 
#define         SCLK    porta,clk       ;rtc clk
#define         CLKDATA porta,dataq     ;rtc (I/O)
#define         SERTX   porta,tx        ;rs232 serial transmit line

;portb
eoc             equ     0               ;du = end of conv. int. and HOLD
bcd_q0          equ     1               ;bcd data. ls bit
bcd_q1          equ     2               ;bcd
bcd_q2          equ     3               ;bcd
bcd_q3          equ     4               ;bcd data. ms bit
up_switch       equ     5               ;normally HI switch for up time
down_switch     equ     6               ;"      "       " retard time
;               equ     7               ;spare
#define         EOC     portb,eoc
#define         BCD_Q0  portb,bcd_q0
#define         BCD_Q1  portb,bcd_q1
#define         BCD_Q2  portb,bcd_q2
#define         BCD_Q3  portb,bcd_q3
#define         UP_SWIT portb,up_switch
#define         DO_SWIT portb,down_switch
;#define                portb,

;general purpose bit definitions
;gpflags register
reed            equ     0               ;reed relay  `got pushed' flag
up              equ     1               ;up button `already pushed' flag
down            equ     2               ;down button `already pushed'

;general purpose register definitions
        cblock  0x0C
                m_porta                 ;mirror of port a
                m_portb                 ;mirror of port b
                gpflags                 ;general flags
                scratch                 ;scratch reg for *temporary* use
                scratch1                ;       "    &n;          

                count1                  ;counts 1,2,3 are for gen purpose
                count2                  ;delay routines
                count3                  ;
                up_count                ;count of 0.1 secs when UP switch closed
                down_count              ;count of 0.1 secs when DOWN switch closed

                timer1                  ;for 8bit rotates in/out
                timer2                  ;per Mark Sullivan's code 25.7.96
                timer3                  ;for the ds1202 or ds1302
                count4                  ;real-time-clock
                second                  ;
                minute                  ;
                hour                    ;
                date                    ;
                month                   ;
                day                     ;
                year                    ;
                
                ds1                     ;adc most sig. bcd byte-low nibble only
                ds2                     ; these are the bcd bytes as read in fro
                ds3                     ; the adc, and have been massaged into
                ds4                     ; (not packed) bcd

                tx_buff                 ;rs232 send routine
                pr01                    ;rs232 buffer table pointer offset
                
                samp_index1             ;sample index no. binary l.s.byte EEPROM
                samp_index2             ;binary eeprom
                samp_index3             ;binary most sig. index eeprom byte

                count5                  ;used in binary-to-bcd routine
                temp_f                  ;for outputting binary-held data to 
                buff_4                  ;bcd. bcd-to-ascii and rs232
                buff_3                  ;is then possible
                buff_2
                buff_1
                temp_a
                temp_b
                temp_c
                temp_d
                temp_e

        endc
;*********************************************end system definitions

;*********************************************system origin
                org     0x000
                goto    start
                org     0x004
                goto    service
;*********************************************end system origin

;************************************************tables first 
powerup_table1
                addwf   pcl,same
                retlw   0x59            ;Y      ASCII char
                retlw   0x65            ;e
                retlw   0x61            ;a
                retlw   0x72            ;r
                retlw   0x20            ;       sp
                retlw   0x4D            ;M
                retlw   0x6F            ;o
                retlw   0x20            ;       sp
                retlw   0x44            ;D
                retlw   0x61            ;a
                retlw   0x20            ;       sp
                retlw   0x20            ;       sp
                retlw   0x68            ;h
                retlw   0x72            ;r
                retlw   0x20            ;       sp
                retlw   0x6D            ;m
                retlw   0x69            ;i
                retlw   0x20            ;       sp
                retlw   0x73            ;s
                retlw   0x65            ;e
                retlw   0x20            ;       sp
                retlw   0x20            ;       sp
                retlw   0x69            ;i
                retlw   0x6E            ;n
                retlw   0x64            ;d
                retlw   0x65            ;e
                retlw   0x78            ;x
                retlw   0x20            ;       sp
                retlw   0x20            ;       sp
                retlw   0x20            ;       sp
                retlw   0x46            ;F
                retlw   0x6F            ;o
                retlw   0x72            ;r
                retlw   0x63            ;c
                retlw   0x65            ;e
                retlw   0x20            ;       sp
                retlw   0x20            ;       sp
                retlw   0x20            ;       sp
                retlw   0x20            ;       sp      
                retlw   0x20            ;       sp      40thar
                retlw   0x20            ;       sp      extrfor surety
                retlw   0x20            ;       sp
                retlw   0x20            ;       sp      extrfor surety
                retlw   0x20            ;       sp
;********************************************************end of tables

;*********************************************************initialisations
start
        ;clear all ram registers
                movlw   0x0C            ;start of ram
                movwf   fsr
                clrf    indf            ;$-3 to here
                incf    fsr,same
                btfss   fsr,7
                goto    $-3             ;back to clrf indf       &nbspbsp;                             
                bsf     status,rp0
        ;setup trisa, b
                movlw   00001001b
                movwf   trisa                       &nb   
                movlw   01111111b
                movwf   trisb
                bcf     status,rp0
                movlw   00000000b
                movwf   porta
                movwf   portb

;***************************************************end of initialisation

;*********************************************main loop begins
main_loop                       ;initial stuff
                call    delay_100ms
                call    rclock          ;fetch the current contents of rtc itself
                bcf     second,7        ;clear `clock halt' CH bit
                bcf     hour,7          ;am/pm:24 -> 24 hr mode
                call    wclock
                call    delay_1sec
                call    rclock
                movlw   0x12            ;ascii byte `DC2' resets compressed printing
                call    txbyte
                call    powerup_dump

main_loop1
        ;*********************** check the time advance &  retard switches
                call    rclock
                btfsc   UP_SWIT         ;switch is in, if result=LO
                goto    time_retard0    ;switch open, so drop thru & check DOWN
                btfsc   UP_SWIT
                goto    time_adv1
                call    inc_minute_bcd
                call    wclock
                call    crush_dump
                call    delay_1sec
time_adv1       btfsc   UP_SWIT
                goto    time_adv2
                call    inc_hour_bcd
                call    wclock
                call    crush_dump
                call    delay_1sec
time_adv2       btfsc   UP_SWIT
                goto    time_adv3
                call    inc_date_bcd
                call    wclock
                call    crush_dump
                call    delay_1sec
time_adv3       btfsc   UP_SWIT
                goto    time_adv4
                call    inc_month_bcd
                call    wclock
                call    crush_dump
                call    delay_1sec
time_adv4       btfsc   UP_SWIT
                goto    main_loop3
                call    inc_year_bcd
                call    wclock
                call    crush_dump
                call    delay_1sec
                goto    main_loop3

time_retard0    
                btfsc   DO_SWIT
                goto    main_loop3              ;jump out, no switch is in
                call    dec_minute_bcd
                call    wclock
                call    crush_dump
                call    delay_1sec
time_retard1    btfsc   DO_SWIT
                goto    time_retard2
                call    dec_hour_bcd
                call    wclock
                call    crush_dump
                call    delay_1sec
time_retard2    btfsc   DO_SWIT
                goto    time_retard3
                call    dec_date_bcd
                call    wclock
                call    crush_dump
                call    delay_1sec
time_retard3    btfsc   DO_SWIT
                goto    time_retard4
                call    dec_month_bcd
                call    wclock
                call    crush_dump
                call    delay_1sec
time_retard4    btfsc   DO_SWIT
                goto    main_loop3
                call    dec_year_bcd
                call    wclock
                call    crush_dump
                call    delay_1sec
        ;*********************** finish time advance &  retard
main_loop3      
                nop
                nop
                btfsc   REED4           ;test reed relay closure. LO=closed
                goto    main_loop4      ;open ie no TRIP

                btfss   gpflags,reed
                goto    main_loop6
                goto    main_loop1      
main_loop4      bcf     gpflags,reed
                bsf     status,rp0      ;set to bank1
                bsf     trisb,eoc       ;eoc becomes input
                bcf     status,rp0      ;set to bank0
                goto    main_loop1                     &nbspbsp;       
main_loop6      bsf     gpflags,reed
                call    inc_eeprom      ;increment eeprom regs+1
                call    read_adc        ;capture current force reading
                call    crush_dump      ;print one data dump only
                goto    main_loop1

;*********************************************main loop ends

;************************************** specific RTC ds1302 subroutines begin

;
; Write calendar clock
; per Mark K Sullivan 25.7.96
;       this routine takes ALL the seconds ---> year fregs in 16f84 and
;       writes them to the ds1302. NB that means that if you want to adjust
;       the rtc time registers, you have to do a read first (of them all).
;       The rclock routine following, reads the whole lot in `burst mode'
;       and puts the values into seconds --->year fregs

wclock  bcf     SCLK            ;negate clock line
        nop
        nop
        nop
        nop
        bcf     CLKDATA
        bsf     CLKRST          ;negate RST
        bsf     status,rp0      ;I/O dataq line set to output
        bcf     trisa,dataq
        bcf     status,rp0
        movlw   0x8E            ;write control register
        call    wbyte
        movlw   0x00            ;turn off Write Protect in rtc
        call    wbyte
        movlw   7
        movwf   timer3
        movlw   0x80            ;rtc register address for `second'
        movwf   count4
        movlw   second
        movwf   fsr
wc      bcf     CLKRST          ;assert RST
        bcf     SCLK            ;negate clock
        nop
        nop
        nop
        nop
        bsf     CLKRST          ;negate RST
        movf    count4,W

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -