| Written by Amol Shah, on Aug-2008 |
|
|
Here is a code for generating software based delay i.e. loading a specific value in a register and decrementing it till it becomes zero. One of the most important drawbacks of delay subroutine is that the delay changes as you change the crystal and then again you have to calculate the new values. Here is a code where you just have to load the crystal value and the internal clock divider (12, 6, 2, and 1) and the code itself calculates the exact clock cycles required for 1 MS delay. DELAY.H#ifndef __DELAY_H #define __DELAY_H #define XTAL 11.059200 //Crystal frequency #define XDIVIDER 12.0 //Divider #define delay_1ms 164 * (XTAL/XDIVIDER) void delay(unsigned char j); void delay_ms(unsigned int i); #endif DELAY.Cvoid delay(unsigned char j) { for (;j!=0x00;j--) { } } void delay_ms(unsigned int i) { for(;i!=0x00;i--) { delay(delay_1ms); } } The function delay_ms (unsigned int i) generates precise delay in MS. NOTE: Change the XTAL value and the XDIVIDER according to the crystal and controller used respectively.
Users' Comments (0)
|
|
|