Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7922623
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T17:01:40+00:00 2026-06-03T17:01:40+00:00

Edit: PIC 16F684 Okay, I have a simple 3 LED binary clock that counts

  • 0

Edit: PIC 16F684

Okay, I have a simple 3 LED binary clock that counts from 0-7, and want to add a delay of approx 1 second between each light turning on.

I’ve worked out that each light needs to be in a sort of loop, and I have to use a count to measure ticks, and rollover etc.

I think the clock is 4MHz, here’s a screenshot of the manual: https://i.stack.imgur.com/3A3sJ.png

Here’s the relevant extracts from my code:

COUNT1 EQU 20h      ; Delay counter #1
COUNT2 EQU 21h      ; Delay counter #2

…

LOOP0
        MOVLW   TRIS_D0_D1      ; Move value defined in Constants to TRISA, to switch on LED 0.
        TRIS    PORTA           ;
        CLRF    PORTA           ; Clear all outputs.
        MOVLW   D0              ; Set the accumulator to the value of D0.
        MOVWF   PORTA           ; Move the accumulator to PORTA, to switch on LED 0.

    ; Using COUNTs to add a delay
        decfsz    COUNT1,1       ; Decrease COUNT1 by 1, and skip the next line if the result is 0.
        goto      LOOP0          ; If COUNT1 is 0, carry on. If not, go to LOOP0.   
        decfsz    COUNT2,1       ; Decrease COUNT2 by 1, and skip the next line if the result is 0.
        goto      LOOP0          ; If COUNT1 is 0, carry on. If not, go to LOOP0.

However, I’m fairly sure I’m screwing up on the timing, could someone give me a hand?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T17:01:43+00:00Added an answer on June 3, 2026 at 5:01 pm

    Assumption: The code at LOOP0 is code you want to execute once per delay rather than as many times as possible during the delay. I also assume that you’re setting COUNT1 and COUNT2 to something – the code you posted declares two “variables” but doesn’t assign them.

    The code you have at the moment will repeatedly execute the code at LOOP0 COUNT1 + COUNT2 times. This is because each loop is separate. This gives you a maximum delay of 510 cycles. As other commenters have said, PIC16s execute roughly one instruction per cycle, so you need to delay 1,000,000 cycles to wait one second at 4MHz.

    If we consider a situation where we want to wait 196392 cycles, we essentially need to implement a 16 bit counter. We do this by decrementing one counter in a loop. Each time that loop exits, we decrement another counter. When both counters are zero the loop returns. Here’s an example:

    COUNT1 EQU 20h
    COUNT2 EQU 21h
    
    LOOP0
        ;do some stuff here
            ...
    
        ;delay loop starts here:
        ;assume COUNT1=0 and COUNT2=0
    Delay_0
        decfsz COUNT1
        goto Delay_0
        decfsz COUNT2   ;COUNT1 = 0 so 0xff cycles have passed
        goto Delay_0
        goto LOOP0 ;both COUNT1 and COUNT2 = 0 - 196392 cycles have now passed
    

    Branch instructions cost 1 cycle if they don’t skip, and 2 if they do. goto always takes 2 cycles, meaning the actual time taken to do one full count is 767 cycles (255 * 3 + 2). We can calculate the time taken for both as ((255 * 3 + 2) + 3) * 255 + 2.

    There’s an excellent explanation of delay routines over at Dos4Ever. This looks at how delay routines work and how to calculate the counter values and cost of a delay routine.

    Finally, if you just want cut-and-paste delay routines, the Delay routine generator on PIClist is pretty much perfect.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In IE 7 and 8, whenever I want edit or add a row to
EDIT 2 Okay, based on the advice on the answers below I eliminated my
I want to start a little pet project that will allow a user to
I have a mapping from xml schema(number) to copybook, copybook field is a number
I have a strange problem that the plot overlaps in the graph, but not
edit I do not want to redirect pages, specific files etc. I would like
I want to add whatever is written in a textbox to a menustrip. In
We have an edit form:we populate it with a variable number of checkboxes (it
I want to make application that when press the button it will make each
EDIT: SOLVED I JUST HAD TO WAIT FOR AWHILE So I followed the simple

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.