Generating Sine Wave

Thread Starter

MCU88

Joined Mar 12, 2015
358
I believe that is achievable. I bought my EICO 390 signal generator in 1977.
I was born in 1977. 38-years-old, but I feel 80 after what I have been through...

I just cannot seem to get into 2nd gear anymore... I do take an cocktail of drugs though for schizophrenia and depression.
 

OBW0549

Joined Mar 2, 2015
3,566
Got any pointers on doing this in the C language?
Nope, I'm only starting (at age 65!) to learn C, and I'm not that far enough along in the book. Haven't got there yet.

Surely, though, there must be an easy way to do that; it can't be all that different from defining character strings in program memory, or any other table of constant data. I imagine your compiler user's manual tells how, somewhere.
 

Thread Starter

MCU88

Joined Mar 12, 2015
358
Nope, I'm only starting (at age 65!) to learn C, and I'm not that far enough along in the book. Haven't got there yet.

Surely, though, there must be an easy way to do that; it can't be all that different from defining character strings in program memory, or any other table of constant data. I imagine your compiler user's manual tells how, somewhere.
I know enough about C to get most straightforward tasks accomplished. I probably know half of it. Lots of flight hours too. I used to program in BASIC, but I would never go back after using C. But, either way it eventually ends up in RAM. So I hardcode an table in program memory and load (n) contents into RAM at startup. This is I assume what is being suggested?
 

OBW0549

Joined Mar 2, 2015
3,566
Why does it have to go into RAM at all, ever? I've always kept the table in program memory, and indexed through it right there.
 

jpanhalt

Joined Jan 18, 2008
11,087
It may be that C is using program memory too. In program memory, you offset the program counter to the location in the table you want (i.e., calculate), and it returns that value. If you look at the disassembly of an assembly program you will see the table listed as many lines of program memory with some sort of return. If you use the DT directive (I believe that is appropriate for the 628A), the instruction on each line will be retlw.

I have never attempted to do a look-up table using RAM. If I were made to do that, I would use the indirect FSR and increment it. That is, treat the FSR analogously to the PC.

If you can get a disassembly listing of a C program in which you used a table, you should be able to tell whether program memory or general purpose RAM was used.

John
 

Thread Starter

MCU88

Joined Mar 12, 2015
358
Why does it have to go into RAM at all, ever? I've always kept the table in program memory, and indexed through it right there.
Arh! I get it....

But your working variables are in RAM, and the data goes from program memory to RAM then to port right?
 

OBW0549

Joined Mar 2, 2015
3,566
Here's an example of the code for a sine wave generator I made (before I could afford to buy a proper audio generator). It ran on a dsPIC30F3012, and drove an LTC8043 12-bit DAC. It's in ASM30, so have fun:

Code:
;******************************************************************************
;
;   Filename:       dsPICDDS.s
;   Date:           Monday, January 02, 2006
;
;   NOTES:          48-bit DDS Prototype
; 
;
;******************************************************************************

                .equ        __30F3012, 1
                .include    "p30f3012.inc"

;..............................................................................
;Configuration bits:
;..............................................................................

                config      __FOSC, CSW_FSCM_OFF & HS2_PLL16  
                config      __FWDT, WDT_OFF
                config      __FBORPOR, PBOR_ON & BORV_27 & PWRT_16 & MCLR_EN
                config      __FGS, CODE_PROT_OFF
  
;..............................................................................
;Program Specific Constants (literals used in code)
;..............................................................................

         ;       .equ        Abcde, 1234        ; ....
       
;..............................................................................
;Global Declarations:
;..............................................................................

                .global     __reset
                .global     __T2Interrupt

;..............................................................................
;Uninitialized variables in Near data memory (Lower 8Kb of RAM)
;..............................................................................

                .bss
PhaseIncr:      .space  6                       ; 48-bit phase increment register
Dummy:          .space  2                       ; temporary for watch window, to make PhaseAccum 64 bits
PhaseAccum:     .space  6                       ; 48-bit phase accumulator
DACValue:       .space  2                       ; DAC load value

;..............................................................................
;Code Section in Program Memory
;..............................................................................

                .text                           ; Start of Code section
__reset:
                mov     #__SP_init, W15         ; Initalize the Stack Pointer
                mov     #__SPLIM_init, W0       ; Initialize the Stack Pointer Limit Register
                mov     W0, SPLIM
                nop                             ; Add NOP to follow SPLIM initialization
                rcall   Initialize              ; Set up ports, peripherals and modes, and clear regs
              
                ;
                ; load frequency for debug into PhaseIncr
                ;
                ;       60 Hz = 000D 1B71 758E
                ;      100 Hz = 0015 D867 C3EC
                ;     1000 Hz = 00DA 740D A740
                ;
              
       ;         mov     #0x00DA, w0
       ;         mov     w0, PhaseIncr+4
       ;         mov     #0x740D, w0
       ;         mov     w0, PhaseIncr+2
       ;         mov     #0xA740, w0
       ;         mov     w0, PhaseIncr

                mov     #0x2000, w0             ; 37.5 KHz
                mov     w0, PhaseIncr+4
                mov     #0x0200, w0
                mov     w0, PhaseIncr+2
                mov     #0x0000, w0
                mov     w0, PhaseIncr
              
                clr     PhaseAccum
                clr     PhaseAccum+2
                clr     PhaseAccum+4
              
MainLoop:
                bset    LATC, #14               ; select tens digit
                mov     #363, w0                ; wait 100 us
0:
                dec     w0, w0
                bra     nz, 0b
                mov     PORTB, w1               ; read digit
                and     #0x000F, w1             ; mask off everything but digit
                mov     #10, w0
                mul.uu  w0, w1, w2              ; multiply digit by 10 (result is in w2)
                bclr    LATC, #14               ; unselect tens digit
                bset    LATC, #13               ; select ones digit
                mov     #363, w0                ; wait 100 us
0:
                dec     w0, w0
                bra     nz, 0b
                mov     PORTB, w1               ; read digit
                bclr    LATC, #13               ; unselect ones digit
                and     #0x000F, w1             ; mask off everything but digit
                add     w1, w2, w2              ; finished number is in w2
                mov     #6, w0
                mul.uu  w0, w2, w2              ; multiply number by 6 (result is in w2)

                mov     #psvoffset(FreqTab), w1 ; get address of frequency table into w1
                add     w1, w2, w2              ; add offset to table base address
                mov     [w2++], w0              ; get MS word of table entry into w0
                mov     w0, PhaseIncr+4         ; put in phase increment register
                mov     [w2++], w0              ; get middle word of table entry into w0
                mov     w0, PhaseIncr+2         ; put in phase increment register
                mov     [w2], w0                ; get LS word of table entry into w0
                mov     w0, PhaseIncr           ; put in phase increment register


                mov     #36300, w0                ; wait 10 ms
