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

The diagram above shows a 4 bit thumbwheel switch connected to PortC of an PIC18

ID: 2084379 • Letter: T

Question

The diagram above shows a 4 bit thumbwheel switch connected to PortC of an PIC18 microcontroller. The wheel is used to dial up a number 0 – 9 representing an angle 00 - 90 degrees which is presented to the inputs as a 4 bit binary value. You are required to write a program and subroutines/functions to initialise and operate this interface.

(i) Write a C subroutine to initialise PORTC as needed to interface with the devices.

(ii) Write a C function that checks if the push button is pressed or not, returning 1 if it is pressed or 0 if not pressed. This routine is not to wait for the press.

(iii) Write a C function that reads the thumb wheel switch, returning the angle. (Don’t forget there are other pins used on this port) (iv) Write a C subroutine that sets the LED on or off given a value of 1 or 0. (Don’t forget there are other pins used on this port)

(v) Write a C main program that clears the LED, waits for the button to be pressed, sets the LED on, reads the angle set by the thumb wheel switch and saves that value in a variable called angle.

PIC 18 PORTC 4 bit binary value from PCO thumbwheel switch 0000-1001 for values 0-9 decimal Representing 00-90 degrees of PC3 angle in steps of 10. PC5 GND single LED indicator 10K 5V Normally open momentary push PC7 button switch GND

Explanation / Answer

Part I :

As we observe that we need to initialize PORT C where bit 0,1,2,3,7 should be input(= 1) and the rest are set to output(=0)

which makes TRISC value to 11110001 = 0xF1.

so the subroutine will be :

void init(void)
{
TRISC = 0xF1;
}

PART II :

For this we first sample the PORTC bit 7 to find out the status of the switch.

If the switch is ON it will return 1

If the switch is OFF it will return 0

therefore the subroutine is:

char swit_status(void)
{
if(PORTC & 0b10000000 == 1) // masking other bits
return 1;
else
return 0;
}

PART III

For this part we will first sample first 4 bits of the PORTC and then decode the value to angle.

Subroutine :

int wheel(void)
{
char val = PORTC & 0b00001111 ; // sampling only 1st four bits of PORTC.
int ang = val * 10;
/** As the value accepted is in bits the variable will directly contain its decimal conversion **/
return ang;
}

PART IV

for this part :

The LED turns ON if a 1 is set at PORTC pin 5.

The LED turns OFF if a 0 is set at PORTC pin 5.

Subroutine:

void led(char status)
{
if(status == 1)
PORTC = PORTC | 0b00100000; //setting only bit 5 to '1'
else
PORTC = PORTC & 0b11011111;//setting only bit 5 to '0'
}

PART V:

For this part we finally merge the subroutines written above in the main program :

Program:

void main()
{
init();
led(0);
int angle;
while(swit_status)
{
led(1);
angle = wheel();
}
}

Now below is the complete program for your reference :

void init(void)
{
TRISC = 0xF1;
}

void led(char led_status)
{
if(status == 1)
PORTC = PORTC | 0b00100000; //setting only bit 5 to '1'
else
PORTC = PORTC & 0b11011111;//setting only bit 5 to '0'
}

char swit_status(void)
{
if(PORTC & 0b10000000 == 1) // masking other bits
return 1;
else
return 0;
}

int wheel(void)
{
char val = PORTC & 0b00001111 ; // sampling only 1st four bits of PORTC.
int ang = val * 10;
/** As the value accepted is in bits the variable will directly contain its decimal conversion **/
return ang;
}

void main()
{
init();
led(0);
int angle;
while(swit_status)
{
led(1);
angle = wheel();
}
}

Comment if any further assistance required.