Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Lect05 Prog Dev

Download as pdf or txt
Download as pdf or txt
You are on page 1of 27

Embedded Systems TAB4333

Chapter 5 Programs Development

TAB4333

Timer 0 will be used as example in program development. 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 input or as a general-purpose input/output pin.

Timer 0

TAB4333

Timer 0 Features
Freq < internal clock N = 1,2,4,8, , 256 TMR0H (8-bit) TMR0L (8-bit) prescaler ( N)

synchronizer

presettable & readable counters TMR0IF (flag set on overflow)

internal clock (2.5 MHz)

T0CKI (RA4) Input

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?
TAB4333

If input = 40KHz N=?

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) TMR0ON Timer 0 on/off 2) T08BIT Timer 0 8-bit/16-bit 3) T0CS Timer 0 clock select 4) PSA Prescaler use/bypass
TAB4333 4

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-significant bit (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 from older PIC C (which only had an 8-bit Timer 0). To set Timer 0 to 16-bit mode we could use: bcf T0CON, T08BIT

TAB4333

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

TAB4333

Timer 0 Full Setup


To enable Timer 0 in 16-bit mode using the internal clock and bypass the prescaler we could write: movlw B10001000 movwf T0CON TMR0ON = 1, T08BIT = 0, T0CS = 0, PSA = 1 (only underlined bits matter)
TAB4333 7

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 Why add? 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
TAB4333 8

Add a Value to a Running Timer


Instructions (TMR0LCOPY, TMR0HCOPY, and ValueToAdd are defined in user code):
movff movff movlw addwf movlw addwfc movff movff TMR0L, TMR0LCOPY TMR0H, TMR0HCOPY low ValueToAdd TMR0LCOPY, F high ValueToAdd TMR0HCOPY, F TMR0HCOPY, TMR0H TMR0LCOPY, TMR0L

How many cycles?

TAB4333

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.

TAB4333

10

Timer 0 Rollover
All PIC timers count up (increment) only and never down (decrement). Timer 0 sets the TMR0IF (Timer 0 interrupt flag) bit of the INTCONT (interrupt control) SFR when the timer counts from 0xFFFF to 0x0000 (regardless of whether interrupts are used or not). We can skip the next instruction on Timer 0 rollover using: btfss INTCON, TMR0IF

TAB4333

11

Typical Program Structure


Ignoring interrupts, most PIC programs look like:
Initialize; Loop forever { Do tasks; Wait until looptime has expired }
Ref pg 66 lines 55 to 61
TAB4333 12

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 10 milliseconds per rollover. To get 25,000 counts per rollover, we must skip the remaining 65,536 25,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.

TAB4333

13

Symbolic Constants
The equ directive can be used to assign a symbolic name to a constant:
Bignum

ValueToAdd equ 40550

If the default radix has been set to hex, the above results in the symbolic name ValueToAdd being equated 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
TAB4333 14

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 <Add value to Timer 0> ;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

TAB4333

15

Macros
Macros are used to create single-line pseudo-instructions that expand to commonly-used multi-instruction sequences. Macro definitions have the form: <macro name> macro <parameters> <macro body> endm

TAB4333

16

Macros
A macro can be defined to 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 invoked by 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

TAB4333

17

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

invoke macro

bsf decf bnz movlf bcf

PORTA, RA4 ;off LED ALIVECNT, F ;counter 1 BAend ;counter != 0 100, ALIVECNT ;reset counter PORTA, RA4 ;on LED

BAend return (Note: ALIVECNT is a user-defined variable)

TAB4333

18

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

TAB4333

19

Macro Failure with Fixed Labels


If we define the macro: INFLOOP macro FixLabelloop loop Fixlabeloop endm 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 is ok with $ loop $ endm Repeated use of INFLOOP works: INFLOOP ; OK INFLOOP ; OK
TAB4333 20

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=PIC18F452 processor 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 program memory starting at the address specified:

org 0x1000 ; assemble starting at 0x1000 btg PORTC, RC2 ; the program address of this instruction is 0x1000
TAB4333 21

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 output using the __CONFIG command (note that it starts with a double underscore): __CONFIG <config_word>, <bit> & <bit>

Only those bits that need to be different than the default need to be specifed.

TAB4333

22

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.

TAB4333

23

Automatic Register Assignment


The cblock directive automatically assigns variables sequentially to registers starting at a give register number:
cblock Variable1 Variable2 endc 0X000 ; start at register 0x000 ; Variable1 = 0x000 ; Variable2 = 0x001 ; end of cblock

Vector Addresses
Reset vector (address executed at reset): 0x0000 High priority interrupt vector: 0x0008 Low priority interrupt vector: 0x0018
TAB4333 24

Putting all together


All the preceding bits and pieces can be put together to form a complete assembly language program. Refer to the 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

TAB4333

25

What is this program doing?


movlw movwf clrf nop incf nop decfsz goto 6 COUNT SCRATCH SCRATCH COUNT,f repeat

repeat

TAB4333

26

What is the time delay generated if the crystal used is 10 MHz?


MOVLW MOVWF MOVLW MOVWF MOVLW MOVWF BCF CALL BRA BSF BTFSS BRA BCF RETURN 0X08 ;16-bit, int clock, no prescaler T0CON 0X76 TMR0H 0X34 TMR0L INTC0N,TMR0IF DELAY HERE T0CON,TMR0ON INTCON,TMR0IF AGAIN T0CON,TMR0ON

HERE

DELAY AGAIN

TAB4333

27

You might also like