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