CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Three Phase AC Induction Motor Control with Direction Change

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
ecentric70



Joined: 05 May 2012
Posts: 3
Location: West Lafayette, IN

View user's profile Send private message

Three Phase AC Induction Motor Control with Direction Change
PostPosted: Sat May 05, 2012 2:11 am     Reply with quote

Idea Here is some code for three phase AC induction motor control using the PIC18F2431 or PIC18F4431 (PIC18LF2431 or PIC18LF4431):
Code:

////////////////////////////////////////////////////////////////////////////////
///           Three Phase PWM Control for AC Induction Motor                 ///
///                                 by                                       ///
///                           Larry E Himes Jr                               ///
///                            April 30, 2012                                ///
///                                                                          ///
///                  ECET 581 Applied Electronic Drives                      ///
///                                                                          ///
///          PIN A0 - AN0 is used for the Closed Loop Feedback               ///
///          PIN A1 - AN1 is used for User Speed Control Input               ///
///             PIN C3 - User Direction Control Switch Input                 ///
///                                                                          ///
///              Frequency Range Control from 20Hz to 120Hz                  ///
///                                                                          ///
///               Thirteen Samples per PWM Sine Wave Output                  ///
///                                                                          ///
////////////////////////////////////////////////////////////////////////////////

#include <18f4431.h>
#device adc=10
#fuses  INTRC_IO,NOWDT,NOPROTECT,NOMCLR
#use delay(clock=8000000)

