I have a real noob question about variable overflow.
I'm writing a function to generate sinusoidal output via PWM. In my function, I have an 32-element array containing sinusoidal PWM duty cycle values, and I wish to move through the values of that array when the function is called. When I get to the 31th element of the array, I want to start over at the 0th element--the array values are 'circular'.
The way I'm doing this is that I have unsigned byte variable "index". I increment that by 1 when the function is called, and then copy the (index % N)th array element to the PWM output compare register. I believe that my index variable can overflow without any problem. The modulo will work correctly and everything, even through the overflow from 255 to 0.
I'm not sure exactly what happens going the other way, however. When I _decrement_ my index variable, eventually I'm going to get down to 0. When I decrement it again, does it "underflow" back to 255? And in this case, is the modulo operator going to work the "expected" way?
I have a feeling it will and everything will be alright, but I don't really know. I can use a signed variable, if that is necessary.
In your example, N is 32, because you have 32 registers. What you are doing by incrementing the index, and then retrieving the (index % N)th element in the array works to create your loop, but the only reason that your loop doesn't break when you overflow the index is the coincidence that 256%32=0.
So, you can go happily along, index++ing all day and as your 8-bit value loops from 0-255, index%N does 8 tidy loops from 0-32, and they match back up again at 0.
The issue comes up when index==0 and you want to index--, you are worried that the result mod32 might not be 31 and in truth I am not sure what happens when you decrement an 8-bit unsigned int from 0, but you don't need to know, and here's why:
Every time you increment index, just modN index itself, and then retrieve the indexth element in the array. To decrement, don't -1, add N-1
Your current:
index = index+1;
value = array[index%N]
My suggestion:
index = (index+1)%N
value = array[index]
This will keep your index in a tight 0 to (N-1) loop, and isn't dependant upon a magic N which neatly divides the maximum value of index.
But, how to decrement? Simple:
Current:
index = index -1:
Suggested:
index = (index + N -1) % N
Now, since adding multiples of N doesn't really matter to the outcome of the modulo, you could replace both equations with one, simple conglomeration:
if (we want to go up one) {
delta = 1;
} else {
delta = -1;
}
index = (index + N + delta) % N;
value = array[index];