pic16f877 stopwatch program

Publisher:dswecdLatest update time:2016-11-04 Source: eefocusKeywords:pic16f877 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
The source program is as follows:

LIST P=16F877

      INCLUDE "P16F877.INC"

; Clock display range: 00.00 seconds - 99.99 seconds, resolution: 0.01 seconds; The registers used are as follows:

S0 EQU 0X20 ; 0.01 second timer

S1 EQU S0+1; 0.1 second timer

S2 EQU S0+2; 1 second timer

S3 EQU S0+3; 10 second timer

SREG EQU S0+4 ;soft counter

LEDF EQU S0+5 ; Displayed LED position indication register

XW_TEMP EQU S0+6; used to protect the value of W during interruption

XHOSTS EQU S0+7; used to protect the STATUS value in the interrupt

DEYH EQU S0+8

DEYL EQU S0+9; The above two registers are used for software delay

HOSTF EQU S0+0A; used to protect the value of FSR during interruption

   ORG 0X00

   GOTO MAIN

   ORG 0X04

   GOTO CLKINT ; Go to interrupt service routine

   ORG 0X10

CONVERT; Common anode code table without decimal point  

    ADDWF PCL,1                         

    RETLW 0XC0

        RETLW 0XF9

        RETLW 0XA4

        RETLW 0XB0

        RETLW 0X99

        RETLW 0X92

        RETLW 0X82

        RETLW 0XD8

        RETLW 0X80

        RETLW 0X90

        RETURN

                      

CONVERT2 ; Common anode code table with decimal point

    ADDWF PCL,1        

        RETLW 0X40

        RETLW 0X79

        RETLW 0X24

        RETLW 0X30

        RETLW 0X19

        RETLW 0X12

        RETLW 0X02

        RETLW 0X78

        RETLW 0X00

        RETLW 0X10

        RETURN

                       

TRANSMIT; SPI sends display submodule

   CLRF PORTA; LACK sends a low level to prepare for latching

   MOVWF SSPBUF ; Start sending  

WAIT BANKSEL PIR1

   BTFSS PIR1, SSPIF

   GOTO WAIT ; Wait for sending to complete

   BCF PIR1, SSPIF; Clear interrupt flag

   NOP

   RETURN

; **************Display subroutine module************

DISPLAY MOVLW 0X01

   MOVWF LEDF

   MOVLW 0XA0

   MOVWF FSR

AGAINXIAN MOVF LEDF,W

   SUBLW 0X02

   BTFSS STATUS,Z

   GOTO XIANB

   MOVF INDF,W

   CALL CONVERT2; if it is the second digit, check the code table with decimal point

   GOTO TRAN

XIANB MOVF INDF, W

   CALL CONVERT

TRAN CALL TRANSMIT ;Send a display data

   INCF FSR

   INCF LEDF

   MOVF LEDF,W

   SUBLW 0X05

   BTFSS STATUS,Z

   GOTO AGAINXIAN

   MOVLW 0X01

   MOVWF LEDF

   BSF PORTA, 5; finally a latch signal is given, indicating that a display task is completed

   RETURN

; ********** S0 count overflow processing subroutine ******************

CS0 CLRF S0

   INCF S1

   RETURN

; ********** S1 count overflow processing subroutine ******************

CS1 CLRF S1

      INCF S2

      RETURN

; ********** S2 count overflow processing subroutine ******************

CS2 CLRF S2

      INCF S3

      RETURN

; ********** S3 count overflow processing subroutine ******************

CS3 CLRF S3

      RETURN

; **************Clock interrupt service routine********************

CLKINT MOVWF XW_TEMP ;Save the value of W

   SWAPF STATUS,W

   MOVWF XHOSTS ; Temporarily store the value of STATUS

   MOVF FSR,W

   MOVWF HOSTF ;Save the value of FSR. The above program is for interrupt scene protection

   MOVLW 0X14

   MOVWF TMR0 ; Write an adjustment value to TMR0, because writing

