Delays can be useful for many applications. For example in synchronizing a signal or generating a signal of your desired frequency. A simple application can be to blink a LED so that a human eye can see its blinking.
You must first put a value to a general purpose register. Then you can decrement it by the instruction “decfsz”. “decfsz” can be used to decrement a value until it becomes zero. When it reaches 0 it will skip the next instruction. You need to make a loop to keep decrementing. So if you use a loop you will need the goto instruction. When you use decfsz each time it decrements will consume 1 machine cycle. Which is 1/4 of your oscillator speed. Now can you guess why 4 MHz crystal is most suitable for many time intensive applications? Because internally the microcontroller runs at 1 MHz which is only 1uS for an instruction. So “decfsz” will consume 1uS. But “goto” instruction consumes 2 machine cycles and therefore 2uS. Now you can get any amount of delay.
Maximum number that a GPR can hold is 255. Sometimes this value is not accessible since it is reserved as an address. You can see this clearly if you put movlw FFh. (FFh = 255). If you want 255 get this value by using an untouched general purpose register’s value (this will be always 255). So if you want more you can put delay after delay. To be more flexible you can use the “nop” instruction which does nothing but consumes 1uS. This method is widely used when using infrared and many applications. Example Code for delay as follows. Calculate the delay yourself.
delay equ 0Ch
loop decfsz delay,1
Following code gives you a continuous 38 kHz signal. The label “IRED” must be equaled to the port bit which is connected to an IR LED using equ directive.
Start bsf IRED NOP NOP NOP NOP NOP NOP NOP NOP NOP NOP NOP NOP bcf IRED NOP NOP NOP NOP NOP NOP NOP NOP NOP NOP goto Start