Writing an Embedded C Program to Flash an LED Connected to a Port Pin at 500ms Rate

30 Apr 2023 Balmiki Mandal 0 Embedded C

Embedded C Programming to Flash an LED Connected to a Port Pin

If you're looking for a way to flash an LED connected to a port pin at a rate of 500ms, embedded C programming could be the answer. This tutorial will show you how to write a program in embedded C that can do just that.

Before we dive into writing an embedded C program, it's important to understand the basics of how the LED is connected to the port pin. If you're using an active low circuit, the LED will connect to the ground pin on the port and the port pin will act as the positive. This means the LED will be on when the port pin is LOW and off when the port pin is HIGH.

Writing the Program

To write a program in embedded C to flash the LED attached to the port pin, we need to understand the process that needs to happen. First, we need to define the port pin we want our LED to be connected to. Then we need to configure that port pin as an output. This will allow us to control the LED using the port pin. Then, we need to write a loop that will enable us to toggle the port pin between high and low, at the rate of 500ms. Finally, we will add a delay to the loop so that the LED will toggle at the rate of 500ms.

EXAMPLE 01

Let's take a look at the embedded C code required to complete this project:

#include <avr/io.h>  // library containing I/O definitions 
#define MYLED_PORT PORTB  // define the port to which the LED is attached
#define MYLED_DDR DDRB    // define data direction register to control port
 
int main() 
{
    //configure the led port pin as an output 
    MYLED_DDR |= (1<<PB0); //set port pin PB0 as output 
 
    while(1) 
    {
        //toggle the port pin at 500ms intervals 
        MYLED_PORT ^= (1<<PB0);// xor with mask to toggle the bit 
 
        _delay_ms(500);
    }
    return 0;
}

This code will take care of toggling the port pin between high and low at the rate of 500ms. All that's left to do is compile the program and upload it to the microcontroller, and you should be all set!

Conclusion

By following this tutorial, you should now know how to write an embedded C program to flash an LED connected to a port pin at a rate of 500ms. Although it requires some knowledge and understanding of C programming, the end result is worth it. Have fun playing around with your new circuit!

 

EXAMPLE 02

here's an example 02 of an embedded C program that flashes an LED connected to a port pin at the rate of 500ms:

#include <avr/io.h>
#include <util/delay.h>

#define LED_PIN PB0  // replace with your LED pin

int main(void)
{
    // set LED pin as output
    DDRB |= (1 << LED_PIN);
    
    while(1)
    {
        // turn LED on (active low)
        PORTB &= ~(1 << LED_PIN);
        
        // delay for 500ms
        _delay_ms(500);
        
        // turn LED off
        PORTB |= (1 << LED_PIN);
        
        // delay for 500ms
        _delay_ms(500);
    }
    
    return 0;
}

In this program, we use the delay.h library provided by AVR-GCC to generate the 500ms delay. The LED is connected to the PB0 pin (replace with your LED pin), which is set as an output using the DDRB register.

To flash the LED, we use a simple loop that turns the LED on, waits for 500ms using _delay_ms, turns the LED off, and waits for another 500ms using _delay_ms. Note that since the LED is active low, we turn it on by setting the pin low and turn it off by setting the pin high.

Here's an example delay.h library that provides a delay_ms function:

#ifndef DELAY_H
#define DELAY_H

#include <stdint.h>

void delay_ms(uint16_t count)
{
    while(count--)
    {
        // 1ms delay using __builtin_avr_delay_cycles
        __builtin_avr_delay_cycles(12000);
    }
}

#endif  /* DELAY_H */

This library provides a delay_ms function that uses the __builtin_avr_delay_cycles function to generate a 1ms delay. The argument to delay_ms specifies the number of milliseconds to delay. The actual delay time may vary depending on the clock frequency and other factors. In this example, we assume a clock frequency of 12MHz.

BY: Balmiki Mandal

Related Blogs

Post Comments.

Login to Post a Comment

No comments yet, Be the first to comment.