Knud Dalgaard / 310-TMC3-TestHW

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers pwm.cpp Source File

pwm.cpp

00001 #include "mbed.h"
00002 #include "pwm.h"
00003 
00004 
00005 
00006 //---------------------------------------
00007 // Hardware recources
00008 //---------------------------------------
00009 PwmOut  PWM0(p26);
00010 PwmOut  PWM1(p25);
00011 PwmOut  PWM2(p24);
00012 PwmOut  PWM3(p23);
00013 PwmOut  PWM4(p22);
00014 PwmOut  PWM5(p21);
00015 
00016 
00017 
00018 //---------------------------------------
00019 // Prototypes
00020 //---------------------------------------
00021 static int PWM_calcDutyCycle( unsigned char DutyCyclePerc );
00022 static void PWM_updateAllDC( void );
00023 
00024 
00025 //---------------------------------------
00026 // Internal variables
00027 //---------------------------------------
00028 static int PWMPeriode_us = 0;                           // [us] actual periode time of pwm signal
00029 static unsigned char PWMDutyCycle[CNT_ePWM_channel];    // [%] Scale: 1 DutyCycle ratio of each channel
00030 
00031 
00032 
00033 //---------------------------------------
00034 // External variables
00035 //---------------------------------------
00036 
00037 
00038 
00039 //---------------------------------------
00040 // Global Functions
00041 //---------------------------------------
00042 void PWM_init( void )
00043 {
00044     int i;
00045     
00046     for( i = 0; i < CNT_ePWM_channel; i++ )
00047     {
00048         PWMDutyCycle[i] = 0;
00049     }
00050     
00051     // Update Freq and duty cycle
00052     PWM_updateFreq( PWM_INIT_FREQ );
00053 }
00054 
00055 
00056 // Return:
00057 // 0: freq_Hz is outside of limit
00058 // 1: frequency was updated
00059 int PWM_updateFreq( unsigned int freq_Hz )
00060 {
00061     float tempFloat;
00062 
00063     // check limit
00064     if( (freq_Hz < PWM_FREQ_MIN) || (freq_Hz > PWM_FREQ_MAX) )
00065     {
00066         return 0;
00067     }
00068     
00069     // convert to period time in us
00070     tempFloat = 1.0 / (float)(freq_Hz);
00071     tempFloat *= 1000000.0;
00072 
00073     // Save period to global variable, update PWM and update duty cycle
00074     PWMPeriode_us = (int)tempFloat; 
00075     PWM0.period_us(PWMPeriode_us); // periode effects all 6 channels 
00076     PWM_updateAllDC();
00077     
00078     return 1;
00079 }
00080 
00081 
00082 // Return:
00083 // 0: channel or dutyCycle is outside of limit
00084 // 1: duty cycle was updated
00085 int PWM_setDC( int channel, int dutyCycle )
00086 {          
00087     // Check duty cycle and channel limits
00088     if( channel >= CNT_ePWM_channel )
00089     {
00090         return 0;
00091     }
00092     
00093     if( dutyCycle < 0 || dutyCycle > PWM_DC_MAX )
00094     {
00095         return 0;
00096     }
00097       
00098     // update buffer array of channel and update duty cycle register of all channel
00099     PWMDutyCycle[channel] = dutyCycle;
00100     PWM_updateAllDC();
00101  
00102     return 1;
00103 }
00104 
00105 
00106 
00107 //---------------------------------------
00108 // Internal Functions
00109 //---------------------------------------
00110 static void PWM_updateAllDC( void )
00111 {
00112     // update duty cycle of all channels    
00113     PWM0.pulsewidth_us( PWM_calcDutyCycle( PWMDutyCycle[PWM_CH0] ) );
00114     PWM1.pulsewidth_us( PWM_calcDutyCycle( PWMDutyCycle[PWM_CH1] ) );
00115     PWM2.pulsewidth_us( PWM_calcDutyCycle( PWMDutyCycle[PWM_CH2] ) );
00116     PWM3.pulsewidth_us( PWM_calcDutyCycle( PWMDutyCycle[PWM_CH3] ) );
00117     PWM4.pulsewidth_us( PWM_calcDutyCycle( PWMDutyCycle[PWM_CH4] ) );
00118     PWM5.pulsewidth_us( PWM_calcDutyCycle( PWMDutyCycle[PWM_CH5] ) ); 
00119 }
00120 
00121 
00122 // Calculate duty cycle register value with respect to period time
00123 static int PWM_calcDutyCycle( unsigned char DutyCyclePerc )
00124 {
00125     if( DutyCyclePerc == 0 )
00126     {
00127         return 0;
00128     }
00129     
00130     if( DutyCyclePerc >= 100 )
00131     {
00132         return PWMPeriode_us + 1;
00133     }
00134     
00135     return (int)( (float)PWMPeriode_us * ( (float)DutyCyclePerc / 100.0) );
00136 }