How does this function for PIC delay 1us?

Discussion in 'Embedded Systems and Microcontrollers' started by spinnaker, Jan 22, 2011.

  1. spinnaker

    Thread Starter AAC Fanatic!

    Oct 29, 2009
    4,866
    990
    This code is part of the 1 wire protocol library found on the microchip website:

    Code ( (Unknown Language)):
    1.  
    2.  
    3. #elif (CLK_FREQ_12000000)
    4.         #define wait(a) _asm movlw a\
    5.                       movwf macro_delay,1\
    6.               loop:      decfsz macro_delay,1,1\
    7.                       bra loop\
    8.                      _endasm
    9.                      
    10.         #define BAUD_RATE    18
    11.                    
    12. #endif
    13.  
    14.  
    If a 1 is passed to this function. It is supposed to delay 1us. I thought each instruction took 4 clock cycles? So each instruction for a 12MHZ clock would be .3333us. But I am counting 5 instructions.

    But loops actually take 8 clock cycles correct? Then I would see where the loop code would take 1 us with a 12mhz clock

    So this function would not be accurate for smaller delays like one or two us but increase in accuracy and the delay is increased?

    Is my assessment correct?
     
    Last edited: Jan 22, 2011
  2. thatoneguy

    AAC Fanatic!

    Feb 19, 2009
    6,357
    718
    What is their def for a 20Mhz Clock?

    Some protocols won't be picky about 333 nanoseconds, as long as the signal still arrives in the "Valid" timing window.
     
  3. spinnaker

    Thread Starter AAC Fanatic!

    Oct 29, 2009
    4,866
    990

    Is my assessment of the function correct?

    Here is what they have for 20mhz

    Code ( (Unknown Language)):
    1.  
    2. #elif (CLK_FREQ_20000000)
    3.         #define wait(a) _asm movlw a\
    4.                       movwf macro_delay,1\
    5.               loop:      nop\
    6.                       nop\
    7.                       decfsz macro_delay,1,1\
    8.                       bra loop\
    9.                      _endasm
    10.  
    11.         #define BAUD_RATE    31
    12.  


    Yes one wire gives you are range of delays. So they are not hugely picky.
     
  4. thatoneguy

    AAC Fanatic!

    Feb 19, 2009
    6,357
    718
    It would seem your interpretation is correct. I haven't looked at the libs for 1 wire, it just works. :)

    The only change I see with the higher clock is the baud rate, which makes sense. It's a fairly simple protocol, compared to other options.
     
  5. spinnaker

    Thread Starter AAC Fanatic!

    Oct 29, 2009
    4,866
    990
    I bought a few DS1820s that I have been messing with. So far got them to report it's serial number.

    Kind of cool.
     
  6. thatoneguy

    AAC Fanatic!

    Feb 19, 2009
    6,357
    718
    Odd that, I just got some in the mail yesterday as well!

    I'm making the Pic dev board that can be expanded with a breadboard, and am just adding some basic stuff to the PCB where there's room.

    The Temp on the LCD will be a "Hello, I'm working" signal, so if something else doesn't work, it means PEBKAC. :D
     
  7. spinnaker

    Thread Starter AAC Fanatic!

    Oct 29, 2009
    4,866
    990
    Great minds must think alike. I also just built a bread / breakout board. It started out as a simple board to test LCDs and sort of turned into a mini Frankenstein, adding LEDs. switches, headers etc.

    I bought the sensor as an idea for a project I got months ago. The temperature in our office seemed to vary during the day. I thought a cool project would be to build a temperature logger. I got microchip to send me a free pic with RTC and USB. I plan to have a log file on a thumb drive. Probably a CSV file. Then it can be graphed with Excel or whatever. Maybe if I get really fancy I will get a graphic LCD and do a graph right on the LCD.
     
  8. thatoneguy

    AAC Fanatic!

    Feb 19, 2009
    6,357
    718
    That'd be cool, but the code to "zoom" on an area for more detailed data on GLCD is a bit of a pain. Well, a lot of a pain. Showing a rough graph is easy enough though, interaction takes a bit more.

    Right now, board is 20 pins long, supplies power to the + and - rails on breadboard, and brings out all of the I/O from an 18F2550, which leaves 40 full rows (w/gutter) and 1 half row (no ICs) to play with on a standard protoboard.

    Original was a 16F628A for the base, but I run out of I/O too quick if the built in functions are used, not to mention the LCD width taking up room.

    It's a "halfway point" between a PIC sitting on perfboard and something like the PICAxe. Programmed native through ICSP still, but not limited to what can be slapped on around it. Both 5v and 3.3v are available as well. Current status is "Ugly". Once I get some things straightened out, I'll post a layout to make your own PCB. (uses SMD though)
     
Loading...