Embedded C Programming, 10mSec Delay, 1mSec Delay Frequency, 12 MHz
How to Create 10ms Delay Using Embedded C Programming and 1ms Delay Frequency of 12MHz
In embedded c programming, generating delays is a very common task. In many cases, a precise delay is required, which depends on the clock speed of the microcontroller. In this article, we will discuss how to use embedded C programming to generate 10 milliseconds of delay, using 1 millisecond delay frequency of 12MHz.
Step 1: Calculate the Number of Cycles Required
Since the clock speed is 12MHz, it means that one cycle will take 1/12000000 seconds, or 0.000000833 seconds. To create a 10 milliseconds delay, we need to calculate how many cycles are required for a 10 milliseconds duration. To do that, we first multiply 10 milliseconds with 1000 to convert it into microseconds, i.e. 10000 microseconds. Now we divide the duration in microseconds with the duration of a single cycle and the result will be the number of cycles required for a 10 milliseconds delay.
Step 2: Creating the Delay Loop
Now that we have calculated the number of cycles required for a 10 milliseconds delay, we can create a for loop that runs for the required number of cycles. Since each cycle takes 0.000000833 seconds, we can determine the total time taken by the loop with the following equation: Total Time Taken = Cycle Time x No. of Cycles.
So the code for the delay loop will look like this:
for (int i=0; i < num_cycles; i++)
{
//delay by 1 millisecond
_delay_ms(1);
}
The above code will generate a 10 milliseconds delay, as we have calculated the number of cycles required and written the loop accordingly.
Conclusion
In this article, we discussed how to create a 10 milliseconds delay using embedded C programming and 1 millisecond delay frequency of 12MHz. We started by calculating the number of cycles required for a 10 milliseconds delay. We then wrote a loop to generate the delay as per the number of cycles calculated. By following these steps, you can easily generate a precise 10 milliseconds delay using embedded C programming.
Example 02
To Generate a 10ms 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 10ms delay using Timer 0 of an 8051 microcontroller:
#include <reg52.h>
// Function to initialize Timer 0
void init_timer0(void)
{
TMOD |= 0x01; // Set Timer 0 in mode 1 (16-bit timer)
TH0 = 0xF0; // Set initial value of high byte for 10ms delay at 12MHz
TL0 = 0x8F; // Set initial value of low byte for 10ms delay at 12MHz
TR0 = 1; // Start Timer 0
}
// Function to generate 10ms delay using Timer 0
void delay_10ms(void)
{
unsigned int i, j;
for(i = 0; i < 10; i++) // Loop to generate 10ms delay
{
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 10ms delay and toggle an LED
P1 ^= 0x01; // Toggle LED connected to P1.0
delay_10ms(); // Generate a 10ms delay
}
}
In the code above, the init_timer0() function initializes Timer 0 with the values for a 10ms delay at 12MHz, and starts the timer. The delay_10ms() function uses a nested loop to generate a delay of 10ms by waiting for Timer 0 to overflow 10 times, with a 1ms delay generated each time using the same method as in the previous example. The main function toggles an LED connected to P1.0 every 10ms using the delay_10ms() 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.