Starting from:
$35

$29

ECE Experiment 7: Interrupts Solution


Instructional Objectives:

    • To understand how to implement interrupts to simultaneous perform several time and event sensitive tasks.

    • To understand how to alter the linker parameter file to handle multiple entries in the vector table.

Introduction:

The final lab for the ECE362 course explains how to implement interrupts. An interrupt is a signal to the microcontroller that is generated by a hardware or software event. Whenever an interrupt occurs, the program completes the current instruction and then jumps to a special subroutine called an interrupt service routine (ISR). In previous labs, polling was used to control just one or two peripherals on the I/O board. However, polling is not an ideal way to control several peripherals in one program.

A good analogy is the ECE362 lecture. Suppose that at the end of every lecture, the professor asks every student one at a time if they understood the lecture or if they have any questions. This is effective if the classroom only has one or two students. However, in a classroom with 40-50 students, this becomes ineffective. In this scenario, the student raises their hand to interrupt the lecture and ask a question. The professor completes their sentence and then stops the lecture to answer the question. Once the question has been answered, the professor resumes the lecture from where they left off.

This experiment will introduce the concept of interrupts and give two examples of how to implement interrupt sources on the HC(S)12. In addition, this experiment will provide instructions for how to alter the linker parameter file to handle interrupts.

Interrupts:

As stated above, an interrupt is a signal to the microcontroller that is generated by a hardware or software event. There are several sources for interrupt signals, including external hardware signals, timers, software interrupts, and others. Each interrupt has its own interrupt service routine (ISR). Upon the occurrence of an interrupt event, the following steps occur:

    • The CPU finishes the currently executed instruction.

    • The return address is pushed to the stack.

    • The contents of all the registers are pushed onto the stack.

    • The program jumps to the ISR.

    • The program executes the ISR.

    • The contents of all the registers are pulled from the stack.

    • The CPU returns from the ISR and continues the main program.




[1]    © J. Lee, N. Wheeler
ECE 362 – Experiment 7    Rev – 3/17




















Above is an excerpt from the vector table for the HC(S)12. The interrupt vector table is a predefined memory space for storing interrupt vectors. Interrupt vectors are pointers to the beginning of an ISR. Every interrupt source has an associated interrupt vector. On the HC(S)12, interrupt vectors begin at the address $FF80 and ends at the address $FFFF. The higher the vector is in the table, the higher priority that interrupt has. For example, external reset has a higher priority than real time interrupts. The vector table is initialized at the bottom of the linker parameter file, as seen below.






























Note: The keyword DEFAULT_ROM has been moved next to the label NON_BANKED. This forces the entire program to be allocated to the page of non-banked memory starting at the memory location 0xC000. Failing to do this will result in an error upon assembling the program.




[2]    © J. Lee, N. Wheeler
ECE 362 – Experiment 7    Rev – 3/17


Real Time Interrupts:

Real-time interrupts are an interrupt signal that occurs whenever the internal clock of the microcontroller reaches a certain value. This generates a hardware interrupt that occurs at a programmed periodic rate. Real-time interrupts are useful for periodic tasks, such as motor controls, keypad reads, timed patterns on LEDs, and playing different tones through a speaker.

Real-time interrupts must be enabled both at the CPU and at the interrupt enable register. To enable maskable interrupts, the I flag of the CCR must be cleared. This can be done using the CLI instruction. In addition, the interrupt enable register (CRGINT) must also be configured to enable real time interrupts. This register is located at memory location $0038. Bit 7 of this register is the bit that enables/disables real time interrupts. To enable Real-time interrupts, set bit

7 of this register. Please refer to the excerpt from the MC9S12E128 below:



































To control the Real-time Interrupt interval, the control register (RTICTL) at address $003B must be set to a value corresponding to the desired rate. The values are used to enable or disable t-gates and set the modulus counter that scales the OSCCLK from the crystal. The circuit that handles this scaling operation is presented in the lecture slides. Please refer to the next page for the data sheet information on how to configure the control register to reach a desired RTI interval.




[3]    © J. Lee, N. Wheeler
ECE 362 – Experiment 7    Rev – 3/17




































































[4]    © J. Lee, N. Wheeler
ECE 362 – Experiment 7    Rev – 3/17


The above table can be used to choose a period for the RTI interval. For example, $40 corresponds with the entry in the table above that is circled. If the value $40 is written to the register RTICTL, then the RTI interval is given as:






=

=

=   .







Whenever an RTI event has occurred, the RTIF bit of the flag register (CRGFLG) at address $37 is set. If this bit is not cleared at the end of the interrupt service routine, the program will not properly return to the main program. Please refer to the information from the datasheet below.



































On the next page is an example of how to use real-time interrupts to count the seconds from 0-255 in binary on the LEDs.








[5]    © J. Lee, N. Wheeler
ECE 362 – Experiment 7    Rev – 3/17































Interrupt request line (IRQ):

In a microcontroller, an interrupt request is a hardware signal sent to the processor that temporarily stops a running program and allows an interrupt handler to run. Instead of the signal being generated internally by the OSCCLK, as with Real-time Interrupts, the signal is generated by an external source. For the equipment used in the lab, the IRQ pin is connected to a pushbutton on the I/O board. Please see the figure below.




























[6]    © J. Lee, N. Wheeler
ECE 362 – Experiment 7    Rev – 3/17











