good Tutorial /code examples / text for dspic/pic24 assembly programming

Author
picinvader
Starting Member
  • Total Posts : 31
  • Reward points : 0
  • Joined: 2007/05/15 22:38:17
  • Location: 0
  • Status: offline
2007/10/21 16:29:19 (permalink)
0

good Tutorial /code examples / text for dspic/pic24 assembly programming

Hi all,
I've been coding for the dspic30F micros for a while now. But most of my coding is in C. I was wondering what kind of resources (other than microchip's datasheets and the dspic30/33 programmers reference manual) are there for programming these micro's in assembly. i couldn't find any starter tutorials / texts  that deal specifically with assembly programming so i figured i'd ask here. I'm primarily looking for either a tutorial or valid code that is a bit more advanced than a barebones "toggle the LED" type but not crazy complicated that it would drive the readers insane. I'm specifically interested in code snippets that deal with the DSP engine on the dspic30/33's but code snippets dealing with other aspects are also welcome.
Thanx for all your help in advance!

picinvader
#1

6 Replies Related Threads

    daver_intrepid
    Starting Member
    • Total Posts : 40
    • Reward points : 0
    • Joined: 2007/03/15 10:15:24
    • Location: 0
    • Status: offline
    RE: good Tutorial /code examples / text for dspic/pic24 assembly programming 2007/10/22 06:08:27 (permalink)
    0
    Here is a code snippet that switches the Oscillator at run time.


    ;// This assembly file switches the oscillator of the dspic during run time
     
        .text
        .include "p33FJ256GP710.inc"


        .global _OscSwitchNRed
    _OscSwitchNRed:

        ;// 3 words on the locals
        lnk        #6

        ;// load the local variables on the stack
        mov.b    #0x78,W0
        mov.b    w0,[w14]
        mov.b    #0x9A,W0
        mov.b    w0,[w14+1]
        mov.b    #0x03,W0
        mov.b    w0,[w14+2]
       
        ;// write the sequence
        mov        w14,W0
        mov        #OSCCONH,w1
        mov.b    [W0++],[w1]
        mov.b    [W0++],[w1]
        mov.b    [W0++],[w1]



        ;// load the local variables on the stack
        mov.b    #0x46,W0
        mov.b    w0,[w14]
        mov.b    #0x57,W0
        mov.b    w0,[w14+1]
        mov.b    #0x01,W0
        mov.b    w0,[w14+2]
       
        ;// write the sequence
        mov        w14,W0
        mov        #OSCCONL,w1
        mov.b    [W0++],[w1]
        mov.b    [W0++],[w1]
        mov.b    [W0++],[w1]





        ulnk






        return
        .end
    #2
    picinvader
    Starting Member
    • Total Posts : 31
    • Reward points : 0
    • Joined: 2007/05/15 22:38:17
    • Location: 0
    • Status: offline
    RE: good Tutorial /code examples / text for dspic/pic24 assembly programming 2007/10/22 09:16:25 (permalink)
    0
    Thanx!, This piece of code also demonstrates the use of the frame pointer using ulnk lnk instructions and w14 to create space for temporary variables.....One of the dspic assembly mysteries uncovered.....If anyone else wants to share their dspic/pic24  assembly code   please feel free to do so..and again thanx in advance!
    #3
    pic30
    Super Member
    • Total Posts : 1221
    • Reward points : 0
    • Joined: 2005/06/24 10:38:21
    • Location: Moscow, Russia
    • Status: offline
    RE: good Tutorial /code examples / text for dspic/pic24 assembly programming 2007/10/22 09:31:53 (permalink)
    0
    Here are some of my tested and working practical routines:
     

    ;------------------------------------------------------------------------------
    ; Converts a 5-byte ASCII numerical string [w4] to an integer w0
    ; Treats all 5 ASCII characters as valid numbers '0..9' - no (isnumber) check
    ; Trashes w1, w2, w5, w6, w8, ACCA
    ; 34 clocks including RETURN, 17 program words, 5 constant words (part of atol reused)
    atoi:
            push    CORCON                  ; Save Core Control register
            bset    CORCON,#US              ; Force unsigned MAC mode
            push    w4                      ; Save ASCII pointer
     
            mov     #psvoffset(atoltab),w8  ; Initialize coeff pointer to a RAM mapped table in code space
            mov     #15,w2                  ; w2 = 0x0F = bit mask for a single char atoi
            clr     A,[w8]+=2,w6            ; Clear ACCA, pre-fetch a first coeff
     
            bra     atoi_e                  ; Enter a lower part of atol
     
    ;------------------------------------------------------------------------------
    ; Converts a 10-byte ASCII numerical string [w4] to a long integer w1w0
    ; Treats all 10 ASCII characters as valid numbers '0..9' - no (isnumber) check
    ; Trashes w2, w5, w6, w8, ACCA
    ; 54 clocks including RETURN, 25 program words, 5 constant words
    atol:
            push    CORCON                  ; Save Core Control register
            bset    CORCON,#US              ; Force unsigned MAC mode
            push    w4                      ; Save ASCII pointer
     
            mov     #psvoffset(atoltab),w8  ; Initialize coeff pointer to a RAM mapped table in code space
            mov     #15,w2                  ; w2 = 0x0F = bit mask for a single char atoi
            clr     w5                      ; w5h must remain zeroed all the time
            clr     A,[w8]+=2,w6            ; Clear ACCA, pre-fetch a first coeff
     
            do      #4,1f                   ; Repeat the following 5 times to calculate MSW/100,000:
            and.b   w2,[w4++],w5            ; - read next ASCII char and convert it to a binary word 0..9 in w5
    1:      mac     w5*w6,A,[w8]+=2,w6      ; - MAC, advance table ptr and pre-fetch next coeff
     
            mov     #psvoffset(atoltab),w8  ; Reinitialize coeff pointer to a RAM mapped table in code space
            mov     ACCAL,w5                ; MSW/100,000 calculated, now multiply it by 100,000
            mpy     w5*w6,A,[w8]+=2,w6      ; ACCA = ACCA*50,000, pre-fetch a first coeff
            sftac   A,#-1                   ; ACCA = ACCA*2*50,000 = ACCA*100,000
     
    atoi_e:                                 ; External entry point for atoi
            clr     w5                      ; w5h must remain zeroed all the time
     
            do      #4,1f                   ; Repeat the following 5 times to calculate LSW+(MSW*100,100):
            and.b   w2,[w4++],w5            ; - read next ASCII char and convert it to a binary word 0..9 in w5
    1:      mac     w5*w6,A,[w8]+=2,w6      ; - MAC, advance table ptr and pre-fetch next coeff
     
            mov     ACCAL,w0                ; Read result to w1w0
            mov     ACCAH,w1                ;
     
            pop     w4                      ; Restore ASCII pointer
            pop     CORCON                  ; Restore Core Control register
            return
     
    atoltab:
            .word   10000,1000,100,10,1,50000
    ;------------------------------------------------------------------------------
     
    ;------------------------------------------------------------------
    ; Divides a 16-bit number N in W0 by 3 (result in W1) using a multiplication by a scaled
    ; reciprocal of 3 ("magic number"). Doesn't calculate a remainder
    ; Registers trashed: W0
    ; 5 clocks including RETURN (2 clocks if inline), 3(2) program words
    div3:
            mov     #0x5556,w1      ; w0 = X
            mul.uu  w0,w1,w0        ; w1 = X/3
     
            return
    ;------------------------------------------------------------------
     
    ;------------------------------------------------------------------------------
    ; Divides a 16-bit number N in W0 (result in W1) by 10 using a multiplication by a scaled
    ; reciprocal of 10 ("magic number"). Doesn't calculate a remainder
    ; Registers trashed: W0
    ; 6 clocks including RETURN (3 clocks inline), 4(3) program words
    div10:
            mov     #65536*8/10+1,w1        ; 52429 = 0xCCCD
            mul.uu  w0,w1,w0                ; w1=MSW(w0*65536*8/10)
            lsr     w1,#3,w1                ; w1=w1/8=N/10
     
            return
    ;------------------------------------------------------------------------------
     
    ;------------------------------------------------------------------
    ; Mirrors a byte W0 to W1
    ; Registers trashed: NONE
    ; 16 clocks including RETURN, 14 program words
    mirror:
            mov.b   w0,w1
     
            and.b   #0x55,w0
            sl      w0,w0
     
            and.b   #0xAA,w1
            lsr     w1,w1
            ior.b   w0,w1,w0
     
            mov.b   w0,w1
            and.b   #0x33,w0
            sl      w0,#2,w0
      
           and.b   #0xCC,w1
            lsr     w1,#2,w1
            ior.b   w0,w1,w0
      
           swap.b  w0
      
           return
    ;------------------------------------------------------------------------------
     
    ;------------------------------------------------------------------
    ; Mirrors a byte w0 to w1 using rotates and a DO loop
    ; Registers trashed: NONE
    ; 17 clocks including RETURN, 9 program words
    mirror2:
            mov.b   w0,w1
     
            do      #3,0f
            rlc.b   w0,w0
    0:      rrc.b   w1,w1
     
            rlc.b   w0,w0
     
            sl      w0,#4,w0
            and     w1,w0,w1
     
            return
    ;------------------------------------------------------------------------------
     
    ;------------------------------------------------------------------------------
    ; Unsigned 16 bit by 32 bit multiplication
    ; w0*w1w2 = w456, all Big Endian
    ; 9 clocks including return, 7 program words
    ; Registers trashed: NONE
    mul16x32:
            push    w7
     
            mul.uu  w0,w1,w4
            mul.uu  w0,w2,w6
            add     w5,w6,w5
            addc    w7,#0,w6
     
            pop     w7
     
            return
    ;------------------------------------------------------------------------------
    ;------------------------------------------------------------------------------
    ; Unsigned 32 bit by 32 bit multiplication
    ; w01*w23 = w4567, all Big Endian
    ; 18 clocks including return, 14 program words
    ; Registers trashed: NONE
    mul32:
            push.d  w8
     
            mul.uu  w0,w2,w4
            mul.uu  w1,w2,w6
            add     w5,w6,w5
            addc    w7,#0,w6
     
            mul.uu  w0,w3,w8
            add     w5,w8,w5
            addc    w6,w9,w6
     
            mul.uu  w1,w3,w8
            addc    w9,#0,w9
            add     w6,w8,w6
            addc    w9,#0,w7
     
            pop.d   w8
     
            return
    ;------------------------------------------------------------------------------


    No dark sarcasm in the classroom (c) Pink Floyd
    #4
    picinvader
    Starting Member
    • Total Posts : 31
    • Reward points : 0
    • Joined: 2007/05/15 22:38:17
    • Location: 0
    • Status: offline
    RE: good Tutorial /code examples / text for dspic/pic24 assembly programming 2007/10/22 13:45:25 (permalink)
    0
    Thanx! Incase others are interested, i managed to find some dspic30F assembly code with detailed explanations here:
    http://jwnall.googlepages.com/30ftutorialOne.htm




    #5
    pic30
    Super Member
    • Total Posts : 1221
    • Reward points : 0
    • Joined: 2005/06/24 10:38:21
    • Location: Moscow, Russia
    • Status: offline
    RE: good Tutorial /code examples / text for dspic/pic24 assembly programming 2007/10/24 01:47:54 (permalink)
    0
    i managed to find some dspic30F assembly code with detailed explanations here:
    I wouldn't recommend that "tutorial" to anybody. It is written with lots of worst hobbyist's programming practices and with an ineradicable PIC16xx in mind. PIC24/dsPIC architecture is so reach - why one should blindly transfer a dumb PIC16xx technique to it?

    No dark sarcasm in the classroom (c) Pink Floyd
    #6
    picinvader
    Starting Member
    • Total Posts : 31
    • Reward points : 0
    • Joined: 2007/05/15 22:38:17
    • Location: 0
    • Status: offline
    RE: good Tutorial /code examples / text for dspic/pic24 assembly programming 2007/10/26 11:08:40 (permalink)
    0
     Thanx for that comment. it makes sense...the code is a little dumb.....however, for the PIC16 and PIC18 families there's lots of assembly langauge resources and example code on the web not to mention many good textbooks addressing the subject. Unfortunately this is not the case for the dsPIC/PIC24 family.  This was one of the few websites that had any form (dumb or smart) of dspic/pic24 assembly code on it...It makes sense though since most people will want to use C for most of their programming especially for 16-bit + chips. Personally I've been finding out that the best way to learn assembly for the dspic/pic24 is from the C30 compiler ( by looking at the disassembly listing of C cprograms)...kinda a scary when you think about it ...

    For the mean time i thank all for their input and continue to welcome any "efficient" dspic/pic24 assembly  code snippets / resources.

      

       
    #7
    Jump to:
    © 2017 APG vNext Commercial Version 4.5