I am using a random number generator that allows me to generate a number within a specific range. The routine works great but I need to use it on a 16f688. Unfortunately the routine uses a lot of ROM not leaving enough room for the other functions. I have identified the section that is causing the high ROM usage which is the division of a float number. Without the (float) the random number generator does not work. Does anyone know of a better way of doing this or better yet doing float point math without using float?
int16 random(int min,int max)
{
int16 rand; // Declare a temporary 16-bit variable
max = (max + 1); // Increment max so that max value is used otherwise max-1 is performed
seed = READ_SEED; // Get last known seed value from NV memory
seed = seed * 1103515245 + 12345; // Calculate new seed value
SAVE_SEED(seed); // Save seed value in NV memory
rand =(int16) (seed >> 16) % 32767; // Generate a random number
return((int16)min + rand *((max - min)/(float)32767)); // Return random number between specified range
}
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