How to kill your micro controller's time.
I think an example will clear it all out. Let assumed that our micro controller clock is set to 4 MHz, and we would like to make a delay of 100 uS.
- ;***** VARIABLE DEFINITIONS (examples)
- #define COUNTER_us 0x20
- ....
- ;somewhere in the program
- call Delay100us
- ......
- ......
- Delay100us
- movlw d'31'
- movwf COUNTER_us
- delay100us
- decfsz COUNTER_us, 1
- goto delay100us
- nop
- nop
- return
Instructions = 2(call) + 1 (movlw) + 1 (movwf) + 30x[1+2](decfsz and goto) + 2(decfsz the last time) + 2 (2 nop) + 2 (return) => Instructions = 100
You have to remember that every instruction cycle = 4 Tosc (Tosc = 1/frequency of the pic). So lets do the maths:
Tosc=1/Fosc = 1/4MHz = 0.25uS
100 instruction cycles = 4x0.25us =>uS = 100/(4x0.25) => Time = 100uS
And now you can create more delay subroutines, based on that logic. For example if you want a delay of 1ms you can use this delay 10 times, and so on.
Please keep in mind that post because we are going to use delay functions to a lot articles.
More about delay functions you can find here:
http://pcbheaven.com/picpages/Instruction_Cycle_Duration_and_Programmable_Delays/
http://www.piclist.com/techref/piclist/codegen/delay.htm (i tried this and is very good)
http://waihung.net/microchip-delay-calculator/ ( i didn't tried it)
No comments:
Post a Comment