'Arduino C++ - call registers by arrays
I am using a Teensy with Teensyduino to control several stepper motors. This requires writing to timer value registers, and there are 4 needed for each stepper. Currently I use
FTM3_C0V = val[0][0];
FTM3_C1V = val[0][1];
FTM3_C2V = val[0][2];
FTM3_C3V = val[0][3];
FTM3_C4V = val[1][0];
FTM3_C5V = val[1][1];
FTM3_C6V = val[1][2];
FTM3_C7V = val[1][3];
` for this. I'd like to add a few more motors and use an array to hold the addresses of FTMx_CyV. if possible. References to these registers are defined in a .h file as< for example
< #define FTM3_C0V (*(volatile uint32_t *)0x400B9010) // Channel 0 Value
etc.
I'd appreciate any pointers (no pun intended) on how I can do this. I have tried a number of possibilities but all have either failed to work or given me error messages.
Thanks in advance.
Solution 1:[1]
Declaring the register array as
volatile uint32_t* FTM_CV[2][4] = { {&FTM3_C0V, &FTM3_C1V, &FTM3_C2V, &FTM3_C3V},
{&FTM3_C4V, &FTM3_C5V, &FTM3_C6V, &FTM3_C7V} };
and referring to it with
void setPwms(int ph, uint8_t motor)
{
int j = ph%16;
*FTM_CV[motor][0] = (j < 8) ? pwmValues[j] : 0;
*FTM_CV[motor][1] = (j < 8) ? 0 : pwmValues[j-8];
j = (ph+4)%16;
*FTM_CV[motor][2] = (j < 8) ? pwmValues[j] : 0;
*FTM_CV[motor][3] = (j < 8) ? 0 : pwmValues[j-8];
}
seems to be working.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |