### EE 308

Solutions to Exam 2

1. Consider the programmable timer in a 68HC11 operating at 2 MHz.

1. What is the shortest interval of time the timer can measure? That is, how often is the timer clocked?

• The timer is connected to the E-clock through the pre-scaler. With a 2 MHz E-clock, 500 ns is the shortest interval of time.

2. This interval can be changed. What are the possible choices? What special consideration must be observed when changing the timer rate?

• On Page 64 of the M68HC11 E Series Programming Reference Guide, you can divide (slow down) the clock by 1, 4, 8 or 16, so the possible values are 500 ns, 2 us, 4 us, or 8 us.
• This must be done in the first 64 clock cycles, so should be the first thing in your program.

3. What is the maximum time that can be dealt with before the timer's counter cycles back to where it started?

• The timer is a 16-bit register, so cycles after 65,536 counts. The longest time interval is 65,536 x 8 us = 524.888 ms.

4. Write some C code to increase the timer's clock interval by a factor of 4.

• Set Bit 0 and clear Bit 1 of TMSK2:
`TMSK2 = TMSK2 | 0x01;`
`TMSK2 = TMSK2 & ~0x02;`

5. How are flags cleared in the timer subsystem?

• Write a 1 to the flag to be cleared, and 0s to all other bits of the flag register.

6. Write some C code to clear the Timer Overflow flag.

• Write a 1 to Bit 7 of TFLG2:
`TFLG2 = 0x80;`

2. Here are some questions about using interrupts on the HC11:
1. List at least six things you need to do in software to use interrupts on the HC11.

1. Initialize Stack Pointer
2. Write an Interrupt Service Routine -- be sure to clear the flag which caused the interrupt in the ISR, and exit with an rti instruction.
3. Do setup required to use interrupt.
4. Set interrupt vector to point to your ISR.
5. Enable specific interrupt -- set interrupt bit in mask register.
6. Clear flag for specific interrupt.
7. Enable interrupts in general -- clear I bit of CCR.

2. What happens to the HC11's stack, stack pointer and program counter when the HC11 gets an interrupt?

• HC11 pushes Return Address, Y, X, B, A, CCR onto stack, so SP decreases by 9.
• PC loaded with appropriate interrupt vector from table in memory.

3. Before getting a Timer Overflow interrupt, an HC11's stack pointer has a value of 0x01f5. What will be the value of the stack pointer when the HC11 starts executing its interrupt service routine?

• 0x01f5 - 0x0009 = 0x1ec

Below is some data in an HC11 memory:

```     |  0   1   2   3   4   5   6   7   8   9   A   B   C   D   E   F
-----|---------------------------------------------------------------
2000 | 10  23  3B  7C  10  04  86  80  B7  10  25  3B  FC  10  18  F3
2010 | 12  50  FD  10  18  86  40  B7  10  23  3B  FC  10  12  DD  02
2020 | 86  02  B7  10  23  3B  7C  10  03  86  40  B7  10  25  3B  86
-----|---------------------------------------------------------------
FFD0 | 7E  E3  4B  7E  E5  38  21  54  25  83  20  34  E5  38  20  03
FFE0 | E5  38  2A  F2  26  13  20  0C  25  F2  E5  38  20  1B  27  1A
FFF0 | 20  26  22  13  25  AA  E9  1F  E5  38  E5  38  E5  38  E0  00
```

1. Where is the interrupt service routine located for the TOF interrupt? That is, what will the program counter of the PC be loaded with when the HC11 gets an TOF interrupt?

• TOF interrupt vector is at 0xFFDE - 0xFFDF, so program counter will be loaded with 0x2003 (contents of 0xFFDE - 0xFFDF).

2. How many bytes long is the TOF interrupt service routine? That is, how many bytes are in the routine up to and including the rti instruction?

• rti machine code is 0x3B, which is at 0x200B, so ISR is from 0x2003 to 0x200B, or 9 bytes.

3. What does the TOF interrupt service routine do?

• E.g., at 0x2003 is a 7C, INC using extended addressing. Continuing with reverse assembly, we get:
```         7C 10 04     INC    0x1004     ; increment Port B (0x1004)
86 80        LDAA   #0x80      ; clear TOF flag by writing 1 to
B7 10 25     STAA   0x1025     ;   Bit 7 of TFLG2 register (0x1025)
3B           RTI
```

3. A 68HC11 is used to capture the time an event happens -- when a signal going to the HC11 goes high, the HC11 should get an interrupt, find the time the signal went high, and write the time to a global variable called time. Use one of your input capture pins to measure this time. You want your basic time interval to be 4 us.

1. Which Input Capture did you decide to use? Which pin of the HC11 should you connect your signal to?

• You could use any of the input captures; I'll use IC1, which is on Pin 32 of the HC11. See Figure on Page 65 of the M68HC11 E Series Programming Reference Guide.

2. Here is the start of a program to measure the time. Finish writing the program. Be sure you do all the steps you listed in Problem 2(a).

```#include <hc11.h>

#define TRUE 1

unsigned short time;

void tic2_isr(void);

main()
{
TMSK2 = TMSK2 |  0x02;    /* Set timer for 4~us interval */
TMSK2 = TMSK2 & ~0x01;

TCTL2 = TCTL2 |  0x10;    /* Capture rising edge of Input Capture 1 */
TCTL2 = TCTL2 & ~0x20;

TIC2_JMP = JMP_OP_CODE;   /* Set interrupt vector to point to */
TIC2_VEC = tic2_isr;      /* service routine */

TMSK1 = TMSK1 | 0x04;     /* Enable IC1 interrupt */

TFLG1 = 0x04;             /* Clear IC1 flag */

enable();                 /* Enable interrupts -- clear I bit of CCR */

while (TRUE) { ; }

}

#pragma interrupt_handler tic2_isr
void tic2_isr(void)
{
time = TIC2;              /* Read time of event */

TFLG1 = 0x04;             /* Clear IC1 flage */
}
```

Bill Rison, <rison@ee.nmt.edu >