Upload
hidayah-akses
View
231
Download
0
Embed Size (px)
Citation preview
7/30/2019 Lect05 Prog Dev
1/27
TAB4333 1
Embedded Systems
TAB4333
Chapter 5
Programs Development
7/30/2019 Lect05 Prog Dev
2/27
TAB4333 2
Timer 0
PIC18F452 has four 16-bit timers: Timer 0, Timer 1, Timer 2, and
Timer 3. All counters are count-up type.
Timers 1-3 can be paired capture / compare / pulse-width modulation
(CCP) circuitry for external time control and measurement.
Since Timer 0 can not be paired with CCP it is usually used for
internal Looptime timing.
Timer 0 can count pulses of either the internal program clock or on pin
RA4.
Throughout the slides it is assumed that the internal clock is used.
The notation RA4 means that it is pin 4 (of pins 0-7) on I/O port A. The RA4 pin is also labeled T0CKI to show that it can be used as
Timer 0 clock inputor as a general-purpose input/output pin.
Timer 0 will be used as example in program development.
7/30/2019 Lect05 Prog Dev
3/27
TAB4333 3
Timer 0Features
prescaler
( N)synchronizer
TMR0H
(8-bit)
TMR0L
(8-bit)
presettable & readable
countersinternal clock
(2.5 MHz) T0CKI
(RA4)
Input
TMR0IF
(flag set on overflow)
N = 1,2,4,8, , 256
Freq < internal clock
If input = 40KHz
N = ?
If only on internal clock:
What time interval TMR0IF overflow?
How many counts need to be deducted
from the counter for overflow interval
of 10ms?
7/30/2019 Lect05 Prog Dev
4/27
TAB4333 4
Timer 0 The two 8-bit halves of the 16-bit Timer 0 are special function
registers (SFRs).
The most-significant byte (MSB) of the timer has symbolic name
TMR0H and is register 0xFD7.
The LSB of the timer is called TMR0L and is register 0xFD6.
All of the SFRs are listed in Appendix A8 of the book and defined in
18F452 header file.
The Timer 0 control(T0CON) - special function register sets up the
way Timer 0 functions.
Four bits of T0CON are important for internal-clock Timer 0
operation:
1) TMR0ONTimer 0 on/off2) T08BITTimer 0 8-bit/16-bit
3) T0CSTimer 0 clock select
4) PSAPrescaler use/bypass
7/30/2019 Lect05 Prog Dev
5/27
TAB4333 5
Timer 0 On/Off TMR0ON is a symbolic name (defined in P18F452.inc) equal to the
value 7.
The bit that turns Timer 0 on or off happens to be the most-significantbit (MSB) of T0CON (a symbolic name defined in P18F452.inc as
0xFD5).
To turn Timer 0 on, we could use:
bsf T0CON, TMR0ON ; [bsf 0xFD5,7]
Timer 0 8-bit/16-bit
Timer 0 is the only timer that can be used as an 8-bit or 16-bit timer.
Probably the only reason to use 8-bit mode is for porting code fromolder PIC C (which only had an 8-bit Timer 0).
To set Timer 0 to 16-bit mode we could use:
bcf T0CON, T08BIT
7/30/2019 Lect05 Prog Dev
6/27
TAB4333 6
Timer 0 Clock Select To use the RA4 pin as a clock input to Timer 0, bit T0CS of T0CON
should be 1.
To use the internal program clock as the clock input to Timer 0, bit
T0CS of T0CON should be 0.
To use the internal clock we could write:
bcf T0CON, T0CS
Timer 0 Prescaler Use/Bypass
To get a resolution of one clock cycle, bypass the prescaler.
To get a longer timer duration (at less resolution) use the prescaler.
The PSA bit of T0CON = 0 means to use prescaler ; and = 1 is to bypass
prescaler. We can write:
bsf T0CON, PSA
7/30/2019 Lect05 Prog Dev
7/27TAB4333 7
Timer 0 Full Setup
To enable Timer 0 in 16-bit mode using theinternal clock and bypass the prescaler we
could write:
TMR0ON = 1, T08BIT = 0, T0CS = 0,
PSA = 1 (only underlined bits matter)
movlw B10001000
movwf T0CON
7/30/2019 Lect05 Prog Dev
8/27TAB4333 8
Add a Value to a Running Timer
To shorten the cycle time of Timer 0, we need to add a
value (deducted count) to the counter by:
1) Read the 16-bit timer value in
TMR0H:TMR0L into a buffer (RAM)2) Add a 16-bit number to this value.
3) Store the result back to TMR0H:TMR0L.
The timer runs between read and store, so this
incrementing will be lost.
Note: These steps are needed because there
is no such instruction as movlf k,f
Why add?
7/30/2019 Lect05 Prog Dev
9/27TAB4333 9
Add a Value to a Running Timer
Instructions (TMR0LCOPY, TMR0HCOPY, andValueToAdd are defined in user code):
movff TMR0L, TMR0LCOPYmovff TMR0H, TMR0HCOPY
movlw low ValueToAdd
addwf TMR0LCOPY, F
movlw high ValueToAdd
addwfc TMR0HCOPY, F
movff TMR0HCOPY, TMR0H
movff TMR0LCOPY, TMR0L
How many cycles?
7/30/2019 Lect05 Prog Dev
10/27TAB4333 10
Add a Value to a Running Timer
The preceding code takes 12 clocks to execute: 2
clocks for each movff and 1 clock for other
instructions. There is also always a loss of 2 clocks on any
write to TMR0L due to synchronizer reset.
The net result is that ValueToAdd should be 14
larger than the desired value to add to Timer 0.
7/30/2019 Lect05 Prog Dev
11/27TAB4333 11
Timer 0 Rollover
All PIC timers count up (increment) only andnever down (decrement).
Timer 0 sets the TMR0IF (Timer 0 interrupt flag)bit of the INTCONT (interrupt control) SFR whenthe timer counts from 0xFFFF to 0x0000(regardless of whether interrupts are used or not).
We can skip the next instruction on Timer 0rollover using:
btfss INTCON, TMR0IF
7/30/2019 Lect05 Prog Dev
12/27TAB4333 12
Typical Program Structure
Ignoring interrupts, most PIC programs look like:
Initialize;
Loop forever {
Do tasks;
Wait until looptime has expired}
Refpg 66 lines 55 to 61
7/30/2019 Lect05 Prog Dev
13/27TAB4333 13
Looptime Subroutine Using a 10 MHz crystal (like the QwikFlash board has) and not
using the PICs internal PLL results in an internal clock frequency
of 2.5 MHz.
Bypassing the Timer 0 prescaler for maximum timing resolution
allows for no more than 65,536 clock cycles between Timer 0
rollovers.
Using 25,000 counts at 2.5 MHz gives a nice round number of 10milliseconds per rollover.
To get 25,000 counts per rollover, we must skip the remaining
65,53625,000 = 40,536 of the maximum counts between
rollovers.
Since the code to add a value to the running counter causes 14
counts to be missed, we actually need to add 40,536 + 14 = 40,550
to the timer value.
7/30/2019 Lect05 Prog Dev
14/27TAB4333 14
Symbolic Constants
The equ directive can be used to assign asymbolic name to a constant:
ValueToAdd equ 40550
If the default radix has been set to hex, the aboveresults in the symbolic name ValueToAdd beingequated with 0x9E66.
The MSB or LSB of a symbolic constant can be
obtained with the high or low directive:
high ValueToAdd ; gets 0x9E
low ValueToAdd ; gets 0x66
Bignum
7/30/2019 Lect05 Prog Dev
15/27TAB4333 15
Looptime Subroutine Code
Looptime btfss INTCON, TMR0IF ;chk if Intr flag is set (10ms passed)
bra Looptime ;no! check again
movff INTCON, INTCOPY ;yes, preserve INTCON contents and
bcf INTCON, GIEH ;disable all interrupts
;reset time value
movf INTCOPY, W ;restore INTCON contents and
andlw B1000000 ;enble all interrupts
iorwf INTCON, F ;double check all interrupts are enable
bcf INTCON, TMR0IF ;clear the Intr flag
return ;returns to the main
7/30/2019 Lect05 Prog Dev
16/27TAB4333 16
Macros
Macros are used to create single-linepseudo-instructions that expand tocommonly-used multi-instruction
sequences. Macro definitions have the form:
macro
endm
7/30/2019 Lect05 Prog Dev
17/27TAB4333 17
Macros A macro can be definedto move a literal into a specified register
(with the side effect of trashing the W value):movlf macro literal, dest
movlw literal
movwf dest
endm
The macro MOVLF defined above can be invokedby using it like
an instruction:
movlf 17, NUM
On assembly, the macro will be expanded to act as if we had
written:
movlw 17
movwf NUM
7/30/2019 Lect05 Prog Dev
18/27TAB4333 18
BlinkAlive Subroutine
It is often useful during development to have an LED that does nothing
but blink regularly when the code is running.
Toggling the LED every 10 ms would be too fast for the eye to see.
To make the LED come on every 1 second requires the BlinkAlive
subroutine to maintain a count variable that counts from 100 downto 0.
Why 100?
BlinkAlive bsf PORTA, RA4 ;off LED
decf ALIVECNT, F ;counter 1
bnz BAend ;counter != 0
movlf 100, ALIVECNT ;reset counter
bcf PORTA, RA4 ;on LED
BAend return
(Note: ALIVECNT is a user-defined variable)
invoke macro
7/30/2019 Lect05 Prog Dev
19/27
TAB4333 19
Using $
The $ symbol refers to the program address
of the current instruction.
An infinite loop can be generated using:
goto $
The previous instruction is equivalent to:
Loop goto Loop
7/30/2019 Lect05 Prog Dev
20/27
TAB4333 20
Macro Failure with Fixed Labels If we define the macro:
INFLOOP macro
FixLabelloop loop Fixlabeloopendm
The second invocation of INFLOOP will fail because of repeated fixed
label use:
INFLOOP ; OK
INFLOOP ; fails due to reuse of Fixlabel
If we define the macro:
INFLOOP macro
loop $
endm Repeated use of INFLOOP works:
INFLOOP ; OK
INFLOOP ; OK
is ok with $
7/30/2019 Lect05 Prog Dev
21/27
TAB4333 21
Parameters on List Directive Programs in the book start with:
list P=PIC18F452, F=INHX32, C=160, N=0, ST=OFF, MM=OFF,R=DEC, X=ON
The important parameters are:
P=PIC18F452processor is PIC18F452
F=INHX32 output is Intel HEX file
R=DEC default radix is decimal
The other parameters all have to do with what is in the list file (ref
pg66 or MPASM user manual).
Org Directive
The org assembler directive says to locate the following code in programmemory starting at the address specified:
org 0x1000 ; assemble starting at 0x1000
btg PORTC, RC2 ; the program address of this instruction is 0x1000
7/30/2019 Lect05 Prog Dev
22/27
TAB4333 22
Processor Include File
All of the symbolic names associated with SFRs and bits in SFRs are
defined in the processor-dependent include file:
#include P18F452.inc This include file is distributed by Microchip with its free MPLAB
development environment.
Configuration Bits The desired configuration bits can be included in the HEX file outputusing the __CONFIG command (note that it starts with a double
underscore):
__CONFIG , &
Only those bits that need to be different than the default need to be
specifed.
7/30/2019 Lect05 Prog Dev
23/27
TAB4333 23
Oscillator Configuration The high speed oscillator is selected using the _HS_OSC_1H bit of the
_CONFIG1H configuration word
__CONFIG _CONFIG1H, _HS_OSC_1H
This oscillator will result in a program clock that is as fast as the
crystal input.
Configure PWRT and BOR
The power-up reset timer (PWRT) and brown-out reset (BOR) could
be turned on using:
__CONFIG _CONFIG2L,_PWRT_ON_2L & _BOR_ON_2L
Dont worry about what PWRT and BOR are now.
7/30/2019 Lect05 Prog Dev
24/27
TAB4333 24
Automatic Register Assignment
The cblock directive automatically assigns variables sequentially to
registers starting at a give register number:
cblock 0X000 ; start at register 0x000
Variable1 ; Variable1 = 0x000
Variable2 ; Variable2 = 0x001
endc ; end of cblock
Vector Addresses
Reset vector (address executed at reset):
0x0000 High priority interrupt vector:
0x0008
Low priority interrupt vector:
0x0018
7/30/2019 Lect05 Prog Dev
25/27
TAB4333 25
Putting all together
All the preceding bits and pieces can be put together to
form a complete assembly language program. Refer tothe code in page 61.
Note the followings:
the general format
initialization of registers & ports
the syntax of instruction
the declaration/definition of data/variables
the macros
the directives
interrupt vectors
7/30/2019 Lect05 Prog Dev
26/27
TAB4333 26
What is this program doing?
movlw 6movwf COUNT
clrf SCRATCH
repeat nop
incf SCRATCH
nop
decfsz COUNT,f
goto repeat
7/30/2019 Lect05 Prog Dev
27/27
What is the time delay generated
if the crystal used is 10 MHz?
MOVLW 0X08MOVWF T0CON
HERE MOVLW 0X76
MOVWF TMR0H
MOVLW 0X34
MOVWF TMR0L
BCF INTC0N,TMR0IF
CALL DELAY
BRA HERE
DELAY BSF T0CON,TMR0ON
AGAIN BTFSS INTCON,TMR0IF
BRA AGAIN
BCF T0CON,TMR0ON
RETURN
;16-bit, int clock, no prescaler