pacgreg Posted February 7, 2015 Share Posted February 7, 2015 I want to make a constant timer that controls a bunch of stuff, including activating a block of code every 5 cycles, but it needs to also go to 45, is there a way i could activate something every multiple of 5 like if timer % 5 == 0 then goto..., but in basic? Quote Link to comment Share on other sites More sharing options...
ZackAttack Posted February 7, 2015 Share Posted February 7, 2015 Off the top of my head I can think of 3 options: 1) Use BCD for the variable that holds the cycle count. Then AND it with %00001111 and check if it is equal to 0 or 5. This option will use a few more CPU cycles than the other two, but is good if you absolutely must use mod 5 and don't have a lot of ROM space available. 2) Create a 45 byte lookup table with a 1 stored in every 5th byte and 0 in the rest. Then simply index into the lookup table with the cycle count variable and compare that value with 1. This option is good if you must use mod 5 and have more ROM space than CPU cycles. 3) Make it mod 4 or 8 (or any power of 2). Then you can simply AND the count variable with n-1 to obtain mod n quickly. This is the best option to use if you can tolerate using a value that is a power of 2. Ex: cycleCount AND %00000011 is equivalent to cycleCount % 4 Quote Link to comment Share on other sites More sharing options...
pacgreg Posted February 7, 2015 Author Share Posted February 7, 2015 The second option seems like its useful for this and possibly more things in the future, is it called a data array in the batari page? Quote Link to comment Share on other sites More sharing options...
ZackAttack Posted February 7, 2015 Share Posted February 7, 2015 Yes, I'm referring to this. Just use the cycle count variable to index into the array. if modFiveLookup[cycleCount]!=0 then goto FifthCycle Quote Link to comment Share on other sites More sharing options...
bogax Posted February 7, 2015 Share Posted February 7, 2015 what else does the timer have to do? do you really want 45 or do you want 0..44? count = count + 1 if count = 68 then count = 0 if count & 7 = 5 then count = count + 3 will go through a cycle with a length of 45 but it doesn't count 0..44 bits 0..2 count 0..4 and bits 3..6 (ie count/8) count 0..8 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.