Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Class Worked Examples

LED Sequencing

1. Blinking the 8-LEDs in PORT B

void main()
{
trisb = 0x00;
portb = 0x00;

while (1)
{
{
portb = 0xff;
delay_ms(250);
portb = 0x00;
delay_ms(250);
}
}
}

2. Making ON the first four LEDs and OFF the other four LEDs and vice versa

void main()
{
trisb=0x00;
portb=0x00;

while (1)
{
{
portb = 0x0f;
delay_ms(250);
portb = 0xf0;
delay_ms(250);
}
}
}

3. Making ON sequentially all the 8 - LEDs in PORT B

int i=0;

void main()
{
trisb=0x00;
portb=0x00;
delay_ms(250);

while (1)
{
for (i=0; i<8; i++)
{
portb = portb | 0x01<<i;
delay_ms(250);
}
if (i == 8)
{
portb=0x00;
delay_ms(250);
}
}
}

4. Making OFF sequentially all the 8 - LEDs in port B


int i=0;

void main()
{
trisb=0x00;
portb=0xff;
delay_ms(250);

while (1)
{
for (i=0; i<8; i++)
{
portb = portb ^ 0x01<<i;
delay_ms(250);
}
if (i == 8)
{
portb = 0xff;
delay_ms(250);
}
}
}

5. Making ON only one LED sequentially at a time from the 8 - LEDs in port B
(Hint: initially all LEDs are OFF)
int i=0;

void main()
{
trisb=0x00;
portb=0x00;
delay_ms(250);

while (1)
{
for (i=0; i<8; i++)
{
portb = 0x00 | 0x01<<i;
delay_ms(250);
}
if (i == 8)
{
portb = 0x00;
delay_ms(250);
}
}
}

6. Making OFF only one LED sequentially at a time from the 8 - LEDs in port B
(Hint: initially all LEDs are ON)
int i=0;

void main()
{
trisb=0x00;
portb=0xff;
delay_ms(250);

while (1)
{
for (i=0; i<8; i++)
{
portb = 0xff ^ 0x01<<i;
delay_ms(250);
}
if (i == 8)
{
portb = 0xff;
delay_ms(250);
}
}
}

You might also like