David's dead reckoning code for the LVBots competition on March 6th. Uses the mbed LPC1768, DRV8835, QTR-3RC, and two DC motors with encoders.

Dependencies:   PololuEncoder Pacer mbed GeneralDebouncer

Revision:
7:85b8b5acfb22
Parent:
6:89a39870e23d
Child:
8:78b1ff957cba
--- a/motors.cpp	Thu Feb 20 23:33:45 2014 +0000
+++ b/motors.cpp	Fri Feb 21 00:00:43 2014 +0000
@@ -1,4 +1,11 @@
 #include <mbed.h>
+#include "motors.h"
+
+// Application     mbed pin    LPC1768
+// Motor 1 PWM     p26         P2[0]/PWM1[1]
+// Motor 1 dir     p25
+// Motor 2 PWM     p27         P2[2]/PWM1[3]
+// Motor 2 dir     p28
 
 static PwmOut motor1Pwm(p26);
 DigitalOut motor1Dir(p25);
@@ -8,17 +15,29 @@
 
 void motors_init()
 {
-    motor1Pwm.period_us(50);
-    motor2Pwm.period_us(50);
-    motor1Pwm.pulsewidth_us(20);
-    motor2Pwm.pulsewidth_us(20);
+    motor1Pwm.period_us(100);
+    
+    // Set most parts of the PWM module to their defaults.
+    LPC_PWM1->TCR = 0;
+    LPC_PWM1->CTCR = 0;
+    LPC_PWM1->CCR = 0;
     
-    LPC_PWM1->MR0 = 1200;       // Set the period
+    // Enable PWM output 1 and PWM output 3.
+    LPC_PWM1->PCR = (1 << 9) | (1 << 11);
+
     LPC_PWM1->MCR = (1 << 1);   // Reset PWMTC when it is equal to MR0.
+    
+    LPC_PWM1->MR0 = 1200;       // Set the period.  This must be done before enabling PWM.
+    LPC_PWM1->LER = (1 << 0);
+    motors_speed_set(0, 0);
+    
+    LPC_PWM1->TCR = (1 << 0) | (1 << 3);   // Enable the PWM counter and enable PWM.
+
 }
 
 void motors_speed_set(int16_t motor1_speed, int16_t motor2_speed)
 {
     LPC_PWM1->MR1 = motor1_speed;
     LPC_PWM1->MR3 = motor2_speed;
+    LPC_PWM1->LER |= (1<<1) | (1<<3);
 }