Last active
January 18, 2019 19:25
-
-
Save marioIncandeza/dd45dc98a1b190534cdc to your computer and use it in GitHub Desktop.
Code for a stopwatch for a PIC16F887 microcontroller
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;****************************************************** | |
;PIC Configuration for PIC16F887 | |
#include "p16F887.inc" | |
; CONFIG1 | |
; __config 0x2032 | |
__CONFIG _CONFIG1, _FOSC_HS & _WDTE_OFF & _PWRTE_OFF & _MCLRE_ON & _CP_ON & _CPD_ON & _BOREN_OFF & _IESO_OFF & _FCMEN_OFF & _LVP_OFF | |
; CONFIG2 | |
; __config 0x3FFF | |
__CONFIG _CONFIG2, _BOR4V_BOR40V & _WRT_OFF | |
;****************************************************** | |
DCOUNT EQU 20H | |
CNT_100th EQU 21H ; counter for 0.01s | |
CNT_10th EQU 22H ; counter for 0.1s | |
CNT_sec EQU 23H ; counter for seconds | |
CNT_10sec EQU 24H ; counter for tens of seconds | |
CNT_min EQU 25H ; counter for minutes | |
CNT_10min EQU 26H ; counter for tens of minutes | |
RUN EQU 70H ; put in the shared region | |
WCOPY EQU 71H ; put in the shared region | |
STATCOPY EQU 72H ; put in the shared region | |
OCOUNT EQU 28H ; counter for wait, outerloop | |
ICOUNT EQU 29H ; counter for wait, innerloop | |
ORG 000H | |
GOTO MAIN | |
ORG 004H | |
MOVWF WCOPY ;context save | |
SWAPF STATUS,W | |
MOVWF STATCOPY | |
CLRF INTCON | |
COMF RUN,1 | |
BANKSEL PORTB | |
BCF PORTB,RB0 | |
BCF INTCON,INTF | |
BSF INTCON,INTE | |
SWAPF STATCOPY,W ;context restore | |
MOVWF STATUS | |
SWAPF WCOPY,F | |
SWAPF WCOPY,W | |
RETFIE | |
;******************************************************* | |
MAIN | |
BANKSEL CNT_100th | |
CLRF CNT_100th | |
CLRF CNT_10th | |
CLRF CNT_sec | |
CLRF CNT_10sec | |
CLRF CNT_min | |
CLRF CNT_10min | |
CLRF RUN | |
BANKSEL ANSEL | |
CLRF ANSEL ;Using all digital mode, turns off analog | |
CLRF ANSELH ;Make all analog ports digital | |
BANKSEL TRISA ;change to correct bank to configure TRIS registers | |
;MAKE RA0 OUTPUT, RB0 INPUT, PORTC AND PORTD ALL OUTPUTS | |
CLRF TRISA | |
CLRF TRISB | |
COMF TRISB,1 | |
CLRF TRISD | |
BANKSEL CCP1CON ;clear other banks associated with the operation of PORTB | |
CLRF CCP1CON | |
BANKSEL IOCB | |
CLRF IOCB | |
BANKSEL WPUB | |
CLRF WPUB | |
CLRF INTCON | |
BSF INTCON,7 | |
BSF INTCON,4 | |
BANKSEL OPTION_REG | |
CLRF OPTION_REG | |
BSF OPTION_REG,6 | |
LOOP | |
BANKSEL CNT_100th | |
MOVF CNT_100th,0 ; Put count of 1/100ths of second in W | |
CALL TABLE ; Get value to write out to PORTD from table | |
MOVWF PORTD ; Put value out to PORTD | |
BCF PORTA,5 ; Turn on and off display to show digit | |
CALL DELAY | |
BSF PORTA,5 | |
MOVF CNT_10th,0 ; Put count of 1/10ths of second in W | |
CALL TABLE ; Get value to write out to PORTD from table | |
MOVWF PORTD ; Put value out to PORTD | |
BCF PORTA,4 ; Turn on and off display to show digit | |
CALL DELAY | |
BSF PORTA,4 | |
MOVF CNT_sec,0 ; Put count of 1s seconds in W | |
CALL TABLE ; Get value to write out to PORTD from table | |
MOVWF PORTD ; Put value out to PORTD | |
BCF PORTA,3 ; Turn on and off display to show digit | |
CALL DELAY | |
BSF PORTA,3 | |
MOVF CNT_10sec,0 ; Put count of 10s of seconds in W | |
CALL TABLE ; Get value to write out to PORTD from table | |
MOVWF PORTD ; Put value out to PORTD | |
BCF PORTA,2 ; Turn on and off display to show digit | |
CALL DELAY | |
BSF PORTA,2 | |
MOVF CNT_min,0 ; Put count of 1s of minutes in W | |
CALL TABLE ; Get value to write out to PORTD from table | |
MOVWF PORTD ; Put value out to PORTD | |
BCF PORTA,1 ; Turn on and off display to show digit | |
CALL DELAY | |
BSF PORTA,1 | |
MOVF CNT_10min,0 ; Put count of 10s of minutes in W | |
CALL TABLE ; Get value to write out to PORTD from table | |
MOVWF PORTD ; Put value out to PORTD | |
BCF PORTA,0 ; Turn on and off display to show digit | |
CALL DELAY | |
BSF PORTA,0 | |
CALL WAIT | |
BTFSC RUN,0 | |
CALL INCCNT | |
GOTO LOOP | |
WAIT ;adjustable delay to make timer accurate | |
MOVLW 07H | |
MOVWF OCOUNT | |
MOVLW 0D0H | |
MOVWF ICOUNT | |
OLOOP | |
ILOOP | |
DECFSZ ICOUNT,1 | |
GOTO ILOOP | |
DECFSZ OCOUNT,1 | |
GOTO OLOOP | |
RETURN | |
INCCNT | |
INCF CNT_100th,1 ; increment 0.01s counter storing back in counter | |
MOVLW 0X0A ; check to see if equal to 10 | |
XORWF CNT_100th,0 | |
BTFSC STATUS,Z | |
GOTO OVER_100th ; if it is 10, goto OVER100th | |
RETURN ; if no overflow, return | |
OVER_100th INCF CNT_10th,1 ;there was overflow from previous digit, increment next dig | |
CLRF CNT_100th ;clear previous digit | |
MOVLW 0x0A ;check if this results in overflow | |
XORWF CNT_10th,0 | |
BTFSC STATUS,Z | |
GOTO OVER_10th ;there was another overflow | |
RETURN ;no overflow, go back | |
OVER_10th INCF CNT_sec,1 ;this process is repeated | |
CLRF CNT_10th | |
MOVLW 0x0A | |
XORWF CNT_sec,0 | |
BTFSC STATUS,Z | |
GOTO OVER_sec | |
RETURN | |
OVER_sec INCF CNT_10sec,1 | |
CLRF CNT_sec | |
MOVLW 0x06 | |
XORWF CNT_10sec,0 | |
BTFSC STATUS,Z | |
GOTO OVER_10sec | |
RETURN | |
OVER_10sec INCF CNT_min,1 | |
CLRF CNT_10sec | |
MOVLW 0x0A | |
XORWF CNT_min,0 | |
BTFSC STATUS,Z | |
GOTO OVER_min | |
RETURN | |
OVER_min INCF CNT_10min,1 | |
CLRF CNT_min | |
MOVLW 0x06 | |
XORWF CNT_10min,0 | |
BTFSC STATUS,Z | |
GOTO OVER_10min | |
RETURN | |
OVER_10min CLRF CNT_100th | |
CLRF CNT_10th | |
CLRF CNT_sec | |
CLRF CNT_10sec | |
CLRF CNT_min | |
CLRF CNT_10min | |
RETURN | |
DELAY ;delay for flashing the segment on/off | |
MOVLW 0xFF | |
MOVWF DCOUNT | |
DLOOP DECFSZ DCOUNT | |
GOTO DLOOP | |
RETURN | |
TABLE | |
ADDWF PCL,1 ;instruction register add excursion address | |
RETLW 0C0H ;the code for 0( the common LED is anode) | |
RETLW 0F9H ;the code for 1 | |
RETLW 0A4H ;the code for 2 | |
RETLW 0B0H ;the code for 3 | |
RETLW 99H ;the code for 4 | |
RETLW 92H ;the code for 5 | |
RETLW 82H ;the code for 6 | |
RETLW 0F8H ;the code for 7 | |
RETLW 80H ;the code for 8 | |
RETLW 90H ;the code for 9 | |
END | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment