DOC PREVIEW
NMT EE 308 - USING INPUT CAPTURE ON THE 9S12

This preview shows page 1-2-3 out of 10 pages.

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

Unformatted text preview:

• The 9S12 Input Capture Function • Huang Sections 8.1-8.5 • ECT_16B8C Block User Guide o Interrupts on the 9S12 o Capturing the time of an external event o The 9S12 Input Capture Function o Registers used to enable the Input Capture Function o Using the 9S12 Input Capture Function o A program to use the 9S12 Input Capture in polling mode o Using the Keyword volatile in C o Using D-Bug12 Routines to Print Information to the Terminal o A program to use the 9S12 Input Capture in interrupt mode USING INPUT CAPTURE ON THE 9S12Input Capture: Connect a digital signal to a pin of Port T. Can capturethe time of an edge (rising, falling or either) – the edge will latch the valueof TCNT into TCx register. This is used to measure the difference betweentwo times.To use Port T Pin x as an input capture pin:1. Turn on timer subsystem (1 -> Bit 7 of TSCR1 reg)2. Set prescaler (TSCR2 reg). To get most accuracy set overflow rate as small as possible, but larger than the maximum time difference you need to measure.3. Set up PTx as IC (0 -> bit x of TIOS reg)4. Set edge to capture (EDGxB EDGxA of TCTL 3-4 regs)EDGxB EDGxA0 0 Disabled0 1 Rising Edge1 0 Falling Edge1 1 Either Edge5. Clear flag (1 -> bit x of TFLG1 reg, 0 -> all other bits of TFLG1)6. If using interrupts(a) Enable interrupt (1 -> bit x of TIE reg)(b) Clear I bit of CCR (cli or enable())(c) In interrupt service routine,i. Read time of edge from TCxii. Clear flag (1 -> bit x of TFLG1 reg, 0 -> all other bits of TFLG1)7. If polling in main program(a) Wait for Bit x of TFLG1 to become set(b) Read time of edge from TCx(c) Clear flag (1 -> bit x of TFLG1 reg, 0 -> all other bits of TFLG1)/* Program to determine the time between two rising edges using the ** 9S12 Input Capture subsystem*/#include "hcs12.h"#include "DBug12.h"unsigned int first, second, time;main(){TSCR1 = 0x80; /* Turn on timer subsystem */TSCR2 = 0x05; /* Set prescaler for divide by 32 *//* 87.38 ms overflow time *//* Setup for IC1 */TIOS = TIOS & ~0x02; /* IOC1 set for Input Capture */TCTL4 = (TCTL4 | 0x04) & ~0x08; /* Capture Rising Edge */TFLG1 = 0x02; /* Clear IC1 Flag *//* Setup for IC2 */TIOS = TIOS & ~0x04; /* IOC2 set for Input Capture */TCTL4 = (TCTL4 | 0x10) & ~0x20; /* Capture Rising Edge */TFLG1 = 0x04; /* Clear IC2 Flag *//* Get first rising edge */while ((TFLG1 & 0x02) == 0) ; /* Wait for 1st rising edge; */first = TC1; /* Read time of 1st edge; *//* Capture 2nd rising edge */while ((TFLG1 & 0x04) == 0) ; /* Wait for 2nd rising edge; */second = TC2; /* Read time of 2nd edge; */time = second - first; /* Calculate total time */DB12FNP->printf("time = %d cycles\n",time);asm(" swi");}Using the Keyword volatile in C• Consider the following code fragment, which waits until an event occurs on Pin 2 of PORTT:#define TRUE 1#define FALSE 0#include "hcs12.h"#include "DBug12.h"#include "vectors12.h"#define enable() asm(" cli")void INTERRUPT tic2_isr(void);unsigned int time, done;main(){/* Code to set up Input Capture 2 */TFLG1 = 0x04; /* Clear CF2 */UserTimerCh2 = (short) &tic2_isr; /* Set interrupt vector */enable(); /* Enable Interrupts */done = FALSE;while (!done) ;asm( "swi");}void INTERRUPT tic2_isr(void){time = TC2;TFLG1 = 0x04;done = TRUE;}• An optimizing compiler knows that done will not change in the main() function. It may decide that, since done is FALSE in the main() function, and nothing in the main() function changes the value of done, then done will always be FALSE, so there is no need to check if it will ever become TRUE.• An optimizing compiler might change the linewhile (!done) ;towhile (TRUE) ;and the program will never get beyond that line.• By declaring done to be volatile, you tell the compiler that the value of done might change somewhere else other than in the main() function (such as in an interrupt service routine), and the compiler should not optimize on the done variable.volatile unsigned int time, done; • If a variable can change its value outside the normal flow of the program (i.e., inside an interrupt service routine), declare the variable to be of type volatile.Using D-Bug12 Routines to Print Information to the TerminalD-Bug12 has several built-in C routines. Descriptions of these can be found in D-BUG12 V4.x.x Reference Guide. To use these routines you need to include the header file DBug12.h. These work like oridnary C functions, but you call them with pointers to the routines in D-Bug12. For example, you would call the putchar() function with the following line of C code:DB12FNP->putchar(c);Here is a C program to print Hello, world! to the terminal:#include "DBug12.h"void main(void){DB12FNP->printf("Hello, world!\n\r");}Here is a program to print a number to the terminal in three different forms:#include "DBug12.h"void main(void){unsigned int i;i = 0xf000;DB12FNP->printf("Hex: 0x%04x, Unsigned: %u, Signed: %d\n\r",i,i,i);}The output of the above program will be:Hex: 0xf000, Unsigned: 61440, Signed: -4096Program to measure the time between two rising edges, and print out the result/* Program to determine the time between two rising edges using the 9S12 Input Capture * subsystem.* This program uses interrupts to determine when the two edges have occurred.*/#include "hcs12.h"#include "DBug12.h"#include "vectors12.h"#define enable() asm("cli")#define stop() asm("swi");/* Function Prototypes */void INTERRUPT tic1_isr(void);/* Declare things changed inside ISR as volatile */volatile unsigned int first, second, time;volatile unsigned char count=0;main(){ /* Turn on timer subsystem */ /* Set prescaler to 350 ms */ TSCR1 = 0x80; TSCR2 = 0x07; /* Setup for IC1 */ TIOS = TIOS & ~0x02; /* Configure PT1 as IC */ TCTL4 = (TCTL4 | 0x04) & ~0x08; /* Capture Rising Edge */ TFLG1 = 0x02; /* Clear IC1 Flag */ /* Set interrupt vector for Timer Channel 1 */ UserTimerCh1 = (short) &tic1_isr; TIE = TIE | 0x02; /* Enable IC1 Interrupt */ /* Enable interrupts by clearing I bit of CCR */ enable(); while (count < 2) { asm("wai"); /* Low power mode while waiting */ } time = first - second; /* Calculate total time */ DB12FNP->printf("delta time = %d cycles\r\n",time); /* print dt */;stop();}void INTERRUPT tic1_isr(void){ if(count == 0) { first = TC1; count = 1; DB12FNP->printf("first = %u \r\n",first); } else { second = TC1; count = 2;


View Full Document

NMT EE 308 - USING INPUT CAPTURE ON THE 9S12

Documents in this Course
Load more
Download USING INPUT CAPTURE ON THE 9S12
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 USING INPUT CAPTURE ON THE 9S12 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 USING INPUT CAPTURE ON THE 9S12 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?