So I just started learning about PIC microcontrollers starting with the 8-bit PIC16F84A. I have written a simple code that blinks an LED and I wanted to ask something. Here is a segment of the code that turns on the LED and waits. What I wanted to ask is that in the loop it counts to 255, 255 times. If one instruction takes 1uS to execute then the whole loop takes a time of 255*255*(1E-06)=65ms. But when I simulate the same program in Proteus, it shows that the LED stays on for 198ms and not 65ms. What could I be doing wrong?

  • \$\begingroup\$ maybe one instruction takes 3us to execute... or maybe it takes three instructions to execute one iteration of your loop... \$\endgroup\$ – vicatcu Apr 19 '17 at 3:41
  • 1
    \$\begingroup\$ If your two count variables both start as zero (and I can't tell if they do, but the code looks like it expects that), then the total number of cycles is: \$256\cdot\left(256\cdot 3+3\right)=197376\$ cycles. That doesn't count set-up times and/or cycles after the code that takes place in between changing LED port pins. \$\endgroup\$ – jonk Apr 19 '17 at 4:19

Your loop takes 3 instruction cycles to execute. Not every instruction on the PIC takes exactly one instruction cycle. See Table 7-2 in the PIC16F84A datasheet.

DECFSZ takes 1 instruction cycle usually (2 when it skips) and GOTO takes 2 instruction cycles. That's 3 cycles normally. So, given 3uS per loop, that's almost exactly the timing you got (196ms). The extra time probably comes from the 5 cycles that the loop takes when COUNT1 underflows and the 4 cycles in the very last loop cycle.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.