SCT- Timer Match Value modification

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

SCT- Timer Match Value modification

Jump to solution
5,870 Views
athmesh_n
Contributor IV

Can we change the Match value of SCTimer anywhere in the program, after it has been intialized? 
If so, what are the things to be noted for changing the match value?

processor : LPC1517
IDE      : LPCXpresso v8.2.2

for eg :- after initializing the SCTimer for 10ms; will timer work for 100ms by changing SCTimer match value in between the program? 

Or will it work for 10ms?

Labels (2)
Tags (2)
1 Solution
5,283 Views
ianbenton
Senior Contributor I

You can only write to the MATCH registers when the timer is halted (bit 2 set in the CTRL register). If you write to MATCH when the counter is running it will be ignored. So the answer to your question is that it will time for 10ms.

If you want to change the time period while the counter is running, then you have to write to the Match Reload register (MATCHREL). If the relevant bit is set in the LIMIT register, when the MATCH occurs, the counter will be reset to zero, and at the same time it will transfer the value from MATCHREL to MATCH, so that the length of the next cycle will be the new value (the value written to MATCHREL).

If anything else causes the counter to LIMIT ("LIMIT" in NXP-speak for "reset") then MATCHREL will be transferred to MATCH.

View solution in original post

16 Replies
5,284 Views
athmesh_n
Contributor IV

Is there any way to enable lowerbit interrupt and upperbit interrupt of SCTimer? Or will both have same interrupt? 

for eg: I have 1 ms counter for lower bit and at the same time 100ms counter for upper bit, so how will I enable interrupt for both?

0 Kudos
Reply
5,283 Views
ianbenton
Senior Contributor I

It's in the EVFLAG register. Both interrupts will invoke the same interrupt handler. Then by looking in the EVFLAG register you can see which event caused the interrupt. (The first thing the handler must do is read EVFLAG, because it has to write back to EVFLAG to cancel the interrupt).

You can enable and disable interrupts by setting the corresponding bit in the EVEN register.

5,283 Views
athmesh_n
Contributor IV

Hello Benton,

Thank you! so I should check EVFLAG inside interrupt handler?

0 Kudos
Reply
5,283 Views
ianbenton
Senior Contributor I

At the start of the interrupt routine, you MUST read EVFLAG and write the result back to it. That resets it. It can't interrupt again until it is reset.

Apart from that,

You can read it any time you like, not necessarily in the interrupt handler.

5,283 Views
athmesh_n
Contributor IV

oh okay thank you very much, I went through MRT example and I got the idea of EVFLAG

0 Kudos
Reply
5,284 Views
ianbenton
Senior Contributor I

You can only write to the MATCH registers when the timer is halted (bit 2 set in the CTRL register). If you write to MATCH when the counter is running it will be ignored. So the answer to your question is that it will time for 10ms.

If you want to change the time period while the counter is running, then you have to write to the Match Reload register (MATCHREL). If the relevant bit is set in the LIMIT register, when the MATCH occurs, the counter will be reset to zero, and at the same time it will transfer the value from MATCHREL to MATCH, so that the length of the next cycle will be the new value (the value written to MATCHREL).

If anything else causes the counter to LIMIT ("LIMIT" in NXP-speak for "reset") then MATCHREL will be transferred to MATCH.

5,284 Views
athmesh_n
Contributor IV

Thank you Ian,

I will try it by HALTING and modification 

0 Kudos
Reply
5,284 Views
ianbenton
Senior Contributor I

but don't forget to write the value to MATCHREL as well as MATCH, otherwise you will only get the correct pulse width for the first cycle. After that, it will transfer the value of MATCHREL to MATCH and all subsequent cycles will have the pulse width of the value in MATCHREL.

5,283 Views
athmesh_n
Contributor IV

I've created a delay_ms function, but the timer stops after 1 minute. 
delay_ms() contains a timer start , then a wait loop and then a timer stop.
in timer start:

count=0;

LPC_SCT2->CTRL_U |= (1<<2);                                                           //stops timer

LPC_SCT2->MATCH[0].U=(SystemCoreClock/1000-1);                       //for 1 ms timer

LPC_SCT2->MATCHREL[0].U=(SystemCoreClock/1000-1);                //for 1ms reload

LPC_SCT2->CTRL_U &= ~(1<<2);                                                        //start timer

0 Kudos
Reply
5,283 Views
ianbenton
Senior Contributor I

How are you trying to use this timer?

0 Kudos
Reply
5,283 Views
athmesh_n
Contributor IV

Hello Ian, 

I've two delay functions, delay_us and delay_ms, which works on single SCtimer, i.e. SCT2. So I initially initialized it for 1us. Then if im using delay_ms() i'll stop timer and change MATCH and MATCHRELAOD value for 1 ms. 

0 Kudos
Reply
5,283 Views
ianbenton
Senior Contributor I

So you use it for a fixed delay in the software? By starting the counter by writing to the HALT bit and waiting for the interrupt? Or by polling EVFLAG? 

It should be fine for 1ms, but won't the number of instructions require to read and write to the timer make the 1us delay inaccurate. After all, it can only execute 72 instructions in a microsecond.

0 Kudos
Reply
5,283 Views
athmesh_n
Contributor IV

Hello Ian, 

I'll give a more wide detail.

delay_ms(period); is a function which can be called from anywhere in the program, period is the value in ms, similarly delay_us(period) period is in us.

in this delay_ms function 

1. I've a start function as shown above, 

count=0;

LPC_SCT2->CTRL_U |= (1<<2);                                                           //stops timer

LPC_SCT2->MATCH[0].U=(SystemCoreClock/1000-1);                       //for 1 ms timer

LPC_SCT2->MATCHREL[0].U=(SystemCoreClock/1000-1);                //for 1ms reload

LPC_SCT2->CTRL_U &= ~(1<<2);                                                        //start timer

2. and then a while loop : while(count<period);

3. and then a stop function.
so if I call a delay_us(period) function between, then the match value of that function changes.

Hope you understood.

Have a great day :smileyhappy:

0 Kudos
Reply
5,283 Views
ianbenton
Senior Contributor I

Have you thought about using the Multi Rate Timer? That is specifically for introducing a delay in a software routine?

0 Kudos
Reply
5,283 Views
athmesh_n
Contributor IV

Never thought about that, let me check! 

Thank you

0 Kudos
Reply
5,283 Views
athmesh_n
Contributor IV

Yes, I get it now, it will work like PWM if by just loading to MATCHREL!

0 Kudos
Reply