DOC PREVIEW
NMT EE 308 - What Happens When You Reset the HCS12

This preview shows page 1-2-3-4-5-6 out of 18 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

EE 308 Spring 2011 • Interrupts and the Timer Overflow Interrupts • Huang Sections 6.1-6.4 o Using the Timer Overflow Flag to interrupt a delay o Introduction to Interrupts o How to generate an interrupt when the timer overflows o How to tell the MC9S12 where the ISR is located o Using interrupts on the HC12 o The MC9S12 registers and stack when a TOF interrupt is received o The MC9S12 registers and stack after a TOF interrupt is received o Interrupt vectors for the MC9S12 o Using interrupts on the MC9S12: Assembly and C What Happens When You Reset the HCS12? • What happens to the HCS12 when you turn on power or push the reset button? • How does the HCS12 know which instruction to execute first? • On reset the HCS12 loads the PC with the address located at address 0xFFFE and 0xFFFF. • Here is what is in the memory of our MC9S12: 0 1 2 3 4 5 6 7 8 9 A B C D E F FFF0 F6 EC F6 F0 F6 F4 F6 F8 F6 FC F7 00 F7 04 F0 00 • On reset or power-up, the first instruction your MC9S12 will execute is the one located at address 0xF000.EE 308 Spring 2011 The MC9S12 Timer • The MC9S12 has a 16-bit free-running counter (timer). • The MC9S12 allows you to slow down the clock which drives the counter. • You can slow down the clock by dividing the 24 MHz clock by 2, 4, 8, 16, 32, 64 or 128. • You do this by writing to the prescaler bits (PR2:0) of the Timer System Control Register 2 (TSCR2) Register at address 0x004D. 2.7307 ms will be too short if you want to see lights flash. You can slow down clock by dividing it before you send it to the 16−bit counter. By setting prescaler bits PR2, PR1, PR0 of TSCR2 you can slow down the clock: PR Divide Freq Overflow Rate 000 1 24 MHz 2.7307 ms 001 2 12 MHz 5.4613 ms 010 4 6 MHz 10.9227 ms 011 8 3 MHz 21.8453 ms 100 16 1.5 MHz 43.6907 ms 101 32 0.75 MHz 87.3813 ms 110 64 0.375 MHz 174.7627 ms 111 128 0.1875 MHz 349.5253 ms To set up timer so it will overflow every 87.3813 ms: bset TSCR1,#$80 ldaa #$05 staa TSCR2 TSCR1 = TSCR1 | 0x80; TSCR2 = 0x05;EE 308 Spring 2011EE 308 Spring 2011 Using the Timer Overflow Flag to implement a delay • The MC9S12 timer counts at a rate set by the prescaler: PR2:0 Divide Clock Freq Clock Period Overflow Period 000 1 24 MHZ 0.042 µs 2.73 ms 001 2 12 MHZ 0.083 µs 5.46 ms 010 4 6 MHZ 0.167 µs 10.92 ms 011 8 3 MHZ 0.333 µs 21.85 ms 100 16 1.5 MHZ 0.667 µs 43.69 ms 101 32 750 MHZ 1.333 µs 87.38 ms 110 64 375 MHZ 2.667 µs 174.76 ms 111 128 187.5 MHZ 5.333 µs 349.53 ms • When the timer overflows it sets the TOF flag (bit 7 of the TFLG2 register). • To clear the TOF flag write a 1 to bit 7 of the TFLG2 register, and 0 to all other bits of TFLG2: TFLG2 = 0x80; • You can implement a delay using the TOF flag by waiting for the TOF flag to be set, then clearing it: void delay(void) { while ((TFLG2 & 0x80) == 0) ; /* Wait for TOF */ TFLG2 = 0x80; /* Clear flag */ } • If the prescaler is set to 010, you will exit the delay subroutine after 10.92 ms have passed. Problem: Can’t do anything else while waiting. Solution: Have timer generate an interrupt. Program can do other things; automatically switches to service interrupt when interrupt occurs.EE 308 Spring 2011 How to generate an interrupt when the timer overflows To generate a TOF interrupt: Enable timer (set Bit 7 of TSCR1) Set prescaler (Bits 2:0 of TSCR2) Enable TCF interrupt (set Bit 7 of TSCR2) Enable interrupts (clear I bit of CCR) Inside TOF ISR: Take care of event Clear TOF flag (Write 1 to Bit 7 of TFLG2) Return with RTI #include "derivative.h" main() { DDRB = 0xff; /* Make Port B output */ TSCR1 = 0x80; /* Turn on timer */ TSCR2 = 0x85; /* Enable timer overflow interrupt, set prescaler */ TFLG2 = 0x80; /* Clear timer interrupt flag */ enable(); /* Enable interrupts (clear I bit) */ while (1) { /* Do nothing */ } } interrupt void toi_isr(void) { PORTB = PORTB + 1; /* Increment Port B */ TFLG2 = 0x80; /* Clear timer interrupt flag */ }EE 308 Spring 2011 How to tell the HCS12 where the Interrupt Service Routine (ISR) is located • You need to tell the HCS12 where to go when it receives a TOF interrupt. • You do this by setting the TOF Interrupt Vector. • The TOF interrupt vector is located at 0xFFDE. This is in flash EPROM, and is very difficult to change — you would have to modify and reload DBug-12 to change it. • DBug-12 redirects the interrupts to a set of vectors in RAM, from 0x3E00 to 0x3E7F. The TOF interrupt is redirected to 0x3E5E. When you get a TOF interrupt, the HCS12 initially executes code starting at 0xFFDE. This code tells the HCS12 to load the program counter with the address in 0x3E5E. Because this address in RAM, you can change it without having to modify and reload DBug-12. • Because the redirected interrupt vectors are in RAM, you can change them in your program.EE 308 Spring 2011 How to Use Interrupts in Assembly Programs • For our assembler, you can set the interrupt vector by including the file hcs12.inc. In this file, the addresses of all of the 9212 interrupt vectors are defined. • For example, the pointer to the Timer Overflow Interrupt vector is called UserTimerOvf: UserTimerOvf equ $3E5E You can set the interrupt vector to point to the interrupt service routine toi_isr with the Assembly statement: movw #toi_isr,UserTimerOvfEE 308 Spring 2011 • Here is a program where the interrupt vector is set in the program: include ’derivative.inc’ include "vectors12.inc" prog: equ $2000 org prog movw #toi_isr,UserTimerOvf ; Set interrupt vector movb #$ff,DDRP bset PTP,#$0f bset DDRJ,#$02 bclr PTJ,#$02 movb #$ff,DDRB ; Port B output movb #$80,TSCR1 ; Turn on timer movb #$86,TSCR2 ; Enable timer overflow interrupt, set prescaler ; so interrupt period is 175 ms movb #$80,TFLG2 ; Clear timer interrupt flag cli ; Enable interrupts l1: wai ; Do nothing - go into low power mode */ bra l1 toi_isr: inc PORTB movb #$80,TFLG2 ; Clear timer overflow interrupt flag rti • When the MC9S12 receives a Timer Overflow Interrupt, it finishes the current instruction, puts return address and all registers on the stack, sets the I bit of the CCR to disable interrupts, then loads the contents of UserTimerOvf (0x3E5E) into the


View Full Document

NMT EE 308 - What Happens When You Reset the HCS12

Documents in this Course
Load more
Download What Happens When You Reset the HCS12
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view What Happens When You Reset the HCS12 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view What Happens When You Reset the HCS12 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?