金沙js5线路(中国)有限公司

技术热线: 4007-888-234

PIC单片机数字钟程序

更新时间: 2019-03-22
阅读量:2144

十年单片机开发方案公司深圳金沙js5线路分享PIC单片机数字钟程序,希望对大家有所帮助!!!
;*******************************************
;数字钟程序
;*******************************************
        LIST        P=16F877
        INCLUDE         BCDMIAO        EQU        70H;存放秒的二进制转化为BCD后的BCD吗
BCDFEN        EQU        71H;存分的
BCDSHI        EQU        72H;存十的
GMIAO        EQU        73H;秒个位寄存器
SMIAO        EQU        74H;十位
GFEN        EQU 75H;分个位寄存器
SFEN        EQU 76H;十位
GSHI        EQU 77H;时个位寄存器
SSHI        EQU 78H;十位
COUNT        EQU        79H;计数器
DY1                EQU 7AH;延时计数器1
DY2                EQU 7BH;延时计数器2
WTEMP        EQU        7CH;W备份寄存器
STEMP        EQU 7DH;STATUS备份寄存器
TEMP        EQU        7EH;定时器0初值寄存器
RBTEMP        EQU        7FH;RB口寄存器
DY5                EQU 20H;延时计数器5
DY6                EQU        21H;6
PCLATH_TEMP        EQU        22H;PCLATH备份寄存器
DISPBUF EQU 24H          ;显示值暂存

DISPBUF_H EQU 25H         ;显示器高位
DISPBUF_L EQU 26H         ;显示器低位
BUFFER    EQU 27H;移位寄存器(之所以这样称呼它,是应为BUFFER1中得数左移到它中)
BUFFER1   EQU 28H;存放个/分/时的数据(可对它进行移位)
SUM       EQU 29H;判断寄存器(判断高低四位加三后是否大于7)
DIGHT    EQU 2AH ;存放个/分/时的数据(中转寄存器)
CMIAO        EQU        30H;存秒的数(二进制)
CFEN        EQU 31H;分的
CSHI        EQU 32H;时的
COUNTER        EQU 33H 
;主程序
                ORG        0000H
                GOTO MAIN
                ORG        0004H
                GOTO SERVE
                ORG        0005H
MAIN        NOP;
                BCF        STATUS,RP0;
                MOVLW        00H;P口清0
                MOVWF        PORTC;
                BSF        STATUS,RP0;
                MOVLW        00H        ;P口输出
                MOVWF        TRISC;
                MOVLW        0FFH;
                MOVWF        TRISB;
                MOVLW        47H;INT上升沿触发,分频器为TMR0,1:256
                MOVWF        OPTION_REG;
                BCF                STATUS,RP0;

     MOVLW        0F8H;开中断
                MOVWF        INTCON;
                BCF                INTCON,T0IF;清标志位
                BCF                INTCON,INTF;
                BCF                INTCON,RBIF;
                CLRF        CMIAO;清存寄存器
                CLRF        CFEN;
                CLRF        CSHI;
                MOVLW        D'20';设置定时期的中断次数
                MOVWF        COUNT;
                MOVLW        D'60';定时期初值
                MOVWF        TEMP;
                MOVWF        TMR0;开定时期
;循环扫描程序
SHAOMIAO

BCF                STATUS,RP0;
                MOVF        CMIAO,0;
                MOVWF        DIGHT;秒位数据送出转换为BCD码
                CALL        BCD2;
                MOVWF        BCDMIAO;带会BCD码                           
                MOVWF        GMIAO;给秒的个位积存器
                MOVLW        0FH;屏蔽高四位
                ANDWF        GMIAO;
                MOVLW        50H;位选码
                IORWF        GMIAO;BCD码+位选
                MOVF        GMIAO,0;
                MOVWF        PORTC;送显
                CALL        YANSHI;
               
                MOVF        BCDMIAO,0;同上
                MOVWF        SMIAO;
                MOVLW        0F0H;
                ANDWF        SMIAO,1;
                SWAPF        SMIAO,1;注意此处要反转
                MOVLW        40H;
                IORWF        SMIAO;

