The task of this exercise is to create a program and successfully download it to
ID: 2247628 • Letter: T
Question
The task of this exercise is to create a program and successfully download it to a dragon12-light board. Using a CodeWarrior_CProgramShell.txt and the HCS12 Microcontrollers and Embedded Systems, 1st Edition. I am struggling to determine the condition of the exercise requirement and keep getting error messages. The CodeWarrior_CProgramShell.txt below is a guideline of how to create a program and successfully download to the dragon12-light board. I need help to solve this problem and it is due tomorrow night? Thank you.
Objective: Used the port H as an input port and port B as an output port and download the program to the dragon12-Light boards.
When both DIP switches #8 and #1 are high, turn on all LEDS.
When both DIP switches #8 and #1 are low, turn off all LEDs
When DIP switch #8 is high and #1 is low, turn on all the even numbered LEDs.
When DIP switch #1 is high and #8 is low, turn on all the odd numbered LEDs.
Your program must to read DIP switches and turn on/off corresponding LRDs continuously until a Reset.
//*****************************************************************
// Include derivative-specific definitions
//*****************************************************************
//The microcontroller chip used by Dragon12-Light boards
#include <mc9s12dg256.h> /* derivative information */
//*****************************************************************
// Gloable Variables
//*****************************************************************
//*****************************************************************
// Function Prototype
//*****************************************************************
void InitSwitches(void);
void InitLEDs(void);
void DelayTime(unsigned int,unsigned int);
//*****************************************************************
// Main program section
//*****************************************************************
void main(void)
{
/* put your own code here */
InitSwitches();
InitLEDs();
//do this forever
for (;;)
{
unsigned char x;
x = PTH; //get data from DIP Switches via. PTH
DelayTime(1, 4000); //optional, delay some time
PORTB = x; //and send it to PORTB LEDs
DelayTime(1, 4000); //optional
}
}
//*****************************************************************
// Function: InitSwitches
//*****************************************************************
void InitSwitches(void)
{
DDRH = 0x0; //make port PTH an input port for reading DIP switches
}
//*****************************************************************
// Function: InitLEDs
//*****************************************************************
void InitLEDs(void)
{
//LEDs are connected to PORTB on Dragon12-Light
DDRB = 0xFF; //make PORTB an output for LEDs
//PTP0, PTP1, PTP2 and PTP3 of port PTP control the 4 units of the seven seg. display
//GRB LED is controlled by PP4-PP6: PP4=1, red; PP5=1, blue; PP6=1, green
DDRP = 0xFF; //make port PTP an output port
PTP = 0x4F; //make PTP0-PTP3 high to disable the seven seg. dispaly
//and make PTP6 high to turn on green RGB LED
}
//*****************************************************************
// Function: DelayTime
//*****************************************************************
//provide milli seconds delay
void DelayTime(unsigned int i_limit, unsigned int j_limit)
{
unsigned int i;
unsigned int j;
for(i=0;i<i_limit;i++) {
for(j=0;j<j_limit;j++) {
//do nothing
}
}
}
//*****************************************************************
// Interrupt Service Routines
//*****************************************************************
Explanation / Answer
Assumption : Var x contains the value of input port and PORT B bits represent LEDs that need too be switched on and off
Relevant code segment for accomplishing the objective outlined in the question ( This needs to go into the infinite loop in main method:
unsigned char sw1=x & 0f00000001; // DIP switch 1 state
unsigned char sw8=x & 0f1000000; // DIP switch 8 state
if(sw1 && sw8){
PORTB=0xff; //Turn on all LEDs
}
if((!sw1) && (!sw8)){
PORTB=0xff; //Turn off all LEDs
}
if((!sw1) && sw8){
PORTB=0xaa; // Turn on all even LEDs
}
if((sw1) && (!sw8)){
PORTB=0x55; // Turn on all odd LEDs
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.