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

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

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 16 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 16 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 16 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 16 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 16 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 16 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

EE 308 Spring 2010What Happens When You Reset the MC9S12?• What happens to the MC9S12 when you turn on power or push the reset button?• How does the MC9S12 know which instruction to execute first?• On reset the MC9S12 loads the PC with the address located at address 0xFFFE and0xFFFF.• 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 FFFF0 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 locatedat address 0xF000.1EE 308 Spring 2010The 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 ControlRegister 2 (TSCR2) Register at address 0x004D.slow down the clock:2.7307 ms will be too short if you want to see lights flash.PR2:0 Divide Freq Overflow Rate000 1 24 MHz 2.7307 ms001 2 12 MHz 5.4613 ms010 4 6 MHz 10.9227 ms011 8 3 MHz 21.8453 ms100 16 1.5 MHz 43.6907 ms101 32 0.75 MHz 87.3813 msYou can slow down clock by dividing it before you send it to110 64 0.375 MHz 174.7627 ms111 128 0.1875 MHz 349.5253 msthe 16−bit counter. By setting prescaler bits PR2,PR1,PR0 of TSCR2 you canbset TSCR1,#$80staa TSCR2TSCR1 = TSCR1 | 0x80;To set up timer so it will overflow every 87.3813 ms:ldaa #$05 TSCR2 = 0x05; VCC16−Bit CounterTIMER OVERFLOW INTERRUPTPrescalerTENPR[2..0]OverflowD QTOFReadRWriteTOF(Bit 7 of TFLG2, addr 0x4F)24 MHzTCNT (addr 0x44)(Bit 7 of TFLG2, addr 0x4F)(Bit 7 of TSCR1, addr 0x46)(Bits 2−0 of TSCR2, addr 0x4D)2EE 308 Spring 2010Using the Timer Overflow Flag to implement a delay• The MC9S12 timer counts at a rate set by the prescaler:PR2:0 Divide Clock Clock OverflowFreq Period Period000 1 24 MHz 0.042 µs 2.73 ms001 2 12 MHz 0.083 µs 5.46 ms010 4 6 MHz 0.167 µs 10.92 ms011 8 3 MHz 0.333 µs 21.85 ms100 16 1.5 MHz 0.667 µs 43.69 ms101 32 750 kHz 1.333 µs 87.38 ms110 64 375 kHz 2.667 µs 174.76 ms111 128 187.5 kHz 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 bitsof 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 e xit the delay subroutine after 10.92 ms havepassed.• Problem: Program cannot do anything while waiting• Solution: Have timer generate an interrupt. Program can do other things; automati-cally switches to service interrupt when interrupt occurs3EE 308 Spring 2010How to generate an interrupt when the timer overflowsTake care of eventTo generate a TOF interrupt: Inside TOF ISR:Enable interrupts (clear I bit of CCR)Clear TOF flag (Write 1 to Bit 7 of TFLG2)Return with RTIEnable timer (set Bit 7 of TSCR1)Set prescaler (Bits 2:0 of TSCR2)Enable TOF interrupt (set Bit 7 of TSCR2)OverflowDRInterruptI BitCCRTOI BitQVCC16−Bit CounterTIMER OVERFLOW INTERRUPTWriteTOFRead(Enable by clearing I bit with CLI instr)PrescalerTENPR[2..0]TOFP Clock24 MHz(Bit 7 of TSCR1, addr 0x46)(Bits 2−0 of TSCR2, addr 0x4D)TCNT (addr 0x44)(Bit 7 of TFLG2, addr 0x4F)(Bit 7 of TFLG2, addr 0x4F)TSCR2(Bit 7 of TSCR2, addr 0x4D)(Enable by setting Bit 7 of TSCR2)#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 */}4EE 308 Spring 2010How to tell the MC9S12 where the Interrupt Service Routine is located• You need to tell the MC9S12 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. T his is in flash EPROM, and is verydifficult 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, theMC9S12 initially executes code starting at 0xFFDE. This code tells the MC9S12 toload the program counter with the address in 0x3E5E. Because this address in 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 yourprogram.5EE 308 Spring 2010How to Use Interrupts in Assembly Programs• For our assembler, you can set the interrupt vector by including the file vectors12.inc.In this file, the addresses of all of the MC9S12 interrupt vectors are defined.• For example, the pointer to the Timer Overflow Interrupt vector is called UserTimerOvf:UserTimerOvf equ $3E5EYou can set the interrupt vector to point to the interrupt service routine toi_isrwith the Assembly statement:movw #toi_isr,UserTimerOvf6EE 308 Spring 2010• Here is a program where the interrupt vector is set in the program:include ’derivative.inc’include "vectors12.inc"prog: equ $2000org progmovw #toi_isr,UserTimerOvf ; Set interrupt vectormovb #$ff,DDRPbset PTP,#$0fbset DDRJ,#$02bclr PTJ,#$02movb #$ff,DDRB ; Port B outputmovb #$80,TSCR1 ; Turn on timermovb #$86,TSCR2 ; Enable timer overflow interrupt, set prescaler; so interrupt period is 175 msmovb #$80,TFLG2 ; Clear timer interrupt flagcli ; Enable interruptsl1: wai ; Do nothing - go into low power mode */bra l1toi_isr:inc PORTBmovb #$80,TFLG2 ; Clear timer overflow interrupt flagrti• When the MC9S12 receives a Timer Overflow Interrupt, it finishes the current instruc-tion, puts return address and all registers on the stack, sets the I bit of the CCR todisable interrupts, then loads the contents of UserTimerOvf (0x3E5E) into the PC• After executing the ISR, the rti instruction pulls the registers off the stack, and loadsthe PC with the return address – the program resumes from where it received theinterrupt7EE 308 Spring


View Full Document

NMT EE 308 - What Happens When You Reset the MC9S12

Documents in this Course
Load more
Download What Happens When You Reset the MC9S12
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 MC9S12 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 MC9S12 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?