BM Balmiki Mandal 30 Apr 2023 2 views 0 comments Embedded C

embedded c program, 12MHz crystal, 1ms time delay, generating time delay

Writing an Embedded C Program to Generate 1ms Time Delay by Using 12MHz Frequency

To generate a 1ms time delay using a 1ms delay frequency and a 12MHz clock in Embedded C, you can use a timer module in the microcontroller. Here's an example code that demonstrates how to generate a 1ms delay using Timer 0 of an 8051 microcontroller:

Steps:

  1. Include the necessary header files.
  2. Set the frequency value to 12MHz.
  3. Calculate the amount of clock cycles needed to generate the desired time delay.
  4. Create a loop that runs for the total amount of clock cycles.
  5. Within the loop, use the delay function to wait for a fixed interval of time.

The following is an example of the code required to generate a 1ms time delay using an embedded c programming language and a 12MHz frequency.

#include <reg52.h>

// Function to initialize Timer 0
void init_timer0(void)
{
    TMOD |= 0x01;   // Set Timer 0 in mode 1 (16-bit timer)
    TH0 = 0xFC;     // Set initial value of high byte for 1ms delay at 12MHz
    TL0 = 0x18;     // Set initial value of low byte for 1ms delay at 12MHz
    TR0 = 1;        // Start Timer 0
}

// Function to generate 1ms delay using Timer 0
void delay_ms(unsigned int time_ms)
{
    unsigned int i, j;
    for(i = 0; i < time_ms; i++)
    {
        for(j = 0; j < 125; j++) // Loop to generate 1ms delay at 12MHz
        {
            while(!TF0);    // Wait until Timer 0 overflows
            TF0 = 0;        // Reset Timer 0 overflow flag
        }
    }
}

// Main function
void main()
{
    init_timer0();  // Initialize Timer 0
    while(1)
    {
        // Generate a 1ms delay and toggle an LED
        P1 ^= 0x01;     // Toggle LED connected to P1.0
        delay_ms(1000); // Generate a 1s delay
    }
}

In the code above, the init_timer0() function initializes Timer 0 with the values for a 1ms delay at 12MHz, and starts the timer. The delay_ms() function uses a nested loop to generate a delay of the specified number of milliseconds by waiting for Timer 0 to overflow. The main function toggles an LED connected to P1.0 every 1 second using the delay_ms() function.

Note that the specific values for TH0 and TL0 used in the init_timer0() function may vary depending on the exact microcontroller being used and the desired delay frequency.

0 Comments

No comments yet, Be the first to comment.

Post a Comment

Please sign in to post a comment.

Related Posts

Introduction to D Programming

Introduction to D Programming D is a relatively new programming language that has been gaining popularity amo...

BM Balmiki Mandal 17 Aug 2023
Level triggered interrupt, edge triggered interrupt, comparison, hardware system, interrupt types

What are Level Triggered Interrupts and Edge Triggered Interrupts? Level triggered interrupts and edge trigge...

BM Balmiki Mandal 30 Apr 2023
What are External Hardware Interrupts?

External Hardware Interrupts Explained External hardware interrupts provide a way for devices to interrupt th...

BM Balmiki Mandal 30 Apr 2023