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.

pic16f690 and stepper motor

Status
Not open for further replies.

siti noorzazlina

Newbie level 4
Joined
Aug 28, 2010
Messages
6
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,335
im in desperately wanna learn how to code c programming in PIC microcontroller. And also how to connect it to stepper motor, do anyone here have any schematic diagram for me to refer to?
 

Thanks for the reply. That is really informative. However, I want to ask you another question. I come across this video on youtube. stepper motor controller with pic and c code - YouTube and trying to understand how he built the code, but I cant figure it out! Attached is the code that I copied from the youtube. My specific question is, why he assigned specified hex number for PORT C?
PORTC = 0X40; ----> what is the meaning behind this?
Delay_ms(200);
i = 2;

And if you care to explain the other part of the code as well, i really appreciate it. Thank youuu
 

Attachments

  • motor.txt
    2.2 KB · Views: 57

tnx Big Dog for the info, I really appreciate the links you give, I'm also interested in this topic coz i'm also working to run a stepper motor using pic16f877a, I have the basic idea on using pic16f877a and run simple programs using
micro basic and use LED's as output, this topic is great and I'l be waiting for informative replies and comments.
 


My specific question is, why he assigned specified hex number for PORT C?
PORTC = 0X40; ----> what is the meaning behind this?
Delay_ms(200);
i = 2;

A stepper motor is comprised of a series of windings strategically arranged around the rotor. The motor speed and direction are controlled by sequentially energizing these windings in particular patterns and rates. The stepper motor in the video has four of these windings, each of which are energized by one of the four most significant bits/PINs of PORTC.

The PORTC assignment, I've highlighted in red above, simply energizes a particular winding by setting the associated PORTC PIN high. In the case above, RC6 or PIN7 of PORTC is set high energizing its associated winding in the stepper motor. The energizing of one winding produces a full step rotation of the motor's rotor. If the windings are energized first with a single winding followed by energizing a particular pair the routine produces a half step rotation of the motor's rotor.

For example:

RC7 -> Winding A
RC6 -> Winding B
RC5 -> Winding C
RC4 -> Winding D

Code:
int i=0; // State Counter is incremented sequentially to determine the next state of the winding energizing sequence
int t=0;

void main() 
{
	TRISC = 0X00;
	PORTC = 0X00;
	TRISD = 0XFF;

	while (1)
	{
           [COLOR="#FF0000"]// Forward Rotation - Full Step[/COLOR]
	   if(RD0_bit==0 && i==0)
		{
			PORTC = 0X10;  [COLOR="#FF0000"]// 0001 - Energize winding D[/COLOR]
			i = 1;
			Delay_ms(200);
		};
	
		if(RD0_bit==0 && i==1)
		{
			PORTC = 0X40; [COLOR="#FF0000"]// 0100 - Energize winding B[/COLOR]
			Delay_ms(200);
			i = 2;	
		}
		if(RD0_bit==0 && i==2)
		{
			PORTC = 0X20; [COLOR="#FF0000"]// 0010 - Energize winding C[/COLOR]
			Delay_ms(200);
			i = 3;	
		}

		if(RD0_bit==0 && i==3)
		{
			PORTC = 0X80; [COLOR="#FF0000"]// 1000 - Energize winding A[/COLOR]
			Delay_ms(200);
			i = 0;	
		}

                [COLOR="#FF0000"]// Backward Rotation - Full Step[/COLOR]
		if(RD1_bit==0 && i==0)
		{
			PORTC = 0X80; [COLOR="#FF0000"]// 1000 - Energize winding A[/COLOR]
			i = 1;
			Delay_ms(200);
				
		};

		if(RD1_bit==0 && i==1)
		{
			PORTC = 0X20; [COLOR="#FF0000"]// 0010 - Energize winding C[/COLOR]
			Delay_ms(200);
			i = 2;
		}

		if(RD1_bit==0 && i==2)
		{
			PORTC = 0X40; [COLOR="#FF0000"]// 0100 - Energize winding B[/COLOR]
			Delay_ms(200);
			i = 3;
		}
			
		if(RD1_bit==0 && i==3)
		{
			PORTC = 0X10; [COLOR="#FF0000"]// 0001 - Energize winding A[/COLOR]
			Delay_ms(200);
			i = 0;
		}	
	
                [COLOR="#FF0000"]// Forward Rotation - Half Step[/COLOR]
		if(RD2_bit==0 && t==0)
		{
			PORTC = 0X10; [COLOR="#FF0000"]// 1000 - Energize winding A[/COLOR]
			t = 1;	
			Delay_ms(500);
		};

		if(RD2_bit==0 && t==1)
		{
			PORTC = 0X50; [COLOR="#FF0000"]// 0101 - Energize windings B & D[/COLOR]
			Delay_ms(500);
			t = 2;
		}
	
		if(RD2_bit==0 && t==2) 
		{
			PORTC = 0X40;  [COLOR="#FF0000"]// 0100 - Energize winding B[/COLOR]
			Delay_ms(500);
			t = 3;
		}

	    if(RD2_bit==0 && t==3)
		{
			PORTC = 0X60; [COLOR="#FF0000"]// 0110 -Energize windings B & C[/COLOR]
			Delay_ms(500);
			t = 4;
		}

		...
                ...
                ...

By varying the delays after each energizing sequence, the speed of the rotor can be controlled. Each routine controlling direction and half or full step is controlled by the closure of a particular switch attached to PORTD and is first tested in each "if" conditional statement.

Does this description help clear up the purpose of the example code?

Hope the info helps in your understanding,

BigDog
 
Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top