×

Embedded Systems Tutorial

AVR Microcontroller

8051 Microcontroller

8085 Microprocessor

8086 Microprocessor

8255 Microprocessor

Embedded Systems Practice

LED Blinking Program Using 8051 Microcontroller & Keil C

In this tutorial, we will learn about the most basic and introductory program in embedded C by flashing the LED of the 8051 microcontroller. By Sudarshan Paul Last updated : May 13, 2023

LED Blinking using 8051 Microcontroller and Keil C

#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++)
  }
}

Code Explanation

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!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.