void main()
   {
   int16 adc_val;                                   // ADC Variable
   int16 period;                                    // PWM Time Period Variable
   int16 feedbackI;                                 // Feedback Varaiable
   int16 feedbackL;                                 // Feedback Variable
   int16 adjust;                                    // Speed Adjust Variable
   int16 speed;                                     // Speed Control Variable
   int16 i;                                         // Counter Variable
   int8  x;                                         // Soft Start Variable

   //PPWM channels 0 and 1 are both on and always opposite values
   setup_power_pwm_pins(PWM_COMPLEMENTARY,PWM_COMPLEMENTARY,PWM_COMPLEMENTARY,
                        PWM_OFF);

   // Continuously Sample ADC Channels
   setup_adc_ports(ADC_CONT_A | ADC_WHEN_PPWM | VSS_VDD);
   setup_adc(ADC_CLOCK_INTERNAL);

   x=0;                                             // Initial Startup

   while(TRUE)
      {
      set_adc_channel(1);                           // Select ADC Channel 1
      adc_val = read_adc();                         // Read Current ADC Value
      feedbackL=0;                                  // Clear ADC Variable
      feedbackI=0;                                  // Clear ADC Variable
      // Prepare Variables for Startup
      if (x==0)
         {
         feedbackL=adc_val;                         // Initial Speed Setting
         adc_val=0;                                 // Clear ADC Variable
         }
      // Ramp Up Speed in Forward Direction
      while(adc_val<<feedbackL && x==0 && !input(PIN_C3))
         {
         period = ( adc_val / 20 ) + 9;             // Set PR2 for Frequency
         speed = 511 - ( ( 1023 - adc_val ) / 2 );  // Set User Specified Speed
         adjust=0;                                  // Clear Adjust Variable
         //add dead time for reactive loads
         setup_power_pwm(PWM_CLOCK_DIV_128 | PWM_FREE_RUN,1,0,period,0,1,0);
         // Phase Segment #1
         set_power_pwm0_duty(adjust+57+speed);      // Phase U PWM Duty Setting
         set_power_pwm2_duty(adjust+29+speed/4);    // Phase V PWM Duty Setting
         set_power_pwm4_duty(adjust+1);             // Phase W PWM Duty Setting
         // Phase Segment #2
         set_power_pwm0_duty(adjust+57+speed);      // Phase U PWM Duty Setting
         set_power_pwm2_duty(adjust+43+speed/2);    // Phase V PWM Duty Setting
         set_power_pwm4_duty(adjust+1);             // Phase W PWM Duty Setting
         // Phase Segment #3
         set_power_pwm0_duty(adjust+43+speed/2);    // Phase U PWM Duty Setting
         set_power_pwm2_duty(adjust+57+speed);      // Phase V PWM Duty Setting
         set_power_pwm4_duty(adjust+1);             // Phase W PWM Duty Setting
         // Phase Segment #4
         set_power_pwm0_duty(adjust+29+speed/4);    // Phase U PWM Duty Setting
         set_power_pwm2_duty(adjust+57+speed);      // Phase V PWM Duty Setting
         set_power_pwm4_duty(adjust+14+speed/6);    // Phase W PWM Duty Setting
         // Phase Segment #5
         set_power_pwm0_duty(adjust+14+speed/6);    // Phase U PWM Duty Setting
         set_power_pwm2_duty(adjust+57+speed);      // Phase V PWM Duty Setting
         set_power_pwm4_duty(adjust+29+speed/4);    // Phase W PWM Duty Setting
         // Phase Segment #6
         set_power_pwm0_duty(adjust+1);             // Phase U PWM Duty Setting
         set_power_pwm2_duty(adjust+57+speed);      // Phase V PWM Duty Setting
         set_power_pwm4_duty(adjust+43+speed/2);    // Phase W PWM Duty Setting
         // Phase Segment #7
         set_power_pwm0_duty(adjust+1);             // Phase U PWM Duty Setting
         set_power_pwm2_duty(adjust+43+speed/2);    // Phase V PWM Duty Setting
         set_power_pwm4_duty(adjust+57+speed);      // Phase W PWM Duty Setting
         // Phase Segment #8
         set_power_pwm0_duty(adjust+1);             // Phase U PWM Duty Setting
         set_power_pwm2_duty(adjust+29+speed/4);    // Phase V PWM Duty Setting
         set_power_pwm4_duty(adjust+57+speed);      // Phase W PWM Duty Setting
         // Phase Segment #9
         set_power_pwm0_duty(adjust+14+speed/6);    // Phase U PWM Duty Setting
         set_power_pwm2_duty(adjust+14+speed/6);    // Phase V PWM Duty Setting
         set_power_pwm4_duty(adjust+57+speed);      // Phase W PWM Duty Setting
         // Phase Segment #10
         set_power_pwm0_duty(adjust+29+speed/4);    // Phase U PWM Duty Setting
         set_power_pwm2_duty(adjust+1);             // Phase V PWM Duty Setting
         set_power_pwm4_duty(adjust+57+speed);      // Phase W PWM Duty Setting
         // Phase Segment #11
         set_power_pwm0_duty(adjust+43+speed/2);    // Phase U PWM Duty Setting
         set_power_pwm2_duty(adjust+1);             // Phase V PWM Duty Setting
         set_power_pwm4_duty(adjust+43+speed/2);    // Phase W PWM Duty Setting
         // Phase Segment #12
         set_power_pwm0_duty(adjust+57+speed);      // Phase U PWM Duty Setting
         set_power_pwm2_duty(adjust+1);             // Phase V PWM Duty Setting
         set_power_pwm4_duty(adjust+29+speed/4);    // Phase W PWM Duty Setting
         // Phase Segment #13
         set_power_pwm0_duty(adjust+57+speed);      // Phase U PWM Duty Setting
         set_power_pwm2_duty(adjust+14+speed/6);    // Phase V PWM Duty Setting
         set_power_pwm4_duty(adjust+14+speed/6);    // Phase W PWM Duty Setting
         x=1;                                       // Ramp Up Cycle Completed
         adc_val=adc_val+3;                         // Increment ADC Value
         }
      i=0;                                          // Clear Counter Variable
      feedbackL=0;                                  // Clear ADC Variable
      // Rotate Motor in Forward Direction
      while(!input(PIN_C3))
         {
         // Read Initial ADC Values
         set_adc_channel(0);                        // Select ADC Channel 0
         feedbackI = read_adc();                    // Read Initial Feedback
         set_adc_channel(1);                        // Select ADC Channel 1
         adc_val = read_adc();                      // Read Current ADC Value
         period = ( adc_val / 20 ) + 9;             // Set PR2 for Frequency
         speed = 511 - ( ( 1023 - adc_val ) / 2 );  // Set User Specified Speed
         //add dead time for reactive loads
         setup_power_pwm(PWM_CLOCK_DIV_128 | PWM_FREE_RUN,1,0,period,0,1,0);
         // Phase Segment #1
         set_power_pwm0_duty(adjust+57+speed);      // Phase U PWM Duty Setting
         set_power_pwm2_duty(adjust+29+speed/4);    // Phase V PWM Duty Setting
         set_power_pwm4_duty(adjust+1);             // Phase W PWM Duty Setting
         // Phase Segment #2
         set_power_pwm0_duty(adjust+57+speed);      // Phase U PWM Duty Setting
         set_power_pwm2_duty(adjust+43+speed/2);    // Phase V PWM Duty Setting
         set_power_pwm4_duty(adjust+1);             // Phase W PWM Duty Setting
         // Phase Segment #3
         set_power_pwm0_duty(adjust+43+speed/2);    // Phase U PWM Duty Setting
         set_power_pwm2_duty(adjust+57+speed);      // Phase V PWM Duty Setting
         set_power_pwm4_duty(adjust+1);             // Phase W PWM Duty Setting
         // Phase Segment #4
         set_power_pwm0_duty(adjust+29+speed/4);    // Phase U PWM Duty Setting
         set_power_pwm2_duty(adjust+57+speed);      // Phase V PWM Duty Setting
         set_power_pwm4_duty(adjust+14+speed/6);    // Phase W PWM Duty Setting
         // Phase Segment #5
         set_power_pwm0_duty(adjust+14+speed/6);    // Phase U PWM Duty Setting
         set_power_pwm2_duty(adjust+57+speed);      // Phase V PWM Duty Setting
         set_power_pwm4_duty(adjust+29+speed/4);    // Phase W PWM Duty Setting
         // Phase Segment #6
         set_power_pwm0_duty(adjust+1);             // Phase U PWM Duty Setting
         set_power_pwm2_duty(adjust+57+speed);      // Phase V PWM Duty Setting
         set_power_pwm4_duty(adjust+43+speed/2);    // Phase W PWM Duty Setting
         // Phase Segment #7
         set_power_pwm0_duty(adjust+1);             // Phase U PWM Duty Setting
         set_power_pwm2_duty(adjust+43+speed/2);    // Phase V PWM Duty Setting
         set_power_pwm4_duty(adjust+57+speed);      // Phase W PWM Duty Setting
         // Phase Segment #8
         set_power_pwm0_duty(adjust+1);             // Phase U PWM Duty Setting
         set_power_pwm2_duty(adjust+29+speed/4);    // Phase V PWM Duty Setting
         set_power_pwm4_duty(adjust+57+speed);      // Phase W PWM Duty Setting
         // Phase Segment #9
         set_power_pwm0_duty(adjust+14+speed/6);    // Phase U PWM Duty Setting
         set_power_pwm2_duty(adjust+14+speed/6);    // Phase V PWM Duty Setting
         set_power_pwm4_duty(adjust+speed);         // Phase W PWM Duty Setting
         // Phase Segment #10
         set_power_pwm0_duty(adjust+29+speed/4);    // Phase U PWM Duty Setting
         set_power_pwm2_duty(adjust+1);             // Phase V PWM Duty Setting
         set_power_pwm4_duty(adjust+57+speed);      // Phase W PWM Duty Setting
         // Phase Segment #11
         set_power_pwm0_duty(adjust+43+speed/2);    // Phase U PWM Duty Setting
         set_power_pwm2_duty(adjust+1);             // Phase V PWM Duty Setting
         set_power_pwm4_duty(adjust+43+speed/2);    // Phase W PWM Duty Setting
         // Phase Segment #12
         set_power_pwm0_duty(adjust+57+speed);      // Phase U PWM Duty Setting
         set_power_pwm2_duty(adjust+1);             // Phase V PWM Duty Setting
         set_power_pwm4_duty(adjust+29+speed/4);    // Phase W PWM Duty Setting
         // Phase Segment #13
         set_power_pwm0_duty(adjust+57+speed);      // Phase U PWM Duty Setting
         set_power_pwm2_duty(adjust+14+speed/6);    // Phase V PWM Duty Setting
         set_power_pwm4_duty(adjust+14+speed/6);    // Phase W PWM Duty Setting
         i++;                                       // Increment Counter
         if (i>>1799)                               // Check Counter
            {
            set_adc_channel(0);                     // Select ADC Channel 0
            feedbackL = read_adc();                 // Read Feedback from ADC
            adjust = feedbackI - feedbackL;         // Calculate Speed Adjust
            i=0;                                    // Clear Counter Variable
            }
         }
         i=0;                                       // Clear Counter Variable
      // Ramp Down Motor Speed for Direction Change
      while(adc_val>>0 && input(PIN_C3))
         {
         period = ( adc_val / 20 ) + 9;             // Set PR2 for Frequency
         speed = 511 - ( ( 1023 - adc_val ) / 2 );  // Set User Specified Speed
         adjust=0;                                  // Clear Adjust Variable
         //add dead time for reactive loads
         setup_power_pwm(PWM_CLOCK_DIV_128 | PWM_FREE_RUN,1,0,period,0,1,0);
         // Phase Segment #1
         set_power_pwm0_duty(adjust+57+speed);      // Phase U PWM Duty Setting
         set_power_pwm2_duty(adjust+29+speed/4);    // Phase V PWM Duty Setting
         set_power_pwm4_duty(adjust+1);             // Phase W PWM Duty Setting
         // Phase Segment #2
         set_power_pwm0_duty(adjust+57+speed);      // Phase U PWM Duty Setting
         set_power_pwm2_duty(adjust+43+speed/2);    // Phase V PWM Duty Setting
         set_power_pwm4_duty(adjust+1);             // Phase W PWM Duty Setting
         // Phase Segment #3
         set_power_pwm0_duty(adjust+43+speed/2);    // Phase U PWM Duty Setting
         set_power_pwm2_duty(adjust+57+speed);      // Phase V PWM Duty Setting
         set_power_pwm4_duty(adjust+1);             // Phase W PWM Duty Setting
         // Phase Segment #4
         set_power_pwm0_duty(adjust+29+speed/4);    // Phase U PWM Duty Setting
         set_power_pwm2_duty(adjust+57+speed);      // Phase V PWM Duty Setting
         set_power_pwm4_duty(adjust+14+speed/6);    // Phase W PWM Duty Setting
         // Phase Segment #5
         set_power_pwm0_duty(adjust+14+speed/6);    // Phase U PWM Duty Setting
         set_power_pwm2_duty(adjust+57+speed);      // Phase V PWM Duty Setting
         set_power_pwm4_duty(adjust+29+speed/4);    // Phase W PWM Duty Setting
         // Phase Segment #6
         set_power_pwm0_duty(adjust+1);             // Phase U PWM Duty Setting
         set_power_pwm2_duty(adjust+57+speed);      // Phase V PWM Duty Setting
         set_power_pwm4_duty(adjust+43+speed/2);    // Phase W PWM Duty Setting
         // Phase Segment #7
         set_power_pwm0_duty(adjust+1);             // Phase U PWM Duty Setting
         set_power_pwm2_duty(adjust+43+speed/2);    // Phase V PWM Duty Setting
         set_power_pwm4_duty(adjust+57+speed);      // Phase W PWM Duty Setting
         // Phase Segment #8
         set_power_pwm0_duty(adjust+1);             // Phase U PWM Duty Setting
         set_power_pwm2_duty(adjust+29+speed/4);    // Phase V PWM Duty Setting
         set_power_pwm4_duty(adjust+57+speed);      // Phase W PWM Duty Setting
         // Phase Segment #9
         set_power_pwm0_duty(adjust+14+speed/6);    // Phase U PWM Duty Setting
         set_power_pwm2_duty(adjust+14+speed/6);    // Phase V PWM Duty Setting
         set_power_pwm4_duty(adjust+57+speed);      // Phase W PWM Duty Setting
         // Phase Segment #10
         set_power_pwm0_duty(adjust+29+speed/4);    // Phase U PWM Duty Setting
         set_power_pwm2_duty(adjust+1);             // Phase V PWM Duty Setting
         set_power_pwm4_duty(adjust+57+speed);      // Phase W PWM Duty Setting
         // Phase Segment #11
         set_power_pwm0_duty(adjust+43+speed/2);    // Phase U PWM Duty Setting
         set_power_pwm2_duty(adjust+1);             // Phase V PWM Duty Setting
         set_power_pwm4_duty(adjust+43+speed/2);    // Phase W PWM Duty Setting
         // Phase Segment #12
         set_power_pwm0_duty(adjust+57+speed);      // Phase U PWM Duty Setting
         set_power_pwm2_duty(adjust+1);             // Phase V PWM Duty Setting
         set_power_pwm4_duty(adjust+29+speed/4);    // Phase W PWM Duty Setting
         // Phase Segment #13
         set_power_pwm0_duty(adjust+57+speed);      // Phase U PWM Duty Setting
         set_power_pwm2_duty(adjust+14+speed/6);    // Phase V PWM Duty Setting
         set_power_pwm4_duty(adjust+14+speed/6);    // Phase W PWM Duty Setting
         adc_val=adc_val-3;                         // Decrement ADC Value
         x=0;                                       // Direction Change Complete
         }
      i=0;                                          // Clear Counter Variable
      // Ramp Up Speed in Reverse Direction
      while(adc_val<<feedbackL && x==0 && !input(PIN_C3))
         {
         period = ( adc_val / 20 ) + 9;             // Set PR2 for Frequency
         speed = 511 - ( ( 1023 - adc_val ) / 2 );  // Set User Specified Speed
         adjust=0;                                  // Clear Adjust Variable
         //add dead time for reactive loads
         setup_power_pwm(PWM_CLOCK_DIV_128 | PWM_FREE_RUN,1,0,period,0,1,0);
         // Phase Segment #1
         set_power_pwm0_duty(adjust+57+speed);      // Phase U PWM Duty Setting
         set_power_pwm2_duty(adjust+29+speed/4);    // Phase V PWM Duty Setting
         set_power_pwm4_duty(adjust+1);             // Phase W PWM Duty Setting
         // Phase Segment #2
         set_power_pwm0_duty(adjust+57+speed);      // Phase U PWM Duty Setting
         set_power_pwm2_duty(adjust+43+speed/2);    // Phase V PWM Duty Setting
         set_power_pwm4_duty(adjust+1);             // Phase W PWM Duty Setting
         // Phase Segment #3
         set_power_pwm0_duty(adjust+43+speed/2);    // Phase U PWM Duty Setting
         set_power_pwm2_duty(adjust+57+speed);      // Phase V PWM Duty Setting
         set_power_pwm4_duty(adjust+1);             // Phase W PWM Duty Setting
         // Phase Segment #4
         set_power_pwm0_duty(adjust+29+speed/4);    // Phase U PWM Duty Setting
         set_power_pwm2_duty(adjust+57+speed);      // Phase V PWM Duty Setting
         set_power_pwm4_duty(adjust+14+speed/6);    // Phase W PWM Duty Setting
         // Phase Segment #5
         set_power_pwm0_duty(adjust+14+speed/6);    // Phase U PWM Duty Setting
         set_power_pwm2_duty(adjust+57+speed);      // Phase V PWM Duty Setting
         set_power_pwm4_duty(adjust+29+speed/4);    // Phase W PWM Duty Setting
         // Phase Segment #6
         set_power_pwm0_duty(adjust+1);             // Phase U PWM Duty Setting
         set_power_pwm2_duty(adjust+57+speed);      // Phase V PWM Duty Setting
         set_power_pwm4_duty(adjust+43+speed/2);    // Phase W PWM Duty Setting
         // Phase Segment #7
         set_power_pwm0_duty(adjust+1);             // Phase U PWM Duty Setting
         set_power_pwm2_duty(adjust+43+speed/2);    // Phase V PWM Duty Setting
         set_power_pwm4_duty(adjust+57+speed);      // Phase W PWM Duty Setting
         // Phase Segment #8
         set_power_pwm0_duty(adjust+1);             // Phase U PWM Duty Setting
         set_power_pwm2_duty(adjust+29+speed/4);    // Phase V PWM Duty Setting
         set_power_pwm4_duty(adjust+57+speed);      // Phase W PWM Duty Setting
         // Phase Segment #9
         set_power_pwm0_duty(adjust+14+speed/6);    // Phase U PWM Duty Setting
         set_power_pwm2_duty(adjust+14+speed/6);    // Phase V PWM Duty Setting
         set_power_pwm4_duty(adjust+57+speed);      // Phase W PWM Duty Setting
         // Phase Segment #10
         set_power_pwm0_duty(adjust+29+speed/4);    // Phase U PWM Duty Setting
         set_power_pwm2_duty(adjust+1);             // Phase V PWM Duty Setting
         set_power_pwm4_duty(adjust+57+speed);      // Phase W PWM Duty Setting
         // Phase Segment #11
         set_power_pwm0_duty(adjust+43+speed/2);    // Phase U PWM Duty Setting
         set_power_pwm2_duty(adjust+1);             // Phase V PWM Duty Setting
         set_power_pwm4_duty(adjust+43+speed/2);    // Phase W PWM Duty Setting
         // Phase Segment #12
         set_power_pwm0_duty(adjust+57+speed);      // Phase U PWM Duty Setting
         set_power_pwm2_duty(adjust+1);             // Phase V PWM Duty Setting
         set_power_pwm4_duty(adjust+29+speed/4);    // Phase W PWM Duty Setting
         // Phase Segment #13
         set_power_pwm0_duty(adjust+57+speed);      // Phase U PWM Duty Setting
         set_power_pwm2_duty(adjust+14+speed/6);    // Phase V PWM Duty Setting
         set_power_pwm4_duty(adjust+14+speed/6);    // Phase W PWM Duty Setting
         adc_val=adc_val+3;                         // Increment ADC Value
         x=-1;                                      // Ramp Up Cycle Complete
         }
      feedbackL=0;                                  // Clear Feedback Variable
      // Rotate Motor in Reverse Direction
      while(input(PIN_C3) && x<<0)
         {
         // Read Initial ADC Values
         set_adc_channel(0);                        // Select ADC Channel 0
         feedbackI=read_adc();                      // Read Initial Feedback
         set_adc_channel(1);                        // Select ADC Channel 1
         adc_val=read_adc();                        // Read Current ADC Value
         period = ( adc_val / 20 ) + 9;             // Set PR2 for Frequency
         speed = 568 - ( adc_val / 2 );             // Set User Specified Speed
         //add dead time for reactive loads
         setup_power_pwm(PWM_CLOCK_DIV_128 | PWM_FREE_RUN,1,0,period,0,1,0);
         // Phase Segment #1
         set_power_pwm0_duty(adjust+57+speed);      // Phase U PWM Duty Setting
         set_power_pwm2_duty(adjust+1);             // Phase V PWM Duty Setting
         set_power_pwm4_duty(adjust+29+speed/4);    // Phase W PWM Duty Setting
         // Phase Segment #2
         set_power_pwm0_duty(adjust+57+speed);      // Phase U PWM Duty Setting
         set_power_pwm2_duty(adjust+1);             // Phase V PWM Duty Setting
         set_power_pwm4_duty(adjust+29+speed/2);    // Phase W PWM Duty Setting
         // Phase Segment #3
         set_power_pwm0_duty(adjust+29+speed/2);    // Phase U PWM Duty Setting
         set_power_pwm2_duty(adjust+1);             // Phase V PWM Duty Setting
         set_power_pwm4_duty(adjust+57+speed);      // Phase W PWM Duty Setting
         // Phase Segment #4
         set_power_pwm0_duty(adjust+29+speed/4);    // Phase U PWM Duty Setting
         set_power_pwm2_duty(adjust+14+speed/6);    // Phase V PWM Duty Setting
         set_power_pwm4_duty(adjust+57+speed);      // Phase W PWM Duty Setting
         // Phase Segment #5
         set_power_pwm0_duty(adjust+14+speed/6);    // Phase U PWM Duty Setting
         set_power_pwm2_duty(adjust+29+speed/4);    // Phase V PWM Duty Setting
         set_power_pwm4_duty(adjust+57+speed);      // Phase W PWM Duty Setting
         // Phase Segment #6
         set_power_pwm0_duty(adjust+1);             // Phase U PWM Duty Setting
         set_power_pwm2_duty(adjust+43+speed/2);    // Phase V PWM Duty Setting
         set_power_pwm4_duty(adjust+57+speed);      // Phase W PWM Duty Setting
         // Phase Segment #7
         set_power_pwm0_duty(adjust+1);             // Phase U PWM Duty Setting
         set_power_pwm2_duty(adjust+57+speed);      // Phase V PWM Duty Setting
         set_power_pwm4_duty(adjust+43+speed/2);    // Phase W PWM Duty Setting
         // Phase Segment #8
         set_power_pwm0_duty(adjust+1);             // Phase U PWM Duty Setting
         set_power_pwm2_duty(adjust+57+speed);      // Phase V PWM Duty Setting
         set_power_pwm4_duty(adjust+29+speed/4);    // Phase W PWM Duty Setting
         // Phase Segment #9
         set_power_pwm0_duty(adjust+14+speed/6);    // Phase U PWM Duty Setting
         set_power_pwm2_duty(adjust+57+speed);      // Phase V PWM Duty Setting
         set_power_pwm4_duty(adjust+14+speed/6);    // Phase W PWM Duty Setting
         // Phase Segment #10
         set_power_pwm0_duty(adjust+29+speed/4);    // Phase U PWM Duty Setting
         set_power_pwm2_duty(adjust+57+speed);      // Phase V PWM Duty Setting
         set_power_pwm4_duty(adjust+1);             // Phase W PWM Duty Setting
         // Phase Segment #11
         set_power_pwm0_duty(adjust+43+speed/2);    // Phase U PWM Duty Setting
         set_power_pwm2_duty(adjust+43+speed/2);    // Phase V PWM Duty Setting
         set_power_pwm4_duty(adjust+1);             // Phase W PWM Duty Setting
         // Phase Segment #12
         set_power_pwm0_duty(adjust+57+speed);      // Phase U PWM Duty Setting
         set_power_pwm2_duty(adjust+29+speed/4);    // Phase V PWM Duty Setting
         set_power_pwm4_duty(adjust+1);             // Phase W PWM Duty Setting
         // Phase Segment #13
         set_power_pwm0_duty(adjust+57+speed);      // Phase U PWM Duty Setting
         set_power_pwm2_duty(adjust+14+speed/6);    // Phase V PWM Duty Setting
         set_power_pwm4_duty(adjust+14+speed/6);    // Phase W PWM Duty Setting
         if (i>>1799)                               // Check Counter
            {
            set_adc_channel(0);                     // Select ADC Channel 0
            feedbackL = read_adc();                 // Read Feedback from ADC
            adjust = feedbackI - feedbackL;         // Calculate Speed Adjust
            i=0;                                    // Clear Counter Variable
            }     
         }
         i=0;                                       // Clear Counter Variable
      // Ramp Down Motor Speed for Direction Change
      while(adc_val>>0 && input(PIN_C3))
         {
         period = ( adc_val / 20 ) + 9;             // Set PR2 for Frequency
         speed = 511 - ( ( 1023 - adc_val ) / 2 );  // Set User Specified Speed
         adjust=0;                                  // Clear Adjust Variable
         //add dead time for reactive loads
         setup_power_pwm(PWM_CLOCK_DIV_128 | PWM_FREE_RUN,1,0,period,0,1,0);
         // Phase Segment #1
         set_power_pwm0_duty(adjust+57+speed);      // Phase U PWM Duty Setting
         set_power_pwm2_duty(adjust+29+speed/4);    // Phase V PWM Duty Setting
         set_power_pwm4_duty(adjust+1);             // Phase W PWM Duty Setting
         // Phase Segment #2
         set_power_pwm0_duty(adjust+57+speed);      // Phase U PWM Duty Setting
         set_power_pwm2_duty(adjust+43+speed/2);    // Phase V PWM Duty Setting
         set_power_pwm4_duty(adjust+1);             // Phase W PWM Duty Setting
         // Phase Segment #3
         set_power_pwm0_duty(adjust+43+speed/2);    // Phase U PWM Duty Setting
         set_power_pwm2_duty(adjust+57+speed);      // Phase V PWM Duty Setting
         set_power_pwm4_duty(adjust+1);             // Phase W PWM Duty Setting
         // Phase Segment #4
         set_power_pwm0_duty(adjust+29+speed/4);    // Phase U PWM Duty Setting
         set_power_pwm2_duty(adjust+57+speed);      // Phase V PWM Duty Setting
         set_power_pwm4_duty(adjust+14+speed/6);    // Phase W PWM Duty Setting
         // Phase Segment #5
         set_power_pwm0_duty(adjust+14+speed/6);    // Phase U PWM Duty Setting
         set_power_pwm2_duty(adjust+57+speed);      // Phase V PWM Duty Setting
         set_power_pwm4_duty(adjust+29+speed/4);    // Phase W PWM Duty Setting
         // Phase Segment #6
         set_power_pwm0_duty(adjust+1);             // Phase U PWM Duty Setting
         set_power_pwm2_duty(adjust+57+speed);      // Phase V PWM Duty Setting
         set_power_pwm4_duty(adjust+43+speed/2);    // Phase W PWM Duty Setting
         // Phase Segment #7
         set_power_pwm0_duty(adjust+1);             // Phase U PWM Duty Setting
         set_power_pwm2_duty(adjust+43+speed/2);    // Phase V PWM Duty Setting
         set_power_pwm4_duty(adjust+57+speed);      // Phase W PWM Duty Setting
         // Phase Segment #8
         set_power_pwm0_duty(adjust+1);             // Phase U PWM Duty Setting
         set_power_pwm2_duty(adjust+29+speed/4);    // Phase V PWM Duty Setting
         set_power_pwm4_duty(adjust+57+speed);      // Phase W PWM Duty Setting
         // Phase Segment #9
         set_power_pwm0_duty(adjust+14+speed/6);    // Phase U PWM Duty Setting
         set_power_pwm2_duty(adjust+14+speed/6);    // Phase V PWM Duty Setting
         set_power_pwm4_duty(adjust+57+speed);      // Phase W PWM Duty Setting
         // Phase Segment #10
         set_power_pwm0_duty(adjust+29+speed/4);    // Phase U PWM Duty Setting
         set_power_pwm2_duty(adjust+1);             // Phase V PWM Duty Setting
         set_power_pwm4_duty(adjust+57+speed);      // Phase W PWM Duty Setting
         // Phase Segment #11
         set_power_pwm0_duty(adjust+43+speed/2);    // Phase U PWM Duty Setting
         set_power_pwm2_duty(adjust+1);             // Phase V PWM Duty Setting
         set_power_pwm4_duty(adjust+43+speed/2);    // Phase W PWM Duty Setting
         // Phase Segment #12
         set_power_pwm0_duty(adjust+57+speed);      // Phase U PWM Duty Setting
         set_power_pwm2_duty(adjust+1);             // Phase V PWM Duty Setting
         set_power_pwm4_duty(adjust+29+speed/4);    // Phase W PWM Duty Setting
         // Phase Segment #13
         set_power_pwm0_duty(adjust+57+speed);      // Phase U PWM Duty Setting
         set_power_pwm2_duty(adjust+14+speed/6);    // Phase V PWM Duty Setting
         set_power_pwm4_duty(adjust+14+speed/6);    // Phase W PWM Duty Setting
         adc_val=adc_val-3;                         // Decrement ADC Value
         x=0;                                       // Direction Change Complete
         }
         i=0;                                       // Clear Counter Variable
      }
   }

