Jeremy,
Thanks for the reply.
I've changed to a 824 & have got a working project running now.
However I've found that I need to stop & restart the counter to achieve consistent timings. Using this technique however it looks like the minimum time I can get from starting the timer, it reaching a limit & triggering the event (setting an output high in state 1) is approx 5ms. I don't see anything in the manual mentioning delays between starting the counter & it running so its difficult to understand the delay. Ideally I'd like to achieve a minimum delay of 500uSec. I attach the project & the main file below.
#include "fsl_debug_console.h"
#include "board.h"
#include "fsl_sctimer.h"
#include "fsl_gpio.h"
#include "pin_mux.h"
#include "fsl_inputmux.h"
#include <stdbool.h>
#define SCTIMER_CLK_FREQ CLOCK_GetFreq(kCLOCK_Irc)
#define DEMO_FIRST_SCTIMER_OUT kSCTIMER_Out_2
#define DEMO_SECOND_SCTIMER_OUT kSCTIMER_Out_4
#define INPUTSYNC0 (0U)
#define INPUTSYNC1 (1U)
#define INPUTSYNC2 (2U)
#define INPUTSYNC3 (3U)
int main(void)
{
sctimer_config_t sctimerInfo;
uint32_t riseEvent, eventCounterL, fallEvent, riseEvent2;
uint32_t stateNumber;
uint32_t sctimerClock;
uint32_t matchValueL;
CLOCK_EnableClock(kCLOCK_Uart0);
CLOCK_SetClkDivider(kCLOCK_DivUsartClk, 1U);
INPUTMUX_Init(INPUTMUX);
INPUTMUX_AttachSignal(INPUTMUX, 0U, kINPUTMUX_SctPin0ToSct0);
BOARD_InitPins();
BOARD_BootClockIRC12M();
BOARD_InitDebugConsole();
CLOCK_EnableClock(kCLOCK_Sct);
sctimerClock = SCTIMER_CLK_FREQ;
PRINTF("\r\nAC Fan Controller using an LPC824 & SCT\r\n");
SCTIMER_GetDefaultConfig(&sctimerInfo);
#if defined(SCTIMER_NEED_CHANGE_CLOCK_SOURCE)
sctimerInfo.clockMode = DEMO_CLOCK_MODE;
sctimerInfo.clockSelect = DEMO_CLOCK_SEL;
#endif
sctimerInfo.enableCounterUnify = false;
matchValueL = USEC_TO_COUNT(1000U, sctimerClock);
sctimerInfo.prescale_l = matchValueL / 65536;
matchValueL = matchValueL / (sctimerInfo.prescale_l + 1) - 1;
SCTIMER_Init(SCT0, &sctimerInfo);
stateNumber = SCTIMER_GetCurrentState(SCT0);
void SCTIMER_COUNTER_HANDLER()
{
SCT0->CTRL |= (1 << SCT_CTRL_CLRCTR_L_SHIFT);
}
if (SCTIMER_CreateAndScheduleEvent(SCT0, kSCTIMER_InputRiseEvent, 0u, kSCTIMER_Input_0, kSCTIMER_Counter_L,
&riseEvent) == kStatus_Fail)
{
return -1;
}
SCTIMER_SetupCounterStartAction(SCT0, kSCTIMER_Counter_L,riseEvent);
SCTIMER_SetupNextStateAction(SCT0, stateNumber + 1, riseEvent);
SCTIMER_IncreaseState(SCT0);
if (SCTIMER_CreateAndScheduleEvent(SCT0, kSCTIMER_MatchEventOnly, matchValueL, 0, kSCTIMER_Counter_L,
&eventCounterL) == kStatus_Fail)
{
return -1;
}
SCTIMER_SetupOutputSetAction(SCT0, kSCTIMER_Out_0, eventCounterL);
SCTIMER_SetupNextStateAction(SCT0, stateNumber + 2, eventCounterL);
SCTIMER_SetupCounterStopAction(SCT0,kSCTIMER_Counter_L,eventCounterL);
SCTIMER_EnableInterrupts(SCT0,(1 << eventCounterL));
SCTIMER_SetCallback(SCT0, SCTIMER_COUNTER_HANDLER, eventCounterL);
SCTIMER_IncreaseState(SCT0);
if (SCTIMER_CreateAndScheduleEvent(SCT0, kSCTIMER_InputFallEvent, 0u, kSCTIMER_Input_0, kSCTIMER_Counter_L,
&fallEvent) == kStatus_Fail)
{
return -1;
}
SCTIMER_SetupOutputClearAction(SCT0, kSCTIMER_Out_0, fallEvent);
SCTIMER_SetupNextStateAction(SCT0, stateNumber, fallEvent);
if (SCTIMER_CreateAndScheduleEvent(SCT0, kSCTIMER_InputRiseEvent, 0u, kSCTIMER_Input_0, kSCTIMER_Counter_L,
&riseEvent2) == kStatus_Fail)
SCTIMER_SetupOutputSetAction(SCT0, kSCTIMER_Out_0, riseEvent2);
EnableIRQ(SCT0_IRQn);
SCTIMER_StartTimer(SCT0, kSCTIMER_Counter_L);
volatile static int i = 0 ;
while(1) {
i++ ;
for(int i=0; i < 5000000; i++);
PRINTF(".");
__asm volatile ("nop");
}
}