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

Help me with my mini-project MPLAB pls i wan to creat a mini game -- if you clic

ID: 2266907 • Letter: H

Question

Help me with my mini-project MPLAB pls

i wan to creat a mini game -- if you click the switch the LED will start lighting up so fast from 1 to 10 ... your goal is to make it stop at 10/10

if you make it stop at 10/10 LEDs it will start flashes if you fail it will reset

here is my code so far ... Helpp pls :)

File: main.c *Created on February 9, 2010, 10:53 AM 1b #include 811 #include 9L #include 101 #de fine Leds LATB 11 #de fine SwI RAO 12 13 14 15 16 void delay (void) { 17 18 19 20L h 21 22 int main( ) 23 24 25 26 27 28 29 30 31while (Swl_RAO0) 32 long i 65535 ; while (i--) int i; TRISB = 0x0000; TRI SAO=1 ; // Setup PortB IOs as digital AD1 PCFG = 0xffff; Leds 0x8000; while (1) t delay Leds=Leds>>1 ; Leds 0x8000; 35 36 37 38 // delay / delay

Explanation / Answer

Answer:- Assuming LEDs are connected to PORTB as defined in macro definination.Since we have only 8 pins for PORTB, so we can connect at most 8-LEDs, rest two LEDs we can connect, lets say on PORTA pin2 and pin3.

The program can be written as-

#include<stdio.h>

#include<stdlib.h>

#include<p24Fxxxx.h>

#define Sw1_RA0 PORTA0 //start switch

#define Sw2_RA1 PORTA1 //stop switch

int main()

{

int i;

TRISB = 0x00; //make all port B pins as output

TRISA0 = 1; //make port A pin0 as input for switch

TRISA1 = 0; //make port A pin 1 as output

TRISA2 = 0; //makeport A pin 2 as output

LATA2 = 0; //make Led 9 off

LATA3 = 0; //make Led10 off, initially

while(Sw1_RA0 == 0); //wait till start switch is not pressed, if execute next

while(1)

{

Leds = 0x400; //initaially all leds are off

for(i=0; i<8; i++)

{

Leds = Leds >> 1;

delay();

}

LATA2 = 1; //glow led9

delay();

LATA2 = 0; //make led9 off

LATA3 = 1; //make led10 on

delay();

LATA3 = 0; //make led10 off

}

return 0;

}

void delay(void) //delay function with checking of stop switch press

{

long int i = 65536, j;

while(i--)

{

if(Sw2_RA1 == 1 && PORTA3 == 1) //if stop switch is pressed and Led10 is glowing

{

//the player wins in this case, hence start flashing leds

while(1)

{

j = 65536;

Leds = 0x1FF;

LATA2 = LATA3 = 1; //make all LEDs on

while(j--); //give delay for on time

j = 65536;

Leds = 0x000; //make Leds off

LATA2 = LATA3 = 0; //make Led9 and Led10 off

while(j--); //delay for off time

}

else if(Sw2_RA1 == 1 && PORTA3 == 0) //if stop switch is pressed and Led10 is not glowing

{

//the player loses here, so reset

break;

}

else

{

continue; //if switch hasn't pressed then do as usual.

}

}

}

Note:- To read a port pin we use PORTx register and to write use LATx register.

To set direction of port pin we use TRISx register. Here x denotes port name like A, B, C etc.