04-07-2013, 03:53 PM
Clock delay function
Clock delay.docx (Size: 14.81 KB / Downloads: 18)
A function was written to get precision clock delay for fuel delivery. As it can be noticed from table 1 (page 7) that the fuel has to be delivered at different flow rates for every combination of RD6 and RD7 push button switches. The code listed in listing 2 above implements a generic delay of certain length (the calculations given in the following page) and then it has been put into a FOR loop. The number of time loop executes depends on the variable Y which the function takes from the designer.
Therefore any appropriate precise length of delay can be produced from this generic delay function. The delay was implemented using TMR0 which can go up to 256 and it takes a precise length of time for TMR0 to go from 0 to 1, 1 to 2 and so on. Thus the time was calculated for TMR0 clock cycle and it was put in a While loop to go from TMR0 to 133 to give a delay of 41.67ms. Therefore TMR0 overflows every 41.66ms during one cycle of FOR loop execution. As it can be calculated that if TMR0 overflows 24 times (FOR loop executes 24 times, Y = 24) then total delay created by the function is:
Delay = 41.67ms x 24 = 1s
Delay = 41.67ms x 12 = 500ms times (FOR loop executes 12 times, Y = 12)
Delay = 41.67ms x 6 = 250ms times (FOR loop executes 6 times, Y = 6)
This was required by the main program to precisely deliver amount of fuel required. Because when fuel need to be delivered at 1L/s then the delay can be set to 24 to calculate the volume of the fuel delivered as it will increase the variable for volume by 1 in every 1s.