Skip to content

Instantly share code, notes, and snippets.

@marioIncandeza
Last active September 10, 2015 22:09
Show Gist options
  • Save marioIncandeza/872c4278f300412415e3 to your computer and use it in GitHub Desktop.
Save marioIncandeza/872c4278f300412415e3 to your computer and use it in GitHub Desktop.
Microcontrollers Lab 3
;******************************************************
;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
;******************************************************
PREV EQU 20H ;Used to track whether previous value of button was 1 or 0
COUNT EQU 21H ;Keeps track of how many button presses have happened
DCOUNT EQU 22H ;Use for delay
;******************************************************
ORG 000H
GOTO MAIN
ORG 0008H
MOVLW 21
;*******************************************************
MAIN
BSF STATUS,RP1 ;change to bank 3
BSF STATUS,RP0
CLRF ANSEL ;Using all digital mode, turns off analog
CLRF ANSELH ;Make all analog ports digital
BCF STATUS,RP1 ;change to correct bank to configure TRIS registers
;MAKE RA0 OUTPUT, RB0 INPUT, PORTC AND PORTD ALL OUTPUTS
MOVLW 0FEH
MOVWF TRISA
CLRF TRISB
COMF TRISB,1
CLRF TRISC
CLRF TRISD
BCF STATUS,RP0 ;return to bank 0
CLRF PORTB ;CLEAR PORTB, PORTC, PREV, AND COUNT
CLRF PORTC
CLRF PREV
CLRF COUNT
LOOP
MOVF COUNT,0 ;MOVE COUNT INTO W
CALL TABLE ;Look up value for 7 segment display to show count
MOVWF PORTD ;PUT RETURNED VALUE (now in W) ON PORTD
BCF PORTA,0 ;CLEAR RA0, CALL DELAY, SET RA0
CALL DELAY
BSF PORTA,0
BTFSC PORTB,0 ;ADD BIT TEST ON RB0
GOTO RB0HI ;GO HERE IF RB0 IS 1
GOTO RB0LO ;GO HERE IF RB0 IS 0
RB0HI ;ADD BIT TEST TO SEE IF BIT 0 OF PREV WAS LOW, THIS INDICATES RISING EDGE
BTFSS PREV,0
CALL INCCNT
BSF PREV,0 ;SET THIS BIT TO TRACK HISTORY
GOTO NEXT
RB0LO CALL DELAY
BCF PREV,0 ;CLEAR THIS BIT TO TRACK HISTORY
NEXT GOTO LOOP ;INFINITE LOOP
INCCNT
;INCREMENT COUNT
INCF COUNT
;CHECK IF VALUE OF COUNT IS 10 AND TO RESET COUNT TO 0 IF IT EQUALS 10
MOVLW 0x0A
XORWF COUNT,0
BTFSC STATUS,Z
CLRF COUNT
;WRITE COUNT TO PORTC (via W)
MOVF COUNT,0
MOVWF PORTC
CALL DELAY
RETURN
DELAY
MOVLW 0FFH
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