AVR & 8051 User Defined Functions

This entry is part 3 of 6 in the series 8051 MCU Projects

The user defined functions play a very important part in simplifying the coding structure of AVR and 8051. Following are the simple guidelines as to how you can create your own user defined functions (UDFs) to reduce typing and make the code very compact.

Examples of AVR UDFs

    
        void Forward()
	{
	   PORTB=0b00001001;
	}

	void Backward()
	{
	   PORTB=0b00000110;
	}

	void SoftLeft()
	{
	   PORTB=0b00000001;
	}

	void SoftRight()
	{
	   PORTB=0b00001000;
	}

	void PowerLeft()
	{
	   PORTB=0b00000101;
	}

	void PowerRight()
	{
	   PORTB=0b00001010;
	}

Examples of 8051 UDFs

   
        void Forward()
	{
	   P1=0x09;
	}

	void Backward()
	{
	   P1=0x06;
	}

	void SoftLeft()
	{
	   P1=0x01;
	}

	void SoftRight()
	{
	   P1=0x08;
	}

	void PowerLeft()
	{
	   P1=0x05;
	}

	void PowerRight()
	{
	   P1=0x0A;
	}

Download the .txt files of both these UDFs codes by clicking following buttons.

AVR UDF 8051 UDF

More Guidelines on AVR UDFs

Create more detailed UDFs in AVR using following guidelines. Read the codes very carefully. You can equally apply these codes to 8051 coding also.

	void All_LEDs_Flash() // function for flashing all LEDs simultaneously
			      // To flash all LEDs connect only PC0 to Vcc
	{ 

		PORTB=0b11111111;
		_delay_ms(1000)
		PORTB=0b00000000;
		_delay_ms(1000);
	}

	void Forward_Backward() // function for forward backward robot
				// To move robot forward backward
				// connect only PC1 to Vcc
	{ 
		PORTB=0b00001001;
		_delay_ms(5000);
		PORTB=0b00000110;
		_delay_ms(5000);
	}

	void BLFR() // function for black line following robot
	            // To use robot as BLFR connect only PC2 to Vcc
	{
	
	if((LS==0b00000000)&(RS==0b00000000)) // when both sensors are on black
	{
		PORTB=0b00000000; // stop
	}

	if((LS==0b10000000)&(RS==0b00000000)) // LS on white, RS on black
	{
		PORTB=0b00001000; // soft right turn
	}

	if((LS==0b00000000)&(RS==0b0000010)) // RS on white, LS on black
	{
		PORTB=0b00000001; // soft left turn
	}

	if((LS==0b10000000)&(RS==0b00000010)) // both on white
	{
		PORTB=0b00001001; // go forward
	}

	} // BLFR function closed

	void Edge_Avoiding_Robot() // function for edge avoiding robot
 				   // To use robot as edge avoiding robot
				   // connect only PC3 to Vcc
	{
	
	if((LS==0b00000000)&(RS==0b00000000)) // when both sensors are out of edge
	{
		PORTB=0b00000000; // stop
		_delay_ms(1000);
		PORTB=0b00000110; // backward
		_delay_ms(500);
		PORTB=0b00000000; // stop again
		_delay_ms(1000);
		PORTB=0b00001010; // power right turn
		_delay_ms(400);
		PORTB=0b00000000; // stop again
		_delay_ms(500);
	}

	if((LS==0b10000000)&(RS==0b00000000)) // LS on table, RS on edge
	{
		PORTB=0b00000000; // stop
		_delay_ms(1000);
		PORTB=0b00000110; // backward
		_delay_ms(500);
		PORTB=0b00000000; // stop again
		_delay_ms(1000);
		PORTB=0b00000101; // power left turn
		_delay_ms(400);
		PORTB=0b00000000; // stop again
		_delay_ms(500);
	}

	if((LS==0b00000000)&(RS==0b0000010)) // RS on table, LS on edge
	{
		PORTB=0b00000000; // stop
		_delay_ms(1000);
		PORTB=0b00000110; // backward
		_delay_ms(500);
		PORTB=0b00000000; // stop again
		_delay_ms(1000);
		PORTB=0b00001010; // power right turn
		_delay_ms(400);
		PORTB=0b00000000; // stop again
		_delay_ms(500);
	}

	if((LS==0b10000000)&(RS==0b00000010)) // both LS, RS are on table
	{
		PORTB=0b00001001; // go forward
	}

	} // function of edge avoiding robot closed

	void Obstacle_Avoiding_Robot() // function for obstacle avoiding robot
					// here connect only one IR sensor 
					// at the middle to PA4
					// To use robot as obstacle avoiding robot									// connect only PC4 to Vcc

	{
	if(MS==0b00000000) // no obstacle infront of middle sensor
	{
		PORTB=0b00001001; // go forward
	}

	if(MS==0b00010000) // obstacle detected
	{
		PORTB=0b00000000; // stop
		_delay_ms(1000);
		PORTB=0b00000110; // backward
		_delay_ms(500);
		PORTB=0b00000000; // stop again
		_delay_ms(1000);
		PORTB=0b00001010; // power right turn
		_delay_ms(400);
		PORTB=0b00000000; // stop again
		_delay_ms(500);
	}

	} // function of obstacle avoiding robot closed

	void Clap_Controlling_Robot() // function for sound controlled robot
				      // connect output of sound sensor
				      // to PA6 of PORTA
				      // Also connect PC5 to Vcc 
				      // to control robot with claps
	{
	if(Clap==0b01000000) // clap sound received
	{
		PORTB=0b00001010; // power right turn
		_delay_ms(400);
	}

	else // no clap sound
	{
		PORTB=0b00001001; // go forward
	}

	} // clap controlling robot function closed

/*
	==== IMPORTANT NOTE ====
	In this way, you can add number of such user defined functions
	here in this file and call them in your main program file
	of "main_code.c"
*/

Download .txt file of guideline code by clicking here.

Series Navigation<< Distance Learning 8051 RoboticsStarting fresh new batch of advanced robotics using 8051 microcontroller >>