0:
                dec     w0, w0
                bra     nz, 0b
                goto    MainLoop

Initialize:
                ;
                ; Set up port latches and port direction registers
                ;
              
                clr     LATB                    ; make all pins outputs except RB0-RB3, set to all zeroes
                clr     LATC
                clr     LATD
                mov     #0x0F, w0
                mov     w0, TRISB               ; set RB0-RB3 as inputs
                mov     w0, ADPCFG              ; and make them digital
                clr     TRISC
                clr     TRISD

                bset    CORCON, #PSV            ; enable PSV operation in CORCON
                nop
                nop
                mov     #psvpage(SineTab), w0   ; get upper address of sine table in program memory
                mov     w0, PSVPAG              ; load upper address into PSVPAG
              
                ;
                ; Set up DSP core
                ;

                bset    CORCON, #SATA           ; Enable accumulator A saturation
                bset    CORCON, #SATB           ; Enable accumulator B saturation
                bset    CORCON, #ACCSAT         ; Enable supersaturation
                bclr    CORCON, #IF             ; Enable fractional mode for DSP multiplies
              
                ;
                ; Clear all working registers
                ;
      
                clr     w0
                mov     w0, w14
                repeat  #12
                mov     w0, [++w14]
                clr     w14
              
                ;
                ; Set up SPI
                ;
              
                mov     #0x0000, w0             ; SPI disabled, set to run in IDLE mode
                mov     w0, SPI1STAT
                mov     #0x053E, w0             ; FRMEN=SPIFSD=DISSDO=SMP=SSEN=CKP=0, MODE16=CKE=MSTEN=1, SPRE=111, PPRE=10
                mov     w0, SPI1CON
                nop
                bset    SPI1STAT, #SPIEN        ; turn on SPI
              
                ;
                ; Set up Timer 2 and OC2 to generate DAC load pulse on pin 15
                ;
              
                clr     OC2CON                  ; turn off OC2 module
                bclr    IEC0, #T2IE             ; disable Timer 2 interrupts
                mov     #0x004D, w0             ; set duty cycle to 78 clock periods
                mov     w0, OC2RS               ; write to duty cycle buffer register
                mov     w0, OC2R                ; and value register

                mov     #0x0006, w0             ; set OC2 mode to PWM w/o fault detection
                mov     w0, OC2CON
                mov     #0x004F, w0             ; initialize PR2 for 80 clock periods (300kHz @ 96MHz Fosc)
                mov     w0, PR2
                bset    IPC0, #T2IP0            ; setup Timer 2 interrupt for
                bclr    IPC0, #T2IP1            ; level 1 priority
                bclr    IPC0, #T2IP2
                bclr    IFS0, #T2IF             ; clear Timer 2 interrupt flag
                bset    IEC0, #T2IE             ; enable Timer 2 interrupts
                bset    T2CON, #TON             ; start Timer 2

                return

__T2Interrupt:
                bset    LATD, #0                ; set pin 10 for timing measurement
                bclr    IFS0, #T2IF             ; Reset Timer 2 interrupt flag
                push.s                          ; save w0-w3
              
                mov     DACValue, w0            ; get DAC value from last session and
                mov     w0, SPI1BUF             ; put in SPI transmit buffer for sending to LTC8043
              
                mov     PhaseAccum, w0          ; add 48-bit phase increment to phase accumulator
                mov     PhaseIncr, w1
                add     w1, w0, w0
                mov     w0, PhaseAccum
                mov     PhaseAccum+2, w0
                mov     PhaseIncr+2, w1
                addc    w1, w0, w0
                mov     w0, PhaseAccum+2
                mov     PhaseAccum+4, w0
                mov     PhaseIncr+4, w1
                addc    w1, w0, w0
                mov     w0, PhaseAccum+4
              
                mov     w0, w1                  ; copy to w1
                mov     w1, w3                  ; save a copy in w3
                lsr     w1, #5, w1              ; shift right with zero fill so that sine table offset is in w1<10:1>
                bclr    w1, #0                  ; mask off LSB because each table entry is two bytes
                sl      w3, #10, w3             ; shift offset left 10 bits to get interpolation fraction

                mov     #psvoffset(SineTab), w2 ; get lower address of sine table into w2
              
                add     w1, w2, w2              ; add offset to table base address to get entry address in w2
                mov     [w2++], w0              ; get table entry into w0, advance w2 to next entry
                mov     [w2], w1                ; get next table entry into w1
                sub     w1, w0, w1              ; subtract this entry from next entry to get delta into w1
                mul.us  w3, w1, w2              ; multiply fraction (unsigned) by delta (signed), MS product word in w3
                add     w3, w0, w0              ; add interpolation result to table entry, sum in w0
                lsr     w0, #4, w0              ; shift right 4 bits for the 12-bit DAC
                mov     w0, DACValue            ; and save for sending to DAC on next cycle
              
                pop.s                           ; restore w0-w3
                bclr    LATD, #0                ; clear pin 10
                RETFIE                          ; Return from ISR

