Home » Embedded Systems

Hello world program for 8051 Microcontroller

In this section we will discuss about the most basic and introductory program in embedded C by flashing the LED of the 8051 microcontroller.
Submitted by Sudarshan Paul, on June 21, 2018

‘Hello World’ Embedded test program for 8051

#include <reg52.h>
sbitLED_pin = P1^5;
bit LED_state_G;

void LED_FLASH_Init(void);
void LED_FLASH_Change_State(void);
void DELAY_LOOP_Wait(const unsigned int);

void main(void)
{
	LED__FLASH_Init();

	while (1)
	{
		LED_FLASH_Change_State();
		DELAY_LOOP_Wait(1000);
	}
}

void LED_FLASH_Init(void)
{
	LED_state_G=0;
}

void LED_FLASH_Change_State(void)
{
	if (LED_state_G ==1)
	{
		LED_state_G = 0;
		LED_pin = 0;
	}
	else
	{
		LED_state_G = 1;
		LED_pin =1;
	}
}

void DELAY_LOOP_Wait(const unsigned int DELAY)
{
	unsigned int x, y;
	for (x=0 ; x<= DELAY; x++)
	{
		for (y=0; y<=120 : y++)
	}
}

Explanation of various sections of the code in detail

1) The Super Loop

In this function, we use a super loop in which we alternately call two functions LED_FLASH_Change_State(), and DELAY_ LOOP_Wait(). One functions is used to flash the LED by changing the state of glow and the other function is used to insert a delay of 1 second in between the interval. We declare few variables before using the super loop.

2) Using function to change the state of LED, LED_FLASH_Change_State()

On execution of this function, the state of L.E.D. changes on the specified port pin. If you want one single flash of LED the function must be called twice. Since the LED will be ON for 0.5 seconds and OFF for 0.5 seconds. So as to deliver the required numbers of hertz of flash( in this case 1Hz) we require this function to be called double the number.

3) Using function DELAY_LOOP_Wait()

The delay duration depends upon the parameter. Parameter for an 8051 microcontroller is roughly around 12MHz (12 oscillatory cycles). The delay values are usually determined by trial and error. The delay may vary depending upon the compiler optimization settings and other factors. You can check the delay duration for your compiler. If it is keil simulator, you can view the delay using the performance analyzer when your simulation is running.






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.
Learn PCB Designing: PCB DESIGNING TUTORIAL




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.