I need to Tx always three digits


+ Reply to Thread
Results 1 to 10 of 10
  1. #1
    Join Date
    Jun 2016
    Posts
    65

    Default I need to Tx always three digits

    I am extracting a CRC from a string to send out serially. The resulting CRC can be any number from = 0 to 255 but I need to Tx always 3 digits so I came up with the following code. Is there any way to convert the CRC number into 3 digits in a simpler way?

    Code:
    W0 = 0
    For DMY = 5 To 42
    W0 = W0 + Pulse[DMY]
    next DMY
    
    
    DMY = W0.lowbyte
    
    
    if DMY < 10 then
    Pulse[43] = "0"
    Pulse[44] = "0"
    pulse[45] = DMY + 48
    goto skip_CRC
    endif
    
    
    if DMY < 100 then
    Pulse[43] = "0"
    DMD = DMY DIG 0
    Pulse[44] = DMD + 48
    DMD = DMY DIG 1
    pulse[45] = DMD + 48
    goto skip_CRC
    endif
    
    
    if DMY >= 100 then
    DMD = DMY DIG 0
    Pulse[43] = DMD + 48
    DMD = DMY DIG 1
    Pulse[44] = DMD + 48
    DMD = DMY DIG 2
    pulse[45] = DMD + 48
    endif
    
    
    skip_CRC:
    
    
    Hserout [str Pulse\46]
    Thank you for any suggestion.

    Alberto

  2. #2
    Join Date
    Sep 2009
    Posts
    677

    Default Re: I need to Tx always three digits

    This should work for any number from 0 - 255
    Code:
    DMD = DMY DIG 0
    Pulse[43] = DMD + 48
    DMD = DMY DIG 1
    Pulse[44] = DMD + 48
    DMD = DMY DIG 2
    pulse[45] = DMD + 48

  3. #3
    Join Date
    Jun 2016
    Posts
    65

    Default Re: I need to Tx always three digits

    Thank you pedja089 for the tip. Definetly much better if it works! I will test it and let you know.

    I will also test the following short code (if it works)

    Hserout [str Pulse\43,dec3 DMY]

    Alberto

  4. #4
    Join Date
    Jun 2016
    Posts
    65

    Default Re: I need to Tx always three digits

    OK I have verified the two alternative proposed. Neither of them work both send out the true number of digits, so for number from 0 to 9 I get only one digit with number from 10 to 99 two digits and from 100 to 255 three digits.
    Since it is mandatory I send always three digits my original code is the only solution so far. (By the way: the dig sequence must be reverted)

    Thank you for the attention anyway.

    Alberto

  5. #5
    Join Date
    Apr 2014
    Location
    Northeast
    Posts
    252

    Default Re: I need to Tx always three digits

    Not sure how to integrate this thought into what you're doing, but I found when dealing with decimals for display that I create the variable that holds the actual value, and create an array to hold the ASCii characters:
    Code:
    Val  VAR  BYTE
    ValA VAR BYTE[3]
    In the Display subroutine, I check the value of Val:
    Code:
    ARRAYWRITE ValA, [#Val]
    IF Val < 100 THEN
     ValA[2] = Val[1]
     Val[1] = Val[0]
     Val[0] = " "
    ENDIF
    IF Val < 10 THEN
     ValA[2] = Val[1]
     Val[1] = " "
     Val[0] = " "
    ENDIF
    HSEROUT ValA ;(or whatever method, Pulse, DEBUG, etc)
    I start with the <100 first and <10 second.

  6. #6
    Join Date
    May 2013
    Location
    australia
    Posts
    1,508

    Default Re: I need to Tx always three digits

    Since it is mandatory I send always three digits my original code is the only solution so far. (By the way: the dig sequence must be reverted)
    this makes no sense to me , that's exactly what pedja's code will do and ditto for Hserout [str Pulse\43,dec3 DMY]

    do you mean that 3 characters must be sent but leading zeros suppressed ?
    This is more entertaining than Free to Air TV

  7. #7
    Join Date
    Jun 2016
    Posts
    65

    Default Re: I need to Tx always three digits


    do you mean that 3 characters must be sent but leading zeros suppressed ?
    No! I do need to send also the leading zeros! Both shortcut suggested, do suppress leading zeros, so not usable.

    Alberto

  8. #8
    Join Date
    May 2013
    Location
    australia
    Posts
    1,508

    Default Re: I need to Tx always three digits

    No! I do need to send also the leading zeros! Both shortcut suggested, do suppress leading zeros, so not usable.
    both those methods send leading zeros pedja's is little end first so the order need to be reversed

    the alternative is to let the complier do the work for you with a little bit of pointer use like this

    I made the buffer and the insertion point a little smaller to suit the limited chip currently in my ep7 boad
    and my own var names in some cases

    Code:
    ;16f648a
    #CONFIG
    cfg = _INTOSC_OSC_NOCLKOUT
    cfg&= _WDT_ON
    cfg&= _PWRTE_OFF
    cfg&= _MCLRE_ON
    cfg&= _BODEN_ON
    cfg&= _LVP_OFF
    cfg&= _DATA_CP_OFF
    cfg&= _CP_OFF
      __CONFIG cfg
    #ENDCONFIG
     Buff1 var byte[27]   ; 23 payload + 3 crc + null
     crc   var byte ext   ; pointer to buff1 pos 23
     @crc = _Buff1+23     ; pointer to buff1 pos 23  [the insertion point]
     led var porta.0
     
     j var byte
     
     dmd  var byte
     dmy  var byte
     CMCON=7
     trisb=%11111011
     trisa=%11111110
     DEFINE HSER_BAUD 9600 
     DEFINE HSER_RCSTA 0D0h 'Set receive register
     DEFINE HSER_TXSTA 64h 'Set transmit register use brgh
     DEFINE HSER_CLROERR 1 'Clear overrun error upon execution of every 
     DEFINE HSER_BITS 9 'Use 8 bits + parity 
     DEFINE HSER_EVEN 1 'Use only if even parity desired
     DEFINE HSER_SPBRG 25  ' 9600 Baud @ 4MHz, 0.16%
        
    ;load 23 chr payload into buffer
    arraywrite buff1,["jjjjjjaaaaaaaaaaaaaaaax"]
    crc[3]=0 ;null terminate string 
     
    ;calc the crc
    DMY = 0
    For j = 0 To 22
    DMY = DMY + buff1[j]
    next 
    ;easy way uses a little bit more space
    HSEROUT [13,10,"easy way",13,10]
    arraywrite crc,[dec3 dmy]
    HSEROUT [str Buff1]
     
     ;slighty less code space  
    HSEROUT [13,10,"other way",13,10]
    DMD = DMY DIG 0
    crc[2] = DMD + 48
    DMD = DMY DIG 1
    crc[1] = DMD + 48
    DMD = DMY DIG 2
    crc[0] = DMD + 48 
    HSEROUT [str Buff1]
    This is more entertaining than Free to Air TV

  9. #9
    Join Date
    May 2013
    Location
    australia
    Posts
    1,508

    Default Re: I need to Tx always three digits

    nearly forgot


    ;calc the crc
    DMY = 0
    For j = 0 To 22
    DMY = DMY + buff1[j]
    next
    no point in using a word var for the calc then throwing away the high byte , complete waste ot time / resources /code space
    Attached Images Attached Images
    Last edited by richard; - 25th November 2018 at 07:43.
    This is more entertaining than Free to Air TV

  10. #10
    Join Date
    Jun 2016
    Posts
    65

    Default Re: I need to Tx always three digits

    Ok I did re-tested the suggested approch and discover that today they work! I should remember to avoid to do any testing late at night.

    So I did delate the the first two blocks of my code and I keep the last block without the IF condition. So now the code is as follow (much simpler) and works fine.

    Code:
    DMD = DMY DIG 2
    Pulse[43] = DMD + 48
    DMD = DMY DIG 1
    Pulse[44] = DMD + 48
    DMD = DMY DIG 0
    pulse[45] = DMD + 48
    
    
    Hserout [str Pulse\46]
    Thank you to all of you for the attention.

    Alberto

Similar Threads

  1. Manipulating Digits
    By ruijc in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 27th February 2011, 16:18
  2. How to display many informations on 6 digits?
    By Mihai Popa in forum mel PIC BASIC Pro
    Replies: 10
    Last Post: - 8th June 2010, 13:41
  3. Digits on an LCD
    By malc-c in forum mel PIC BASIC Pro
    Replies: 8
    Last Post: - 20th April 2010, 20:44
  4. Split decimal digits
    By emavil in forum mel PIC BASIC Pro
    Replies: 10
    Last Post: - 10th January 2007, 01:20
  5. Decimals and Digits and Division Oh My!
    By Spindle in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 23rd June 2005, 08:16

Members who have read this thread : 22

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts