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

1. Write a program to repeat the following tasks continuously. - Turn all four L

ID: 3538051 • Letter: 1

Question

1. Write a program to repeat the following tasks continuously.

- Turn all four LED lights OFF.

- Wait in a loop until one of the switches is pressed.

- Use the C-language %u201Cswitch statement%u201D to select one of the following:

Turn ON LED1 and LED2 if SW1 is pressed;

Turn ON LED2 and LED3 if SW2 is pressed;

Turn ON LED3 and LED4 if SW3 is pressed; and

Turn ON LED4 and LED1 if SW4 is pressed.

- Keep the values ON for three seconds.

Here is my code:

// 330_lab6_2 - C test program - tested 3-12 FM
// Tower Module - 6.25 MHz; CodeWarrior 5.1
// Flashes the LEDs ON and OFF with a delay
// Runs in hardware (HCS12 Open Source BDM)

#include <hidef.h>      /* common defines and macros */
#include "derivative.h"      /* derivative-specific definitions */

void delay(void);
void main(void)
{

    unsigned char mydata;
    DDRT = 0xF0;                                //PT[7-4] as output for LEDs
    ATDDIEN = 0x00F0;  // Enable SW input on PAD[7:4]  
   PER1AD = 0xF0;  // Enable internal pull-ups for the PAD[7-4] pins
  
    for ( ; ; )    
      {
        PTT=0xFF;       //Turn all LEDs off
       
        while ((PT1AD & 0xF0)==0xF0);
        mydata =PT1AD & 0xF0; //read switches
         PTT=0x00;
    switch(mydata)
    {
      case 0b11100000: /*SW1 on PAD4 pressed*/
        PTT =0b11100000; //code to turn on LED1 on PT4;
        break;
      case 0b11010000:    /*SW2 on PAD5 pressed*/
        PTT=0b11010000;   //code to turn on LED2 on PT5;
        break;
      case 0b10110000:
        PTT=0b10110000;   //code to turn on LED3 on PT6;
        break;
      case 0b01110000:
        PTT=0b01110000;   //code to turn on LED4 on PT7;
        break;
       
      }
     
     
      delay();
     
      
      }
}

/*1 second delay using the 10ms delay loop based on a 6.25Mhz clock speed code from an earlier presentation*/

void delay(void)
{
asm
{
   
    ldy #100
loop1: ldx #15624
loop2: dex
      bne loop2
      dbne y,loop1
}
}
   

Explanation / Answer

Was very close, here's it tweaked a bit for the correct results.

#include <hidef.h> /* common defines and macros */
#include "derivative.h" /* derivative-specific definitions */
#include <mc9s12g128.h>

void delay(void);
void main(void)
{

unsigned char mydata;
DDRT = 0xF0; //PT[7-4] as output for LEDs
ATDDIEN = 0x00F0; //Enable SW input on PAD[7:4]
PER1AD = 0xF0; // Enable internal pull-ups for the PAD[7-4] pins
  
for ( ; ; )
{
PTT=0xFF; //Turn all LEDs off
  
while ((PT1AD & 0xF0)==0xF0);
mydata =PT1AD & 0xF0; //read switches
PTT=0x00;
switch(mydata)
{
case 0b11100000: //SW1
PTT =0b11000000; //turn on LED1 and LED2 on PT4
break;
case 0b11010000: //SW2
PTT=0b10010000; //turn on LED2 and LED3 on PT5
break;
case 0b10110000: //SW3
PTT=0b00110000; //turn on LED3 and LED4 on PT6
break;
case 0b01110000: //SW4
PTT=0b01100000; //turn on LED4 and LED1 on PT7
break;
}
delay();
}
}

//3 second delay based on a 6.25Mhz clock speed

void delay(void) {
asm
{
LDY #300
LOOP1: LDX #15624
LOOP2: DEX
BNE LOOP2
DBNE Y,LOOP1
}
}