Library for MAX14871 Shield, MAXREFDES89#

Dependencies:   MAX5387 MAX7300

Dependents:   MAXREFDES89_MAX14871_Shield_Demo MAXREFDES89_Test_Program Line_Following_Bot Line_Following_Bot_Pololu

MAXREFDES89# Component Page

Revision:
1:7e9b864ddacf
Parent:
0:b5189f4ce1cb
Child:
2:9b50d36d69c8
--- a/max14871_shield.cpp	Thu Jul 23 23:36:46 2015 +0000
+++ b/max14871_shield.cpp	Sun Jul 26 22:19:50 2015 +0000
@@ -1,5 +1,5 @@
 /******************************************************************//**
-* @file max14871_shield.h
+* @file max14871_shield.cpp
 *
 * @author Justin Jordan
 *
@@ -45,7 +45,7 @@
 
 #include "max14871_shield.h"
 
-#define MIN_PERIOD (20) //50KHz
+#define MIN_PERIOD (0.00002) //50KHz
 
 //Motor Driver control inputs
 #define MD_EN    (0x01)
@@ -53,7 +53,7 @@
 #define MD_MODE0 (0x04)
 #define MD_MODE1 (0x08)
 
-#define MAX_VREF (100)
+#define MAX_VREF (2.0)
 
 //GPIO Expander Default Configurations
 #define MAX7300_ALL_OUTPUTS    (0x55)
@@ -180,6 +180,8 @@
             {
                 port_data |= (MD_EN << 4);
             }
+            
+            set_pwm_duty_cycle(md, 0.0);
         break;
         
         case BRAKE:
@@ -192,7 +194,7 @@
                 port_data &= ~(MD_EN << 4);
             }
             
-            set_pwm_duty_cycle(md, 0);
+            set_pwm_duty_cycle(md, 0.0);
         break;
         
         case REVERSE:
@@ -237,15 +239,23 @@
 //*********************************************************************
 int16_t Max14871_Shield::set_current_regulation_mode(max14871_motor_driver_t md, 
                                                      max14871_current_regulation_mode_t mode,
-                                                     uint8_t vref)
+                                                     float vref)
 {
     int16_t result = 0;
     int16_t port_data;
+    uint8_t local_vref = 0;
     
     Max7300::max7300_port_number_t low_port;
     Max5387 *p_digi_pot;
     
+    if(vref > MAX_VREF)
+    {
+        vref = MAX_VREF;
+    }
+    local_vref = ((uint8_t) ((vref * 255) / 3.3));
+    
     //determine the low port of an 8 bit register to read/write 
+    //and digipot associated with motor driver
     if(md < MD3)
     {
         low_port = Max7300::MAX7300_PORT_04;
@@ -282,23 +292,13 @@
             {
                 port_data &= ~MD_MODE0;
                 port_data |= MD_MODE1;
-                
-                if(vref > MAX_VREF)
-                {
-                    vref = MAX_VREF;
-                }
-                p_digi_pot->write_ch_A(vref);
+                p_digi_pot->write_ch_A(local_vref);
             }
             else
             {
                 port_data &= ~(MD_MODE0 << 4);
                 port_data |= (MD_MODE1 << 4);
-                
-                if(vref > MAX_VREF)
-                {
-                    vref = MAX_VREF;
-                }
-                p_digi_pot->write_ch_B(vref);
+                p_digi_pot->write_ch_B(local_vref);
             }
         break;
         
@@ -334,22 +334,12 @@
             if(md % 2)
             {
                 port_data |= (MD_MODE1 + MD_MODE0);
-                
-                if(vref > MAX_VREF)
-                {
-                    vref = MAX_VREF;
-                }
-                p_digi_pot->write_ch_A(vref);
+                p_digi_pot->write_ch_A(local_vref);
             }
             else
             {
                 port_data |= ((MD_MODE1 + MD_MODE0) << 4);
-                
-                if(vref > MAX_VREF)
-                {
-                    vref = MAX_VREF;
-                }
-                p_digi_pot->write_ch_B(vref);
+                p_digi_pot->write_ch_B(local_vref);
             }
         break;
         
@@ -358,23 +348,13 @@
             {
                 port_data |= MD_MODE0;
                 port_data &= ~MD_MODE1;
-                
-                if(vref > MAX_VREF)
-                {
-                    vref = MAX_VREF;
-                }
-                p_digi_pot->write_ch_A(vref);
+                p_digi_pot->write_ch_A(local_vref);
             }
             else
             {
                 port_data |= (MD_MODE0 << 4);
                 port_data &= ~(MD_MODE1 << 4);
-                
-                if(vref > MAX_VREF)
-                {
-                    vref = MAX_VREF;
-                }
-                p_digi_pot->write_ch_B(vref);
+                p_digi_pot->write_ch_B(local_vref);
             }
         break;
         
@@ -394,7 +374,7 @@
 
 
 //*********************************************************************
-int16_t Max14871_Shield::set_pwm_period(max14871_motor_driver_t md, uint16_t period)
+int16_t Max14871_Shield::set_pwm_period(max14871_motor_driver_t md, float period)
 {
     int16_t result = 0;
     
@@ -407,19 +387,19 @@
         switch(md)
         {
             case MD1:
-                _p_pwm1->period_us(period);
+                _p_pwm1->period(period);
             break;
             
             case MD2:
-                _p_pwm2->period_us(period);
+                _p_pwm2->period(period);
             break;
             
             case MD3:
-                _p_pwm3->period_us(period);
+                _p_pwm3->period(period);
             break;
             
             case MD4:
-                _p_pwm4->period_us(period);
+                _p_pwm4->period(period);
             break;
             
             default:
@@ -433,26 +413,26 @@
 
 
 //*********************************************************************
-int16_t Max14871_Shield::set_pwm_duty_cycle(max14871_motor_driver_t md, uint16_t duty_cycle)
+int16_t Max14871_Shield::set_pwm_duty_cycle(max14871_motor_driver_t md, float duty_cycle)
 {
     int16_t result = 0;
     
     switch(md)
     {
         case MD1:
-            _p_pwm1->pulsewidth_us(duty_cycle);
+            _p_pwm1->write(duty_cycle);
         break;
         
         case MD2:
-            _p_pwm2->pulsewidth_us(duty_cycle);
+            _p_pwm2->write(duty_cycle);
         break;
         
         case MD3:
-            _p_pwm3->pulsewidth_us(duty_cycle);
+            _p_pwm3->write(duty_cycle);
         break;
         
         case MD4:
-            _p_pwm4->pulsewidth_us(duty_cycle);
+            _p_pwm4->write(duty_cycle);
         break;
         
         default:
@@ -489,13 +469,13 @@
     _p_io_expander->enable_transition_detection();
     _p_io_expander->enable_ports();
     
-    //set Vref pin of all motor drivers to 1.3V
+    //set Vref pin of all motor drivers to 2.0V
     _p_digi_pot1->write_ch_AB(MAX_VREF);
     _p_digi_pot2->write_ch_AB(MAX_VREF);
     
     //set switching frequency of all motor drivers to 50KHz
-    _p_pwm1->period_us(MIN_PERIOD);  
-    _p_pwm2->period_us(MIN_PERIOD);
-    _p_pwm3->period_us(MIN_PERIOD);
-    _p_pwm4->period_us(MIN_PERIOD);
+    _p_pwm1->period(MIN_PERIOD);  
+    _p_pwm2->period(MIN_PERIOD);
+    _p_pwm3->period(MIN_PERIOD);
+    _p_pwm4->period(MIN_PERIOD);
 }