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

include <p18f4580.h> void T0Delay(void); #define mybit PORTBbits.RB4 void main(v

ID: 1846434 • Letter: I

Question

include <p18f4580.h>
void T0Delay(void);
#define mybit PORTBbits.RB4
void main(void)
  {
   TRISBbits.TRISB4=0;
   while(1)
    {
      mybit^=1;    
      T0Delay();    
    }
  }
void T0Delay()
  {
    T0CON=0x01;   
    TMR0H=0x85;    
    TMR0L=0xEE;    
    T0CONbits.TMR0ON=1;   
    while(INTCONbits.TMR0IF==0);
    T0CONbits.TMR0ON=0;   
    INTCONbits.TMR0IF=0;  
  }

(a)    The code shows you how to toggle PORTB.4 every 50 ms.  Modify the code (without adding or removing a line, but changing some lines) to show a way of toggling  every x ms where x is an integer bigger than 3,000 picked by you or explain why this can not be done.

(b)   Repeat the same question as in (a) with x changed to an integer bigger than 4,000 picked by you.

            (c) Modify and remove or add line(s) if necessary this time for the 8 bit timer (not 16 bit timer).

(d)Show the modified code for 8 bits timer.

(e) Do you have both TMR0H and TMR0L for 8 bits timer ? Explain!

(f) Can you still have PORTB.4 toggling every 50 ms for the 8 bits timer version here? Explain!

(g)   For 8 bits timer, can you toggle PORTB.4 every y ms (microseconds), where y is between 5 and 20? Explain! (show the code if this can be done or show the reason if this can not be done).

Explanation / Answer

Assuming XTAL =40 Mhz

since 40 Mhz clock will give 50 ms delay for the code shown


(a) Minimum we can make TMR0 =0x0000

max prescalar= 1/256

prescaled clock =1/256*40Mhz = 0.15625Mhz


max delay =65535/(0.15625x10^6)= 419.424ms . hence without modifying the code maximum we can give a delay 0f 419.424ms. by making TMR0H=0x00 and TMR0L=0 TCON=0x07

hence 3000ms is not possible


b) 4000 ms is also not possible as explained


c) To make it 8 bit timer we need to make 6 bit 1 TCON =0x21


d)

include <p18f4580.h>
void T0Delay(void);
#define mybit PORTBbits.RB4
void main(void)
{
TRISBbits.TRISB4=0;
while(1)
{
mybit^=1;
T0Delay();
}
}
void T0Delay()
{
T0CON=0x21;
TMR0L=0xEE;
T0CONbits.TMR0ON=1;
while(INTCONbits.TMR0IF==0);
T0CONbits.TMR0ON=0;
INTCONbits.TMR0IF=0;
}


e) No for 8 bit timer only TMR0L register is needed since only 8 but are required


f) with the same prescalar setting i.e. 1:4 and clock of 40 MHz


8 bits we can go maximum uptao 255

let frequency is f

255/f=50*10^-3

f=5100hz=0.0051Mhz (frequency required)


let prescalr be maximum 1/256

40000000*1/256= 0.15625Mhz (minimum frequency attainable)


since minimum frequency attainable> frequency required

50 ms can not be obtained directly

however what we can do is we can obatian 1 ms delay from 8 bit timer and call the delay 50 times

let value required for 1ms delay is a

so a/(0.15625*1000)=1ms a= 156 rounded

value in TMR0L =255-a =99 =0x63


void T0Delay(void);
#define mybit PORTBbits.RB4
void main(void)
{
TRISBbits.TRISB4=0;
while(1)
{
mybit^=1;
T0Delay();
}
}void T0Delay()
{

byte i=0

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

{

T0CON=0x27;//timer 8 bit prescalar 1:256
TMR0L=0x63;//value for 1ms delay
T0CONbits.TMR0ON=1;
while(INTCONbits.TMR0IF==0);
T0CONbits.TMR0ON=0;
INTCONbits.TMR0IF=0;
}

}


(g)

void T0Delay(byte a);
#define mybit PORTBbits.RB4
void main(void)
{
TRISBbits.TRISB4=0;
while(1)
{
mybit^=1;
T0Delay(10);
}
}void T0Delay(byte a) // a ms delay will be created
{

byte i=0

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

{

T0CON=0x27;//timer 8 bit prescalar 1:256
TMR0L=0x63;//value for 1ms delay
T0CONbits.TMR0ON=1;
while(INTCONbits.TMR0IF==0);
T0CONbits.TMR0ON=0;
INTCONbits.TMR0IF=0;
}

}