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

Question 3 (8 points). Write two functions, void InitKeys) and int ReadKeys O to

ID: 2267982 • Letter: Q

Question

Question 3 (8 points). Write two functions, void InitKeys) and int ReadKeys O to interface a 4-key matrix connected to Port B of a PIC32 as shown below. ReadKeys should return the state of the four switches, SW0 through SW3, in the four least significant bits of the return argument. InitKeys) should make appropriate initializations in Port B. 10i sW2 PORT B bit0 X SW1 SW3 10k PORT 8 bi1 X PORT B bit3 The following PLIB routines may be useful. void PortSetPinsDigitalIn(IoPortId portid, unsigned int pins); void PortSetPinsDigitalout (IoPortId portid, unsigned int pins); unsigned PortRead (IoPortId portid) unsigned PortReadBits (IoPortId portid, unsigned int bits); void Porthrite(IoPortId portid, unsigned int bits); void PortSetBits (IoPortId portid, unsigned int bits); void PortClearBits(IoPortId portid, unsigned int bits); Use the back of the page if you need more space. void InitkeysO; int Readkeys );

Explanation / Answer

// Declaration of Functions
void InitKeys(void);
void ReadKeys(int);


// Defining of Functions
void InitKeys()
{
// psudo Code
// PORTB bit 0 and PORTB bit 1 are defined as Inputs
PortSetPinsDigitalIn(PORTB,0);
PortSetPinsDigitalIn(PORTB,1);
// PORTB bit 2 and PORTB bit 3 are desfined as outputs
PortSetPinsDigitalOut(PORTB,2);
PortSetPinsDigitalOut(PORTB,3);
// Set outputs to logic level zero
PortClearBits(PORTB,2);
PortClearBits(PORTB,3);
}

void ReadKeys(int x)
{
// psudo Code
// intialise the variable x which defines the state of switchs
int x = 0 // non of the switchs are ON
int c0 = 0,c1 = 0,c2 = 0,c3 = 0; // indicates the state of each switch
// First set PORTB bit2 and clear bit3 check for logic levels of bit0 and bit1
PortSetBits(PORTB,2);
PortClearBits(PORTB,3);
c0 = PortReadBits(PORTB,0); // indicates the state of sw0
c1 = PortReadBits(PORTB,1); // indicates the state of sw1
// First set PORTB bit2 and clear bit3 check for logic levels of bit0 and bit1
PortClearBits(PORTB,2);
PortSetBits(PORTB,3);
c2 = PortReadBits(PORTB,0); // indicates the state of sw2
c3 = PortReadBits(PORTB,1); // indicates the state of sw3
// encode the state of all switchs in single value
x = c0 + (c1*2) + (c2*4) + (c3*8);
/*
Here X value indicates the state of each switch, NP for not pressed P for pressed
x = 0 --> sw0 = NP, sw1 = NP, sw2 = NP, sw3 = NP.
x = 1 --> sw0 = P, sw1 = NP, sw2 = NP, sw3 = NP.
x = 2 --> sw0 = NP, sw1 = P, sw2 = NP, sw3 = NP.
x = 3 --> sw0 = P, sw1 = P, sw2 = NP, sw3 = NP.
x = 4 --> sw0 = NP, sw1 = NP, sw2 = P, sw3 = NP.
x = 5 --> sw0 = P, sw1 = NP, sw2 = P, sw3 = NP.
x = 6 --> sw0 = NP, sw1 = P, sw2 = P, sw3 = NP.
x = 7 --> sw0 = P, sw1 = P, sw2 = P, sw3 = NP.
x = 8 --> sw0 = NP, sw1 = NP, sw2 = NP, sw3 = P.
x = 9 --> sw0 = P, sw1 = NP, sw2 = NP, sw3 = P.
x = 10 --> sw0 = NP, sw1 = P, sw2 = NP, sw3 = P.
x = 11 --> sw0 = P, sw1 = P, sw2 = NP, sw3 = P.
x = 12 --> sw0 = NP, sw1 = NP, sw2 = P, sw3 = P.
x = 13 --> sw0 = P, sw1 = NP, sw2 = P, sw3 = P.
x = 14 --> sw0 = NP, sw1 = P, sw2 = P, sw3 = P.
x = 15 --> sw0 = P, sw1 = P, sw2 = P, sw3 = P.
*/
return(x);
}

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