|
|
View previous topic :: View next topic |
Author |
Message |
Tunfiskur
Joined: 01 Jun 2012 Posts: 13
|
Input not working - Stepper motor control 16f84a |
Posted: Fri Jun 01, 2012 9:08 pm |
|
|
Hello guys, I was hoping that someone could help me with a small problem I have with my code. I'm using V 4.013.
Desired function of code:
Basically I want the 16f84a to run the stepper_FW() function when I press a button on pin A1. What happens now, with the current code is that the motor will turn if A1 is high when I turn him on. And it cycles through the stepper_FW() one time and one time only, even if I give HIGH on A1 again.
Code: |
#define w1 PIN_B0
#define w2 PIN_B1
#define w3 PIN_B2
#define w4 PIN_B3
#define time 50
void stepper_FW()
{
int num=100;
int i=0;
for(i=0; i<num; i++)
{
output_high(w1);
output_low(w2);
output_high(w3);
output_low(w4);
delay_ms(time);
output_low(w1);
output_high(w2);
output_high(w3);
output_low(w4);
delay_ms(time);
output_low(w1);
output_high(w2);
output_low(w3);
output_high(w4);
delay_ms(time);
output_high(w1);
output_low(w2);
output_low(w3);
output_high(w4);
delay_ms(time);
}
}
void main()
{
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
do{
stepper_FW();
}while(input(PIN_A1)==1);
return;
}
|
Thanks |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19520
|
|
Posted: Sat Jun 02, 2012 2:20 am |
|
|
First, 'of course it does'. Currently, if pin A1 goes low, the code drops off the end, and dies. 'return' does nothing here - remember, unlike on a PC, where the code is being 'called' from an OS, here the code is everything. Outside there is just (effectively) a 'void' into which if the processor drops, nothing happens. You code has to stay looping whatever the state of pin A1, and do two different things according to whether this is high or low, or look for another pin.
Second comment though. Don't control a stepper with individual bit outputs. Output the whole drive 'byte' in one piece. Problem is that you will be generating moments with extra bits on as you move from state to state, potentially doing nasty things. The bits need to update all in one 'piece', _or_ you must turn off the last bits before switching new ones on.
So:
Code: |
#define time (50) //It is good practice to always 'bracket' defines. Can
//avoid some unexpected results when these are substituted.
#define FORWARD TRUE;
#define REVERSE FALSE;
const int8 patterns[4]; = {0b0101,0b0110,0b1010,0b1001);
//The four patterns of drive bits required - from your code.
int16 position=0;
void step(int1 direction) {
if (direction) position++;
else position--;
output_b(patterns[position & 0x3];
}
void stepper_100(int1 direction) {
int i;
for (i=0;i<100;i++) {
step(direction);
delay_ms(TIME);
}
}
#define stepper_FW() stepper_100(FORWARD)
#define stepper_BACK() stepper_100(REVERSE)
void main(void) {
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
do{
if (input(PIN_A1))
stepper_FW();
if (input(PIN_A2)) //have added this to move the opposite way if A2
stepper_BACK();
}while(TRUE);
}
|
Now, have modified the code so it gives 100 steps forward when A1 is made (and will keep doing so, for so long as it is made), or does 100 steps back if A2 is made. Realise that if _both_ are on, the unit will do 100 forward, 100 back, 100 forward, etc....
The comment about 'bracketing defines', is a general one. Basically you can get some unexpected results when values are put into more complex equations using defines, and bracketing helps to avoid these. Just worth being 'aware' of this.
Best Wishes |
|
|
Tunfiskur
Joined: 01 Jun 2012 Posts: 13
|
|
Posted: Sat Jun 02, 2012 7:57 am |
|
|
Thanks for the awesome info, it helped alot. |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|