This code was written so that either the 28 pin or the 40 pin version can be used without code modification. The constant offsets for each PWM segment allow for better use of the PWM signal. But adding those constants puts the code limit a little beyond that of the PIC18Fx331. An array for the sine wave samples was attempted originally but the PIC would not ready quickly enough for the third phase to be implemented. That is the reason for processing the PWM settings on a line by line basis. This code also includes soft start and direction change of the three phase AC motor. Along with code to slow the motor to stop before changing direction. Addition or subtraction of 3 for the adc_val variable ensures that the motor speed up or ramp down time is not greater than six seconds. This code will work with the IRAMS06UP60A, for example. The two feedback variables are used to monitor the rotation speed at two different time instances. Any feedback of questions, comments or suggestions are welcome.
ecentric70



Joined: 05 May 2012
Posts: 3
Location: West Lafayette, IN

View user's profile Send private message

MicroChip Application Note AN900
PostPosted: Sat May 05, 2012 2:57 am     Reply with quote

MicroChip Application Note AN900 is about three phase AC motor control using the PIC18F4431.

http://www.ccsinfo.com/faq.php?page=ccs_mplab
Praful



Joined: 18 Jun 2012
Posts: 23

View user's profile Send private message

Three Phase AC Induction Motor Control
PostPosted: Mon Jun 18, 2012 3:34 am     Reply with quote