The register to enable the IRQ (INTCR) is located at memory location $1E. Bit 6 of this register enables the IRQ line, while bit 7 of this register sets the IRQ to be an edge triggered event instead of a level triggered event. To use the IRQ, write the value $C0 to this register. Although the IRQ will not be implemented in this lab, it will be implemented in the final project, so keep this information handy.

















































[7]    © J. Lee, N. Wheeler
ECE 362 – Experiment 7    Rev – 3/17


Experimental Procedure:

Laboratory 7.1: PWM, DC Motor Control using polling.

The objective of this experiment is to write an assembly program that implements Pulse Width Modulation (PWM) to control the speed of the DC motor on the I/O board in the lab. The DC motor is connected to bit 3 of Port T. Before beginning this experiment, make sure that jumper J2 is on the left two pins and that jumper J5 is on the upper two pins. Also, be sure to set bit 3 of Port T as an output by setting the appropriate bit of the Data Direction Register located at the address $242. Switch 4 must be up for the DC Motor to spin. To start the motor, set bit 3 of Port T at the address $240. To halt the motor, clear bit 3 of Port T.

In order to generate the PWM signal to control the motor, the keypad routine from lab 5 will be used. For lab 7.1, make the following changes to the keypad routine:

    • If a match isn’t returned in any of the four rows of the keypad, instead of branching back to search the rows again, branch down to the routine that handles the PWM signal to the DC motor. If this isn’t done correctly the DC motor will only spin (briefly) when a key is pressed and released.

    • Remove the section of code that debounces and waits for a key to be released. If this isn’t done correctly, the DC motor will stop when the key is pressed, and then resume spinning when the key is released.

Also, in addition to the 1ms delay subroutine used for debouncing the keypad upon a keypress, also write a 4ms delay subroutine in order to implement the PWM strategy.




















Above is an example of a Pulse Width Modulated waveform. Since the DC motor is only controlled by one bit, the only way to control the speed of the motor is by implementing a PWM strategy. This allows the average value of the power sent to the DC motor to change, based on the size of ton. The average value of the above waveform is given by:

1


<  > =

∫0
  (  )  =







[8]




© J. Lee, N. Wheeler
ECE 362 – Experiment 7    Rev – 3/17


The period T should be sufficiently short. Choose a T = 60ms for the period. Divide this into 15 intervals to correspond with keys $0-$F on the keypad. The value read from the keypad will determine the duty cycle. The value ton should be the value returned from the keypad routine. The value toff should be 15 minus the value returned from the keypad routine. For example, if the keypad returns the value of $C = 12, then ton = 12 and toff = 15 – 12 = 3.

Use the following instructions to successfully implement the PWM strategy described above.

    • Call the altered keypad routine. If a key has not been pressed, use the previous value for ton and toff.

    • Check if ton is equal to zero, if it is, branch down to check toff.

    • Else, set bit 3 of Port T, delay for 4ms, decrement ton, and check again.

    • Check if toff is equal to zero, if it is, branch back to the keypad routine.

    • Else, clear bit 3 of Port T, delay for 4ms, decrement toff, and check again.



Laboratory 7.2: PWM, DC Motor Control using Real Time Interrupts.

The objective of this experiment is to write an assembly program that implements Pulse Width Modulation (PWM) to control the speed of the DC motor on the I/O board in the lab. Instead of using delay loops and polling to implement the PWM strategy, Real Time Interrupts will be used. In the main program and the linker parameter file, initialize interrupts by using the information in the background section above. Select an RTI interval of 4ms.

The unaltered keypad subroutine from lab 5.2 should be called in the main program. It is okay to calculate ton and toff in the main program, but delay loops are not to be used to control the

PWM signal.

The simplest way to complete this lab is to define a byte variable to use as a counter and initialize it to zero. In the interrupt routine, do the following:

    • Increment the count. If the count is less than or equal to ton, set bit 3 of Port T and exit the interrupt service routine.

    • Else, if the count is greater than ton, but less than or equal to 15, clear bit 3 of Port T and exit the interrupt service routine.

    • Else, if the count is greater than 15, clear the count and exit the interrupt service routine.















[9]    © J. Lee, N. Wheeler
ECE 362 – Experiment 7    Rev – 3/17


Laboratory 7.3: Wall Clock

The objective of this experiment is to implement the wall clock code found in the lecture and in the background section above to display the seconds on the LEDs. Count from 0-60 instead of from 0-255. Also, instead of counting the seconds in Binary, count the seconds in BCD. Refer to the table below.

Decimal
Binary
BCD





0
0000 0000
0000
0000




1
0000 0001
0000
0001




2
0000 0010
0000
0010




3
0000 0011
0000
0011




4
0000 0100
0000
0100




5
0000 0101
0000
0101




6
0000 0110
0000
0110




7
0000 0111
0000
0111




8
0000 1000
0000
1000




9
0000 1001
0000
1001




10
0000 1010
0001
0000




11
0000 1011
0001
0001




12
0000 1100
0001
0010




13
0000 1101
0001
0011




14
0000 1110
0001
0100




15
0000 1111
0001
0101







Extra Credit: Display the time on the LCD instead of on the LEDs. You’ll have to include the necessary files from lab 6.3 in order to accomplish this.









[10]    © J. Lee, N. Wheeler

More products