Read the code and the comments carefully to understand each step of the pr ogram
ID: 3740261 • Letter: R
Question
Read the code and the comments carefully to understand each step of the pr ogram. The program uses a simple loop (ik100000000) loop as a delay to slow the changes sent to the LEDs to allow us to see them. As a first exercise, modify the for loop to wait a much shorter time by changing the value 100000000 to 1000 and run the program again. You will see that the speed has increased to the point where all the LEDs appear to be lit and flashing. /* Define 8051 registers / /* Delay variable */ /* Loop forever */ # include void main (void) unsigned int i while (1) P3 0x00; for (i-100000000; P0 = 0xff; for (i=100000000; PO0x00: LED Port P3 * /* Output FF to the LED Port */ / Output 00 to the LED Port */ /* End while(1) loop */ i0; i) i>0; ?--) / End of main / Exercise 1 Extend the supplied example to light all LEDs. Later, in exercise 5, you will need to create a rotating bit pattern, rather than one going up and then down. This will be harder as the variable you'll be using will hold 16-bits, but you will only be working with the bottom 8.Explanation / Answer
1) Light all LEDs
#include<reg51.h>
void Delay(unsigned int);
void main(void)
{
while(1)
{
P3=0x00; //Using Port3 as Output
//Light all LEDs
p0=oxff;
P1=0xff;
P2=0xff;
Delay(1000);
p0=ox00;
P1=0x00;
P2=0x00;
Delay(1000);
}
}
void Delay(unsigned int itime)
{
unsigned int i,j;
for(i=0;i<itime;i++)
for(j=0;j<1275;j++);
}
2) ROTATING BIT PATTERN
#include<reg51.h>
void Delay(unsigned int);
void main(void)
{
P1=0x55;
while(1)
{
P1 << 1; //Shift left once ( Rotating Pattern)
Delay(1000);
}
}
void Delay(unsigned int itime)
{
unsigned int i,j;
for(i=0;i<itime;i++)
for(j=0;j<1275;j++);
}
Toggling pins of port 1
#include<reg51.h>
void Delay(unsigned int);
void main(void)
{
while(1)
{
P1=0x55; // 55H(01010101)
Delay(1000);
P1=0xAA; // AAH(10101010)
Delay(1000);
}
}
void Delay(unsigned int itime)
{
unsigned int i,j;
for(i=0;i<itime;i++)
for(j=0;j<1275;j++);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.