Quote:
Originally Posted by RyanN
Could you help me with this. I'm new to programming, and am not very good at all. I was thinking that the cpu must be pretty fast to catch every loop perfectly. So where should I write this code and what should I write?
|
Alright, In User_routines.c you want to declare (globally, at the top where it says initialize user variables) a variable that will count pulses. For this example call it count.
in the User_Initialization function add this line
INTCON3bits.INT2IE=1;
This line enables Interrupt 2 if INTCON3bits.INT2IE is equal to 1 then every time the signal pin on digital input 1 goes high then it will enter the interrupt routine.
The final step is to add a line into the interrupt routine
Open user_routines_fast.c
go down to the InterruptHandlerLow function
The first if statement is the one you want to change just so you know what the if statement actually says.
it says "if (INTCON3bits.INT2IF && INTCON3bits.INT2IE)" INTCON3bits.INT2IF is the interrupt flag, it is tripped whenever the pin goes high, INTCON3bits.INT2IE is the interrupt enable flag you set that to 1 in the last step.
Inside the if statement you want to add "count++;" (or whatever you named the variable)
Make sure you leave INTCON3bits.INT2IF = 0; other wise the code will keep stepping into the interrupt.
Now you can use the count any where in the code you want, you can reset it to 0 if you want to, basically use it just like you were before.
The reason you need the interrupt is that at decent speeds the encoder can pulse more than once per loop, if you were using it in a lower speed application, say on a high torque gearbox it may not be necessary.
One last note, this will only count half as many pulses per revolution as yours did, the interrupt only counts high signals at the pin, your code counted every change, both low and high. THe interrupt however should give you plenty of resolution.
If you have any more questions my s/n on AIM is JamesBrownsSN otherwise just post or PM me. Good luck.
James