Knight Rider Circuit for AVR Micro controller

I crafted a code snippet which would give us the lighting sequence of famous Knight Rider car. This would work in Atmel ATMega 16 series micro controller and the program consumes only 266 bytes. Following goes the code.

#include <avr/io.h>

typedef unsigned char  u08;

int main( void )
{

u08 led, i, j, k,flag;
DDRC = 0xff;

led = 1;
flag = 1;

for (;;)
{
PORTC = led;
if(flag)
led = led<<1;

else
led = led>>1;

if (!led && flag)
{
led = 128;
flag = 0;
}

if (!led && !flag)
{
led = 1;
flag = 1;
}

for (i=0; i<127; i++)   /* outer delay loop */
for(j=0; j<127;j++) /* inner delay loop */
k++;

}
}
The speed of the lighting sequence can be altered by adjusting the i and j values of the outer or inner loops. These loops will provide some delay for the circuit. If anybody who wishes to improve this code, go ahead and enjoy.

Advertisements