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

BSF TRISB,0 BCF TRISB,7 AGAIN BTFSS PORTB,0 GOTO OVER BSF PORTB,7 GOTO AGAIN OVE

ID: 3550205 • Letter: B

Question

BSF TRISB,0

BCF TRISB,7

AGAIN BTFSS PORTB,0

GOTO OVER

BSF PORTB,7

GOTO AGAIN

OVER BCF PORTB,7

GOTO AGAIN



The assembly code is like above (look at the picture in example 4-8 like above to see how theoretically this works.

Convert this code to C code (using code of chapter 7). You can use #include <P18F458.h> as in the book or #include <P18F4321.h> etc. if your device is say PIC18F4321.

The BSF, BCF will be converted to instruction like TRISBbits.TRISB7 = 1; etc., like in Example 7-5 on page 264.

You probably need to add some code that shows that when RB0 is 0 (switch is 0), LED RB7 is 0 and when RB0 is 1, RB7 is 1. Show by using SPR if necessary how RB7 changes with RB0.

Do this also with RB0 as input, but RC0 as output.

Explanation / Answer

#include <P18F458.h>

void main(void)

{

TRISB = TRISB | 0x01; // RB0 as input or TRISBbits.TRISB0 = 1 ;

TRISB = TRISB & 0x7F; //RB7 as output or TRISBbits.TRISB7 = 0;

while(1)

{

if((PORTB&0x01) == 1)

{

PORTB = PORTB | 0x80;

}

else

{

PORTB = PORTB & 0x7F;

}

}

RB0 as input, but RC0 as output.

#include <P18F458.h>

void main(void)

{

TRISB = TRISB | 0x01; // RB0 as input or TRISBbits.TRISB0 = 1 ;

TRISC = TRISC & 0xFE; //RC0 as output or TRISCbits.TRISC0 = 0 ;

while(1)

{

if((PORTB&0x01) == 1)

{

PORTC = PORTC | 0x01;

}

else

{

PORTC = PORTC & 0xFE;

}

}