Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
10 years, 2 months ago.
Timing issues
Hi,
I have limited experience with 8 bit PIC's, and recently decided to try mbed environment, LPC1768 mcu, so basically a newbie.. I do not seem to be able to have the timer (nor the PWM) running at the desired Clock speed. I seem to understand that the PWM is limited to 48Mhz, but I do not see this limitation for the timer. I am sure I am doing something wrong, but can anyone give me a hint of why I am not able to get the count number that I am supposed to get for the square wave described in the below code?
Thank you in advance.
- include "mbed.h"
- include "C12832_lcd.h"
long int volatile Count; int volatile freq; C12832_LCD lcd; Timer F; InterruptIn FR (p21);
void ReadF(void); void setup(void);
void ReadF(void) { F.stop(); Count=F.read_us(); F.reset(); F.start(); }
void setup(void) { LPC_SC->PCONP |= (1<<23); Power up Timer 3 LPC_SC->PCLKSEL0 |=(1<<12);Set PWM clock = CCLK = 96Mhz LPC_SC->PCLKSEL1 |=(1<<2);Set GPIO interrupt clock = 96Mhz LPC_SC->PCLKSEL1 |=(1<<14);Set Timer3 clock = CCLK = 96Mhz
} The board works at 96Mhz (std.) Input to p21 is a square wave, 100Hz, 50% duty cycle
int main () { lcd.cls(); setup(); while(1) { FR.fall(&ReadF); lcd.locate(0,0); lcd.printf("FR : %ld",Count); freq=(1000000/Count); lcd.locate(0,10); lcd.printf("Hz : %d",freq);
} } Display shows a count of about 40000 and a resulting frequency of about 25hz.
1 Answer
10 years, 2 months ago.
Please use <<code>> and <</code>>
to make it readable.
Start by getting rid of your entire setup code, this is done by the libs themselves. Easiest way to make PWM is simply using PwmOut, then that is also set automatically correct.
The mbed timer runs at 1us resolution, so 1MHz. I don't know exactly without looking it up, but it could very well be that the Timer timer (Timer3) runs by default at Fclk/4. If I see your comments, you seem to be changed this to Fclk. The library still assumes it runs at Fclk/4, so you get 4 times too large time back, which is what you observe.
Dank je wel Erik. I must have missed something then, by looking at pages 29 and 57 I thought somehow that I could get a pclk = cclk. It then raises the question: how can I change that Fclk divider to 1? Also, if I wanted to have 4 external interrupt, is there any way I can assign them a priority? Would it be the same as for internal interrupts?
Thank you
posted by 20 Aug 2015You just did that with your code, but the Timer library assumes it is not at Fclk. If you want higher resolution you can't use the Timer lib and have to write something yourself (which isn't too hard, but additionally there are a bunch of LPC1768 alternative timer libs floating around).
All library InterruptIns use one interrupt vector, so they can't have different priorities. You can use some EINT pin interrupts, which are a bit different, and also far fewer of them. Then you could set different interrupt priorities. For what would you need that?
posted by 20 Aug 2015Ok Erik, I guess I will have to dig in more into registers and have a big Init() that takes care of PLL's, various registers and so on. I am trying to program an automotive application, where I need to input the events of four square waves coming from different transducers (various duty cycles but relatively low frequencies, max 170Hz), and cannot afford to lose any interrupt event, if possible. Upon computing some event-based calculation (counting freqs. and pulse widths) I need to output two "modified" pulsewidths. The initial code that I posted was only to troubleshoot the timing issue.
posted by 21 Aug 2015Generally using the timer capture input is more reliable to count frequencies/pulse widths. However max 170Hz is so low that it really shouldn't be an issue with just using InterruptIns, also don't think you are going to really need to modify PLL settings and stuff like that.
Just keep your interrupt handlers are simple as possible, so it isn't stuck long in them.
posted by 21 Aug 2015