How to count external pulses with SCTimer?

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

How to count external pulses with SCTimer?

2,052 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by rsg on Tue May 31 13:37:48 MST 2016
Ugh!  All I want to do is configure the SCT peripheral to count rising edges of a tach signal that is input on a pin.  I would like to read the count periodically to determine the RPM of a motor.  I don't need all these states and transitions!  As is often the case with fancy peripherals, it seems the simplest uses get no documentation!

So far, this is the extent of my code; the count never seems to change.  Any help would be greatly appreciated!
    // enable clock to and reset peripheral
    Chip_Clock_EnablePeriphClock(SYSCTL_CLOCK_SCT);
    Chip_SYSCTL_PeriphReset(RESET_SCT);

    /* From the manual:
     * The SCT can be used as standard counter/timer with external capture
     * inputs and match outputs without using the state logic. To operate the
     * SCT without states, configure the SCT as follows:
     *   • Write zero to the STATE register (zero is the default).
     *   • Write zero to the STATELD and STATEV fields in the EVCTRL registers
     *     for each event.
     *   • Write 0x1 to the EVn_STATE register of each event. Writing 0x1
     *     enables the event.
     *     In effect, the event is allowed to occur in a single state which
     *     never changes while the counter is running.
     */
    Chip_SCT_Init(LPC_SCT);
    Chip_INMUX_SetSCTInMux(LPC_INMUX, SCT_INMUX_1, SCT_INP_IN1);
    LPC_SCT->STATE_U = 0;
    LPC_SCT->EV[0].CTRL &= 0xfff03fff;
    LPC_SCT->EV[0].STATE = 1;
    Chip_SCT_Config(LPC_SCT, 0x0415);      // 0000 0100 0001 0101
    Chip_SCT_SetControl(LPC_SCT, 0x0008);  // 0000 0000 0000 1000
Labels (1)
5 Replies

1,116 Views
athmesh_n
Contributor IV
0 Kudos

1,116 Views
lpcware
NXP Employee
NXP Employee
bump
0 Kudos

1,116 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by R2D2 on Fri Jun 03 08:20:50 MST 2016
This is a very simple SCT sample for LPC15xx, which should be similar to LPC8xx 

//input capture PIO0_17
 Chip_IOCON_PinMuxSet(LPC_IOCON, 0, 17, (IOCON_MODE_PULLDOWN));
 Chip_SCT_Init(LPC_SCT0);
//unified 32bit counter, SYNC all inputs, SCT0 input counter
 LPC_SCT0->CONFIG =  SCT_CONFIG_32BIT_COUNTER | (1<<9) | SCT_CONFIG_CLKMODE_INCLK ;
 LPC_INMUX->SCT0_INMUX[0] = SCT0_INMUX_PIO0_17;//SCT0_IN0 = 2 = P0IO_17
 LPC_SCT0->CAPCTRL[1].U  = (SCT_EVT_1);//set capture control event
//event 1: capture rising
 LPC_SCT0->EVENT[1].STATE = (SCT_EVT_1); // event happens in state
 LPC_SCT0->EVENT[1].CTRL = (0)       |  // capture, not used
                           (0 << 5)  |  // OUTSEL[5]     = selects input
                           (0 << 6)  |  // IOSEL[9:6]    = 0 select SCT0_IN0
                           (1 << 10) |  // IOCOND[11:10] = 1 rising
                           (2 << 12) |  // COMBMODE[13:12] = IO condition
                           (1 << 14) |  // STATELD[14]   = STATEV is loaded
                           (1 << 15);   // STATEV[19:15] = new state 1

 LPC_SCT0->STATE =1;//start state
 LPC_SCT0->EVFLAG = (SCT_EVT_1); //reset flags
 Chip_SCT_ClearControl(LPC_SCT0, SCT_CTRL_HALT_L | SCT_CTRL_HALT_H);//and start
}



It's important to config the correct input and also that you don't change the state 
0 Kudos

1,116 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by lpcmunich on Fri Jun 03 05:23:27 MST 2016
Hi rsg,
Ofcourse you don't have to use state logic if you don't like or your application doesn't demand it.
The SCT is very flexible peripheral, but that also means you need to configure it properly.
Looking at what you are trying to achive with few lines of code, would like to raise few flags/concerns.

- Are you enabling clocks to Switch Matrix, GPIO & IOCON blocks? If not, please do so.
- Program PINASSIGN5 or PINASSIGN56 register for CTIN function assigment on a particular pin.
- I can cleary see that you are messing up with the Event Control Register, pay attention to bit positions 13-10. You would be better of with
  LPC_SCT->EV[0].CTRL  = (1 << 10) | (2 << 12);      // rising edge condition on a specified IO

Hope it helps!
0 Kudos

1,116 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by MarcVonWindscooting on Thu Jun 02 13:45:48 MST 2016

Quote: rsg
I don't need all these states and transitions!


If you say so. Then you have my deep respect, because you obviously plan to use some fancy modwrap-math to calculate the speed totally asynchronous from the difference of the counts c1,c2 at two different times t1,t2, determined by some hardware timer 'T'.
  v = scale*(c1-c2) / (t1-t2)
But wait...what are you planning to use as timer 'T' ? Don't say, the SCT! Because then it's no longer the 'simplest use' you're after  ;-)
(And for all other timers, I want to see your modwrap math, really!  0:)

Marc
0 Kudos