can anyone help me on how to make this program short? what this program does is that its Initially on : R=1x1 off to the right, L=1x1 off to the left, G= night rider one cycle, S= off. This kind of program is made to control a micro controller...i need your help as soon as possible...thanks

#include <reg420.h>
#include <stdio.h>
#include <ctype.h>
#include "serial.h"

void delay();
void delay2();

void main()
{
	char o;
	InitSerialHardware();

	do {
		o = getchar();
		P1 = 0xFF;

		if(isspace(o)) continue;
		o = toupper(o);

		if(o == 'S')
		{
			puts(" Stop");
			P1 = 0;
		}

		if(o == 'L')
		{
			puts(" Left");
			P1 = 0x01;
			delay();
			P1 = 0x02;
			delay();
			P1 = 0x04;
			delay();
			P1 = 0x08;
			delay();
			P1 = 0x10;
			delay();
			P1 = 0x20;
			delay();
			P1 = 0x40;
			delay();
			P1 = 0x80;
			delay();
		}

		if(o == 'R')
		{
			puts(" Right");
			P1 = 0x80;
			delay();
			P1 = 0x40;
			delay();
			P1 = 0x20;
			delay();
			P1 = 0x10;
			delay();
			P1 = 0x08;
			delay();
			P1 = 0x04;
			delay();
			P1 = 0x02;
			delay();
			P1 = 0x01;
			delay();
		}

		if(o == 'G')
		{
			puts(" Go Night Rider");
			P1 = 0x01;
			delay2();
			P1 = 0x02;
			delay2();
			P1 = 0x04;
			delay2();
			P1 = 0x08;
			delay2();
			P1 = 0x10;
			delay2();
			P1 = 0x20;
			delay2();
			P1 = 0x40;
			delay2();
			P1 = 0x80;
			delay2();
			P1 = 0x80;
			delay2();
			P1 = 0x40;
			delay2();
			P1 = 0x20;
			delay2();
			P1 = 0x10;
			delay2();
			P1 = 0x08;
			delay2();
			P1 = 0x04;
			delay2();
			P1 = 0x02;
			delay2();
			P1 = 0x01;
			delay2();
		} cx
		else	continue;

	} while (1);
}

void delay()
{
	int x, y;
	for(x=0;x<255;x++)
	{
		for(y=0;y<255;y++)
		{
		}
	}
}

void delay2()
{
	int x, y;
	for(x=0;x<200;x++)
	{
		for(y=0;y<100;y++)
		{
		}
	}
}

Recommended Answers

All 4 Replies

what's the significance of those delay functions? What is P1 because it isn't defined anywhere in the code you posted.

for case G:

int a1 = 0x01;
for(int i = 0; i < 8; i++)
{
    P1 = a1;
    a1 <<= 1;
    delay();
}

or you could create int arrays to hold the values for each case.

What compiler are you using for this?

the delay function slows down the function to make it visible to human eyes. because machine running time is different from our time thats why we use delays...P1 is a pre declared variable for LEDs in our PCB..WE use kiel microvision, its interface is similar to c++

what operating system will this program run on ? and what compiler are you using. I guess there is probably something else such as another thread that does something with P1 to make the LEDs change.

Do you have an os-function similar to sleep() and Sleep() available ? Loops are a terrible way to implement a delay function.

what operating system will this program run on ? and what compiler are you using.

WE use kiel microvision...

Keil is a firmware compiler, so the program is the system.

I guess there is probably something else such as another thread that does something with P1 to make the LEDs change.

P1 is the LEDs. Set P1 and the LEDs change. It would be hardware mapped

Do you have an os-function similar to sleep() and Sleep() available ? Loops are a terrible way to implement a delay function.

Agreed. It's been a while so I don't remember the functions but I'm sure there is a delay function that accepts milliseconds as input. Ask someone that knows the compiler what that function is.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.