piclist 2001\01\10\170404a >
Thread: yet another algorithm challange
www.piclist.com/techref/microchip/devices.htm?key=pic
face
flavicon
face BY : Scott Dattalo email (remove spam text)



On Wed, 10 Jan 2001, Bob Ammerman wrote:

{Quote hidden}

Another approach that is rather short, but quite slow and complex yet versatile
is to maintain three counters:

  cBitExp   - bit expansion counter
  cBitSnd   - bit in byte to be sent
  cBitCnt   - bit in byte that we are expanding

The idea is that the bytes to be sent are built up a bit-at-a-time.
Here's some sample code off of the top of my head:

init:

   movlw 8          ;8bits/byte
   movwf cBitSnd
   movlw 1
   movwf cBitCnt

   movf  bit_expansion_factor  ;e.g. 2,3, or 6 whatever
   movwf cBitExp

   call  get_next_byte_to_send
   movwf next_byte   ; save a copy here

   return

expand:

   decfsz cBitSnd        ;If we've built up 8 bits
    goto  l1

   movf   next_byte,w    ; then send them out
   call   send_a_byte
   bsf    cBitCnt,3      ;set count to 8 (#bits/byte)

l1:
   clrc                  ;Get the next bit that we
   rlf    byte_to_send   ;want to send by copying the
   btfsc  next_byte,7    ;msb of the source byte into
    bsf   byte_to_send,0 ;the lsb of the destination
                         ;Note that we could bit-bang
                         ;an I/O port right here instead.

   decfsz cBitExp        ;If the expansion counter has
    goto  l2             ;terminated then
                         ;reload it and shift the source
                         ;byte one position

   movf   bit_expansion_factor
   movwf  cBitExp

   rlf    next_byte,f

l2:
   decfsz cBitCnt        ;If we've gone through all 8-bits
    goto  l3             ;of the source
                         ;then get the next byte.
   call   get_next_byte_to_send
   movwf  next_byte      ; save a copy here

   bsf    cBitCnt,3      ;8-bits per byte

l3:
   return                ; or make this a loop

This routine assumes a UART, but could easily be adapted to bit-banging (since
the bytes to send are built a bit-at-a-time). Also, this code works for any
sized expansion - you could even make it one-to-one (or one-to-256).

Scott

--
http://www.piclist.com#nomail Going offline? Don't AutoReply us!
email KILLspamlistservEraseMEspamEraseMEmitvma.mit.edu with SET PICList DIGEST in the body


<Pine.LNX.4.21.0101101331050.22644-100000@tempest2.blackhat.net>

In reply to: <004d01c07b3a$a61b4ae0$dce8c540@sciencekit.com>
See also: www.piclist.com/techref/microchip/devices.htm?key=pic
Reply You must be a member of the piclist mailing list (not only a www.piclist.com member) to post to the piclist. This form requires JavaScript and a browser/email client that can handle form mailto: posts.
Subject (change) yet another algorithm challange

month overview.

new search...