Showing posts with label pic. Show all posts
Showing posts with label pic. Show all posts

Sunday, July 7, 2013

Create Delay using Number of Cycles in PIC MCU

Most of the delay code available on the Internet for PIC is based on real time delay.  The problem with this approach is that it's hardcoded for certain clock frequency only.  When we use the code for different frequency clock, we have to change the code, sometimes drastically.

Another approach is just to make a routine to waste number of instruction cycles, regardless of the clock frequency.  The caller then later calculate how many cycles it needs to waste in order to get the wanted delay.  It's more maintainable this way than the former.  All PIC MCUs have each instruction executed in 4 clock frequency, or F_cy = F_clock/4, hence T_cy = 1/F_cy = 4/F_clock.

Here is the example of the code for PIC16F877A to waste 10 T_cy cycles.  If F_clock is 16 MHz, F_Cy = 4MHz and T_Cy = 0.25 uSec.  So 10*T_cy = 2.5 uSec delay.
I don't see any reason it won't be compiled and working on other pic16-based MCUs.  I compiled the code with gpasm and header file from sdcc.
 


    title "Delay 10 Tcy"

    include <p16f_common.inc>

    list n=0

    radix    dec
    global    _delay10tcy
    extern   _d1
 
; -----------------------------------------------------------------------
; Variables declaration
;DLY_VAR UDATA_SHR  0x190
;WREG         res 1
WREG        equ     _d1



code_delay10tcy code

_delay10tcy:
    ; polynomial for 10tcy delay is f(x) = 10 + 10 * (x-1)

    ; caller takes 2 TCy, return takes 2 Tcy, so we need 6 more Tcy here
      banksel   WREG            ; 2 Tcy
      decf      WREG,f          ; (x-1), 1 TCy.  TCy so far = 4 + 2 + 1 = 7

      movfw     WREG            ; 1, TCy so far = 8
      bz        @delay10_end    ; 2 Tcy if x=0, otherwise 1, TCy so far = 9 (if x>0)
      nop                       ; 1 TCy, TCy so far = 10

@delay10_loop:                  ; (x-1) * 10
      goto       $+1            ; 2 TCy, TCy so far = 2
      goto       $+1            ; 2 TCy, TCy so far = 4
      goto       $+1            ; 2 TCy, TCy so far = 6

      nop                       ; TCy so far 7
      decfsz    WREG, f         ; TCy so far 8 (if x>0), else 9
      goto       @delay10_loop  ; TCy so far 10
      nop                     

@delay10_end:

      return                    ; 2

      end



Save the code to a file name delay10tcy.S.

To compile it on Linux:

gpasm -c -M -m --mpasm-compatible -e ON -DSDCC -Dpic16f877a -p16f877a -I/usr/local/share/sdcc/include/pic14  delay10tcy.S

The variable "_d1" above has been defined elsewhere as 8-bit user data (so it's shareable among other delayxtcl routines), but if we want to make it local just declare is such as:

DELAY_VAR  udata_shr 0x190
d1  res 1

and remove underscore in all references to d1.

Monday, May 27, 2013

MSP430 or PIC?

After sometime using LaunchPad, I was ready to buid my own board utilizing some msp430g2211 on breakout boards laying around on my workbench.  I was going to use the JTAG pins available to program them, but bumped with the overpriced USB-based JTAG programmers/debuggers available in the market.  Yes, I could use the LaunchPad to program these separate microcontrollers, but it's not a good and decent way.

I got a  msp430 BSL usb-to-serial programmer from eBay.  After reading the datasheet for msp430g2211, I realized these type of low-cost microcontrollers don't support BSL, either.  So the only way (other than wiring LauchPad to my protoboard) to in-circuit program the chips is to use a JTAG programmer.  That's a lame, as with PIC microcontrollers, I still could used my PicKit2 to program 44-pin 18Fxx chips.  Even AVR from Atmel could do the similar way using cheap programmer available on eBay or even BusPirate.

Well, sorry msp430, for now I am back to PIC as it has so many features I need (wide availability of PDIP footprints, wide I/O voltage (2 - 5 volt) and cheap programmer).  I am aware there are some open-source efforts to build this kind of programmer for msp430 (GoodFet is one of them, the pre-assembled board is sold at https://www.adafruit.com/products/1279#Technical%20Details)