MOVF        SMIAO,0;
                MOVWF        PORTC;
                CALL        YANSHI;
               
                MOVF        CFEN,0;同上
                MOVWF        DIGHT;
                CALL        BCD2;
                MOVWF        BCDFEN;
                MOVWF        GFEN;
                MOVLW        0FH;
                ANDWF        GFEN;
                MOVLW        30H;
                IORWF        GFEN;
                MOVF        GFEN,0;
                MOVWF        PORTC;
                CALL        YANSHI;
               
                MOVF        BCDFEN,0;
                MOVWF        SFEN;
                MOVLW        0F0H;
                ANDWF        SFEN;
                SWAPF        SFEN;
                MOVLW        20H;

IORWF        SFEN;
                MOVF        SFEN,0;
                MOVWF        PORTC;
                CALL        YANSHI;
               
                MOVF        CSHI,0;
                MOVWF        DIGHT;
                CALL        BCD2;
                MOVWF        BCDSHI;
                MOVWF        GSHI;
                MOVLW        0FH;
                ANDWF        GSHI;
                MOVLW        10H;
                IORWF        GSHI;
                MOVF        GSHI,0;
                MOVWF        PORTC;
                CALL        YANSHI;
               
                MOVF        BCDSHI,0;
                MOVWF        SSHI;
                MOVLW        0F0H;
                ANDWF        SSHI;
                SWAPF        SSHI;
                MOVLW        00H;

IORWF        SSHI;
                MOVF        SSHI,0;
                MOVWF        PORTC;
                CALL        YANSHI;
                GOTO        SHAOMIAO;循环扫描
;查中断源
SERVE   MOVWF        WTEMP;
                SWAPF        STATUS,W;
                CLRF        STATUS;
                MOVWF        STEMP;保护现场 
                MOVF        PCLATH,W;
                MOVWF        PCLATH_TEMP;
                CLRF        PCLATH
            BTFSS        INTCON,INTF;是INT中断吗?
                GOTO        SERVE1;不是,转SERVE1
                GOTO        INTT;是,转INT中断处理
SERVE1        BTFSS        INTCON,T0IF;是定时期中断吗?
                GOTO        SERVE2;不是,转SERVE2
                GOTO        T0II;是,转定时期中断处理
SERVE2        BTFSS        INTCON,RBIF;是RB中断吗?

  GOTO        SERVE3;不是,返回
                GOTO        PDRB;是,转RB中断处理
SERVE3        RETFIE
               
               
               
;定时器中断处理               
T0II  
               
        BCF                INTCON,T0IF;清中断标志位
        BCF                INTCON,T0IE;关定时期中断
                MOVF        TEMP,0;重新值数
                MOVWF        TMR0;
                BCF                INTCON,GIE;防止/原因是:产生中断时,GIE位清0,并转入中断入口,当GIE清0后,如果这时正好CPU在执行
                BSF                INTCON,T0IE;产生/一条对INTCON“读,修改,写”的指令,则GIE位还会被写会操作重新值1,这样会使CPU
                BSF                INTCON,GIE;两次中断/产生两次中断,解决的办法就是,在对INTCON进行修改之前,事先值GIE=0,修改完后
                DECFSZ        COUNT,1;/再恢复GIE=1。
                GOTO        LOOP;返回

 MOVLW        D'20';
                MOVWF        COUNT;赋记数次数
                INCF        CMIAO,1;
                MOVLW        D'60';
                XORWF        CMIAO,W;
                BTFSS        STATUS,Z;
                GOTO        LOOP;
                CLRF        CMIAO;60秒到清0
                INCF        CFEN;
                MOVLW        D'60';
                XORWF        CFEN,W;
                BTFSS        STATUS,Z;
                GOTO        LOOP;
                CLRF        CFEN;60分到清0
                INCF        CSHI;
                MOVLW        D'24';
                XORWF        CSHI,W;
                BTFSS        STATUS,Z;
                GOTO        LOOP;
                CLRF        CSHI;24时到清0
                RETFIE;
               
;判断是那一个产生的中断

PDRB        MOVF        PORTB,0;
                MOVWF        RBTEMP        ;
                BTFSC        RBTEMP,5;是0吗?是转向RBII
                GOTO        RBIII;不是,转向RBIII
;RB5口中断处理   调秒               
               
RBII 
                CALL        DELAY;去抖
                BTFSC        PORTB,5;是下降沿吗?
                GOTO        LOOP8;不是,转(清除上升沿产生的虚假中断)
                MOVF        PORTB,1;读PORTB,终结失陪条件
                BCF                INTCON,RBIF;清标志位
