Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] Pic32 and encoder Problem to rotate stepper motor ?

Status
Not open for further replies.

nick703

Advanced Member level 1
Advanced Member level 1
Joined
Oct 17, 2011
Messages
422
Helped
21
Reputation
44
Reaction score
22
Trophy points
1,298
Location
surat
Visit site
Activity points
3,987
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.
 

mStepper_B_For();

mStepper_B_Rev();

mStepper_B_Fourth();

what these functions do?

post the codes of these functions.
 

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 ?
 

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 ?
 

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
 

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.
 

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:

yes i tried but not work. below is my code

Code:
#include <plib.h>

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?
 

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..
 
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.
 

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?
 

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

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top