Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

A math problem / challenge (PIC32)

Status
Not open for further replies.

Borisw37

Newbie level 4
Newbie level 4
Joined
Dec 16, 2013
Messages
6
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Visit site
Activity points
65
I'm trying to come up with a simple function to do basically frequency division / multiplication. This will be written in C running on a PIC32, but doesn't really effect the basic math.
Inputs to the function are:
- Integer variable "Frame" that increments on an constant time base.
- Integer "Speed" in 1/16 increment (Rate * 16 for integer math).

What I want the function to return is "increment". Think of this as an animation.
A few examples of what I want the function to do:

Speed = 16 (16/16 = 1, thus function should increment on every Frame)
Frame = 0, 1, 2, 3, 4, 5, ....
Result = 1, 1, 1, 1, 1, 1, ....

Speed = 32 (32/16 = 2, thus function should increment twice on every Frame)
Frame = 0, 1, 2, 3, 4, 5, ....
Result = 2, 2, 2, 2, 2, 2, ....

Speed = 8 (8/16 = 1/2, thus function should increment on every 2nd Frame)
Frame = 0, 1, 2, 3, 4, 5, ....
Result = 0, 1, 0, 1, 0, 1, ....

Speed = 20 (20/16 = 5/4, thus function should increment on every Frame, twice on every 4th frame)
Frame = 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ....
Result = 1, 1, 1, 2, 1, 1, 1, 2, 1, 1 ....

Speed = 12 (12/16 = 3/4, thus function should increment on every Frame, except for every 4th)
Frame = 0, 1, 2, 3, 4, 5, 6, ....
Result = 0, 1, 1, 1, 0, 1, 1, ....

I hope I'm making sense. I think this should be possible using the modulo operator but having a brain fart coming up with a function.
If ((Frame*16)%(Speed*16) == 0) ?

Thank you,

Boris.
 

Inputs to function are Frame and Speed. Always Speed is divided by 16, then where is Frame value used?


What do you mean by function should increment every Xth frame? How is Result calculated?
 

Inputs to function are Frame and Speed. Always Speed is divided by 16, then where is Frame value used?


What do you mean by function should increment every Xth frame? How is Result calculated?

Not following you 100%....
I could have used speed as a float, having
Speed = 1 (increment every frame)
Speed = 0.5 (increment every other frame)
Speed = 2 (increment twice per frame)
etc....
To do this on a microcontroller I decided to avoid using floats and 1/16th resolution is plenty. Thus I took Speed * 16.
So think of the function as a black box, that I'm trying to figure out.
Inputs are "Frame #" and Speed, output is "Result"

Maybe to make it more understandable I'll briefly explain what I'm trying to do:
I have a string of addressable & controllable LEDs.
I want to have a light run back / forth.
It takes X amount of time to send the data to the LED strip (1 Frame), then I do the math for the next Frame, send it, etc...
I can have the LED light jump 1 position every frame, that will be my Speed = 16 (because 16/16 = 1)
If I want the LED to "move" slower I want it to move 1 position every other frame, so speed of 8.
If I want it to "move" fast I'll move it by 2 positions every frame (Speed = 32)
This is all easy with multiples of 2, it becomes a bit more complex, and thus my question, when you have Speed = 12, like in example above. There I want it to move at 3/4 of "normal" rate.

Does that makes sense?
 

Code:
static char cnt = 0;

int inc (int speed){
   int res;
   cnt += speed;
   res = cnt / 16;
   cnt = cnt % 16;
   return res;
}

I *Think* that does what you seem to be after.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top