Dear ecentric70,
Can you provide circuit diagram and also can you describe how to change frequency up to 500Hz.
Thanking You. Cool
asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

PostPosted: Mon Jun 18, 2012 7:52 am     Reply with quote

does this really do what you think you programmed?

int8 in CCS is by default unsigned.......

yet you assign
Code:

x=-1;


but then there is never a decision based on a value of x=-1;
anyway?
meaning it is never differentiated from x !=0
( ???? )

ALSO

i see a recurring assignment:
Code:

 i=0;


but no other ref to the var ??
does it ever 'count' anything ???

BTW: learn to use the CODE button when you post

my eyes are old and tired
Laughing Laughing Laughing Laughing Laughing Very Happy Very Happy
SherpaDoug



Joined: 07 Sep 2003
Posts: 1640
Location: Cape Cod Mass USA

View user's profile Send private message

Re: Three Phase AC Induction Motor Control
PostPosted: Tue Jun 19, 2012 6:46 am     Reply with quote

Praful wrote:
Dear ecentric70,
Can u provide circuit diagram also can u describe it how to change frequency up to 500Hz.
Thanking You. Cool


As a first guess I would think going from 120Hz to 500Hz would require going from a 8MHz clock to at least a 33MHz clock. First look for a PIC that can do that. Many of the newer ones go as high as 40MHz.
_________________
The search for better is endless. Instead simply find very good and get the job done.
edwinvillalta



Joined: 17 Dec 2012
Posts: 1

View user's profile Send private message

Re: Three Phase AC Induction Motor Control with Direction Ch
PostPosted: Wed Dec 26, 2012 10:52 am     Reply with quote

Hi, ecentric70.

Good job. your code Three Phase AC Induction Motor Control with Change Direction

I am very interested your code

I wanted to ask what program did you use to simulate your code, there will be simulated in proteus (ISIS) or some other software.

and if you do not mind I would ask more information about the subject for study.


Advance Thanks

Att. Edwin Villalta.
Wolf



Joined: 23 Sep 2011
Posts: 32

View user's profile Send private message AIM Address

PostPosted: Sat Feb 16, 2013 7:06 am     Reply with quote

Can something be done just like this using something like dsPIC30F4011 or dsPIC30F6010A using setup_motor_pwm() rather then setup_power_pwm_pins()? I'm doing some research into maybe trying something like this with the 2 dsPIC's I had mentioned
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group