; TMR0 cannot be incremented in the next two cycles

   BCF INTCON, T0IF; Clear interrupt flag

   INCF SREG

   MOVF SREG,W

   SUBLW 0X28; TMR0 interrupts every 250 μs

   BTFSS STATUS, Z; it will execute S0, S1, S2, S3 after 40 interrupts. 

                      ;operate

   GOTO TIFAN

   CLRF SREG

   INCF S0

   MOVF S0,W

   SUBLW 0X0A

   BTFSC STATUS,Z

   CALL CS0  

   MOVF S1,W

   SUBLW 0X0A

   BTFSC STATUS,Z

   CALL CS1

   MOVF S2,W

   SUBLW 0X0A

   BTFSC STATUS,Z

   CALL CS2

   MOVF S3,W

   SUBLW 0X0A

   BTFSC STATUS,Z

   CALL CS3

TIFAN MOVF HOSTF, W; The following is the interruption scene recovery

   MOVWF FSR ; Restore the value of the indirect addressing pointer FSR

   SWAPF XHOSTS,W

   MOVWF STATUS ; Restore the value of STATUS

   SWAPF XW_TEMP,1

   SWAPF XW_TEMP, W ; Restore the value of W

   RETFIE  

; ************** TMR0 initialization subroutine ******************

CLKINSUB BANKSEL OPTION_REG

   BCF OPTION_REG, T0CS; TMR0 works in timer mode

   BSF OPTION_REG, PSA; TMR0 does not need frequency division

   BANKSEL INTCON

   BCF INTCON, T0IF; Clear the interrupt flag of TMR0

   BCF INTCON, GIE; general interrupt disable

   BSF INTCON, T0IE; TMR0 interrupt enable

   RETURN

; ******************System initialization subroutine********************

MAINSUB BANKSEL TRISA

   BCF TRISA, 5; Set RA5 to output mode to output latch signal

   BCF TRISB, 1

   BCF TRISB, 2

   BSF TRISB, 4

   BSF TRISB, 5; Set the input and output mode of each port related to the keyboard

   BCF TRISC, 5

   BCF TRISC, 3; Set SCK and SDO to output mode

   BCF INTCON, GIE ; turn off all interrupts

   MOVLW 0XC0

   MOVWF SSPSTAT; Set the SSPSTAT register

   BANKSEL SSPCON

   MOVLW 0X30

MOVWF SSPCON; Set the SPI control mode to allow SSP mode.

   BCF STATUS, IRP; Indirect addressing selects BANK0, BANK1

   RETURN ; Return  

; **************Key scanning subroutine********************

KEYSCAN CLRF PORTB ; send 0 on both B1 and B2 lines

   NOP         

  NOP; after a period of delay, avoid the transition process of the pin level

   MOVF PORTB,W

   ANDLW 0X30; only check B4 and B5, shield the other bits

   SUBLW 0X30

   RETURN     

;******************************************

KEYDELAY MOVLW 0X80 ; keyboard debounce subroutine (about 8ms software delay)

          MOVWF DEYH

AGAIN2 MOVLW 0XFF

          MOVWF DEYL

AGAIN1 DECFSZ DEYL,1

          GOTO AGAIN1

          DECFSZ DEYH,1

          GOTO AGAIN2

          RETURN  

;****************Display buffer processing subroutine****************

XIANHUAN MOVLW 0XA0

   MOVWF FSR

   MOVF S3,W

   MOVWF INDF

   INCF FSR

   MOVF S2,W

   MOVWF INDF

   INCF FSR

   MOVF S1,W

   MOVWF INDF

   INCF FSR

   MOVF S0,W

   MOVWF INDF

   RETURN

 

MAIN NOP

          CALL MAINSUB ;System initialization

          CALL CLKINSUB ; Call the clock initialization subroutine

DENJIAN BCF INTCON, GIE ; Disable interrupts

   BANKSEL S0

   CLRF S0

   CLRF S1

   CLRF S2

   CLRF S3

   CLRF SREG

   CLRF LEDF ; Clear S0=S1=S2=S3=0

   CALL XIANHUAN ; load the values ​​of S3, S2, S1, and S0 into the display buffer

      CALL DISPLAY ; Display

