DOC PREVIEW
NMT EE 308 - EE 308 - Course Guide

This preview shows page 1-2-20-21 out of 21 pages.

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

Unformatted text preview:

EE 308 Spring 2011 • More on Programming the 9S12 in C • Huang Sections 5.2 through 5.4 • Introduction to the MC9S12 Hardware Subsystems • Huang Sections 8.2-8.6 • ECT_16B8C Block User Guide • A summary of MC9S12 hardware subsystems • Introduction to the MC9S12 Timer subsystem • Resets on the HCS12 • Introduction to Interrupts on the Mc9s12 • Huang Sections 6.1-6.3 o The MC9S12 has a 16-bit free-running counter to determine the time and event happens, and to make an event happen at a particular time o The counter is normally clocked with an 8 MHz clock o The Timer Overflow (TOF) bit –when the time rolls over from 0x0000 to 0xFFFF it sets a flip-flop to show that it has happened o The time Prescaler (PR2:0) bit of Timer Interrupt Mask 2 (TMSK2) register: Allows you to change the frequency of the clock driving the 16-bit counter o What happens when you reset the HCS12/ o Introduction to InterruptsEE 308 Spring 2011 Hello, World! • Here is the standard ”hello, world” program: #include <stdio.h> main() { printf("hello, world\r\n"); } • To write the ”hello, world” program, you need to use the printf() function. • The printf() function is normally a library function • In CodeWarrior, you can access printf() by doing the following: 1. In your C program, add the following lines: #include <stdio.h> #include <termio.h> 2. In CodeWarrior, select Project, Add Files, and select the file termio.c. This is in the CodeWarrior library, which is in the following location on my computer: c:\Program Files\Freescale\CodeWarrior for S12(X) V5.0\lib\hc12c\src Your C program will look like this: #include <stdio.h> #include <termio.h> main() { printf("hello, world\r\n"); __asm(swi); } • The above program is about 1,500 bytes long.EE 308 Spring 2011 • You can print out variables as well. Here is an example: #include <stdio.h> #include <termio.h> main() { int i; for (i=0;i<100;i++) printf("i = %d\r\n",i); __asm(swi); }EE 308 Spring 2011 Introduction to the MC9S12 Timer Subsystem • The MC9S12 has a 16-bit counter that normally runs with a 24 MHz clock. • Complete information on the MC9S12 timer subsystem can be found in the ECT 16B8C Block User Guide. ECT stands for Enhanced Capture Timer. • When you reset the MC9S12, the clock to the timer subsystem is initially turned off to save power. – To turn on the clock you need to write a 1 to Bit 7 of register TSCR1 (Timer System Control Register 1) at address 0x0046. • The clock starts at 0x0000, counts up (0x0001, 0x0002, etc.) until it gets to 0xFFFF. It rolls over from 0xFFFF to 0x0000, and continues counting forever (until you turn the counter off or reset the MC9S12). • It takes 2.7307 ms (65,536 counts/24,000,000 counts/sec) for the counter to count from 0x0000 to 0xFFFF and roll over to 0x0000. • To determine the time an event happens, you can read the value of the clock (by reading the 16-bit TCNT (Timer Count Register) at address 0x0044.EE 308 Spring 2011 Timer inside the MC9S12: When you enable the timer (by writing a 1 to bit 7 of TSCR1), you connect a 24−MHz oscillator to a 16−bit counter. You can read the counter at address TCNT. The counter will start at 0, will count to 0xFFFF, then will roll over to 0x0000. It will take 2.7307 ms for this to happen.EE 308 Spring 2011 To enable timer on MC9S12, set Bit 7 of register TCSR1: bset TSCR1,#$80 TSCR1 = TSCR1 | 0x80;EE 308 Spring 2011EE 308 Spring 2011EE 308 Spring 2011 • To put in a delay of 2.7307 ms, you could wait from one reading of 0x0000 to the next reading of 0x0000. • Problem: You cannot read the TCNT register quickly enough to make sure you will see the 0x0000. To put in a delay for 2.7307 ms, could watch timer until TCNT == 0x0000: bset TSCR1,#$80 TSCR1 = TSCR1 | 0x80; l1: ldd TCNT while (TCNT != 0x0000) ; bne l1 Problem: You might see 0xFFFF and 0x0001, and miss 0x0000 • Solution: The MC9S12 has built-in hardware with will set a flip-flop every time the counter rolls over from 0xFFFF to 0x0000. • To wait for 2.7307 ms, just wait until the flip-flop is set, then clear the flip-flop, and wait until the next time the flip-flop is set. • You can find the state of the flip-flop by looking at bit 7 (the Timer Overflow Flag (TOF) bit) of the Timer Flag Register 2 (TFLG2) register at address 0x004F. • You can clear the flip-flop by writing a 1 to the TOF bit of TFLG2. Solution: When timer overflows, it latches a 1 into a flip−flop. Now when timer overflows (goes from 0xFFFF to 0x0000), Bit 7 of TFLG2 register is set to one. Can clear register by writing a 1 to Bit 7 of TFLG register. (Note: Bit 7 of TFLG2 for a read is different than Bit 7 of TFLG2 for a write)EE 308 Spring 2011 bset TSCR1,#$80 ; Enable timer l1: brclr TFLG2,#$80,l1 ; Wait until Bit 7 of TFLG2 is set ldaa #$80 … program … … staa TFGL2 ; Clear TOF flag TSCR1 = TSCR1 | 0x80; //Enable timer while ((TFLG2 & 0x80) == 0) ; // Wait for TOF … program … … TFLG2 = 0x80; // Clear TOFEE 308 Spring 2011EE 308 Spring 2011 • Another problem: Sometimes you may want to delay longer than 2.7307 ms, or time an event which takes longer than 2.7307 ms. This is hard to do if the counter rolls over every 2.7307 ms. • Solution: 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 2011EE 308 Spring 2011EE


View Full Document

NMT EE 308 - EE 308 - Course Guide

Documents in this Course
Load more
Download EE 308 - Course Guide
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 EE 308 - Course Guide 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 EE 308 - Course Guide 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?