;                BCF                INTCON,T0IF
                BCF                INTCON,T0IE;关定时期中断
                MOVF        TEMP,0;
                MOVWF        TMR0;重新赋值
                BSF                INTCON,T0IE;开定时期中断
                INCF        CMIAO,1;
                MOVLW        D'60';
                XORWF        CMIAO,W;判断到60秒了吗?

  BTFSS        STATUS,Z;
                GOTO        LOOP;返回
                CLRF        CMIAO;
                INCF        CFEN,1;
                RETFIE
LOOP8               
         MOVF        PORTB,1;清除RBIF前要读口
             BCF        INTCON,RBIF;               
                RETFIE
;RB4口中断处理 调分
RBIII       
                CALL        DELAY;去抖
                BTFSC        PORTB,4;
                GOTO        LOOP8;
                MOVF        PORTB,1;
                BCF                INTCON,RBIF;
                BCF                INTCON,T0IE;
;                BCF                INTCON,T0IF
                MOVF        TEMP,W;
                MOVWF        TMR0;
                BSF                INTCON,T0IE;
                INCF        CFEN,1;
                MOVLW        D'60';
                XORWF        CFEN,W;

  BTFSS        STATUS,Z;
                GOTO        LOOP;
                CLRF        CFEN;
                INCF        CSHI;
                RETFIE               
               
;外部中断处理 调时
INTT               
                BCF                INTCON,INTF;
                CALL        DELAY;
                INCF        CSHI,1;
                MOVLW        D'24';
                XORWF        CSHI,W;
                BTFSS        STATUS,Z;
                GOTO        LOOP;
                CLRF        CSHI;
                RETFIE
;恢复现场               
LOOP        MOVF        PCLATH_TEMP,W;
                MOVWF        PCLATH;
                SWAPF        STEMP,0;
                MOVWF        STATUS;
                SWAPF        WTEMP;
                SWAPF        WTEMP,0;
                RETFIE               
;去抖程序,延时8MS(5-10MS)             

DELAY        MOVLW        D'51';
                MOVWF        DY1;
LOOP4        MOVLW        D'51';
                MOVWF        DY2;
LOOP5        DECFSZ        DY2,1;
                GOTO        LOOP5;
                DECFSZ        DY1,1;
                GOTO        LOOP4;
                RETURN
                               
;延时程序,延时约3MS,(10-20MS循环一次,六个数码官需要18MS,在次范围内)
YANSHI        MOVLW        D'21';
                MOVWF        DY5;
LOOP7        MOVLW        D'51';
                MOVWF        DY6;
LOOP6        DECFSZ        DY6,1;
                GOTO        LOOP6;

DECFSZ        DY5,1;
                GOTO        LOOP7;
                RETURN
BCD2        MOVLW            8;
                  MOVWF            COUNTER;              ;循环左移计数器置8
                  CLRF                   BUFFER  ;        ;清除缓冲器
                  MOVF                   DIGHT,W  ;
                  MOVWF            BUFFER1   ;     
LOOP2                 
        RLF                BUFFER1,1    ;     ;二进制数转换成BCD码(以便显示)
                  RLF                      BUFFER,1;
                  DECFSZ           COUNTER,1;          ;左移8次
                  GOTO                   ADJUST    ;         ;
                  MOVF                   BUFFER,W;
                  MOVWF            DISPBUF ;
                  MOVF                DISPBUF,W;
                  RETURN        ;是否有进位(即已达100),如已计数到100则
;                  SKPC                            ;从0开始再计数显示(显示00~99)
ADJUST                 

 MOVLW            3    ;           ;二进制转BCD的调整
                  ADDWF            BUFFER,W;            ;每次移位后都检查低四位LSD+3是否大于7
                  MOVWF            SUM      ;      
                  BTFSC            SUM,3;
                  MOVWF            BUFFER;
                  MOVLW            30H;
                  ADDWF            BUFFER,W ;       ;如果是则再加3,否则不加。
                  MOVWF            SUM;
                  BTFSC            SUM,7;
                  MOVWF            BUFFER;          ;接着再将高四位MSD作相同处理
                  GOTO                   LOOP2;
                  END 

(文源网络,侵删)

XML 地图