How does this function for PIC delay 1us?

Thread Starter

spinnaker

Joined Oct 29, 2009
7,830
This code is part of the 1 wire protocol library found on the microchip website:

Rich (BB code):
#elif (CLK_FREQ_12000000)
        #define wait(a) _asm movlw a\
                      movwf macro_delay,1\
              loop:      decfsz macro_delay,1,1\
                      bra loop\
                     _endasm
                     
        #define BAUD_RATE    18
                    
#endif
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:

thatoneguy

Joined Feb 19, 2009
6,359
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.
 

Thread Starter

spinnaker

Joined Oct 29, 2009
7,830
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.

Is my assessment of the function correct?

Here is what they have for 20mhz

Rich (BB code):
#elif (CLK_FREQ_20000000)
        #define wait(a) _asm movlw a\
                      movwf macro_delay,1\
              loop:      nop\
                      nop\
                      decfsz macro_delay,1,1\
                      bra loop\
                     _endasm

        #define BAUD_RATE    31


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

thatoneguy

Joined Feb 19, 2009
6,359
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.
 

Thread Starter

spinnaker

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

Kind of cool.
 

thatoneguy

Joined Feb 19, 2009
6,359
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
 

Thread Starter

spinnaker

Joined Oct 29, 2009
7,830
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
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.
 

thatoneguy

Joined Feb 19, 2009
6,359
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.
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)
 
Top