SineTab:
                .palign 2
                .word   32767, 32969, 33170, 33371, 33572, 33773, 33974, 34175, 34375, 34576, 34777, 34978, 35178, 35379, 35579, 35779
                .word   35979, 36179, 36379, 36579, 36779, 36978, 37177, 37377, 37576, 37774, 37973, 38171, 38370, 38568, 38765, 38963
                .word   39160, 39357, 39554, 39751, 39947, 40143, 40339, 40534, 40729, 40924, 41119, 41313, 41507, 41701, 41894, 42087
                .word   42280, 42472, 42664, 42855, 43046, 43237, 43427, 43617, 43807, 43996, 44184, 44373, 44561, 44748, 44935, 45121
                .word   45307, 45493, 45678, 45862, 46046, 46230, 46413, 46596, 46778, 46959, 47140, 47320, 47500, 47680, 47858, 48037
                .word   48214, 48391, 48568, 48744, 48919, 49093, 49267, 49441, 49614, 49786, 49957, 50128, 50298, 50468, 50637, 50805
                .word   50972, 51139, 51305, 51471, 51636, 51800, 51963, 52126, 52287, 52448, 52609, 52769, 52927, 53086, 53243, 53399
                .word   53555, 53710, 53865, 54018, 54171, 54323, 54474, 54624, 54773, 54922, 55069, 55216, 55362, 55508, 55652, 55795
                .word   55938, 56080, 56221, 56361, 56500, 56638, 56775, 56912, 57047, 57182, 57315, 57448, 57580, 57711, 57841, 57969
                .word   58098, 58225, 58351, 58476, 58600, 58723, 58845, 58967, 59087, 59206, 59325, 59442, 59558, 59673, 59788, 59901
                .word   60013, 60124, 60234, 60344, 60452, 60559, 60665, 60770, 60874, 60976, 61078, 61179, 61279, 61377, 61475, 61571
                .word   61666, 61761, 61854, 61946, 62037, 62127, 62215, 62303, 62389, 62475, 62559, 62642, 62724, 62805, 62885, 62964
                .word   63041, 63118, 63193, 63267, 63340, 63412, 63482, 63552, 63620, 63687, 63753, 63818, 63882, 63944, 64005, 64066
                .word   64125, 64182, 64239, 64294, 64349, 64402, 64453, 64504, 64553, 64602, 64649, 64695, 64739, 64783, 64825, 64866
                .word   64906, 64944, 64982, 65018, 65053, 65087, 65119, 65151, 65181, 65210, 65237, 65264, 65289, 65313, 65336, 65357
                .word   65378, 65397, 65415, 65431, 65447, 65461, 65474, 65486, 65496, 65505, 65513, 65520, 65526, 65530, 65533, 65535
                .word   65535, 65535, 65533, 65530, 65526, 65520, 65513, 65505, 65496, 65486, 65474, 65461, 65447, 65431, 65415, 65397
                .word   65378, 65357, 65336, 65313, 65289, 65264, 65237, 65210, 65181, 65151, 65119, 65087, 65053, 65018, 64982, 64944
                .word   64906, 64866, 64825, 64783, 64739, 64695, 64649, 64602, 64553, 64504, 64453, 64402, 64349, 64294, 64239, 64182
                .word   64125, 64066, 64005, 63944, 63882, 63818, 63753, 63687, 63620, 63552, 63482, 63412, 63340, 63267, 63193, 63118
                .word   63041, 62964, 62885, 62805, 62724, 62642, 62559, 62475, 62389, 62303, 62215, 62127, 62037, 61946, 61854, 61761
                .word   61666, 61571, 61475, 61377, 61279, 61179, 61078, 60976, 60874, 60770, 60665, 60559, 60452, 60344, 60234, 60124
                .word   60013, 59901, 59788, 59673, 59558, 59442, 59325, 59206, 59087, 58967, 58845, 58723, 58600, 58476, 58351, 58225
                .word   58098, 57969, 57841, 57711, 57580, 57448, 57315, 57182, 57047, 56912, 56775, 56638, 56500, 56361, 56221, 56080
                .word   55938, 55795, 55652, 55508, 55362, 55216, 55069, 54922, 54773, 54624, 54474, 54323, 54171, 54018, 53865, 53710
                .word   53555, 53399, 53243, 53086, 52927, 52769, 52609, 52448, 52287, 52126, 51963, 51800, 51636, 51471, 51305, 51139
                .word   50972, 50805, 50637, 50468, 50298, 50128, 49957, 49786, 49614, 49441, 49267, 49093, 48919, 48744, 48568, 48391
                .word   48214, 48037, 47858, 47680, 47500, 47320, 47140, 46959, 46778, 46596, 46413, 46230, 46046, 45862, 45678, 45493
                .word   45307, 45121, 44935, 44748, 44561, 44373, 44184, 43996, 43807, 43617, 43427, 43237, 43046, 42855, 42664, 42472
                .word   42280, 42087, 41894, 41701, 41507, 41313, 41119, 40924, 40729, 40534, 40339, 40143, 39947, 39751, 39554, 39357
                .word   39160, 38963, 38765, 38568, 38370, 38171, 37973, 37774, 37576, 37377, 37177, 36978, 36779, 36579, 36379, 36179
                .word   35979, 35779, 35579, 35379, 35178, 34978, 34777, 34576, 34375, 34175, 33974, 33773, 33572, 33371, 33170, 32969
                .word   32767, 32566, 32365, 32164, 31963, 31762, 31561, 31360, 31160, 30959, 30758, 30557, 30357, 30156, 29956, 29756
                .word   29556, 29356, 29156, 28956, 28756, 28557, 28358, 28158, 27959, 27761, 27562, 27364, 27165, 26967, 26770, 26572
                .word   26375, 26178, 25981, 25784, 25588, 25392, 25196, 25001, 24806, 24611, 24416, 24222, 24028, 23834, 23641, 23448
                .word   23255, 23063, 22871, 22680, 22489, 22298, 22108, 21918, 21728, 21539, 21351, 21162, 20974, 20787, 20600, 20414
                .word   20228, 20042, 19857, 19673, 19489, 19305, 19122, 18939, 18757, 18576, 18395, 18215, 18035, 17855, 17677, 17498
                .word   17321, 17144, 16967, 16791, 16616, 16442, 16268, 16094, 15921, 15749, 15578, 15407, 15237, 15067, 14898, 14730
                .word   14563, 14396, 14230, 14064, 13899, 13735, 13572, 13409, 13248, 13086, 12926, 12766, 12608, 12449, 12292, 12136
                .word   11980, 11825, 11670, 11517, 11364, 11212, 11061, 10911, 10762, 10613, 10466, 10319, 10173, 10027,  9883,  9740
                .word    9597,  9455,  9314,  9174,  9035,  8897,  8760,  8623,  8488,  8353,  8220,  8087,  7955,  7824,  7694,  7566
                .word    7437,  7310,  7184,  7059,  6935,  6812,  6690,  6568,  6448,  6329,  6210,  6093,  5977,  5862,  5747,  5634
                .word    5522,  5411,  5301,  5191,  5083,  4976,  4870,  4765,  4661,  4559,  4457,  4356,  4256,  4158,  4060,  3964
                .word    3869,  3774,  3681,  3589,  3498,  3408,  3320,  3232,  3146,  3060,  2976,  2893,  2811,  2730,  2650,  2571
                .word    2494,  2417,  2342,  2268,  2195,  2123,  2053,  1983,  1915,  1848,  1782,  1717,  1653,  1591,  1530,  1469
                .word    1410,  1353,  1296,  1241,  1186,  1133,  1082,  1031,   982,   933,   886,   840,   796,   752,   710,   669
                .word     629,   591,   553,   517,   482,   448,   416,   384,   354,   325,   298,   271,   246,   222,   199,   178
                .word     157,   138,   120,   104,    88,    74,    61,    49,    39,    30,    22,    15,     9,     5,     2,     0
                .word       0,     0,     2,     5,     9,    15,    22,    30,    39,    49,    61,    74,    88,   104,   120,   138
                .word     157,   178,   199,   222,   246,   271,   298,   325,   354,   384,   416,   448,   482,   517,   553,   591
                .word     629,   669,   710,   752,   796,   840,   886,   933,   982,  1031,  1082,  1133,  1186,  1241,  1296,  1353
                .word    1410,  1469,  1530,  1591,  1653,  1717,  1782,  1848,  1915,  1983,  2053,  2123,  2195,  2268,  2342,  2417
                .word    2494,  2571,  2650,  2730,  2811,  2893,  2976,  3060,  3146,  3232,  3320,  3408,  3498,  3589,  3681,  3774
                .word    3869,  3964,  4060,  4158,  4256,  4356,  4457,  4559,  4661,  4765,  4870,  4976,  5083,  5191,  5301,  5411
                .word    5522,  5634,  5747,  5862,  5977,  6093,  6210,  6329,  6448,  6568,  6690,  6812,  6935,  7059,  7184,  7310
                .word    7437,  7566,  7694,  7824,  7955,  8087,  8220,  8353,  8488,  8623,  8760,  8897,  9035,  9174,  9314,  9455
                .word    9597,  9740,  9883, 10027, 10173, 10319, 10466, 10613, 10762, 10911, 11061, 11212, 11364, 11517, 11670, 11825
                .word   11980, 12136, 12292, 12449, 12608, 12766, 12926, 13086, 13248, 13409, 13572, 13735, 13899, 14064, 14230, 14396
                .word   14563, 14730, 14898, 15067, 15237, 15407, 15578, 15749, 15921, 16094, 16268, 16442, 16616, 16791, 16967, 17144
                .word   17321, 17498, 17677, 17855, 18035, 18215, 18395, 18576, 18757, 18939, 19122, 19305, 19489, 19673, 19857, 20042
                .word   20228, 20414, 20600, 20787, 20974, 21162, 21351, 21539, 21728, 21918, 22108, 22298, 22489, 22680, 22871, 23063
                .word   23255, 23448, 23641, 23834, 24028, 24222, 24416, 24611, 24806, 25001, 25196, 25392, 25588, 25784, 25981, 26178
                .word   26375, 26572, 26770, 26967, 27165, 27364, 27562, 27761, 27959, 28158, 28358, 28557, 28756, 28956, 29156, 29356
                .word   29556, 29756, 29956, 30156, 30357, 30557, 30758, 30959, 31160, 31360, 31561, 31762, 31963, 32164, 32365, 32566

                .word   32767                   ; duplicates first word in table, to take care of wraparound

