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

code for 8051 using Keil. Why in my code, R0 is changing with the tiem passing t

ID: 3746211 • Letter: C

Question

code for 8051 using Keil. Why in my code, R0 is changing with the tiem passing the R1 is still 0?

#include <reg51.h>

void MsDelay(unsigned short);

void main(void){

int i=0;

while(1){

P0=i;

if(P0==0)

P1=0;

else if(P0<=3&&P0>0)

P1=1;

else if(P0<=15&&P0>3)

P1=2;

else if(P0<=63&&P0>15)

P1=4;

else if(P0<=255&&P0>63)

P1=8;

else

P1=255;

MsDelay(100);

i=i+10;

}

}

void MsDelay(unsigned short MsTime){

unsigned short i,j;

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

for(j=0;j<1275;j++);

}

Explanation / Answer

In 8051 timer, R0 and R1 are like the parameters. R0 is the timer count and R1 is the starting point.So, when timer starts, R0 will copy the value of R1 and starts incrementing untill it reaches maximum value so generate an interrupt.So, for example , if I want less time value, then we can increase the of R1 , then R0 count will start from there, decreasing the time. So, R1 will always be stagnant and whereas R0 will be incrementing with time