Hi,
I'm working with a Timer32 to create a simple delay API to use for my future projects.
It seems to work with 1s duration (I don't have an oscilloscope at hand to check for smaller values..).
I would like some advices about the code (Is it enough to work with any ms/µs duration values?), and if a better & easier way to do a delay exists (Maybe with the Timer32 interrupt, or even without a Timer32?).
Remark: the minimum delay duration needed for my future projects is 10µs.
#include "driverlib.h"
void delay_init(void)
{
Timer32_initModule(TIMER32_0_BASE, TIMER32_PRESCALER_1, TIMER32_32BIT, TIMER32_PERIODIC_MODE);
Timer32_disableInterrupt(TIMER32_0_BASE);
}
void delay(uint32_t duration_us)
{
Timer32_haltTimer (TIMER32_0_BASE);
Timer32_setCount (TIMER32_0_BASE, 24 * duration_us);
Timer32_startTimer(TIMER32_0_BASE, true);
while (Timer32_getValue(TIMER32_0_BASE) > 0);
}
int main(void)
{
WDT_A_holdTimer();
CS_setDCOCenteredFrequency(CS_DCO_FREQUENCY_24); // 24000000 Hz
CS_initClockSignal(CS_MCLK, CS_DCOCLK_SELECT, CS_CLOCK_DIVIDER_1); // 24000000 Hz
GPIO_setAsOutputPin (GPIO_PORT_P2, GPIO_PIN0);
GPIO_setOutputLowOnPin(GPIO_PORT_P2, GPIO_PIN0);
delay_init();
while(1)
{
delay(1 * 1000 * 1000); // 1s
GPIO_toggleOutputOnPin(GPIO_PORT_P2, GPIO_PIN0);
}
}
Thanks,
Michaël