FreqTab:
                .palign 2
                .word   0, 1431, 42976
                .word   0, 1802, 22794
                .word   0, 2269, 1407
                .word   0, 2856, 34655
                .word   0, 3596, 10268
                .word   0, 4527, 19204
                .word   0, 5699, 34357
                .word   0, 7175, 18083
                .word   0, 9033, 8991
                .word   0, 11372, 3012
                .word   0, 14316, 36546
                .word   0, 18023, 31341
                .word   0, 22690, 14074
                .word   0, 28565, 18872
                .word   0, 35961, 37153
                .word   0, 45272, 60977
                .word   0, 56995, 15898
                .word   1, 6216, 49758
                .word   1, 24795, 24378
                .word   1, 48184, 30125
                .word   2, 12093, 37783
                .word   2, 49162, 51274
                .word   3, 30294, 9675
                .word   4, 23508, 57649
                .word   5, 31935, 43853
                .word   6, 59513, 19947
                .word   8, 45664, 27914
                .word   10, 62167, 38832
                .word   13, 51345, 47179
                .word   17, 23092, 39112
                .word   21, 55399, 50156
                .word   27, 32875, 53996
                .word   34, 40797, 31222
                .word   43, 38480, 52204
                .word   54, 57212, 45320
                .word   69, 5309, 2868
                .word   86, 63428, 17002
                .word   109, 31851, 60643
                .word   137, 54705, 13044
                .word   173, 34317, 63445
                .word   218, 29709, 42816
                .word   275, 1078, 15676
                .word   346, 14758, 50085
                .word   435, 57127, 63293
                .word   548, 47838, 59987
                .word   690, 53090, 28681
                .word   869, 44458, 38951
                .word   1094, 56375, 16606
                .word   1378, 22763, 64909
                .word   1735, 15499, 44630
                .word   2184, 34952, 34952
                .word   2750, 10782, 25688
                .word   3462, 16515, 42103
                .word   4358, 46991, 43106
                .word   5487, 19637, 10048
                .word   6908, 6616, 24673
                .word   9, 43342, 61740
                .word   10, 60467, 57846
                .word   13, 7025, 30094
                .word   13, 27757, 17454
                .word   0, 14316, 36546
                .word   0, 20246, 43909
                .word   0, 28633, 7556
                .word   0, 40493, 22282
                .word   0, 57266, 15113
                .word   1, 15450, 44564
                .word   1, 48996, 30226
                .word   2, 30901, 23592
                .word   3, 32456, 60453
                .word   4, 61802, 47185
                .word   6, 64913, 55371
                .word   9, 58069, 28835
                .word   13, 64291, 45207
                .word   19, 50602, 57671
                .word   27, 63047, 24879
                .word   39, 35669, 49806
                .word   55, 60558, 49758
                .word   79, 5803, 34076
                .word   111, 55581, 33980
                .word   158, 11607, 2616
                .word   223, 45627, 2425
                .word   316, 23214, 5232
                .word   447, 25718, 4851
                .word   632, 46428, 10464
                .word   894, 51436, 9702
                .word   1265, 27320, 20928
                .word   1789, 37336, 19405
                .word   2530, 54640, 41857
                .word   3579, 9136, 38811
                .word   5061, 43745, 18178
                .word   7158, 18273, 12086
                .word   57, 10025, 32060
                .word   64, 9964, 38052
                .word   72, 539, 60468
                .word   76, 19010, 7175
                .word   85, 41465, 16978
                .word   96, 7829, 24082
                .word   107, 58356, 49574
                .word   114, 20050, 64121
                .word   2097, 9961, 30932


                .end                            ; End of program code in this file
 

Thread Starter

MCU88

Joined Mar 12, 2015
358
Here's an example of the code for a sine wave generator I made (before I could afford to buy a proper audio generator). It ran on a dsPIC30F3012, and drove an LTC8043 12-bit DAC. It's in ASM30, so have fun:

Code:
;******************************************************************************
;
;   Filename:       dsPICDDS.s
;   Date:           Monday, January 02, 2006
;
;   NOTES:          48-bit DDS Prototype
;
;
;******************************************************************************

                .equ        __30F3012, 1
                .include    "p30f3012.inc"

