+ Post New Thread
Results 1 to 14 of 14
  1. #1
    Full Member level 6
    Points: 3,008, Level: 12
    Achievements:
    7 years registered

    Join Date
    Oct 2011
    Location
    surat
    Posts
    395
    Helped
    22 / 22
    Points
    3,008
    Level
    12

    Pic32 and encoder Problem to rotate stepper motor ?

    Hello friends i have a implement code to rotate stepper motor using Encoder .
    and complete working as reverse and forward stepper motor using encoder below code.
    My micro controller is PIC32MX575F256H.
    Encoder pin is RE6 AND RE7.
    Below code is working perfectly.
    Code:
    char old = 0;
    void Encoder(void)
    {
     int new = 0 ;
     int value = 0 ;
     new = PORTE ; //// encoder pin here PORTE PIN 6 ,PIN7
     value = new ^ old ;
     if (value & 0x40)
     {
      if(new & 0x40)
      {
       mStepper_B_Enb(); // STEPPER ENABLE
       if(new & 0x80)
       {
        mStepper_B_For(); // STEPPER FORWARD
        mStepper_B_Fourth();
       }
       else
       {
        mStepper_B_Rev(); // STEPPER REVERSE
        mStepper_B_Fourth();
       }
       
        mStepper_B_Off();
         Delay_us(25);
        mStepper_B_On(); // STEPPER ON
       
      }
     }
     old = new ;
     return ;
    }
    
    main()
    {
       while(1)
       {
            Encoder();
        }
    }
    now my question is when i use PORTG pin RG2 AND RG3 . That time i changed above code to below code
    Code:
    char old = 0;
    void Encoder(void)
    {
     int new = 0 ;
     int value = 0 ;
     new = PORTG ; //// encoder pin here PORTG PIN 2 ,PIN3
     value = new ^ old ;
     if (value & 0x04)
     {
      if(new & 0x04)
      {
       mStepper_B_Enb(); // STEPPER ENABLE
       if(new & 0x08)
       {
        mStepper_B_For(); // STEPPER FORWARD
        mStepper_B_Fourth();
       }
       else
       {
        mStepper_B_Rev(); // STEPPER REVERSE
        mStepper_B_Fourth();
       }
       
        mStepper_B_Off();
         Delay_us(25);
        mStepper_B_On(); // STEPPER ON
       
      }
     }
     old = new ;
     return ;
    }
    
    main()
    {
       while(1)
       {
            Encoder();
        }
    }
    but my stepper motor only rotate one direaction is that pin issue of PIC32MX575F256H ?
    OR please give me solution how to resolve this thing to rotate stepper motor in both direaction.
    please help me.

  2. #2
    Full Member level 3
    Points: 1,926, Level: 10

    Join Date
    Nov 2012
    Location
    India
    Posts
    159
    Helped
    13 / 13
    Points
    1,926
    Level
    10

    Re: Pic32 and encoder Problem to rotate stepper motor ?

    mStepper_B_For();

    mStepper_B_Rev();

    mStepper_B_Fourth();

    what these functions do?

    post the codes of these functions.
    aghilwtsn



    •   AltAdvertisement

        
       

  3. #3
    Advanced Member level 5
    Points: 12,735, Level: 27
    Achievements:
    7 years registered

    Join Date
    Nov 2008
    Location
    Norwich, UK
    Posts
    2,123
    Helped
    598 / 598
    Points
    12,735
    Level
    27

    Re: Pic32 and encoder Problem to rotate stepper motor ?

    RG2 and RG3 are also the USB D- and D+ pins - could be some configuration issue?



  4. #4
    Full Member level 6
    Points: 3,008, Level: 12
    Achievements:
    7 years registered

    Join Date
    Oct 2011
    Location
    surat
    Posts
    395
    Helped
    22 / 22
    Points
    3,008
    Level
    12

    Re: Pic32 and encoder Problem to rotate stepper motor ?

    Quote Originally Posted by snishanth512 View Post
    mStepper_B_For();

    mStepper_B_Rev();

    mStepper_B_Fourth();

    what these functions do?

    post the codes of these functions.
    this function only rotate stepper motor .
    Code:
    #define	S_2_Pulse					BIT_10
    #define S_2_Dir						BIT_9
    #define S_2_Enb						BIT_8
    #define S_2_MS1						BIT_11
    #define S_2_MS2						BIT_12
    #define S_2_Opto					BIT_13
    
    #define mInitStepperB()				(mPORTBSetPinsDigitalOut(S_2_Pulse | S_2_Dir | S_2_Enb | S_2_MS1 | S_2_MS2),\
    									mPORTBSetPinsDigitalIn(S_2_Opto),\
    									mPORTBClearBits(S_2_Pulse | S_2_Dir | S_2_Enb))
    
    #define mStepper_B_On()				(LATBSET = S_2_Pulse)
    #define mStepper_B_Off()			(LATBCLR = S_2_Pulse)
    
    #define mStepper_B_For()			(LATBCLR = S_2_Dir)
    #define mStepper_B_Rev()			(LATBSET = S_2_Dir)
    
    #define mStepper_B_Enb()			(LATBCLR = S_2_Enb)
    #define mStepper_B_Dis()			(LATBSET = S_2_Enb)
    
    #define mStepper_B_Half()			(LATBCLR = S_2_MS1, LATBSET = S_2_MS2)
    #define mStepper_B_Fourth()			(LATBSET = S_2_MS1, LATBCLR = S_2_MS2)
    #define mStepper_B_Eighth()			(LATBSET = S_2_MS1, LATBSET = S_2_MS2)

    and horace1 yes i know but my configuration is below
    Code:
    #pragma config FPLLMUL  = MUL_20        // PLL Multiplier
        #pragma config FPLLIDIV = DIV_2         // PLL Input Divider
        #pragma config FPLLODIV = DIV_1         // PLL Output Divider
        #pragma config FPBDIV   = DIV_1         // Peripheral Clock divisor
        #pragma config FSRSSEL =  PRIORITY_7     // SRS Select
        #pragma config FWDTEN   = OFF           // Watchdog Timer 
        #pragma config WDTPS    = PS1           // Watchdog Timer Postscale
        #pragma config FCKSM    = CSDCMD        // Clock Switching & Fail Safe Clock Monitor
        #pragma config OSCIOFNC = OFF           // CLKO Enable
        #pragma config POSCMOD  = HS            // Primary Oscillator
        #pragma config IESO     = OFF           // Internal/External Switch-over
        #pragma config FSOSCEN  = OFF           // Secondary Oscillator Enable
        #pragma config FNOSC    = PRIPLL        // Oscillator Selection
        #pragma config CP       = OFF           // Code Protect
        #pragma config BWP      = OFF           // Boot Flash Write Protect
        #pragma config PWP      = OFF           // Program Flash Write Protect
        #pragma config ICESEL   = ICS_PGx2      // ICE/ICD Comm Channel Select
        #pragma config DEBUG    = OFF           // Debugger Disabled for Starter Kit
    is that any problem if PORTG pin RG2 AND RG3 is only input pin .thats a problem not resolve ?



  5. #5
    Advanced Member level 5
    Points: 12,735, Level: 27
    Achievements:
    7 years registered

    Join Date
    Nov 2008
    Location
    Norwich, UK
    Posts
    2,123
    Helped
    598 / 598
    Points
    12,735
    Level
    27

    Re: Pic32 and encoder Problem to rotate stepper motor ?

    the PIC32MX575F256H data sheet page 38
    http://www.microchip.com/wwwproducts...IC32MX575F256H

    says RG2 and RG3 are PORTG input pins


    1 members found this post helpful.

  6. #6
    Full Member level 6
    Points: 3,008, Level: 12
    Achievements:
    7 years registered

    Join Date
    Oct 2011
    Location
    surat
    Posts
    395
    Helped
    22 / 22
    Points
    3,008
    Level
    12

    Re: Pic32 and encoder Problem to rotate stepper motor ?

    hello horace1 thanks for your reply , but i also have declare RG2 AND RG3 only input pin of encoder .
    i have not configure any output pin of RG2 AND RG3.
    so what's the problem ?



    •   AltAdvertisement

        
       

  7. #7
    Advanced Member level 5
    Points: 12,735, Level: 27
    Achievements:
    7 years registered

    Join Date
    Nov 2008
    Location
    Norwich, UK
    Posts
    2,123
    Helped
    598 / 598
    Points
    12,735
    Level
    27

    Re: Pic32 and encoder Problem to rotate stepper motor ?

    Quote Originally Posted by nick703 View Post
    hello horace1 thanks for your reply , but i also have declare RG2 AND RG3 only input pin of encoder .
    i have not configure any output pin of RG2 AND RG3.
    so what's the problem ?
    can you write a simple program just to display the state of RG2 and RG3 - apply a 0 and 1 to the pins and see if the correct value is diaplyed
    this should indicate if the pin values are not being read correctly or the problem is in your code

    I have often found such simple programs usful for checking individual pins



  8. #8
    Full Member level 6
    Points: 3,008, Level: 12
    Achievements:
    7 years registered

    Join Date
    Oct 2011
    Location
    surat
    Posts
    395
    Helped
    22 / 22
    Points
    3,008
    Level
    12

    Re: Pic32 and encoder Problem to rotate stepper motor ?

    ok horace1 now i changed pin of PORTG . now that time i used RG7 AND RG8 . and code is below
    Code:
    int old = 0;
    void Encoder(void)
    {
     int new = 0 ;
     int value = 0 ;
     new = PORTG ; //// encoder pin here PORTG PIN 2 ,PIN3
     value = new ^ old ;
     if (value & 0x0080)
     {
      if(new & 0x0080)
      {
       mStepper_B_Enb(); // STEPPER ENABLE
       if(new & 0x0100)
       {
        mStepper_B_For(); // STEPPER FORWARD
        mStepper_B_Fourth();
       }
       else
       {
        mStepper_B_Rev(); // STEPPER REVERSE
        mStepper_B_Fourth();
       }
       
        mStepper_B_Off();
         Delay_us(25);
        mStepper_B_On(); // STEPPER ON
       
      }
     }
     old = new ;
     return ;
    }
    
    main()
    {
       while(1)
       {
            Encoder();
        }
    }
    and both pin is input / output pin but that's time my motor doesn't run any side. and above is the simple program.
    i tried with PORTE and working fine.



  9. #9
    Advanced Member level 4
    Points: 7,632, Level: 20
    Achievements:
    7 years registered
    Venkadesh_M's Avatar
    Join Date
    Jun 2013
    Location
    Coimbatore, India
    Posts
    1,375
    Helped
    258 / 258
    Points
    7,632
    Level
    20

    Re: Pic32 and encoder Problem to rotate stepper motor ?

    Code C - [expand]
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    main()
    {
    if( PORTG & 0x04)
    LED1 = 1;
    else
    LED1 = 0;

    if( PORTG & 0x08)
    LED2 = 1;
    else
    LED2 = 0;
    }



    Try like this...

    Also ensure USB is disabled..

    USBEN = 0;
    Last edited by Venkadesh_M; 18th September 2014 at 12:31.



  10. #10
    Full Member level 6
    Points: 3,008, Level: 12
    Achievements:
    7 years registered

    Join Date
    Oct 2011
    Location
    surat
    Posts
    395
    Helped
    22 / 22
    Points
    3,008
    Level
    12

    Re: Pic32 and encoder Problem to rotate stepper motor ?

    yes i tried but not work. below is my code

    Code:
    #include 
    
    void mInitMicro(void);
    void TimerandUartInit(void);
    void PressureEncoder(void);
    
    #ifndef OVERRIDE_CONFIG_BITS
         
        #pragma config FPLLMUL  = MUL_20        // PLL Multiplier
        #pragma config FPLLIDIV = DIV_2         // PLL Input Divider
        #pragma config FPLLODIV = DIV_1         // PLL Output Divider
        #pragma config FPBDIV   = DIV_1         // Peripheral Clock divisor
        #pragma config FSRSSEL =  PRIORITY_7     // SRS Select
        #pragma config FWDTEN   = OFF           // Watchdog Timer 
        #pragma config WDTPS    = PS1           // Watchdog Timer Postscale
        #pragma config FCKSM    = CSDCMD        // Clock Switching & Fail Safe Clock Monitor
        #pragma config OSCIOFNC = OFF           // CLKO Enable
        #pragma config POSCMOD  = HS            // Primary Oscillator
        #pragma config IESO     = OFF           // Internal/External Switch-over
        #pragma config FSOSCEN  = OFF           // Secondary Oscillator Enable
        #pragma config FNOSC    = PRIPLL        // Oscillator Selection
        #pragma config CP       = OFF           // Code Protect
        #pragma config BWP      = OFF           // Boot Flash Write Protect
        #pragma config PWP      = OFF           // Program Flash Write Protect
        #pragma config ICESEL   = ICS_PGx2      // ICE/ICD Comm Channel Select
        #pragma config DEBUG    = ON           // Debugger Disabled for Starter Kit
                
    #endif
    
    
    
    /****Stepper B*********************************************/
    
    #define SYS_FREQ 				(80000000L)
    
    #define	S_2_Pulse					BIT_10
    #define S_2_Dir						BIT_9
    #define S_2_Enb						BIT_8
    #define S_2_MS1						BIT_11
    #define S_2_MS2						BIT_12
    #define S_2_Opto					BIT_13
    
    #define mInitStepperB()				(mPORTBSetPinsDigitalOut(S_2_Pulse | S_2_Dir | S_2_Enb | S_2_MS1 | S_2_MS2),\
    									mPORTBSetPinsDigitalIn(S_2_Opto),\
    									mPORTBClearBits(S_2_Pulse | S_2_Dir | S_2_Enb))
    
    #define mStepper_B_On()				(LATBSET = S_2_Pulse)
    #define mStepper_B_Off()			(LATBCLR = S_2_Pulse)
    
    #define mStepper_B_For()			(LATBCLR = S_2_Dir)
    #define mStepper_B_Rev()			(LATBSET = S_2_Dir)
    
    #define mStepper_B_Enb()			(LATBCLR = S_2_Enb)
    #define mStepper_B_Dis()			(LATBSET = S_2_Enb)
    
    
    #define mStepper_B_Full()			(LATBCLR = S_2_MS1, LATBCLR = S_2_MS2)
    #define mStepper_B_Half()			(LATBCLR = S_2_MS1, LATBSET = S_2_MS2)
    #define mStepper_B_Fourth()			(LATBSET = S_2_MS1, LATBCLR = S_2_MS2)
    #define mStepper_B_Eighth()			(LATBSET = S_2_MS1, LATBSET = S_2_MS2)
    
    #define GetOpto_B_Status()			PORTBbits.RB13;		//S_2_Opto Sense Pin
    
    #define Manual			0
    
    void Delay_us(UINT16 Us)
    {
    	Us *= 10;
    	while(Us != 0)
    	{
    		Us--;
    	}
    }
    
    void MsDelay(unsigned int mS)
    {
    	unsigned int j = 0;
    
    	for(mS; mS > 0; mS--)
    	{
    		for(j = 0; j < 3200; j++)
    		{
    			asm("NOP"); asm("NOP"); asm("NOP"); asm("NOP");
    			asm("NOP"); asm("NOP"); asm("NOP"); asm("NOP");
    			asm("NOP"); asm("NOP"); asm("NOP"); asm("NOP");
    			asm("NOP"); asm("NOP"); asm("NOP"); asm("NOP");
    			asm("NOP");
    		}
    	}
    }
    
    
    int             old1 = 0;
    BYTE			AutoStatus = 0;
    
    int main(void)
    {
    	mInitStepperB();
    	TimerandUartInit() ; 
    	mInitMicro() ;
    
    	INTEnableSystemMultiVectoredInt();
    
    	MsDelay(10);
    
       while(1)
       {
    		if (AutoStatus == Manual)
    		{	
     			PressureEncoder();
    		}	
    
    		
       };
    	return 0;
    
    }
    
    
    void PressureEncoder(void)
    {
    	int new1 = 0 ;
    	int value1 = 0 ;
    	new1 = PORTE ;
    	value1 = new1 ^ old1 ;
    	if (value1 & 0x40)
    	{
    		if(new1 & 0x40)
    		{
    			mStepper_B_Enb(); 
    			if(new1 & 0x80)
    			{
    				mStepper_B_For();
    				mStepper_B_Fourth();
    			
    			}
    			else 
    			{
    				mStepper_B_Rev();
    				mStepper_B_Fourth();
    			}
    			mStepper_B_Off();
    				Delay_us(200);
    			mStepper_B_On();
    			
    		}
    	}
    	old1 = new1 ;
    	return ;
    }
    
    
    //void PressureEncoder(void)
    //{
    //	int new1 = 0 ;
    //	int value1 = 0 ;
    //	new1 = PORTG ;
    //	value1 = new1 ^ old1 ;
    //	if (value1 & 0x0080)
    //	{
    //		if(new1 & 0x0080)
    //		{
    //			mStepper_B_Enb(); 
    //			if(new1 & 0x0100)
    //			{
    //				mStepper_B_For();
    //				mStepper_B_Fourth();
    //			
    //			}
    //			else 
    //			{
    //				mStepper_B_Rev();
    //				mStepper_B_Fourth();
    //			}
    //			mStepper_B_Off();
    //				Delay_us(200);
    //			mStepper_B_On();
    //			
    //		}
    //	}
    //	old1 = new1 ;
    //	return ;
    //}
    
    void TimerandUartInit(void)
    {
    	int    pbClk;
    
       	pbClk=SYSTEMConfig(SYS_FREQ, SYS_CFG_WAIT_STATES | SYS_CFG_PCACHE);
    
    	OpenTimer4(T4_ON | T4_PS_1_4, 0xFFFF);				//Pressure Motor
    	ConfigIntTimer4(T4_INT_ON | T4_INT_PRIOR_2);
    	EnableIntT4;
    }
    
    void mInitMicro(void)
    {
    	DDPCONbits.JTAGEN = 0;			//JTAG debugger off
    
    	mStepper_B_Dis();	          	//Pressure Stepper Disable
    	mStepper_B_For();
    	mStepper_B_On();
    	mStepper_B_Half();
    
    }
    
    void __ISR(_TIMER_4_VECTOR, ipl2) PressureTimerIntHandler(void)
    {
    	if (AutoStatus == Manual)
    	{
    		PressureEncoder();
    	}
    	mT4ClearIntFlag();					
    }
    in above code working perfectly for PORTE but when i comment PORTE section and uncomment PORTG section that time code not working?



  11. #11
    Advanced Member level 4
    Points: 7,632, Level: 20
    Achievements:
    7 years registered
    Venkadesh_M's Avatar
    Join Date
    Jun 2013
    Location
    Coimbatore, India
    Posts
    1,375
    Helped
    258 / 258
    Points
    7,632
    Level
    20

    Re: Pic32 and encoder Problem to rotate stepper motor ?

    seems you have used RG7 and RG8 in this code.

    Don you have any LEDs or just put the bits in some output pin and measure it with a multimeter to check whether the input is taken correctly or not..


    1 members found this post helpful.

    •   AltAdvertisement

        
       

  12. #12
    Full Member level 6
    Points: 3,008, Level: 12
    Achievements:
    7 years registered

    Join Date
    Oct 2011
    Location
    surat
    Posts
    395
    Helped
    22 / 22
    Points
    3,008
    Level
    12

    Re: Pic32 and encoder Problem to rotate stepper motor ?

    hello Venkadesh_M

    yes i have use RG7 AND RG8 as a encoder input pinto rotate stepper motor as a PORTB .

    yes i check with multimeter and i see that input pin of encoder voltage completely coming. when i turn encoder that time variation on input pin of PORTG changed . but doubt in RG7 pin . this pin volt5age not change completely . why i don't understand.



  13. #13
    Advanced Member level 4
    Points: 7,632, Level: 20
    Achievements:
    7 years registered
    Venkadesh_M's Avatar
    Join Date
    Jun 2013
    Location
    Coimbatore, India
    Posts
    1,375
    Helped
    258 / 258
    Points
    7,632
    Level
    20

    Re: Pic32 and encoder Problem to rotate stepper motor ?

    1, What is the state of the pin high or low?

    2, Remove the input and check the status of sensor output by turning the encoder.

    3, check the TRISG reg

    4, what is the other functionality in RG7 pin?



  14. #14
    Full Member level 6
    Points: 3,008, Level: 12
    Achievements:
    7 years registered

    Join Date
    Oct 2011
    Location
    surat
    Posts
    395
    Helped
    22 / 22
    Points
    3,008
    Level
    12

    Re: Pic32 and encoder Problem to rotate stepper motor ?

    now every thing working fine it's hardware issue ?
    but my whole day lost. thanks for your reply to all.



--[[ ]]--