The software for the motor controller is written in PIC assembler. Development tools from Microchip are free and powerful and include the assembler and a simulator which is very helpful in debugging. These days many people prefer to use a higher level language and for many applications this makes a lot of sense. For “bit-banging” code like this I prefer assembler.
The concepts here are simple but like most software, by the time you put in all the bells and whistles you want the code gets longer. The work of generating the pulse to determine the speed is done by two interrupt routines which are very short and clean. If you only wanted one speed and didn’t want to adjust it the interrupt code is all you’d need. All the rest of the code here is for speed adjustment. After turn-on initialization the code just loops, constantly looking for encoder movements and speed selector changes. Whenever an encoder movement is detected the appropriate speed data is updated and a 20 second timer started. When the timer times out the updated speed data is written to the EEPROM so speed settings are saved for the next power-on. Tachometer signals and speed timer timeouts interrupt the polling loop to quickly do their work and return. Here’s a very simplified flowchart:
Motor Controller Software Source Code