|
|
View previous topic :: View next topic |
Author |
Message |
John_Lintern
Joined: 30 Sep 2004 Posts: 14
|
How to load 6 variables into an array, but in order of size? |
Posted: Tue Nov 15, 2005 9:35 am |
|
|
I have written some code which calculates six int16 values to be loaded into CCP1.
Say for example, my code calculates the following values...
A_RisingEdge=12500;
C_FallingEdge=31250;
B_RisingEdge=25000;
A_FallingEdge=6250;
C_RisingEdge=37500;
B_FallingEdge=18750;
I need to load these values into an array called 'FiringPoints'.
But I need to put the above values into the array in numerical order, so I would get.....
FiringPoints[0]=6250;
FiringPoints[1]=12500;
FiringPoints[2]=18750;
FiringPoints[3]=25000;
FiringPoints[4]=31250;
FiringPoints[5]=37500;
Also, when each of the CCP1 interrupts occur at each of the given values in the array, I need to determine which one of the 3 output pins needs to be driven, output A, B or C.
In this example, I would need some code to determine that if CCP1 is set to 6250, then output A (A_FallingEdge) needs to be fired.
Or if CCP1 is set to 25000, then output B (B_RisingEdge) needs to be fired.
How can I go about doing this ?? |
|
|
Ttelmah Guest
|
|
Posted: Tue Nov 15, 2005 9:47 am |
|
|
Basically, you need a sort.
The 'best' sort algorithm, depends on the amount of data involved, which in your case is fairly small. I'd suggest that a 'bubble sort', might well do for this. All you do, is scan through the data, checking each pair of numbers, and if the second is smaller than the first, swap them. Repeat this enough times, and the big items will 'bubble' to the top. The easiest way to stop the sort, is to clear a flag before each pass, and set it if a swap occurs. When the pass occurs without a swap, the sort is finished.
So (a crude vesion), something like:
Code: |
int1 flag=true;
int16 temp;
int8 ctr;
while (flag) {
flag=false;
for (ctr=0;ctr<5;ctr++) {
if (FiringPoints[ctr] > FiringPoints[ctr+1]) {
//Here a swap is needed
flag=true;
temp=FiringPoints[ctr+1];
FiringPoints[ctr+1]=FiringPoints[ctr];
FiringPoints[ctr]=temp;
}
}
}
|
The routine will exit, when no swaps occur.
There are a number of alternatives to this.
Best Wishes |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|