Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Shown below is a switch connected to the IRQ or XIRQ pin of the HCS12. Write a p

ID: 2293966 • Letter: S

Question

Shown below is a switch connected to the IRQ or XIRQ pin of the HCS12. Write a program to get the status of the switch and increment a counting sequence on the 7-segment display each time the key is pressed. The switch should be connected to IRQ/PE1 or XIRQ/PEO of the Trainer (Dragon 12P), namely pin 55 (for Maskable Interrupt) or pin 56 (for Non- Maskable Interrupt) respectively. You do not have to get any physical switch; all you need is represent the closure of a switch by grounding the appropriate pin. VCC 4.7k HCS12 IRO Switch HO XIRO Procedure: 1. Initialize and Program the appropriate Interrupt Registers appropriately. 2. Design an appropriate ISR (Interrupt Service Routine). 3. Write down an Assembly code to create a dummy delay loop, and once the key has been pressed, the incremented sequence would appear on the on the 7-segment display and optionally on the LEDs. Note: 1. In Dragon12+ the RAM address is from $1000-3FFF. 2. We use RAM addresses starting at $1000 for scratch pad (variables) and $3FFF for stack. 3. All bits of Port B are connected to LEDs. The LEDs are controlled by the PTJ1 pin. The LEDs will not show the data on Port B unless you turn off the PTJ1 bit. To do that, we must make PTJ = output (DDRJ=0xFF) and PTJI=0 (Logic 0). See page 24 of the Dragon 12+ User's Manual.

Explanation / Answer

// Simple interrupt routine written for DEMOQE board.
// It toggles LED PTC5 once every two seconds using a RTC ISR
// and at the same time monitors switch PTA2 to see if it has
// been pressed and if it is toggle LEDs PTC0 and PTC1.

#include <hidef.h> /* for EnableInterrupts macro */
#include "derivative.h" /* include peripheral declarations */
#include "main_asm.h" /* interface to the assembly module */

/* Define LED's ON or OFF,*/
#define ON 1
#define OFF 0
/* Define LED's*/
#define LED1 PTCD_PTCD0
#define LED2 PTCD_PTCD1
#define LED3 PTCD_PTCD5
/*Define Switch 1*/
#define SW1 PTAD_PTAD2
void main(void) {
SCGC1 = 0x00; /* Disable clocks to peripherals */
SCGC2 = 0x04; /* Disable clocks to peripherals except clock to RTC */
SOPT1 = 0x21; /* Disable COP timer, Enable STOP Mode and Disable BKGDPE */
SPMSC1 = 0x00; /* Disable LVD in Stop3 Mode */
SPMSC2 = 0xA6; // Sets Low Power mode
PTADD = 0xFB; /*PortA bit 2 input all others outputs */
PTBDD = 0xFF; /*all port B outputs */
PTCDD = 0xFF; /*all port C outputs */
PTDDD = 0xFF; /*all port D outputs */
PTCD = 0xFF; /* turn off all LEDs */
PTAPE = 0x04; /*enable pullup on PTA bit 2 */
RTCSC = 0x91; /* Enable RTC interrupt, prescaler to 8, CLK = 1kHz LPO */
RTCMOD = 0xFC; /* Set RTC Modulo to 252 = 2 second interrupt */

EnableInterrupts; /* enable interrupts */

while (1) { /* this line gives an error about always true, just ignore error */
if (SW1) {
LED1=ON;
LED2=OFF;
// asm STOP; /* you can uncomment this line to see STOP3 work */
} else {
LED1=OFF;
LED2=ON;
// asm STOP; /* you can uncomment this line to see STOP3 work */
}
}
}

// RTC ISR to toggle LED3 every two seconds, 2 seconds on then 2 seconds off
void interrupt VectorNumber_Vrtc rtc_isr(void)
{
RTCSC_RTIF = 1; /* Clear RTIF */
LED3 = !LED3; /* Toggle LED3 */
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote