added prescaler for 16 bit pwm in LPC1347 target

Fork of mbed-dev by mbed official

Files at this revision

API Documentation at this revision

Comitter:
JojoS
Date:
Sat Sep 10 15:32:04 2016 +0000
Parent:
146:11f9a9a04805
Commit message:
added prescaler for 16 bit timers (solution as in LPC11xx), default prescaler 31 for max 28 ms period time

Changed in this revision

targets/hal/TARGET_NXP/TARGET_LPC13XX/pwmout_api.c Show annotated file Show diff for this revision Revisions of this file
diff -r 11f9a9a04805 -r ba84b7dc41a7 targets/hal/TARGET_NXP/TARGET_LPC13XX/pwmout_api.c
--- a/targets/hal/TARGET_NXP/TARGET_LPC13XX/pwmout_api.c	Thu Sep 08 15:05:30 2016 +0100
+++ b/targets/hal/TARGET_NXP/TARGET_LPC13XX/pwmout_api.c	Sat Sep 10 15:32:04 2016 +0000
@@ -90,6 +90,13 @@
     
     /* Reset Functionality on MR3 controlling the PWM period */
     timer->MCR = 1 << 10;
+        
+    if (timer == LPC_CT16B0 || timer == LPC_CT16B1) {
+    /* Set 16-bit timer prescaler to avoid timer expire for default 20ms */
+    /* This can be also modified by user application, but the prescaler value */
+    /* might be trade-off to timer accuracy */
+        timer->PR = 30;
+    } 
     
     pwm_clock_mhz = SystemCoreClock / 1000000;
     
@@ -138,12 +145,13 @@
 // Set the PWM period, keeping the duty cycle the same.
 void pwmout_period_us(pwmout_t* obj, int us) {
     int i = 0;
-    uint32_t period_ticks = pwm_clock_mhz * us;
+    uint32_t period_ticks;
     
     timer_mr tid = pwm_timer_map[obj->pwm];
     LPC_CTxxBx_Type *timer = Timers[tid.timer];
     uint32_t old_period_ticks = timer->MR3;
-    
+    period_ticks = (pwm_clock_mhz * us) / (timer->PR + 1);
+ 
     timer->TCR = TCR_RESET;
     timer->MR3 = period_ticks;
     
@@ -166,9 +174,9 @@
 }
 
 void pwmout_pulsewidth_us(pwmout_t* obj, int us) {
-    uint32_t t_on = (uint32_t)(((uint64_t)SystemCoreClock * (uint64_t)us) / (uint64_t)1000000);
     timer_mr tid = pwm_timer_map[obj->pwm];
     LPC_CTxxBx_Type *timer = Timers[tid.timer];
+    uint32_t t_on = (uint32_t)((((uint64_t)SystemCoreClock * (uint64_t)us) / (uint64_t)1000000) / (timer->PR + 1)); ;
     
     timer->TCR = TCR_RESET;
     if (t_on > timer->MR3) {