; stepdds.asm - a PIC program to generate a tone using a D/A converter ; ; 12/01/99 Chuck Olson, WB9KZY ; Jackson Harbor Press ; http://jacksonharbor.home.att.net ; jacksonharbor@att.net ; ; ; VVHATDOESITDO? ; ; This program uses an 8 bit DAC along with a 16F84 PIC microcontroller to ; generate a keyed sine wave. The 16F84 uses an RC clock which can be ; varied (with a suitable potentiometer) to allow a variable frequency ; control for the sine wave output. ; ; This program uses a step approach to creating the tone output. Each ; entry or step of the sine wave table is output to the dac. The main ; program loop functions as a simple counter/lookup routine. When the ; key is released, a branch is made into a second routine which continues ; to output the tone until the output reaches midscale (128) which is ; a "zero" output. ; ; Even though the waveform begins and ends at midscale, "thumping" is ; definitely audible - an attempt is made in the next program, tonef84.asm, ; to address the thumping by ramping the sine wave up and down. ; ; ;PIC ;pin name function/connection ;--- ---- ----------------------------------------------- ; 1 ra2 key input, pulled up to +5V with a 10K resistor ; 2 ra3 no connect ; 3 ra4 no connect ; 4 MCLR pulled up to +5V with a 10K resistor ; 5 VSS Ground ; 6 rb0 connected to 20k dac resistor LSB ; 7 rb1 connected to 20k dac resistor ; 8 rb2 connected to 20k dac resistor ; 9 rb3 connected to 20k dac resistor ; 10 rb4 connected to 20k dac resistor ; 11 rb5 connected to 20k dac resistor ; 12 rb6 connected to 20k dac resistor ; 13 rb7 connected to 20k dac resistor MSB ; 14 VDD +5V ; 15 osc2 no connect ; 16 osc1 connected to the RC timing circuit, R = 10k pot, C = 22 pf ; 17 ra0 no connect ; 18 ra1 no connect LIST P=16F84, R=DEC INCLUDE "p16F84.inc" __FUSES _PWRTE_OFF & _CP_OFF & _WDT_OFF & _RC_OSC ;constants ; bit definitions ; memory locations tabpos equ H'0C' ;step through table position ORG 0x000 start goto init ;reset vector ORG 0x004 noint goto init ;interrupt vector ; init - the initialization power up entry point of the program init bsf STATUS,5 ;set the bank bit to 1 movlw b'00000000' ; all outputs on PORTB movwf TRISB ; set the tri-state register movlw b'00000100' ; RA2 is an input (key) movwf TRISA ; set the tri-state register movlw b'00000000' ; no option bits are relevant movwf OPTION_REG ; set the option register bcf STATUS,5 ;set the bank bit to 0 ;just load a 1 (for page 2 higher address byte) and put in PCLATH ; since all the table entries are now in page 2. movlw 1 ;1 9 load up the high order bits of 2nd page movwf PCLATH ;1 10 load the high address into the latch notone movlw 128 ;load up mid scale movwf PORTB ; for the DAC (an "AC" zero) clrf tabpos ; zero the table position tstsw1 btfsc PORTA,2 ;see if the sw1 switch is pressed goto tstsw1 ;no, ramp down the tone goto p3ml2 ;yes, enter the regular tone routine ; tabp3 is the main tone loop ; The first number after the semicolon indicates the number of PIC cycles ; that are used to execute the instruction - the second number after the ; semicolon indicates the cumulative total of PIC cycles that have elapsed ; since the start of the routine. It's important that the tabp3 and rdown ; routines both execute in exactly the same number of cycles so that the ; tone will have the exactly the same frequency in each routine. tabp3 btfsc PORTA,2 ;2 2 see if the sw1 switch is pressed goto rdown ;2 2 no, ramp down the tone p3ml2 incf tabpos,1 ;1 3 increment the table position movf tabpos,0 ;1 4 get ready for the table lookup call sinetbl ;6 10 Get the sine value ; the call is 2, addwf is 1, retlw is 2 nop ;1 11 waste a cycle to make this routine ; the same "length" as rdown movwf PORTB ;1 12 Send it to the DAC goto tabp3 ;2 14 Branch to top of the routine rdown ;first end this particular cycle of tone at zero ;w contains the DAC output, see if it's zero (128) sublw 128 ;1 1 compare to a zero output (128) btfsc STATUS,2 ;2 3 is it 128? goto notone ; yes, bail to notone incf tabpos,1 ;1 4 no, increment the table position movf tabpos,0 ;1 5 get ready for the table lookup call sinetbl ;6 11 Get the sine value ; the call is 2, addwf is 2, retlw is 2 ; addwf is 2 because of PCL change?? movwf PORTB ;1 12 Send it to the DAC goto rdown ;2 14 Branch to top of the routine org 0xFF ; sinetbl - a table of sine wave values - 2 x 128 bytes in length ; the values were generated with the Qbasic program: sine10.bas ; and then inserted into this program twice to create the full table ; Question: Why does this routine start at address FF? ; Answer: Because the table is originally 256 entries long - the only ; easy/quick way to access the table is to have all the entries on ; the same 256 word memory page. The register PCLATH is preset ; during initialization to always point to page 1 (address 100h) so ; that the result of the addwf to PCL always points to page 1. sinetbl addwf PCL,1 ;compute the jump value retlw 128 ; 0 0 retlw 134 ; 1 4.906764E-02 retlw 140 ; 2 9.801706E-02 retlw 146 ; 3 .1467304 retlw 152 ; 4 .1950902 retlw 158 ; 5 .24298 retlw 165 ; 6 .2902845 retlw 170 ; 7 .3368896 retlw 176 ; 8 .3826831 retlw 182 ; 9 .4275548 retlw 188 ; 10 .4713964 retlw 193 ; 11 .5141024 retlw 198 ; 12 .5555698 retlw 203 ; 13 .5956989 retlw 208 ; 14 .6343929 retlw 213 ; 15 .6715585 retlw 218 ; 16 .7071064 retlw 222 ; 17 .7409506 retlw 226 ; 18 .77301 retlw 230 ; 19 .8032071 retlw 234 ; 20 .8314692 retlw 237 ; 21 .8577282 retlw 240 ; 22 .8819209 retlw 243 ; 23 .9039889 retlw 245 ; 24 .9238791 retlw 248 ; 25 .9415438 retlw 250 ; 26 .9569401 retlw 251 ; 27 .970031 retlw 253 ; 28 .9807851 retlw 254 ; 29 .9891763 retlw 254 ; 30 .9951846 retlw 255 ; 31 .9987954 retlw 255 ; 32 1 retlw 255 ; 33 .9987955 retlw 254 ; 34 .9951848 retlw 254 ; 35 .9891767 retlw 253 ; 36 .9807855 retlw 251 ; 37 .9700316 retlw 250 ; 38 .9569408 retlw 248 ; 39 .9415446 retlw 245 ; 40 .9238802 retlw 243 ; 41 .90399 retlw 240 ; 42 .8819221 retlw 237 ; 43 .8577295 retlw 234 ; 44 .8314706 retlw 230 ; 45 .8032086 retlw 226 ; 46 .7730116 retlw 222 ; 47 .7409524 retlw 218 ; 48 .7071081 retlw 213 ; 49 .6715604 retlw 208 ; 50 .6343948 retlw 203 ; 51 .5957009 retlw 198 ; 52 .555572 retlw 193 ; 53 .5141045 retlw 188 ; 54 .4713986 retlw 182 ; 55 .4275571 retlw 176 ; 56 .3826855 retlw 170 ; 57 .336892 retlw 165 ; 58 .2902869 retlw 158 ; 59 .2429824 retlw 152 ; 60 .1950926 retlw 146 ; 61 .1467329 retlw 140 ; 62 9.801959E-02 retlw 134 ; 63 4.907017E-02 retlw 128 ; 64 2.535182E-06 retlw 121 ; 65 -.0490651 retlw 115 ; 66 -9.801454E-02 retlw 109 ; 67 -.1467278 retlw 103 ; 68 -.1950877 retlw 97 ; 69 -.2429775 retlw 90 ; 70 -.290282 retlw 85 ; 71 -.3368872 retlw 79 ; 72 -.3826808 retlw 73 ; 73 -.4275525 retlw 67 ; 74 -.4713942 retlw 62 ; 75 -.5141002 retlw 57 ; 76 -.5555677 retlw 52 ; 77 -.5956969 retlw 47 ; 78 -.6343909 retlw 42 ; 79 -.6715567 retlw 37 ; 80 -.7071046 retlw 33 ; 81 -.740949 retlw 29 ; 82 -.7730084 retlw 25 ; 83 -.8032055 retlw 21 ; 84 -.8314677 retlw 18 ; 85 -.8577269 retlw 15 ; 86 -.8819197 retlw 12 ; 87 -.9039878 retlw 10 ; 88 -.9238782 retlw 7 ; 89 -.9415429 retlw 5 ; 90 -.9569393 retlw 4 ; 91 -.9700304 retlw 2 ; 92 -.9807846 retlw 1 ; 93 -.989176 retlw 1 ; 94 -.9951844 retlw 0 ; 95 -.9987953 retlw 0 ; 96 -1 retlw 0 ; 97 -.9987956 retlw 1 ; 98 -.9951851 retlw 1 ; 99 -.9891771 retlw 2 ; 100 -.980786 retlw 4 ; 101 -.9700322 retlw 5 ; 102 -.9569415 retlw 7 ; 103 -.9415454 retlw 10 ; 104 -.9238811 retlw 12 ; 105 -.903991 retlw 15 ; 106 -.8819233 retlw 18 ; 107 -.8577308 retlw 21 ; 108 -.831472 retlw 25 ; 109 -.8032101 retlw 29 ; 110 -.7730132 retlw 33 ; 111 -.7409541 retlw 37 ; 112 -.7071099 retlw 42 ; 113 -.6715623 retlw 47 ; 114 -.6343968 retlw 52 ; 115 -.5957029 retlw 57 ; 116 -.5555741 retlw 62 ; 117 -.5141067 retlw 67 ; 118 -.4714009 retlw 73 ; 119 -.4275593 retlw 79 ; 120 -.3826878 retlw 85 ; 121 -.3368944 retlw 90 ; 122 -.2902893 retlw 97 ; 123 -.2429849 retlw 103 ; 124 -.1950951 retlw 109 ; 125 -.1467354 retlw 115 ; 126 -9.802211E-02 retlw 121 ; 127 -.0490727 retlw 128 ; 0 0 retlw 134 ; 1 4.906764E-02 retlw 140 ; 2 9.801706E-02 retlw 146 ; 3 .1467304 retlw 152 ; 4 .1950902 retlw 158 ; 5 .24298 retlw 165 ; 6 .2902845 retlw 170 ; 7 .3368896 retlw 176 ; 8 .3826831 retlw 182 ; 9 .4275548 retlw 188 ; 10 .4713964 retlw 193 ; 11 .5141024 retlw 198 ; 12 .5555698 retlw 203 ; 13 .5956989 retlw 208 ; 14 .6343929 retlw 213 ; 15 .6715585 retlw 218 ; 16 .7071064 retlw 222 ; 17 .7409506 retlw 226 ; 18 .77301 retlw 230 ; 19 .8032071 retlw 234 ; 20 .8314692 retlw 237 ; 21 .8577282 retlw 240 ; 22 .8819209 retlw 243 ; 23 .9039889 retlw 245 ; 24 .9238791 retlw 248 ; 25 .9415438 retlw 250 ; 26 .9569401 retlw 251 ; 27 .970031 retlw 253 ; 28 .9807851 retlw 254 ; 29 .9891763 retlw 254 ; 30 .9951846 retlw 255 ; 31 .9987954 retlw 255 ; 32 1 retlw 255 ; 33 .9987955 retlw 254 ; 34 .9951848 retlw 254 ; 35 .9891767 retlw 253 ; 36 .9807855 retlw 251 ; 37 .9700316 retlw 250 ; 38 .9569408 retlw 248 ; 39 .9415446 retlw 245 ; 40 .9238802 retlw 243 ; 41 .90399 retlw 240 ; 42 .8819221 retlw 237 ; 43 .8577295 retlw 234 ; 44 .8314706 retlw 230 ; 45 .8032086 retlw 226 ; 46 .7730116 retlw 222 ; 47 .7409524 retlw 218 ; 48 .7071081 retlw 213 ; 49 .6715604 retlw 208 ; 50 .6343948 retlw 203 ; 51 .5957009 retlw 198 ; 52 .555572 retlw 193 ; 53 .5141045 retlw 188 ; 54 .4713986 retlw 182 ; 55 .4275571 retlw 176 ; 56 .3826855 retlw 170 ; 57 .336892 retlw 165 ; 58 .2902869 retlw 158 ; 59 .2429824 retlw 152 ; 60 .1950926 retlw 146 ; 61 .1467329 retlw 140 ; 62 9.801959E-02 retlw 134 ; 63 4.907017E-02 retlw 128 ; 64 2.535182E-06 retlw 121 ; 65 -.0490651 retlw 115 ; 66 -9.801454E-02 retlw 109 ; 67 -.1467278 retlw 103 ; 68 -.1950877 retlw 97 ; 69 -.2429775 retlw 90 ; 70 -.290282 retlw 85 ; 71 -.3368872 retlw 79 ; 72 -.3826808 retlw 73 ; 73 -.4275525 retlw 67 ; 74 -.4713942 retlw 62 ; 75 -.5141002 retlw 57 ; 76 -.5555677 retlw 52 ; 77 -.5956969 retlw 47 ; 78 -.6343909 retlw 42 ; 79 -.6715567 retlw 37 ; 80 -.7071046 retlw 33 ; 81 -.740949 retlw 29 ; 82 -.7730084 retlw 25 ; 83 -.8032055 retlw 21 ; 84 -.8314677 retlw 18 ; 85 -.8577269 retlw 15 ; 86 -.8819197 retlw 12 ; 87 -.9039878 retlw 10 ; 88 -.9238782 retlw 7 ; 89 -.9415429 retlw 5 ; 90 -.9569393 retlw 4 ; 91 -.9700304 retlw 2 ; 92 -.9807846 retlw 1 ; 93 -.989176 retlw 1 ; 94 -.9951844 retlw 0 ; 95 -.9987953 retlw 0 ; 96 -1 retlw 0 ; 97 -.9987956 retlw 1 ; 98 -.9951851 retlw 1 ; 99 -.9891771 retlw 2 ; 100 -.980786 retlw 4 ; 101 -.9700322 retlw 5 ; 102 -.9569415 retlw 7 ; 103 -.9415454 retlw 10 ; 104 -.9238811 retlw 12 ; 105 -.903991 retlw 15 ; 106 -.8819233 retlw 18 ; 107 -.8577308 retlw 21 ; 108 -.831472 retlw 25 ; 109 -.8032101 retlw 29 ; 110 -.7730132 retlw 33 ; 111 -.7409541 retlw 37 ; 112 -.7071099 retlw 42 ; 113 -.6715623 retlw 47 ; 114 -.6343968 retlw 52 ; 115 -.5957029 retlw 57 ; 116 -.5555741 retlw 62 ; 117 -.5141067 retlw 67 ; 118 -.4714009 retlw 73 ; 119 -.4275593 retlw 79 ; 120 -.3826878 retlw 85 ; 121 -.3368944 retlw 90 ; 122 -.2902893 retlw 97 ; 123 -.2429849 retlw 103 ; 124 -.1950951 retlw 109 ; 125 -.1467354 retlw 115 ; 126 -9.802211E-02 retlw 121 ; 127 -.0490727 END