DENAN BCF INTCON, GIE; disable interrupt

   CALL KEYSCAN ; Perform key scan

   BTFSS STATUS,Z

   GOTO XIAODOU1; if the start key is pressed, the jitter is eliminated

   GOTO DENAN; if the start key is not pressed, continue waiting

XIAODOU1 CALL KEYDELAY; delay to eliminate jitter

   CALL KEYSCAN ;Perform key scan again

   BTFSC STATUS,Z

   GOTO DENAN; if it is interference, wait for the key to be pressed again

   BANKSEL S0

   CLRF S0

   CLRF S1

   CLRF S2

   CLRF S3

      CLRF SREG ; Set the initial conditions for a new timing     

WAITS CALL KEYSCAN     

   BTFSS STATUS,Z

   GOTO WAITS; To prevent the key from being too sensitive, wait until the key is released before proceeding.

; The following operation  

   MOVLW 0X08

   MOVWF TMR0

   BSF INTCON, GIE; open general interrupt

HERE CALL XIANHUAN ; Display buffer processing  

   CALL DISPLAY; Update the display content immediately

   CALL KEYSCAN  

   BTFSS STATUS,Z

   GOTO XIAODOU; if the stop timing key is pressed, the bounce will be eliminated.

   GOTO HERE; If the stop timing key is not pressed, continue to wait for the interrupt timing

XIAODOU CALL DISPLAY; Call the display program using the debounce time

   CALL DISPLAY  

   CALL DISPLAY  

   CALL DISPLAY

   CALL DISPLAY

   CALL DISPLAY  

   CALL DISPLAY  

   CALL DISPLAY  

   CALL DISPLAY

   CALL KEYSCAN

   BTFSC STATUS,Z

   GOTO HERE; if it is interference, continue to wait for the stop key to be pressed     

   BCF INTCON, GIE; If the stop key is pressed, the interrupt is disabled

      CALL DISPLAY; if it is not a disturbance, disable interruption and update the display content  

WAITR CALL KEYSCAN     

      BTFSS STATUS,Z

      GOTO WAITR; To prevent the key from being too sensitive, wait until the key is released before proceeding.

; The following operation     

DENDAI CALL KEYSCAN

   BTFSC STATUS,Z

   GOTO DENDAI ; loop key scan, wait for the reset key to be pressed

   CALL KEYDELAY

   CALL KEYSCAN

   BTFSC STATUS,Z

   GOTO DENDAI; delay debounce

WAIT19 CALL KEYSCAN

   BTFSS STATUS,Z

   GOTO WAIT19 ; Wait until the key is released  

   GOTO DENJIAN; a new time starts

   END

Keywords:pic16f877 Reference address:pic16f877 stopwatch program

Previous article:Discussion on PIC microcontroller controlling LED 8-bit water light
Next article:Temperature DS18b20 and PIC microcontroller communication program

Recommended ReadingLatest update time:2024-11-16 16:22

Graphic LCD Display Module Interface Technology of PIC16F877 Single Chip Microcomputer
Introduction Liquid crystal display (LCD) is widely used in various intelligent instruments and low-power electronic products because of its advantages that other displays cannot match, such as low power consumption, small size, light weight, and ultra-thinness. Dot matrix (or graphic) LCD can not only display
[Microcontroller]
Graphic LCD Display Module Interface Technology of PIC16F877 Single Chip Microcomputer
Analysis of PIC16F877 Asynchronous Serial Port Interrupt
PIC16F877 microcontroller asynchronous serial port interrupt program:   #include pic.h #define uchar unsigned char #define uint unsigned int __CONFIG(0x3B31); void init(); void main() {  init(); //TXREG=0x31; //Send  while(!TRMT); //Send completed and exit  while(1); } void init() {  TRIS
[Microcontroller]
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号