1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
 

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;::;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;                                                                                                  ;
;                        Program:                                                                  ;
;                        Author:                                                                   ;
;                        Date:                                                                     ;
;                                                                                                  ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;::;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

        list            P=PIC16F628A
        include         p16f628a.inc
        __config        _CP_OFF & _DATA_CP_OFF & _LVP_OFF & _BODEN_OFF & _MCLRE_OFF & _PWRTE_ON & _WDT_OFF & _INTRC_OSC_NOCLKOUT
        ;__config        B'10000100010000'
        errorlevel      -302    	;Eliminate bank warning

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;::;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;                                                                                                  ;
;                                   PIC16F628A Microcontroller                                     ;
;                                            ____ ____                                             ;
;                             VREF/AN2/RA2 -| 1  - 18 |- RA1/AN1                                   ;
;                       LED - CPM1/AN3/RA3 -| 2    17 |- RA0/AN0                                   ;
;                           CMP2/T0CKI/RA4 -| 3    16 |- RA7/OSC1/CLKIN                            ;
;                             VPP/MCLR/RA5 -| 4    15 |- RA6/OSC2/CLKOUT                           ;
;                                      VSS -| 5    14 |- VDD                                       ;
;                                  INT/RB0 -| 6    13 |- RB7/T1OSC1/ICSPDAT                        ;
;                                DT/RX/RB1 -| 7    12 |- RB6/T1OSCO/T1CLKI/ICSPCLK                 ;
;                                CK/TX/RB2 -| 8    11 |- RB5                                       ;
;                                 CCP1/RB3 -|_9____10_|- RB4/PGM                                   ;
;                                                                                                  ;
;                                                                                                  ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;::;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

W_TEMP                  EQU     H'70'   ; Variable used for context saving
STATUS_TEMP             EQU     H'71'   ; Variable used for context saving
GPR1                    EQU     H'72'   ; Variable used for temporary information storage
GPR2                    EQU     H'73'   ; Variable used for temporary information storage
GPR3                    EQU     H'74'   ; Variable used for temporary information storage
DELAYGPR1               EQU     H'75'   ; Variable used for temporary information storage in the delay subroutines
DELAYGPR2               EQU     H'76'   ; Variable used for temporary information storage in the delay subroutines
DELAYGPR3               EQU     H'77'   ; Variable used for temporary information storage in the delay subroutines


        ORG     0x000                   ; Processor reset vector location. On power up, the program jumps here
        GOTO    SETUP                   ; 

        ORG     0x004                   ; Interrupt vector location. When an Interupt occurs, the program jumps here
        MOVWF   W_TEMP                  ; Save current W register contents
        MOVF    STATUS,W                ; Move STATUS register into W register
        MOVWF   STATUS_TEMP             ; Save contents of STATUS register in temporary register

; Interrupt Service Routine goes here. If no interrupts are enabled and triggered during the program, this section can be ignored

        MOVF    STATUS_TEMP,W           ; Retrieve saved status of STATUS register
        MOVWF   STATUS                  ; Restore pre-ISR STATUS register contents
        SWAPF   W_TEMP,F                ; Restore pre-ISR W register contents
        SWAPF   W_TEMP,W                ; (a MOVF W_TEMP, W would acheive the same result but this affects the STATUS register, therefore 2 SWAPF instructions are used)
        RETFIE                          ; Return From Interrupt


SETUP  ; This routine initialises the chip, setting the inputs and outputs
        CLRF    PORTA                   ; Initialise PORT A by setting ouput data latches
        CLRF    PORTB                   ; Initialise PORT B by setting ouput data latches
        MOVLW   H'07'                   ; Turn Comparators off and enable pins for I/O functions
        MOVWF   CMCON                   ;
        BCF     STATUS, RP1             ; Bank 1
        BSF     STATUS, RP0             ; Bank 1
        MOVLW   B'11100001'             ; RA<7:6>, Inputs. RA<5:2> Outputs. RA<1:0> Inputs.
        MOVWF   TRISA                   ;

        MOVLW   B'11110011'             ; RB<7:4> Inputs. RB<3:2> Ouputs.  RB<0:1> Inputs.
        MOVWF   TRISB                   ;

        BCF     STATUS, RP1             ; Bank 0
        BCF     STATUS, RP0             ; Bank 0
		
MAIN  ; This routine contains the main program

 


DELAY_250mS  ; Actual delay = 0.25 seconds = 250000 cycles
        MOVLW    H'4E'                  ;
        MOVWF    DELAYGPR1              ;
        MOVLW    H'C4'                  ;
        MOVWF    DELAYGPR2              ;
DELAY_250mS_0                           ;
        DECFSZ   DELAYGPR1, F           ;
        GOTO     $+2                    ;
        DECFSZ   DELAYGPR2, F           ;
        GOTO     DELAY_250mS_0          ; 249993 cycles
        GOTO     $+1                    ;
        NOP                             ; 3 cycles
        RETURN                          ; 4 cycles (including call)


END                                     ; Directive 'end of program'