;..............................................................................
;Configuration bits:
;..............................................................................

                config      __FOSC, CSW_FSCM_OFF & HS2_PLL16 
                config      __FWDT, WDT_OFF
                config      __FBORPOR, PBOR_ON & BORV_27 & PWRT_16 & MCLR_EN
                config      __FGS, CODE_PROT_OFF
 
;..............................................................................
;Program Specific Constants (literals used in code)
;..............................................................................

         ;       .equ        Abcde, 1234        ; ....
      
;..............................................................................
;Global Declarations:
;..............................................................................

                .global     __reset
                .global     __T2Interrupt

;..............................................................................
;Uninitialized variables in Near data memory (Lower 8Kb of RAM)
;..............................................................................

                .bss
PhaseIncr:      .space  6                       ; 48-bit phase increment register
Dummy:          .space  2                       ; temporary for watch window, to make PhaseAccum 64 bits
PhaseAccum:     .space  6                       ; 48-bit phase accumulator
DACValue:       .space  2                       ; DAC load value

;..............................................................................
;Code Section in Program Memory
;..............................................................................

                .text                           ; Start of Code section
__reset:
                mov     #__SP_init, W15         ; Initalize the Stack Pointer
                mov     #__SPLIM_init, W0       ; Initialize the Stack Pointer Limit Register
                mov     W0, SPLIM
                nop                             ; Add NOP to follow SPLIM initialization
                rcall   Initialize              ; Set up ports, peripherals and modes, and clear regs
             
                ;
                ; load frequency for debug into PhaseIncr
                ;
                ;       60 Hz = 000D 1B71 758E
                ;      100 Hz = 0015 D867 C3EC
                ;     1000 Hz = 00DA 740D A740
                ;
             
       ;         mov     #0x00DA, w0
       ;         mov     w0, PhaseIncr+4
       ;         mov     #0x740D, w0
       ;         mov     w0, PhaseIncr+2
       ;         mov     #0xA740, w0
       ;         mov     w0, PhaseIncr

                mov     #0x2000, w0             ; 37.5 KHz
                mov     w0, PhaseIncr+4
                mov     #0x0200, w0
                mov     w0, PhaseIncr+2
                mov     #0x0000, w0
                mov     w0, PhaseIncr
             
                clr     PhaseAccum
                clr     PhaseAccum+2
                clr     PhaseAccum+4
             
MainLoop:
                bset    LATC, #14               ; select tens digit
                mov     #363, w0                ; wait 100 us
0:
                dec     w0, w0
                bra     nz, 0b
                mov     PORTB, w1               ; read digit
                and     #0x000F, w1             ; mask off everything but digit
                mov     #10, w0
                mul.uu  w0, w1, w2              ; multiply digit by 10 (result is in w2)
                bclr    LATC, #14               ; unselect tens digit
                bset    LATC, #13               ; select ones digit
                mov     #363, w0                ; wait 100 us
0:
                dec     w0, w0
                bra     nz, 0b
                mov     PORTB, w1               ; read digit
                bclr    LATC, #13               ; unselect ones digit
                and     #0x000F, w1             ; mask off everything but digit
                add     w1, w2, w2              ; finished number is in w2
                mov     #6, w0
                mul.uu  w0, w2, w2              ; multiply number by 6 (result is in w2)

                mov     #psvoffset(FreqTab), w1 ; get address of frequency table into w1
                add     w1, w2, w2              ; add offset to table base address
                mov     [w2++], w0              ; get MS word of table entry into w0
                mov     w0, PhaseIncr+4         ; put in phase increment register
                mov     [w2++], w0              ; get middle word of table entry into w0
                mov     w0, PhaseIncr+2         ; put in phase increment register
                mov     [w2], w0                ; get LS word of table entry into w0
                mov     w0, PhaseIncr           ; put in phase increment register


                mov     #36300, w0                ; wait 10 ms
0:
                dec     w0, w0
                bra     nz, 0b
                goto    MainLoop

Initialize:
                ;
                ; Set up port latches and port direction registers
                ;
             
                clr     LATB                    ; make all pins outputs except RB0-RB3, set to all zeroes
                clr     LATC
                clr     LATD
                mov     #0x0F, w0
                mov     w0, TRISB               ; set RB0-RB3 as inputs
                mov     w0, ADPCFG              ; and make them digital
                clr     TRISC
                clr     TRISD

                bset    CORCON, #PSV            ; enable PSV operation in CORCON
                nop
                nop
                mov     #psvpage(SineTab), w0   ; get upper address of sine table in program memory
                mov     w0, PSVPAG              ; load upper address into PSVPAG
             
                ;
                ; Set up DSP core
                ;

                bset    CORCON, #SATA           ; Enable accumulator A saturation
                bset    CORCON, #SATB           ; Enable accumulator B saturation
                bset    CORCON, #ACCSAT         ; Enable supersaturation
                bclr    CORCON, #IF             ; Enable fractional mode for DSP multiplies
             
                ;
                ; Clear all working registers
                ;
     
                clr     w0
                mov     w0, w14
                repeat  #12
                mov     w0, [++w14]
                clr     w14
             
                ;
                ; Set up SPI
                ;
             
                mov     #0x0000, w0             ; SPI disabled, set to run in IDLE mode
                mov     w0, SPI1STAT
                mov     #0x053E, w0             ; FRMEN=SPIFSD=DISSDO=SMP=SSEN=CKP=0, MODE16=CKE=MSTEN=1, SPRE=111, PPRE=10
                mov     w0, SPI1CON
                nop
                bset    SPI1STAT, #SPIEN        ; turn on SPI
             
                ;
                ; Set up Timer 2 and OC2 to generate DAC load pulse on pin 15
                ;
             
                clr     OC2CON                  ; turn off OC2 module
                bclr    IEC0, #T2IE             ; disable Timer 2 interrupts
                mov     #0x004D, w0             ; set duty cycle to 78 clock periods
                mov     w0, OC2RS               ; write to duty cycle buffer register
                mov     w0, OC2R                ; and value register

                mov     #0x0006, w0             ; set OC2 mode to PWM w/o fault detection
                mov     w0, OC2CON
                mov     #0x004F, w0             ; initialize PR2 for 80 clock periods (300kHz @ 96MHz Fosc)
                mov     w0, PR2
                bset    IPC0, #T2IP0            ; setup Timer 2 interrupt for
                bclr    IPC0, #T2IP1            ; level 1 priority
                bclr    IPC0, #T2IP2
                bclr    IFS0, #T2IF             ; clear Timer 2 interrupt flag
                bset    IEC0, #T2IE             ; enable Timer 2 interrupts
                bset    T2CON, #TON             ; start Timer 2

                return

