用硬件定时,程序如下:
#include <stdlib.h>#include <xdc/std.h>#include <xdc/runtime/System.h>
#include <ti/sysbios/BIOS.h>#include <ti/sysbios/knl/Task.h>#include <ti/sysbios/knl/Mailbox.h>#include <ti/sysbios/knl/Event.h>
/* Drivers */#include <ti/drivers/rf/RF.h>#include <ti/drivers/PIN.h>#include <driverlib/timer.h>#include <ti/drivers/Power.h>#include <ti/drivers/power/PowerCC26XX.h>
/* Board Header files */#include "Board.h"
PIN_Handle ledPinHandle;PIN_State ledPinState;
#define TASK_STACK_SIZE 1024#define TASK_PRIORITY 2
Hwi_Struct timerHwi;
void waitingTaskFunction(UArg arg0, UArg arg1);
static Task_Params waitingTaskParams;Task_Struct waitingTask;static uint8_t waitingTaskStack[TASK_STACK_SIZE];
void interruptTimerA(UArg arg0){ uint32_t status = TimerIntStatus(GPT0_BASE, true); PIN_setOutputValue(ledPinHandle, Board_LED1,1);
if (TIMER_TIMA_TIMEOUT & status) { TimerIntClear(GPT0_BASE, TIMER_TIMA_TIMEOUT); }
PIN_setOutputValue(ledPinHandle, Board_LED1,0);}
void timerInit(void){ /* Switches the peripheral power domain on */ Power_setDependency(PowerCC26XX_PERIPH_GPT0);
/* Prevents the controller from going to standby */ Power_setConstraint(PowerCC26XX_SB_DISALLOW);
// register ISR and enable hardware interrupt for timer Hwi_Params params; Hwi_Params_init(¶ms); params.enableInt = TRUE; Hwi_construct(&timerHwi, INT_GPT0A, &interruptTimerA, ¶ms, NULL);
/* Configure the timer hardware */ TimerDisable(GPT0_BASE, TIMER_A); TimerConfigure(GPT0_BASE, TIMER_CFG_SPLIT_PAIR | TIMER_CFG_A_PERIODIC | TIMER_CFG_B_PERIODIC); TimerPrescaleSet(GPT0_BASE, TIMER_A, 9); // prescaler is 256 – 187.5 kHz
TimerLoadSet(GPT0_BASE, TIMER_A, 100); // a random number
TimerIntClear(GPT0_BASE, TIMER_TIMA_TIMEOUT); TimerIntEnable(GPT0_BASE, TIMER_TIMA_TIMEOUT); TimerEnable(GPT0_BASE, TIMER_A);}
void WaitingTask_init(void){ Task_Params_init(&waitingTaskParams); waitingTaskParams.stackSize = TASK_STACK_SIZE; waitingTaskParams.priority = TASK_PRIORITY; waitingTaskParams.stack = &waitingTaskStack; Task_construct(&waitingTask, waitingTaskFunction, &waitingTaskParams, NULL);}
void waitingTaskFunction(UArg arg0, UArg arg1){ timerInit();
for (;;) { Task_sleep(30 *1000 / Clock_tickPeriod); // 30 ms wait PIN_setOutputValue(ledPinHandle, Board_LED2, 1); PIN_setOutputValue(ledPinHandle, Board_LED2, 0); }}
int main(void){ Board_initGeneral();
ledPinHandle = PIN_open(&ledPinState, BoardGpioInitTable); if (!ledPinHandle) { System_abort("Error initializing board LED pins\n"); }
WaitingTask_init(); BIOS_start(); return (0);}
当定时时间设置在1ms以上时,程序运行正常;
当定时时间小于100us后,时间不准,时快时慢,而且不随设置变化,毫无规律;
请各位大侠帮忙!
多谢!
mingfeng fan:
TI工程师,帮忙看看是啥问题啊!
多谢!
mingfeng fan:
回复 mingfeng fan:
TI工程师,各位大侠:
请帮忙提供一个定时时间小于20us的例程。不胜感谢!