GPIO Manipulation

A project log for PID Motor Control with dsPIC33EP

A PID Controller that is used to control the speed of a brushed DC Motor

JoshuaBRussellJoshuaBRussell 07/28/2017 at 19:050 Comments

Although it would have been quicker in some respects to program the PID controller by setting the correct Special Function Registers to the desired values, part of my goal was to develop a useful library of code that I could use later on for future projects. Before I started on anything too complicated, I wanted to have code to manipulate the GPIO pins. 

I found an article (one I can't seem to find now...) describing a technique to place the same types of SFR in an array. Just index for the desired SFR to access it. For example, here is the array with pointers to the TRISx registers that manipulate whether the pin is an input or an output.  

uint16_t volatile * const TRIS_ARRY[NUM_PORTS] = { (uint16_t*)&TRISA, ... }

To access any specific register, all that is needed to be done is to index it from an array, like so:

*(TRIS_ARRY[port]) |=  (0x1 << pin);

This also adds adaptability to the code. If I decided to use another PIC uC, it isn't difficult to add extra special function registers it might have to the array. 

Once this was done, I had a pattern I could use to develop the rest of the code, and I could start to focus on sections that the PID controller was directly dependent on.