__T2Interrupt:
                bset    LATD, #0                ; set pin 10 for timing measurement
                bclr    IFS0, #T2IF             ; Reset Timer 2 interrupt flag
                push.s                          ; save w0-w3
             
                mov     DACValue, w0            ; get DAC value from last session and
                mov     w0, SPI1BUF             ; put in SPI transmit buffer for sending to LTC8043
             
                mov     PhaseAccum, w0          ; add 48-bit phase increment to phase accumulator
                mov     PhaseIncr, w1
                add     w1, w0, w0
                mov     w0, PhaseAccum
                mov     PhaseAccum+2, w0
                mov     PhaseIncr+2, w1
                addc    w1, w0, w0
                mov     w0, PhaseAccum+2
                mov     PhaseAccum+4, w0
                mov     PhaseIncr+4, w1
                addc    w1, w0, w0
                mov     w0, PhaseAccum+4
             
                mov     w0, w1                  ; copy to w1
                mov     w1, w3                  ; save a copy in w3
                lsr     w1, #5, w1              ; shift right with zero fill so that sine table offset is in w1<10:1>
                bclr    w1, #0                  ; mask off LSB because each table entry is two bytes
                sl      w3, #10, w3             ; shift offset left 10 bits to get interpolation fraction

                mov     #psvoffset(SineTab), w2 ; get lower address of sine table into w2
             
                add     w1, w2, w2              ; add offset to table base address to get entry address in w2
                mov     [w2++], w0              ; get table entry into w0, advance w2 to next entry
                mov     [w2], w1                ; get next table entry into w1
                sub     w1, w0, w1              ; subtract this entry from next entry to get delta into w1
                mul.us  w3, w1, w2              ; multiply fraction (unsigned) by delta (signed), MS product word in w3
                add     w3, w0, w0              ; add interpolation result to table entry, sum in w0
                lsr     w0, #4, w0              ; shift right 4 bits for the 12-bit DAC
                mov     w0, DACValue            ; and save for sending to DAC on next cycle
             
                pop.s                           ; restore w0-w3
                bclr    LATD, #0                ; clear pin 10
                RETFIE                          ; Return from ISR

