Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronic 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.

Register Log in

Controlling a stepper motor using PIC18F452 using C18 compiler of MPLAB

Status
Not open for further replies.

talhataj

Newbie level 3
Joined
Nov 22, 2012
Messages
4
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,327
I am controlling a stepper motor using PIC18F452 using C18 compiler of MPLAB and the code is as follows...


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/** V A R I A B L E S *************************************************/
#include "p18f452.h"
#include "delays.h"
#pragma udata // declare statically allocated uninitialized variables
unsigned char LED_Number; // 8-bit variable
unsigned char RPM,Cycles;
/** D E C L A R A T I O N S *******************************************/
// declare constant data in program memory starting at address 0x180
#pragma romdata Lesson3_Table = 0x180
const rom unsigned char LED_LookupTable[4] = {0x01, 0x02, 0x04, 0x08};
#pragma code // declare executable instructions
void main (void)
{
RPM=100; // setting speed to 100 rpm.
LED_Number = 0;
TRISA = 0x00; // PORTA bits 7:0 are all outputs (0)
TRISB = 0xFF; // PORTB bits 7:0 are all inputs (1)
while (1)
{
Cycles=(3765/RPM);  // Calculates the apprpriate cycles of delay for required RPM
PORTA = LED_LookupTable[LED_Number];
LED_Number++;
if (LED_Number == 4)
{
LED_Number = 0; // go back to LED 0.
}
Delay100TCYx(Cycles); // Delay Cycles x 100 
if (PORTBbits.RB0 == 1) 
RPM=20;
}
}



The first four bits of PORTA are used to send pulses to stepper. Initially the speed is set to 100 rpm but during the execution when first bit of PORTB ( i.e RB0) is set to high, the speed changes to 20 rpm........

But when RB0 goes back to zero the speed goes back to 100 rpm which it is not suppose to go as 100 rpm was initialized outside the while(1) loop.

can anyone tell me what is wrong with my code????
 
Last edited by a moderator:

Tunelabguy

Full Member level 5
Joined
Jan 3, 2011
Messages
319
Helped
92
Reputation
184
Reaction score
89
Trophy points
1,308
Activity points
4,228
I can see no reason in the code why the speed should go back to 100 rpm when RB0 goes low. My guess is that when RB0 goes low it also (for some external reason) resets the PIC, causing it to start over again.
 

bigdogguru

Administrator
Joined
Mar 12, 2010
Messages
9,831
Helped
2,348
Reputation
4,690
Reaction score
2,274
Trophy points
1,413
Location
Southwest, USA
Activity points
62,506
Actually, the PIC maybe resetting itself due to internal reasons.

Have you ensured the watchdog timer is disabled? It is enabled by default.

What are you current Configuration Register settings?


If you are attempting to drive the stepper motor with the same power supply as the PIC circuit, it could certainly be the source of reset as previous reply suggests.

Post the schematic of your circuit so that we can advise you further.


Also the pattern output on PORTA does not appear to be standard pattern to drive a stepper motor.

What type of stepper motor are you driving?


A typical stepper motor drive pattern is as follows:

Unipolar Full Step Forward

1100
0110
0011
1001

BigDog
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top