Ok source code for my servo controller in the attached file.
It's probably not very clear whats going on at a first look - i didn't comment the code with other viewers in mind. Basically all the important stuff is handled by the interupt handlers at the top of pid.c. The SIG_OVERFLOW0 handler gets excuted at a 40kHz rate and reads the encoders and updates a 8 bit postion.
The SIG_OVERFLOW2 handler runs at 400 Hz or every 2.5ms and transfers the 8 bit encoder and target counts into their 32 bit equivalents and then calls DoPID where the PID algorythm takes place and the PWM drive to the motors is updated.
The SIG_INTERRUPT0-2 interrupts are excuted on the falling edge of a step pulse and read the corresponding direction input and update 8 bit target postion.
The rest of the code Initialises the timers and Interrupts etc and Drives the LCD reads the keypad does the menu etc. |