SineTab:
                .palign 2
                .word   32767, 32969, 33170, 33371, 33572, 33773, 33974, 34175, 34375, 34576, 34777, 34978, 35178, 35379, 35579, 35779
                .word   35979, 36179, 36379, 36579, 36779, 36978, 37177, 37377, 37576, 37774, 37973, 38171, 38370, 38568, 38765, 38963
                .word   39160, 39357, 39554, 39751, 39947, 40143, 40339, 40534, 40729, 40924, 41119, 41313, 41507, 41701, 41894, 42087
                .word   42280, 42472, 42664, 42855, 43046, 43237, 43427, 43617, 43807, 43996, 44184, 44373, 44561, 44748, 44935, 45121
                .word   45307, 45493, 45678, 45862, 46046, 46230, 46413, 46596, 46778, 46959, 47140, 47320, 47500, 47680, 47858, 48037
                .word   48214, 48391, 48568, 48744, 48919, 49093, 49267, 49441, 49614, 49786, 49957, 50128, 50298, 50468, 50637, 50805
                .word   50972, 51139, 51305, 51471, 51636, 51800, 51963, 52126, 52287, 52448, 52609, 52769, 52927, 53086, 53243, 53399
                .word   53555, 53710, 53865, 54018, 54171, 54323, 54474, 54624, 54773, 54922, 55069, 55216, 55362, 55508, 55652, 55795
                .word   55938, 56080, 56221, 56361, 56500, 56638, 56775, 56912, 57047, 57182, 57315, 57448, 57580, 57711, 57841, 57969
                .word   58098, 58225, 58351, 58476, 58600, 58723, 58845, 58967, 59087, 59206, 59325, 59442, 59558, 59673, 59788, 59901
                .word   60013, 60124, 60234, 60344, 60452, 60559, 60665, 60770, 60874, 60976, 61078, 61179, 61279, 61377, 61475, 61571
                .word   61666, 61761, 61854, 61946, 62037, 62127, 62215, 62303, 62389, 62475, 62559, 62642, 62724, 62805, 62885, 62964
                .word   63041, 63118, 63193, 63267, 63340, 63412, 63482, 63552, 63620, 63687, 63753, 63818, 63882, 63944, 64005, 64066
                .word   64125, 64182, 64239, 64294, 64349, 64402, 64453, 64504, 64553, 64602, 64649, 64695, 64739, 64783, 64825, 64866
                .word   64906, 64944, 64982, 65018, 65053, 65087, 65119, 65151, 65181, 65210, 65237, 65264, 65289, 65313, 65336, 65357
                .word   65378, 65397, 65415, 65431, 65447, 65461, 65474, 65486, 65496, 65505, 65513, 65520, 65526, 65530, 65533, 65535
                .word   65535, 65535, 65533, 65530, 65526, 65520, 65513, 65505, 65496, 65486, 65474, 65461, 65447, 65431, 65415, 65397
                .word   65378, 65357, 65336, 65313, 65289, 65264, 65237, 65210, 65181, 65151, 65119, 65087, 65053, 65018, 64982, 64944
                .word   64906, 64866, 64825, 64783, 64739, 64695, 64649, 64602, 64553, 64504, 64453, 64402, 64349, 64294, 64239, 64182
                .word   64125, 64066, 64005, 63944, 63882, 63818, 63753, 63687, 63620, 63552, 63482, 63412, 63340, 63267, 63193, 63118
                .word   63041, 62964, 62885, 62805, 62724, 62642, 62559, 62475, 62389, 62303, 62215, 62127, 62037, 61946, 61854, 61761
                .word   61666, 61571, 61475, 61377, 61279, 61179, 61078, 60976, 60874, 60770, 60665, 60559, 60452, 60344, 60234, 60124
                .word   60013, 59901, 59788, 59673, 59558, 59442, 59325, 59206, 59087, 58967, 58845, 58723, 58600, 58476, 58351, 58225
                .word   58098, 57969, 57841, 57711, 57580, 57448, 57315, 57182, 57047, 56912, 56775, 56638, 56500, 56361, 56221, 56080
                .word   55938, 55795, 55652, 55508, 55362, 55216, 55069, 54922, 54773, 54624, 54474, 54323, 54171, 54018, 53865, 53710
                .word   53555, 53399, 53243, 53086, 52927, 52769, 52609, 52448, 52287, 52126, 51963, 51800, 51636, 51471, 51305, 51139
                .word   50972, 50805, 50637, 50468, 50298, 50128, 49957, 49786, 49614, 49441, 49267, 49093, 48919, 48744, 48568, 48391
                .word   48214, 48037, 47858, 47680, 47500, 47320, 47140, 46959, 46778, 46596, 46413, 46230, 46046, 45862, 45678, 45493
                .word   45307, 45121, 44935, 44748, 44561, 44373, 44184, 43996, 43807, 43617, 43427, 43237, 43046, 42855, 42664, 42472
                .word   42280, 42087, 41894, 41701, 41507, 41313, 41119, 40924, 40729, 40534, 40339, 40143, 39947, 39751, 39554, 39357
                .word   39160, 38963, 38765, 38568, 38370, 38171, 37973, 37774, 37576, 37377, 37177, 36978, 36779, 36579, 36379, 36179
                .word   35979, 35779, 35579, 35379, 35178, 34978, 34777, 34576, 34375, 34175, 33974, 33773, 33572, 33371, 33170, 32969
                .word   32767, 32566, 32365, 32164, 31963, 31762, 31561, 31360, 31160, 30959, 30758, 30557, 30357, 30156, 29956, 29756
                .word   29556, 29356, 29156, 28956, 28756, 28557, 28358, 28158, 27959, 27761, 27562, 27364, 27165, 26967, 26770, 26572
                .word   26375, 26178, 25981, 25784, 25588, 25392, 25196, 25001, 24806, 24611, 24416, 24222, 24028, 23834, 23641, 23448
                .word   23255, 23063, 22871, 22680, 22489, 22298, 22108, 21918, 21728, 21539, 21351, 21162, 20974, 20787, 20600, 20414
                .word   20228, 20042, 19857, 19673, 19489, 19305, 19122, 18939, 18757, 18576, 18395, 18215, 18035, 17855, 17677, 17498
                .word   17321, 17144, 16967, 16791, 16616, 16442, 16268, 16094, 15921, 15749, 15578, 15407, 15237, 15067, 14898, 14730
                .word   14563, 14396, 14230, 14064, 13899, 13735, 13572, 13409, 13248, 13086, 12926, 12766, 12608, 12449, 12292, 12136
                .word   11980, 11825, 11670, 11517, 11364, 11212, 11061, 10911, 10762, 10613, 10466, 10319, 10173, 10027,  9883,  9740
                .word    9597,  9455,  9314,  9174,  9035,  8897,  8760,  8623,  8488,  8353,  8220,  8087,  7955,  7824,  7694,  7566
                .word    7437,  7310,  7184,  7059,  6935,  6812,  6690,  6568,  6448,  6329,  6210,  6093,  5977,  5862,  5747,  5634
                .word    5522,  5411,  5301,  5191,  5083,  4976,  4870,  4765,  4661,  4559,  4457,  4356,  4256,  4158,  4060,  3964
                .word    3869,  3774,  3681,  3589,  3498,  3408,  3320,  3232,  3146,  3060,  2976,  2893,  2811,  2730,  2650,  2571
                .word    2494,  2417,  2342,  2268,  2195,  2123,  2053,  1983,  1915,  1848,  1782,  1717,  1653,  1591,  1530,  1469
                .word    1410,  1353,  1296,  1241,  1186,  1133,  1082,  1031,   982,   933,   886,   840,   796,   752,   710,   669
                .word     629,   591,   553,   517,   482,   448,   416,   384,   354,   325,   298,   271,   246,   222,   199,   178
                .word     157,   138,   120,   104,    88,    74,    61,    49,    39,    30,    22,    15,     9,     5,     2,     0
                .word       0,     0,     2,     5,     9,    15,    22,    30,    39,    49,    61,    74,    88,   104,   120,   138
                .word     157,   178,   199,   222,   246,   271,   298,   325,   354,   384,   416,   448,   482,   517,   553,   591
                .word     629,   669,   710,   752,   796,   840,   886,   933,   982,  1031,  1082,  1133,  1186,  1241,  1296,  1353
                .word    1410,  1469,  1530,  1591,  1653,  1717,  1782,  1848,  1915,  1983,  2053,  2123,  2195,  2268,  2342,  2417
                .word    2494,  2571,  2650,  2730,  2811,  2893,  2976,  3060,  3146,  3232,  3320,  3408,  3498,  3589,  3681,  3774
                .word    3869,  3964,  4060,  4158,  4256,  4356,  4457,  4559,  4661,  4765,  4870,  4976,  5083,  5191,  5301,  5411
                .word    5522,  5634,  5747,  5862,  5977,  6093,  6210,  6329,  6448,  6568,  6690,  6812,  6935,  7059,  7184,  7310
                .word    7437,  7566,  7694,  7824,  7955,  8087,  8220,  8353,  8488,  8623,  8760,  8897,  9035,  9174,  9314,  9455
                .word    9597,  9740,  9883, 10027, 10173, 10319, 10466, 10613, 10762, 10911, 11061, 11212, 11364, 11517, 11670, 11825
                .word   11980, 12136, 12292, 12449, 12608, 12766, 12926, 13086, 13248, 13409, 13572, 13735, 13899, 14064, 14230, 14396
                .word   14563, 14730, 14898, 15067, 15237, 15407, 15578, 15749, 15921, 16094, 16268, 16442, 16616, 16791, 16967, 17144
                .word   17321, 17498, 17677, 17855, 18035, 18215, 18395, 18576, 18757, 18939, 19122, 19305, 19489, 19673, 19857, 20042
                .word   20228, 20414, 20600, 20787, 20974, 21162, 21351, 21539, 21728, 21918, 22108, 22298, 22489, 22680, 22871, 23063
                .word   23255, 23448, 23641, 23834, 24028, 24222, 24416, 24611, 24806, 25001, 25196, 25392, 25588, 25784, 25981, 26178
                .word   26375, 26572, 26770, 26967, 27165, 27364, 27562, 27761, 27959, 28158, 28358, 28557, 28756, 28956, 29156, 29356
                .word   29556, 29756, 29956, 30156, 30357, 30557, 30758, 30959, 31160, 31360, 31561, 31762, 31963, 32164, 32365, 32566

                .word   32767                   ; duplicates first word in table, to take care of wraparound

