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] Multi tasking with PIC controller for stepper motors

Status
Not open for further replies.
Thanks a lot for all experts for help. I will try your great suggestions and code and will give feedback tomorrow.
 

Normally for such applications, as suggested by other posters it is better to have timer interrupt running at a suitable timeout (say every 2 milliseconds), this timer interrupt is used as the basis for time keeping throughout the firmware.

The important thing is that your main duty cycle (main function) should not have any blocking delay statements anywhere. However some functions like LCD functions do need small delays in the order of a few milliseconds. In this case such functions should be called when critical functions (stepper motor operations etc.) have been accomplished.

You can do this by need to setup task handlers for each aspect of the design, such as LCD handling, stepper motor operations etc.

For example...

void Task(){

switch (State){

case 1:

///do some action here

//go to the next state
State=2;
break;

case 2:

//do something else here

//wait for some time here
State=3;

break;

//delay
case 3:

if (nTimer>10000){
//do another task after delay
State=4;
}

break;

}

}

nTimer is incremented in the timer interrupt, increase the count value for a longer delay.

You can have other tasks like this for other actions.....

thanks
a
 

I wrote some codes according your instructions. It's right for single motor but not for 3 motors. Because all motors have different speed (different 3 number timer delays) at a time.

I have hide some other codes like ADC, display, EEPROM, Menu, etc. I think you will not need those.
Code:
#include <p18f2520.h>

// High priority interrupt vector
void chk_isr(void);
void InitTimer1(void);
void Run_Motor1(void);
#pragma interrupt chk_isr
void chk_isr(void)
{
	if(PIR1bits.TMR1IF) Run_Motor1();
}

#pragma code isr = 0x08 // store the below code at address 0x08
void isr(void)
{
	_asm
		GOTO chk_isr
	_endasm
}
#pragma code
char FullStepForward [4]={0x03, 0x09, 0x0C, 0x06};
char FullStepBackward[4]={0x06, 0x0C, 0x09, 0x03};


void main ()
{
	TRISA=0xDF;
	TRISB=0x07;
	TRISC=0x00;
	ADCON0=0x01;
	ADCON1=0x0D;
	ADCON2=0xAE;
	T0CON=0x06;
	delay_1sec();		   
	LCD_Init();
	Str_LCD(" Initializing...");
	delay_1sec();
	GetDigitFormEEPROM();
	Time_10ms();
	SowDeskTop();
	InitTimer1();
while(1)
	{
		if(DeskTop==1)
		{
			SowDeskTop();
			DeskTop=0;
		}
		if(loop==0)ADC_ALL();
		if(EnterKey==1) WaitMode();
		loop++;	
		Time_10ms();
		TempSow();
		if(loop>100)loop=0;	
	}
}


//************************************************************************************
 
void InitTimer1(){
	T1CON = 0x19;
	PIR1bits.TMR1IF = 0;
    	TMR1H = 0x48;			// Delay for 300ms (1 rpm)
    
	TMR1L = 0xE5;
	T1CONbits.TMR1ON = 1;
}


void Run_Motor1(){
	union u_type             //Setup a Union
	{
  	unsigned int IntVar;
  	unsigned char Bytes[2];
	}temp;                   //Assign a var name to the Union 

	temp.IntVar = 65536-((M1_ADC * 42.11425)+18661);
	HighByte=temp.Bytes[1];  //Get the High Byte (xxx)
 	LowByte=temp.Bytes[0];   //Get the Low Byte (xxx)
   
	PIR1bits.TMR1IF = 0;
    
	TMR1H = HighByte;
    
	TMR1L = LowByte;
	if(M1Forward)PORTC = FullStepForward[Index];
	else {PORTC = FullStepBackward[Index];}
	Index++;
	if(Index >=4) Index = 0;

} 
//*******************************************************************************

void delay_1sec()
{
	TMR0H=0x67;
	TMR0L=0x69;
 	T0CONbits.TMR0ON=1;
	while(INTCONbits.TMR0IF==0);
	T0CONbits.TMR0ON=0;
	INTCONbits.TMR0IF=0;
}
void Time_10ms()
{
	TMR0H=0xFE;
	TMR0L=0x7A;
 	T0CONbits.TMR0ON=1;
	while(INTCONbits.TMR0IF==0);
	T0CONbits.TMR0ON=0;
	INTCONbits.TMR0IF=0;
}

Please tell me how run 3 motor simultaneously.
 

Do you have buttons to start or stop the motors ? Please post your circuit as image or PDF. If you have Proteus file then zip and post it also. If you zip and post the complete C18 project files it would be easy to edit it and provide you the working code for three motors.
 

Do you have buttons to start or stop the motors ?

No... I do'not have any button for motor on/off. When adc value will go at 0 it will automatically off. It's not in these codes but it is.

Circuit Image
https://obrazki.elektroda.pl/1392727600_1455000738.jpg

- - - Updated - - -

In my circuit a found a problem. I connected resistence ULN2003A input but i have updated.
 

The Circuit shows only 2 motors connections. Where is the 3rd motor connection. Just mention what pins of MCU the motors are connected to. I don't need the whole circuit. Why don't you zip and post the C18 project files ?
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top