Announcement

Collapse
No announcement yet.

a crc command

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • a crc command

    I use this code to calculate crc's for various one wire things ds18b20 etc i would like to make a user command out of it
    its limitation is in having all variables in bank 0 ,to avoid mucking about with bank switching, i assuming using these macros will make this a lot easier and more reliable
    my question is what is the best way to go about it
    should the crc var be a system var and the other just temps located anywhere ?
    can you pass more than one var to a user command ?
    can you pass optional var/s to a user command ?

    code snippet:

    GOTO BEGIN


    asm
    _CRCA
    MOVLW 8
    MOVWF _BC
    YY MOVF _D,W
    MOVWF _TMPS
    MOVF _CRC,W
    XORWF _TMPS,F
    RRF _TMPS,F
    BTFSS STATUS,C
    GOTO XX
    MOVLW 0X18
    XORWF _CRC,F
    XX RRF _CRC,F
    RRF _D,F
    DECFSZ _BC,F
    GOTO YY
    RETURN
    ENDASM



    BEGIN:


    CRC VAR BYTE bank0 'CRC value system?
    D VAR BYTE bank0 'input DATA to add to crc
    BC VAR BYTE bank0 'temp var BIT COUNT
    TMPS VAR BYTE bank0 'temp storage of byte to be crc'ed



    main:
    crc=0 'set crc to 0
    some code
    d= "z" ' a byte to crc
    call crca
    do some more code
    d= "x" more data to crc
    call crca
    send crc to something

    goto main '

  • #2
    crc command

    i was thinking something like this but its still going to need bank switching





    GOTO over_BEGIN

    USERCOMMAND "CRCc" ; Create a command called INCR


    TempD VAR BYTE
    TempBC VAR BYTE
    TempTMPS var byte
    ;----[Main Code - Called by the macros]-------------------------------------


    crca:

    asm
    _CRCa
    MOVLW 8
    MOVWF _TempBC
    YY MOVF _TempD,W
    MOVWF _TempTMPS
    MOVF _CRC,W
    XORWF _TempTMPS,F
    RRF _TempTMPS,F
    BTFSS STATUS,C
    GOTO XX
    MOVLW 0X18
    XORWF _CRC,F
    XX RRF _CRC,F
    RRF _TempD,F
    DECFSZ _TempBC,F
    GOTO YY
    RETURN
    ENDASM










    ASM
    ;----[crc a Byte]-----------------------------------------------------
    CRCC?B macro Bin
    MOVE?BB Bin, _TempD ; copy user var to TempVar
    L?CALL _crca ; run the crc subroutine

    endm

    ;----[Word]-----------------------------------------------------
    CRCC?W macro Win
    ERROR You cannot CRC a WORD
    endm

    ;----[Long]-----------------------------------------------------
    CRCC?N macro Nin
    ERROR You cannot CRC a LONG
    endm

    ;----[Constant]-------------------------------------------------------------
    CRCC?C macro Cin
    ERROR You cannot CRC a constant
    endm

    ;----[String]---------------------------------------------------------------
    CRCC?S macro Cin
    ERROR You cannot CRC a String
    endm

    ;----[Label]----------------------------------------------------------------
    CRCC?L macro Cin
    ERROR You cannot CRC a Label
    endm

    ;----[Bit]------------------------------------------------------------------
    CRCC?T macro Regin, Bitin
    ERROR CRCing a BIT is not supported
    endm

    ENDASM

    over_BEGIN:

    Comment


    • #3
      Improvements

      THIS SEEMS TO WORK
      BUT IT WOULD BE NICE TO CRC a WORD,STRING,OR AN ARRAY
      PLUS OPTIONALLY RESET CRC TO 0
      AND TO USE ANY BANK FOR THE TEMPORY VARS

      ;CODE STARTS HERE

      GOTO over_BEGIN

      USERCOMMAND "CRCC" ; Create a command called CRCC
      ' CRC VAR BYTE SYSTEM 'CRC THIS VAR NEEDS TO BE CREATED IN YOUR CODE

      TempD VAR BYTE bank0
      TempBC VAR BYTE bank0
      TempTMPS var byte bank0
      ;----[Main Code - Called by the macros]-------------------------------------
      asm
      _CRCa
      MOVLW 8
      MOVWF _TempBC
      YY MOVF _TempD,W
      MOVWF _TempTMPS
      MOVF CRC,W
      XORWF _TempTMPS,F
      RRF _TempTMPS,F
      BTFSS STATUS,C
      GOTO XX
      MOVLW 0X18
      XORWF CRC,F
      XX RRF CRC,F
      RRF _TempD,F
      DECFSZ _TempBC,F
      GOTO YY
      RETURN
      ;----[crc a Byte]-----------------------------------------------------
      CRCC?B macro Bin
      MOVE?BB Bin, _TempD ; copy user var to TempVar
      L?CALL _CRCa ; run the crc subroutine

      endm

      ;----[Word]-----------------------------------------------------
      CRCC?W macro Win
      ERROR You cannot CRC a WORD
      endm

      ;----[Long]-----------------------------------------------------
      CRCC?N macro Nin
      ERROR You cannot CRC a LONG
      endm

      ;----[Constant]-------------------------------------------------------------
      CRCC?C macro Cin
      ERROR You cannot CRC a constant
      endm

      ;----[String]---------------------------------------------------------------
      CRCC?S macro Cin
      ERROR You cannot CRC a String
      endm

      ;----[Label]----------------------------------------------------------------
      CRCC?L macro Cin
      ERROR You cannot CRC a Label
      endm

      ;----[Bit]------------------------------------------------------------------
      CRCC?T macro Regin, Bitin
      ERROR CRCing a BIT is not supported
      endm

      ENDASM

      over_BEGIN:

      Comment


      • #4
        That's a good start Richard.

        >> BUT IT WOULD BE NICE TO CRC a WORD,STRING,OR AN ARRAY
        A WORD is simply 2 BYTEs, so you can call the routine twice.

        Code:
            MOVE?BB Win, _TempD     ; copy lowbyte to TempVar
            L?CALL _CRCa            ; run the crc subroutine
            MOVE?BB Win + 1, _TempD ; copy highbyte to TempVar
            L?CALL _CRCa            ; run the crc subroutine
        A LONG has 4 bytes and would need 4 calls.

        STRINGs are fixed at compile time and would always give the same result, so CRCing a string wouldn't be very useful.

        For an Array, you would need to use a loop to cycle through each byte, sending them one at a time to the CRC routine.

        ------------
        As for the BANKing, you have to manage that yourself when writing in assembly language.
        If you write the routine in BASIC, then PBP will handle all the banking issues.
        PBP3 Manual : Microchip Datasheets - 10F, 12F, 16F, 18F
        Never download a PIC datasheet from anywhere but microchip.com

        Comment


        • #5
          usercommand options

          thanks for your reply darrel, i have some more quetsions about user commands
          can you pass more than one var to a user command ?
          can you pass optional var/s to a user command ?
          i would need this to pass the array length to my crc command or must i use some other mechanism.

          Comment


          • #6
            Yes, you can pass more than 1 parameter.
            For each additional parameter, another letter is added to the macro name.

            If you used this line in your program ...
            CRCC MyArray, 10

            It would call the CRCC?BC macro.

            I'm not sure what you mean by "Optional Parameters".
            If a macro exists for the number of parameters used, it will call that macro.
            So you can have commands that can accept any number of parameters, it just gets very complicated with too many of them.

            A command with 1 parameter takes 7 macros, 1 for each type.
            A command with 2 parameters takes 7*7=49 macros.
            3 parameters ... 7*7*7=343 macros.
            PBP3 Manual : Microchip Datasheets - 10F, 12F, 16F, 18F
            Never download a PIC datasheet from anywhere but microchip.com

            Comment

            Working...
            X