FreqTab:
                .palign 2
                .word   0, 1431, 42976
                .word   0, 1802, 22794
                .word   0, 2269, 1407
                .word   0, 2856, 34655
                .word   0, 3596, 10268
                .word   0, 4527, 19204
                .word   0, 5699, 34357
                .word   0, 7175, 18083
                .word   0, 9033, 8991
                .word   0, 11372, 3012
                .word   0, 14316, 36546
                .word   0, 18023, 31341
                .word   0, 22690, 14074
                .word   0, 28565, 18872
                .word   0, 35961, 37153
                .word   0, 45272, 60977
                .word   0, 56995, 15898
                .word   1, 6216, 49758
                .word   1, 24795, 24378
                .word   1, 48184, 30125
                .word   2, 12093, 37783
                .word   2, 49162, 51274
                .word   3, 30294, 9675
                .word   4, 23508, 57649
                .word   5, 31935, 43853
                .word   6, 59513, 19947
                .word   8, 45664, 27914
                .word   10, 62167, 38832
                .word   13, 51345, 47179
                .word   17, 23092, 39112
                .word   21, 55399, 50156
                .word   27, 32875, 53996
                .word   34, 40797, 31222
                .word   43, 38480, 52204
                .word   54, 57212, 45320
                .word   69, 5309, 2868
                .word   86, 63428, 17002
                .word   109, 31851, 60643
                .word   137, 54705, 13044
                .word   173, 34317, 63445
                .word   218, 29709, 42816
                .word   275, 1078, 15676
                .word   346, 14758, 50085
                .word   435, 57127, 63293
                .word   548, 47838, 59987
                .word   690, 53090, 28681
                .word   869, 44458, 38951
                .word   1094, 56375, 16606
                .word   1378, 22763, 64909
                .word   1735, 15499, 44630
                .word   2184, 34952, 34952
                .word   2750, 10782, 25688
                .word   3462, 16515, 42103
                .word   4358, 46991, 43106
                .word   5487, 19637, 10048
                .word   6908, 6616, 24673
                .word   9, 43342, 61740
                .word   10, 60467, 57846
                .word   13, 7025, 30094
                .word   13, 27757, 17454
                .word   0, 14316, 36546
                .word   0, 20246, 43909
                .word   0, 28633, 7556
                .word   0, 40493, 22282
                .word   0, 57266, 15113
                .word   1, 15450, 44564
                .word   1, 48996, 30226
                .word   2, 30901, 23592
                .word   3, 32456, 60453
                .word   4, 61802, 47185
                .word   6, 64913, 55371
                .word   9, 58069, 28835
                .word   13, 64291, 45207
                .word   19, 50602, 57671
                .word   27, 63047, 24879
                .word   39, 35669, 49806
                .word   55, 60558, 49758
                .word   79, 5803, 34076
                .word   111, 55581, 33980
                .word   158, 11607, 2616
                .word   223, 45627, 2425
                .word   316, 23214, 5232
                .word   447, 25718, 4851
                .word   632, 46428, 10464
                .word   894, 51436, 9702
                .word   1265, 27320, 20928
                .word   1789, 37336, 19405
                .word   2530, 54640, 41857
                .word   3579, 9136, 38811
                .word   5061, 43745, 18178
                .word   7158, 18273, 12086
                .word   57, 10025, 32060
                .word   64, 9964, 38052
                .word   72, 539, 60468
                .word   76, 19010, 7175
                .word   85, 41465, 16978
                .word   96, 7829, 24082
                .word   107, 58356, 49574
                .word   114, 20050, 64121
                .word   2097, 9961, 30932


                .end                            ; End of program code in this file
Yeah real nice mate. You are clearly an hotshot assembly programmer. Wish I could read it and understand it.
 

OBW0549

Joined Mar 2, 2015
3,566
Surely there must be a way. What does your compiler user's guide say? Look up "constants in code space" or "tables in program memory" or something like that. I can't believe they didn't provide an easy way to do this, people have to do this all the time....
 

jpanhalt

Joined Jan 18, 2008
11,087
Back you your C program. Have you done any with a table? If so, can you post the HEX, and I can try to sort it out for you. Chance of working is probably low, but it might help you to know whether you have to do anything differently.

John
 

MikeML

Joined Oct 2, 2009
5,444
Look up the Cordic algorithm. Beautiful Sine/Cosine outputs using only integer math (shifts and adds).

It can be executed once at start-up to fill a table, or avoid using a table by generating the next value as needed on the fly.

I first coded the Cordic on a PDP-8 mini-computer...
 
Last edited:

jpanhalt

Joined Jan 18, 2008
11,087
Arh! I get it....

But your working variables are in RAM, and the data goes from program memory to RAM then to port right?
Not sure I understand completely what you mean. Let's say you have a RAM register labeled "Vt" . At each time increment, you would load that register with the binary value for voltage based on the look-up table. You would then go to an DAC to get the proper voltage. That is over simplified, as the sine wave generators I have seen used PWM, so the return value might be a PWM setting.

In any event, you have only used just one RAM location.

Edit: Corrected ADC to DAC. I give dyslexia a bad name. Thanks, Papabravo.
 
Last edited:

Papabravo

Joined Feb 24, 2006
22,082
Not sure I understand completely what you mean. Let's say you have a RAM register labeled "Vt" . At each time increment, you would load that register with the binary value for voltage based on the look-up table. You would then go to an ADC to get the proper voltage. That is over simplified, as the sine wave generators I have seen used PWM, so the return value might be a PWM setting.

In any event, you have only used just one RAM location.
I think you mean a DAC. That's what you use for an analog output.
 

Thread Starter

MCU88

Joined Mar 12, 2015
358
In any event, you have only used just one RAM location.
Yes this is all that I am saying. But would it not be faster if I loaded the whole table into RAM to begin with? It seems like one less step in the process. Table abc in program memory. Fetch (n) from abc function. Write to port.
 

Thread Starter

MCU88

Joined Mar 12, 2015
358
Back you your C program. Have you done any with a table? If so, can you post the HEX, and I can try to sort it out for you. Chance of working is probably low, but it might help you to know whether you have to do anything differently.

John
Thanks John. I will get back to you on this.
 

Thread Starter

MCU88

Joined Mar 12, 2015
358
Surely there must be a way. What does your compiler user's guide say? Look up "constants in code space" or "tables in program memory" or something like that. I can't believe they didn't provide an easy way to do this, people have to do this all the time....
I am using mikroC. The user's guide has nothing that I have come across with regards to this. I will check again using the suggested search words that you have mentioned.

I don't doubt that it can be done. I just don't know how to do it. I now understand the schema of what you are saying, and I appreciate your help. Thank you.

But I am wondering if it would be faster to have the whole table in an array in RAM? I have never done anything like this before, so I am uncertain.
 

Thread Starter

MCU88

Joined Mar 12, 2015
358
Surely there must be a way. What does your compiler user's guide say? Look up "constants in code space" or "tables in program memory" or something like that. I can't believe they didn't provide an easy way to do this, people have to do this all the time....
Got it buddy. Using the search you suggested "Constants in Program Memory"

 

Attachments

Top