Assembly Programming For Mid Range PIC
Assembly Programming For Mid Range PIC
Assembly Programming For Mid Range PIC
for
Mid‐Range PIC
Embedded Systems — Hadassah College — Spring 2011 Mid-Range PIC Programming Dr. Martin Land 1
Producing Executable Code
Assembly
Develop code on PC
Edit / assemble / link / debug
Assembly
Convert assembly language code to machine language
Absolute (executable) code
Single module systems Assembly Assembler Executable
Code Code
Real (absolute) addresses
Relocatable (object) code
Complex multiple module systems
Relative addresses Assembly Assembler Object
Code Code Linker Executable
Code
Assembly Assembler Object
Code Code
Linking
Combine + convert multiple object modules to executable code
Set real addresses
Embedded Systems — Hadassah College — Spring 2011 Mid-Range PIC Programming Dr. Martin Land 2
Producing Executable Code
Compiling
Develop code on PC
Edit / assemble / compile / link / debug
Compile
Convert C language code to object code
C compiler available for Mid-Range PICs and higher
Linking
Combine + convert multiple object modules to executable code
Set real addresses Assembler Assembly Object
Code Code Linker Executable
Code
Assembly Assembler Object
Code Code
C Compiler Object
Code Code
Library
Code
Embedded Systems — Hadassah College — Spring 2011 Mid-Range PIC Programming Dr. Martin Land 3
MPLAB
Integrated Development Environment (IDE)
Text editor
Colorized formatting of PIC assembly code + directives
Assembler
MPASM.EXE / MPASMWIN.EXE
Linker
MPLINK.EXE
Library manager
MPLIB.EXE
Creates library (.lib) from several assembler programs
Simulator/Debugger
Simulates microcontroller on PC
Permits (limited) testing and debugging
Programmer
Program microcontroller device
Requires additional hardware
Embedded Systems — Hadassah College — Spring 2011 Mid-Range PIC Programming Dr. Martin Land 4
Elements of an Assembly Program
Instructions
Assembly language instructions in PIC ISA
Assembly language instruction ↔ machine language instruction
Labels
Constant
Symbolic literal
Variable
Pointer to data register holding value
Line label
Pointer to location of instruction word
GOTO label ⇒ GOTO address_of_instruction_at_label
Directives
Commands to assembler executed at assembly time
Examples
Arithmetic operations on addresses and labels
Assembly configuration
Embedded Systems — Hadassah College — Spring 2011 Mid-Range PIC Programming Dr. Martin Land 5
Program Organization
Absolute code
Directives
Include header files
Set up constants + variables + macros
Reset and interrupt code sections
Main code section
Specify absolute (real) addresses of code sections
Absolute addresses ⇒ direct addressing modes
Subroutine code sections
END directive
Relocatable code
Similar to absolute code
Differences
Variables defined in separate data section
No address specification for code sections
Direct / indirect addressing permitted
Embedded Systems — Hadassah College — Spring 2011 Mid-Range PIC Programming Dr. Martin Land 6
Assembler Input / Output Files
File Extension Contents Generated by
Device‐specific
Include file .inc Microchip
definitions
Embedded Systems — Hadassah College — Spring 2011 Mid-Range PIC Programming Dr. Martin Land 7
Constants
Symbolic literal — no change at run time
Defining constant Specifying constant values
Assign value to symbol May be preceded by + or –
Assembler converts symbol to literal Decimal
D'167'
.'167'
Directives
H'A7'
equ — no reassignment
Hexadecimal 0xA7
set — reassignment as constant 0A7H
O'247'
Octal
Example 247O
CONST1 equ 0xA5 ; assign Binary B'10100111'
BIT equ 3 ; assign A'Z'
ASCII
prog1: 'Z'
movlw CONST1 ; W ← A5h
addlw BIT ; W ← 3 + W
Embedded Systems — Hadassah College — Spring 2011 Mid-Range PIC Programming Dr. Martin Land 8
Variables
Pointer to data register holding value
Defining variable
Relocatable code
Reserve memory space in data section
Absolute code
Assign pointer to symbol
Use symbols as register name
Example (absolute)
CONST1 equ 0xA5 ; assign
REG1 equ 20h ; assign
BIT equ 3 ; assign
prog1:
movlw CONST1 ; W ← A5h
movwf REG1 ; REG1 (address 20h) ← W
bcf REG1, BIT ; REG1<BIT> = bit 3 in reg 20h ← 0
Embedded Systems — Hadassah College — Spring 2011 Mid-Range PIC Programming Dr. Martin Land 9
Operations on Constants
Arithmetic at assembly time
Example
CONST1 equ 0xA5 ; assign
REG1 equ 20h ; assign
INDEX equ 4 ; assign
BIT set 3 ; assign BIT ← 3
BIT set BIT + INDEX ; reassign BIT ← 7
prog1:
movlw CONST1 ; W ← A5h
movwf REG1 ; REG1 (address 20h) ← W
bcf REG1, BIT ; REG1<BIT> = bit 7 in reg 20h ← 0
Embedded Systems — Hadassah College — Spring 2011 Mid-Range PIC Programming Dr. Martin Land 10
Operators on constants
Evaluated at assembly time
+ Addition A1 + A2
– Subtraction A1 – A2
Evaluate
* Multiplication A1 * A2
arithmetically
/ Division A1/A2
% Modulo A1%A2
~ NOT ~A1
& AND A1 & A2
| OR A1 | A2
Evaluate bitwise
^ XOR A1 ^ A2
>> Right shift A1 >> 1
<< Left shift A1 << 2
! NOT !A1
&& AND A1 && A2
|| OR A1 || A2
> Higher than A1 > A2 Evaluate to
< Less than A1 < A2 TRUE = 1 or
> Higher or equal to A1 >= A2 FALSE = 0
< Less or equal to A1 <= A2
= Equal to A1 == A2
!= Different than A1 != A2
Embedded Systems — Hadassah College — Spring 2011 Mid-Range PIC Programming Dr. Martin Land 11
Operators on Variable Pointers
Evaluated at assembly time
Note
var = 0 equivalent to var set 0
Embedded Systems — Hadassah College — Spring 2011 Mid-Range PIC Programming Dr. Martin Land 12
Define Block of Constants
For absolute code
Specify starting absolute address
Defines list of named symbols at sequential addresses
Used as variable pointers
Syntax
cblock [expr]
label[:increment][,label[:increment]]
endc
Example
cblock 0x20 ; name_0 ← 20h
name_0, name_1 ; name_1 ← 21h
name_2, name_3 ; name_2 ← 22h
endc ; name_3 ← 24h
Embedded Systems — Hadassah College — Spring 2011 Mid-Range PIC Programming Dr. Martin Land 13
Address Operators and General Directives
Operator Operation Example
$ Current address goto $ ; loop in place
low Address low byte movlw low label ; W ← label<7:0>
high Address high byte movlw high label ; W ← 000.label<12:8>
Embedded Systems — Hadassah College — Spring 2011 Mid-Range PIC Programming Dr. Martin Land 15
Skeleton for Absolute Code — 1
list p = 16f873 ; Declare device
#include <p16f873.inc> ; include header file
;
; Define constants
;
DATA1 EQU 0x1
DATA2 EQU 0x2
;
; Define variables
;
w_temp equ 0x20 Alternative:
status_temp equ 0x21 cblock 0x20
w_temp
X equ 0x22 status_temp
Y equ 0x23 X, Y
0x20endc
Embedded Systems — Hadassah College — Spring 2011 Mid-Range PIC Programming Dr. Martin Land 16
Skeleton for Absolute Code — 2
;
; Body of program
;
org 0x000 ; Reset vector address
movlw high PP ; W ← 000.PP<12:8>
movwf PCLATH ; PCLATH ← PP<12:8>
goto PP ; PCL ← PP<7:0> = start of main
;
Embedded Systems — Hadassah College — Spring 2011 Mid-Range PIC Programming Dr. Martin Land 17
Skeleton for Absolute Code — 3
org 0x004 Interrupt vector address ;
movwf w_temp w_temp ← W (no flags) ;
movf STATUS, W W ← STATUS (write Z) ;
bcf STATUS, RP0 Select bank 0 ;
movwf status_temp status_temp ← W = STATUS ;
; (no write Z)
; Interrupt Service Routine Here
;
bcf STATUS, RP0 ; Select bank 0
movf status_temp, W ; Restore STATUS (write Z)
movwf STATUS ; (no write Z)
swapf w_temp, f ; swap nibbles to w_temp
swapf w_temp, W ; re-swap nibbles to W
; (no write Z)
retfie ; Return from interrupt
Embedded Systems — Hadassah College — Spring 2011 Mid-Range PIC Programming Dr. Martin Land 18
Skeleton for Absolute Code — 4
PP:
clrf X ; zero variables
clrf Y
;
; main program
;
; subroutine call
;
movlw high SR1 ; W ← 000.SR1<12:8>
movwf PCLATH ; PCLATH ← SR1<12:8>
call SR1 ; push PC
; PCL ← SR1<7:0>
goto $ ; spin loop (jumps to here)
SR1:
; code of subroutine SR1
return ; Return to main
Embedded Systems — Hadassah College — Spring 2011 Mid-Range PIC Programming Dr. Martin Land 19
Section Declarations for Relocatable Code
Initialized data section
Syntax
[label] idata [RAM_address]
Defaults
label = .idata
RAM_address set by linker Data Directives
Example db
Inserts data byte at
idata
next memory address
LimitL dw 0 dw
LimitH dw D'300' Inserts 2 data bytes in
Gain dw D'5' little endian order
Flags res 1 res n
Inserts n data 0 bytes
String db 'Hi there!'
Embedded Systems — Hadassah College — Spring 2011 Mid-Range PIC Programming Dr. Martin Land 20
Section Declarations for Relocatable Code
Uninitialized Data Section
Syntax
[label] udata [RAM_address]
Defaults
label = .udata
RAM_address set by linker
Example
Data Directive
udata
Var1 res 1 res n
Double res 2 reserves n data bytes
Embedded Systems — Hadassah College — Spring 2011 Mid-Range PIC Programming Dr. Martin Land 21
Section Declarations for Relocatable Code
Shared Uninitialized Data Section
Syntax
[label] udata_shr [RAM_address]
Registers shared across memory
Values copied to file address in all banks
Default label = .udata_shr
Example
udata_shr
Var1 res 1
Double res 2
Embedded Systems — Hadassah College — Spring 2011 Mid-Range PIC Programming Dr. Martin Land 22
Section Declarations for Relocatable Code
Overlayed Uninitialized Data Section
Syntax
[label] udata_ovr [RAM_address]
Registers declared in section overlayed
Other udata_ovr sections with same name overwrite same space
Multiple temporary variable sets declared at one memory location
Default label = .udata_ovr
Example
Temps udata_ovr
Temp1 res 1
Temp2 res 1
;
; work with Temp1, Temp2
;
Temps udata_ovr
NewTemp1 res 1 ; reallocate location Temp1
NewTemp2 res 1 ; reallocate location Temp2
Embedded Systems — Hadassah College — Spring 2011 Mid-Range PIC Programming Dr. Martin Land 23
Section Declarations for Relocatable Code
Code Section
Syntax
[label] code [RAM_address] Code Directive
pagesel start
Defaults Generates code:
label = .code movlw high start
RAM_address set by linker
movwf PCLATH
Example
RST CODE 0x0 ; placed at address 0x0
pagesel start
goto start
PGM CODE ; relocatable code section
start:
clrw
goto $
CODE ; relocatable code section
nop ; default section name .code
end
Embedded Systems — Hadassah College — Spring 2011 Mid-Range PIC Programming Dr. Martin Land 24
Skeleton for Relocatable Code — 1
list p = 16f873 ; Declare device
#include <p16f873.inc> ; include header file
;
; Define constants
;
DATA1 EQU 0x1
DATA2 EQU 0x2
;
; Define variables
;
udata_shr ; data shared across banks
w_temp res 1
status_temp res 1
X res 1
Y res 1
Embedded Systems — Hadassah College — Spring 2011 Mid-Range PIC Programming Dr. Martin Land 25
Skeleton for Relocatable Code — 2
;
; Body of program
;
Rst_vector code 0 ; Reset vector address
pagesel PP
goto PP
;
Intr_vector code 4 ; Interrupt vector address
goto SR_Int
;
Embedded Systems — Hadassah College — Spring 2011 Mid-Range PIC Programming Dr. Martin Land 26
Skeleton for Relocatable Code — 3
Intr_Prog code 5 ; ISR
SR_Int:
movwf w_temp ; w_temp ← W (no flags)
movf STATUS, W ; W ← STATUS (write Z)
bcf STATUS, RP0 ; Select bank 0
movwf status_temp ; status_temp ← W = STATUS
; (no write Z)
; Interrupt Service Routine Here
;
bcf STATUS, RP0 ; Select bank 0
movf status_temp, W ; Restore STATUS (write Z)
movwf STATUS ; (no write Z)
swapf w_temp, f ; swap nibbles to w_temp
swapf w_temp, W ; re-swap nibbles to W
; (no write Z)
retfie ; Return from interrupt
Embedded Systems — Hadassah College — Spring 2011 Mid-Range PIC Programming Dr. Martin Land 27
Skeleton for Relocatable Code — 4
Prog_Principal code
PP:
clrf X ; zero variables
clrf Y
;
; main program
;
; subroutine call
;
pagesel
call SR1
goto $ ; spin loop (jumps to here)
Subroutines code
SR1:
; code of subroutine SR1
return ; Return to main
Embedded Systems — Hadassah College — Spring 2011 Mid-Range PIC Programming Dr. Martin Land 28
Define — Single Line Macros
Syntax
#define name [string]
Text substitution
name in assembly code replaced by string
Permits parameter substitution
Example
#define length 20
#define width 30
#define depth 40
#define circumference(X,Y,Z) (X + Y + Z)
:
Size equ circumference(length, width, depth)
Embedded Systems — Hadassah College — Spring 2011 Mid-Range PIC Programming Dr. Martin Land 29
Macros
Syntax
macro_name macro [arg_def1, arg_def2,…]
[ local label [, label, label,…]]
;
; Body of macroinstruction
;
endm
Optional arguments
arg_def1, arg_def2
local labels — local to macro definition
Call macro
macro_name [arg1, arg2,…]
Embedded Systems — Hadassah College — Spring 2011 Mid-Range PIC Programming Dr. Martin Land 30
Macro Example
Convert macro HEXA, ASCII ; Declare macro
local add30, add37, end_mac ; local labels
movf HEXA, W ; HEXA ← W
sublw 9 ; W ← 9 - W
; C ← (W > 9)
movf HEXA, W ; C not changed
btfsc STATUS, C ; if (C == 0){
goto add30 C != 0 ; W ← W + 37h
add37: ; }
addlw 37h C = 0 ; else {
goto end_mac ; W ← W + 30h
add30: ; }
addlw 30h
end_mac:
movwf ASCII ; ASCII ← W
endm ; End of macro
Convert HX, ASC ; insert macro code here
Embedded Systems — Hadassah College — Spring 2011 Mid-Range PIC Programming Dr. Martin Land 31
Macros for Register Save / Restore
PUSH_MACRO MACRO ; Save register contents
MOVWF W_TEMP ; Temporary register ← W
SWAPF STATUS,W ; W ← swap STATUS nibbles
MOVWF STATUS_TEMP ; Temporary register ← STATUS
ENDM ; End this Macro
Embedded Systems — Hadassah College — Spring 2011 Mid-Range PIC Programming Dr. Martin Land 32
Typical Interrupt Service Routine (ISR) — 1
org ISR_ADDR ; store at ISR address
PUSH_MACRO ; save context registers W, STATUS
CLRF STATUS ; Bank0
; switch implementation in PIC assembly language
BTFSC PIR1, TMR1IF ; skip next if (PIR1<TMR1IF> == 1)
GOTO T1_INT ; go to Timer1 ISR
BTFSC PIR1, ADIF ; skip next if (PIR1<ADIF> == 1)
GOTO AD_INT ; go to A/D ISR
BTFSC PIR1, LCDIF ; skip next if (PIR1<LCDIF> == 1)
GOTO LCD_INT ; go to LCD ISR
BTFSC INTCON, RBIF ; skip next if (PIR1<RBIF> == 1)
GOTO PORTB_INT ; go to PortB ISR
GOTO INT_ERROR_LP1 ; default ISR
Embedded Systems — Hadassah College — Spring 2011 Mid-Range PIC Programming Dr. Martin Land 33
Typical Interrupt Service Routine (ISR) — 2
T1_INT ; Timer1 overflow routine
:
BCF PIR1, TMR1IF ; Clear Timer1 overflow interrupt flag
GOTO END_ISR ; Leave ISR
AD_INT ; Routine when A/D completes
:
BCF PIR1, ADIF ; Clear A/D interrupt flag
GOTO END_ISR ; Leave ISR
LCD_INT ; LCD Frame routine
:
BCF PIR1, LCDIF ; Clear LCD interrupt flag
GOTO END_ISR ; Leave ISR
PORTB_INT ; PortB change routine
:
END_ISR ; Leave ISR
POP_MACRO ; Restore registers
RETFIE ; Return and enable interrupts
Embedded Systems — Hadassah College — Spring 2011 Mid-Range PIC Programming Dr. Martin Land 34
Accessing External Modules
Import Label Example
extern label [, label...] ; in module 1
global Var1, Var2
Declare symbol global AddThree
Used in current module ;
udata
Defined as global in different module
Var1 res 1
Must appear before label used Var2 res 1
code
AddThree:
Export Label addlw 3
global label [, label...] return
Embedded Systems — Hadassah College — Spring 2011 Mid-Range PIC Programming Dr. Martin Land 35
Start MPLAB IDE
Embedded Systems — Hadassah College — Spring 2011 Mid-Range PIC Programming Dr. Martin Land 36
Configure > Select Device
Embedded Systems — Hadassah College — Spring 2011 Mid-Range PIC Programming Dr. Martin Land 37
Project > Project Wizard
Save Project by Pathname Add Device Template File
Embedded Systems — Hadassah College — Spring 2011 Mid-Range PIC Programming Dr. Martin Land 38
Build Project
Either
Project > Build All
Right click on project name in Project Window > Build All
Click Build All icon on Project toolbar
Output window shows result of build process
Should be no errors or warnings for default template file
Code
Add constants / variables / code / directives / macros
Rebuild
Embedded Systems — Hadassah College — Spring 2011 Mid-Range PIC Programming Dr. Martin Land 39
Testing Code with Simulator
Debugger > Select Tool > MPLAB SIM
Debug toolbar opens
Debugger > Reset > Processor Reset
Assembly code editor opens
Green arrow points to program start (main)
Step Into
Run program in trace mode (single step)
Embedded Systems — Hadassah College — Spring 2011 Mid-Range PIC Programming Dr. Martin Land 40
View > Watch
Choose + Add items to watch list
SFRs Symbols
Embedded Systems — Hadassah College — Spring 2011 Mid-Range PIC Programming Dr. Martin Land 41
Breakpoints
Set breakpoint
Double-click on line of code
Right click > choose Set Breakpoint from menu
Run
Program stops before breakpoint
Embedded Systems — Hadassah College — Spring 2011 Mid-Range PIC Programming Dr. Martin Land 42
Stopwatch
At breakpoint
Reports clock cycles
Estimates runtime
Embedded Systems — Hadassah College — Spring 2011 Mid-Range PIC Programming Dr. Martin Land 43
Delay Timer with Timer0 — 1
Internal RC oscillator
TCY = 4 × 1 / (4 MHz) = 1 μs = 0.001 ms
1 ms = 1000 counts
Prescale
PS<2:0> ← 010 for 1 / 8 division ⇒ 125 counts
2 cycle delay in synchronizer ⇒ 123 counts
Preset
Timer0 interrupts when FFh = 256 rolls over to 0
Preset counter to 256 – 123 = 133
N ms delay
AUX ← N for N × 1 ms delay
Embedded Systems — Hadassah College — Spring 2011 Mid-Range PIC Programming Dr. Martin Land 44
Delay Timer with Timer0 — 2
List p = 16F873
include "P16F873.INC"
AUX equ 0x20 ; Auxiliary variable
InitTimer0: bcf INTCON, T0IE ; Disable Timer0 interrupt
bsf STATUS, RP0 ; Bank 1
movlw 0xC2 ; Configure timer mode
movwf OPTION_REG ; Prescaler = 8
bcf STATUS, RP0 ; Bank 0
clrf TMR0 ; TMR0 ← 0
bcf INTCON, T0IF ; Clear overflow flag
return
Del1ms: movlw .133 ; Preset value = 133 (decimal)
movwf TMR0 ; TMR0 ← preset
Del1ms_01: btfss INTCON, T0IF ; Skip next if (T0IF == 1)
goto Del1ms_01 ; Keep waiting
bcf INTCON, T0IF ; Clear T0IF = 0
return ; Return after 1 ms
DelNms: movwf AUX ; AUX ← number of ms
DelNms_01: ; Call Del1ms AUX times
call Del1ms ; Wait 1 ms.
decfsz AUX, f ; AUX-- Skip next if (AUX == 0)
goto DemNms_01 ; Keep waiting
return ; Return after AUX interations
end
Embedded Systems — Hadassah College — Spring 2011 Mid-Range PIC Programming Dr. Martin Land 45
Measure Interval Between External Pulses — 1
Internal RC oscillator
TCY = 4 × 1 / (4 MHz) = 1 μs = 0.001 ms
Timer1
Synchronous timer mode
Prescale ← 1
TMR1++ every microsecond
Embedded Systems — Hadassah College — Spring 2011 Mid-Range PIC Programming Dr. Martin Land 46
Measure Interval Between External Pulses — 2
List p = 16F873
include "P16F873.INC"
N1H equ 20h ; High byte of first capture
N1L equ 21h ; Low byte of first capture
NH equ 22h ; High byte of difference
NL equ 23h ; Low byte of difference
Init_capture: clrf T1CON ; Timer mode with prescaler = 1
clrf CCP1CON ; Reset module CCP1
bsf STATUS, RP0 ; Bank 1
bsf TRISC, 2 ; Set CCP1 pin as input
bcf PIE1, TMR1IE ; Disable Timer1 interrupt
bcf PIE1, CCP1IE ; Disable CCP1 interrupt
bcf STATUS, RP0 ; Bank 0
clrf PIR1 ; Clear interrupt flags
movlw 0x05 ; Capture mode on raising edge
movwf CCP1CON ;
bsf T1CON, TMR1ON ; Start Timer1
return
Embedded Systems — Hadassah College — Spring 2011 Mid-Range PIC Programming Dr. Martin Land 47
Measure Interval Between External Pulses — 3
Capture: bcf PIR1, CCP1IF ; Clear capture flag
btfss PIR1, CCP1IF ; Skip next if (CCP1IF == 1)
goto Capture
bcf PIR1, CCP1I ; Clear capture indicator
movf CCPR1L, W ; Store captured value in N1H and N1L
movwf N1L
movf CCPR1H, W
movwf N1H
Capture2: btfss PIR1, CCP1IF ; Skip next if (CCP1IF == 1)
goto Capture2
bcf PIR1, CCP1IF ; Clear capture indicator
movf N1L, W
subwf CCPR1L, W ; Subtract captured values
movwf NL
btfss STATUS, C
goto Subt1
goto Subt0
Subt1: decf CCPR1H, f 16‐bit arithmetic
Subt0: movf N1H, W AH:AL = BH:BL – CH:CL
subwf CCPR1H, W AL ← BL – CL
movwf NH if (C == 1) BH--
return AH ← BH – CH
end
Embedded Systems — Hadassah College — Spring 2011 Mid-Range PIC Programming Dr. Martin Land 48
Real Time Clock (RTC) — 1
Internal RC oscillator
TCY = 4 × 1 / (4 MHz) = 1 μs = 0.001 ms
Timer0
Timer0 interrupts when FFh = 256 rolls over to 0
Prescale = 32
Interrupt every 0.001 ms × 256 × 32 = 8.192 ms
Seconds
1 second per clock tick
(1 second / tick) / (8.192 ms / interrupt) = 122.07 interrupts / tick
1 second = 122 interrupts
Minutes
1 minute = 60 clock ticks
Hours
1 hour = 60 minutes
Days
1 day = 24 hours
Embedded Systems — Hadassah College — Spring 2011 Mid-Range PIC Programming Dr. Martin Land 49
Real Time Clock (RTC) — 2
INT
Embedded Systems — Hadassah College — Spring 2011 Mid-Range PIC Programming Dr. Martin Land 50
Real Time Clock (RTC) — 3
list p = 16f873
#include <p16f873.inc>
TICKS equ 0x20 ; Ticks counter
SEC equ 0x21 ; Seconds counter
MIN equ 0x22 ; Minutes counter
HOUR equ 0x23 ; Hours counter
TEMP_W equ 0x24
TEMP_ST equ 0x25
org 0
goto init
org 4
goto rtc
init: clrf INTCON ; Disable interrupts
bsf STATUS, RP0 ; Bank 1
movlw 0xC4 ; Prescaler 32
movwf OPTION_REG ; Assigned to Timer0
bcf STATUS, RP0 ; Bank 0
movlw 0 ; Count module = 256
movwf TMR0 ; in Timer0
movlw .122 ; Ticks per second
movwf TICKS ; in tick counter
clrf SEC ; Clear Seconds counter
clrf MIN ; Clear Minutes counter
clrf HOUR ; Clear Hour counter
bsf INTCON, T0IE ; Enable Timer0 interrupt
bsf INTCON, GIE ; Enable global interrupts
Embedded Systems — Hadassah College — Spring 2011 Mid-Range PIC Programming Dr. Martin Land 51
Real Time Clock (RTC) — 4
prog: nop
goto prog ; Infinite loop
rtc: bcf STATUS, RP0 ; Bank 0
PUSH_MACRO ; Save STATUS, TEMP_ST
bcf INTCON, T0IF ; Clear overflow flag for Timer0
decfsz TICKS, f ; TICKS-- Skip next if (TICKS == 0)
goto end_rtc
rtc_sec: movlw .122 ; Re-init TICKS
movwf TICKS
incf SEC, f ; seconds++
movf SEC, W
xorlw .60 ; Z ← 1 if (SEC == 60)
btfsc STATUS, Z ; Skip next on (Z == 1)
goto end_rtc
rtc_min: clrf SEC ; Clear seconds
incf MIN, f ; minutes++
movf MIN, W
xorlw . 60 ; Z ← 1 if (MIN == 60)
btfsc STATUS, Z ; Skip next on (Z == 1)
goto end_rtc
rtc_hour: clrf MIN ; Clear minutes
incf HOUR, f ; hours++
movf HOUR, W
xorlw .24 ; Z ← 1 if (HOUR == 60)
btfsc STATUS, Z ; Skip next on (Z == 1)
goto end_rtc
rtc_day: clrf HOUR ; Clear hours
end_rtc: POP_MACRO ; Retrieve STATUS, TEMP_ST
retfie ; Return to interrupted program.
end ; End of source code.
Embedded Systems — Hadassah College — Spring 2011 Mid-Range PIC Programming Dr. Martin Land 52