; * ; This program implements a "1 bit" sine wave generation scheme. * ; Started 6/6/08 after finishing the QBASIC code to program the bit * ; patterns. * ; * ; If the internal RC oscillator is not implemented * ; then the first four instructions following the label 'main' can * ; be removed. * ; * ; Refer to the MPASM User's Guide for additional information on * ; features of the assembler (Document DS33014). * ; * ; Refer to the respective PICmicro data sheet for additional * ; information on the instruction set. * ; * ;********************************************************************** ; * ; Filename: PIC-SINE.asm * ; Date: 06/06/2008 * ; File Version: * ; * ; Author: Nick Kennedy, WA5BDU * ; Company: N/A * ; * ; * ;********************************************************************** ; * ; Files required: None except standard 12F629 'include' AKA * ; P12F169.INC. * ; * ; * ;********************************************************************** ; * ; Notes: See below * ; * ; * ; * ; * ;********************************************************************** ; I'm going to use pin 7, GP0 for the output ; ; Function PIC Pin [Name] ; ; bit stream out 7 [GP0] ; enable tone 2 [GP5] pull low to enable out on pin 7 ; ; I'm going to enable the internal high speed oscillator, which seems to ; be the default configuration bit setup. ; list p=12f629 ; list directive to define processor #include ; processor specific variable definitions ; __idlocs B'00111110000000' errorlevel -302 ; suppress message 302 from list file __CONFIG _CP_OFF & _CPD_OFF & _BODEN_OFF & _MCLRE_OFF & _WDT_OFF & _PWRTE_ON & _INTRC_OSC_NOCLKOUT ; '__CONFIG' directive is used to embed configuration word within .asm file. ; The labels following the directive are located in the respective .inc file. ; ; I had trouble with the chip rebooting after every few sine waves until ; I change MCLRE_ above from ON to OFF, which pulls it high internally ;***** VARIABLE DEFINITIONS w_temp EQU 0x20 ; variable used for context saving status_temp EQU 0x21 ; variable used for context saving ;********************************************************************** ORG 0x000 ; processor reset vector goto main ; go to beginning of program ; I took this interrupt stuff out and "think" I had trouble with the PIC ; not running, so I put it back ... ORG 0x004 ; interrupt vector location movwf w_temp ; save off current W register contents movf STATUS,w ; move status register into W register movwf status_temp ; save off contents of STATUS register ; isr code can go here or be located as a call subroutine elsewhere movf status_temp,w ; retrieve copy of STATUS register movwf STATUS ; restore pre-isr STATUS register contents swapf w_temp,f retfie ; return from interrupt ; these first 4 instructions are not required if the internal oscillator is ; not used. My chip had a factory setting of 0x38 (0x3438 where 34 is the ; retlw instruction. I changed it to 0x40) main call 0x3FF ; retrieve factory calibration value bsf STATUS,RP0 ; set file register bank to 1 movlw 0x44 ; try my own number to tweak frequency movwf OSCCAL ; update register with factory cal value bcf STATUS,RP0 ; set file register bank to 0 bcf INTCON, GIE ; disable all interrupts ; **** Start of my actual code *** ; Configure the GPIO port. Note that direction bits power up as ; "unknown", so I need to configure them. If I were using the ; 12F675, I'd need to set ANSEL here too. bcf STATUS, RP0 ; start in bank 0 clrf GPIO ; all outputs initially low movlw 07h ; CMCON bits 2:0 = to turn movwf CMCON ; comparators OFF bsf STATUS, RP0 ; go to bank 1 movlw B'00111000' ; GP 0, 1, 2, outputs and movwf TRISIO ; 3,4,5 inputs bcf STATUS, RP0 ; back to bank 0 ; Note, address the weak pull-ups (WPU register) if needed for inputs ; MACROs to set the output pin high or low SETLOW macro bcf GPIO, GP0 endm SETHIGH macro bsf GPIO, GP0 endm ; This code created by QBASIC program 1BITSINE.BAS ; period in u-sec = 1703 ; semi-tones above 440: 5 ; frequency = 587.3295 SETHIGH makesine CALL delay13 CALL delay13 CALL delay13 CALL delay13 CALL delay13 CALL delay13 ; CALL delay13 ; the call below uses 15 clocks commented out here ; nop ; nop call ckpin2 ; check status of GP5/pin2 and turn off tone if high nop SETLOW SETHIGH CALL delay13 CALL delay10 SETLOW SETHIGH CALL delay13 nop nop nop SETLOW SETHIGH CALL delay12 SETLOW SETHIGH CALL delay10 SETLOW SETHIGH CALL delay8 SETLOW SETHIGH CALL delay8 SETLOW SETHIGH CALL delay6 SETLOW SETHIGH CALL delay6 SETLOW SETHIGH CALL delay6 SETLOW SETHIGH CALL delay5 SETLOW SETHIGH CALL delay4 SETLOW SETHIGH CALL delay4 SETLOW SETHIGH CALL delay4 SETLOW SETHIGH CALL delay4 SETLOW SETHIGH nop nop nop SETLOW SETHIGH nop nop nop SETLOW SETHIGH nop nop nop SETLOW SETHIGH nop nop nop SETLOW SETHIGH nop nop SETLOW SETHIGH nop nop nop SETLOW SETHIGH nop nop SETLOW SETHIGH nop nop SETLOW SETHIGH nop nop nop SETLOW SETHIGH nop nop SETLOW SETHIGH nop nop SETLOW SETHIGH nop SETLOW SETHIGH nop nop SETLOW SETHIGH nop nop SETLOW SETHIGH nop SETLOW SETHIGH nop nop SETLOW SETHIGH nop SETLOW SETHIGH nop nop SETLOW SETHIGH nop SETLOW SETHIGH nop SETLOW SETHIGH nop SETLOW SETHIGH nop SETLOW SETHIGH nop SETLOW SETHIGH nop SETLOW SETHIGH nop SETLOW SETHIGH nop SETLOW SETHIGH nop SETLOW SETHIGH nop SETLOW SETHIGH nop SETLOW SETHIGH nop SETLOW SETHIGH SETLOW SETHIGH nop SETLOW SETHIGH nop SETLOW SETHIGH SETLOW SETHIGH nop SETLOW SETHIGH nop SETLOW SETHIGH SETLOW SETHIGH nop SETLOW SETHIGH SETLOW SETHIGH nop SETLOW SETHIGH SETLOW SETHIGH SETLOW SETHIGH nop SETLOW SETHIGH SETLOW SETHIGH SETLOW SETHIGH nop SETLOW SETHIGH SETLOW SETHIGH SETLOW SETHIGH nop SETLOW SETHIGH SETLOW SETHIGH SETLOW SETHIGH SETLOW SETHIGH SETLOW SETHIGH SETLOW SETHIGH nop SETLOW SETHIGH SETLOW SETHIGH SETLOW SETHIGH SETLOW SETHIGH SETLOW SETHIGH SETLOW SETHIGH SETLOW SETHIGH SETLOW SETHIGH SETLOW SETHIGH SETLOW SETHIGH SETLOW SETHIGH SETLOW SETHIGH SETLOW SETHIGH SETLOW SETHIGH SETLOW SETHIGH SETLOW SETHIGH SETLOW nop SETHIGH SETLOW SETHIGH SETLOW SETHIGH SETLOW SETHIGH SETLOW SETHIGH SETLOW nop SETHIGH SETLOW SETHIGH SETLOW SETHIGH SETLOW SETHIGH SETLOW nop SETHIGH SETLOW SETHIGH SETLOW SETHIGH SETLOW nop SETHIGH SETLOW SETHIGH SETLOW nop SETHIGH SETLOW SETHIGH SETLOW nop SETHIGH SETLOW SETHIGH SETLOW nop SETHIGH SETLOW SETHIGH SETLOW nop SETHIGH SETLOW nop SETHIGH SETLOW SETHIGH SETLOW nop SETHIGH SETLOW nop SETHIGH SETLOW nop SETHIGH SETLOW SETHIGH SETLOW nop SETHIGH SETLOW nop SETHIGH SETLOW nop SETHIGH SETLOW nop SETHIGH SETLOW nop SETHIGH SETLOW nop SETHIGH SETLOW nop nop SETHIGH SETLOW nop SETHIGH SETLOW nop SETHIGH SETLOW nop SETHIGH SETLOW nop nop SETHIGH SETLOW nop SETHIGH SETLOW nop nop SETHIGH SETLOW nop nop SETHIGH SETLOW nop SETHIGH SETLOW nop nop SETHIGH SETLOW nop nop SETHIGH SETLOW nop nop SETHIGH SETLOW nop nop SETHIGH SETLOW nop nop SETHIGH SETLOW nop nop SETHIGH SETLOW nop nop nop SETHIGH SETLOW nop nop nop SETHIGH SETLOW nop nop SETHIGH SETLOW nop nop nop SETHIGH SETLOW nop nop nop SETHIGH SETLOW CALL delay4 SETHIGH SETLOW nop nop nop SETHIGH SETLOW CALL delay4 SETHIGH SETLOW CALL delay4 SETHIGH SETLOW CALL delay4 SETHIGH SETLOW CALL delay5 SETHIGH SETLOW CALL delay5 SETHIGH SETLOW CALL delay6 SETHIGH SETLOW CALL delay6 SETHIGH SETLOW CALL delay8 SETHIGH SETLOW CALL delay8 SETHIGH SETLOW CALL delay9 SETHIGH SETLOW CALL delay12 SETHIGH SETLOW CALL delay13 nop SETHIGH SETLOW CALL delay13 CALL delay8 SETHIGH SETLOW CALL delay13 CALL delay13 CALL delay13 nop nop SETHIGH SETLOW CALL delay13 CALL delay13 CALL delay13 CALL delay13 CALL delay13 CALL delay13 CALL delay13 CALL delay13 CALL delay13 CALL delay13 CALL delay13 nop nop nop SETHIGH SETLOW CALL delay13 CALL delay13 SETHIGH SETLOW CALL delay13 CALL delay4 SETHIGH SETLOW CALL delay13 SETHIGH SETLOW CALL delay11 SETHIGH SETLOW CALL delay8 SETHIGH SETLOW CALL delay8 SETHIGH SETLOW CALL delay7 SETHIGH SETLOW CALL delay6 SETHIGH SETLOW CALL delay5 SETHIGH SETLOW CALL delay5 SETHIGH SETLOW CALL delay5 SETHIGH SETLOW CALL delay4 SETHIGH SETLOW CALL delay4 SETHIGH SETLOW CALL delay4 SETHIGH SETLOW nop nop nop SETHIGH SETLOW nop nop nop SETHIGH SETLOW nop nop nop SETHIGH SETLOW nop nop nop SETHIGH SETLOW nop nop nop SETHIGH SETLOW nop nop SETHIGH SETLOW nop nop nop SETHIGH SETLOW nop nop SETHIGH SETLOW nop nop SETHIGH SETLOW nop nop SETHIGH SETLOW nop nop SETHIGH SETLOW nop nop SETHIGH SETLOW nop nop SETHIGH SETLOW nop SETHIGH SETLOW nop nop SETHIGH SETLOW nop SETHIGH SETLOW nop nop SETHIGH SETLOW nop SETHIGH SETLOW nop nop SETHIGH SETLOW nop SETHIGH SETLOW nop SETHIGH SETLOW nop SETHIGH SETLOW nop SETHIGH SETLOW nop SETHIGH SETLOW nop SETHIGH SETLOW nop SETHIGH SETLOW nop SETHIGH SETLOW nop SETHIGH SETLOW nop SETHIGH SETLOW nop SETHIGH SETLOW SETHIGH SETLOW nop SETHIGH SETLOW nop SETHIGH SETLOW SETHIGH SETLOW nop SETHIGH SETLOW SETHIGH SETLOW nop SETHIGH SETLOW nop SETHIGH SETLOW SETHIGH SETLOW SETHIGH SETLOW nop SETHIGH SETLOW SETHIGH SETLOW nop SETHIGH SETLOW SETHIGH SETLOW SETHIGH SETLOW nop SETHIGH SETLOW SETHIGH SETLOW SETHIGH SETLOW nop SETHIGH SETLOW SETHIGH SETLOW SETHIGH SETLOW SETHIGH SETLOW SETHIGH SETLOW SETHIGH SETLOW nop SETHIGH SETLOW SETHIGH SETLOW SETHIGH SETLOW SETHIGH SETLOW SETHIGH SETLOW SETHIGH SETLOW SETHIGH SETLOW SETHIGH SETLOW SETHIGH SETLOW SETHIGH SETLOW SETHIGH SETLOW SETHIGH SETLOW SETHIGH SETLOW SETHIGH SETLOW SETHIGH SETLOW SETHIGH SETLOW SETHIGH nop SETLOW SETHIGH SETLOW SETHIGH SETLOW SETHIGH SETLOW SETHIGH SETLOW SETHIGH nop SETLOW SETHIGH SETLOW SETHIGH SETLOW SETHIGH SETLOW SETHIGH nop SETLOW SETHIGH SETLOW SETHIGH SETLOW SETHIGH nop SETLOW SETHIGH SETLOW SETHIGH nop SETLOW SETHIGH SETLOW SETHIGH nop SETLOW SETHIGH SETLOW SETHIGH nop SETLOW SETHIGH SETLOW SETHIGH nop SETLOW SETHIGH nop SETLOW SETHIGH SETLOW SETHIGH nop SETLOW SETHIGH nop SETLOW SETHIGH SETLOW SETHIGH nop SETLOW SETHIGH nop SETLOW SETHIGH nop SETLOW SETHIGH nop SETLOW SETHIGH nop SETLOW SETHIGH nop SETLOW SETHIGH nop SETLOW SETHIGH nop SETLOW SETHIGH nop nop SETLOW SETHIGH nop SETLOW SETHIGH nop SETLOW SETHIGH nop nop SETLOW SETHIGH nop SETLOW SETHIGH nop nop SETLOW SETHIGH nop SETLOW SETHIGH nop nop SETLOW SETHIGH nop nop SETLOW SETHIGH nop SETLOW SETHIGH nop nop SETLOW SETHIGH nop nop SETLOW SETHIGH nop nop nop SETLOW SETHIGH nop nop SETLOW SETHIGH nop nop SETLOW SETHIGH nop nop nop SETLOW SETHIGH nop nop nop SETLOW SETHIGH nop nop SETLOW SETHIGH nop nop nop SETLOW SETHIGH CALL delay4 SETLOW SETHIGH nop nop nop SETLOW SETHIGH CALL delay4 SETLOW SETHIGH CALL delay4 SETLOW SETHIGH CALL delay4 SETLOW SETHIGH CALL delay5 SETLOW SETHIGH CALL delay5 SETLOW SETHIGH CALL delay6 SETLOW SETHIGH CALL delay6 SETLOW SETHIGH CALL delay7 SETLOW SETHIGH CALL delay8 SETLOW SETHIGH CALL delay9 SETLOW SETHIGH CALL delay11 SETLOW SETHIGH CALL delay13 SETLOW SETHIGH CALL delay13 CALL delay6 SETLOW SETHIGH CALL delay13 CALL delay13 CALL delay6 SETLOW SETHIGH CALL delay13 CALL delay13 CALL delay13 CALL delay13 CALL delay13 CALL delay9 ; was 11, reduced to accommodate goto below goto makesine ; DELAY subroutines allow delaying from 4 to 13 clocks ; note that the delay includes the 2 clocks of the CALL statement delay13 nop delay12 nop delay11 nop delay10 nop delay9 nop delay8 nop delay7 nop delay6 nop delay5 nop delay4 return ; code to check GP5's status. Enable tone out via GP0 if low and ; disable if high ckpin2 btfsc GPIO, GP5 ; 2 if skipped, 1 if straight thru goto offtone ; 2 call tone_on ; 2 goto finishcheck ; 2 offtone call tone_off ; 2 finishcheck bcf STATUS, RP0 ; 1 back to bank 0 return ; 2 ; clocks for above check -- ; (tone_on and _off both take 4 clocks) ; if tone_off executed: 1+2+2+4+1+2=12 ; if tone_on executed: 2+2+4+2+1+2=13 ; OK, I could add a nop to the tone_off routine to make them equal, but ; I don't need to -- when I turn off the tone it doesn't matter if the count ; remains correct. Note that my 13 clocks will be 15 when called from the ; main line code ... ; each routine is 1 + 1 + 2 = 4 clocks tone_off bsf STATUS, RP0 ; bank 1 (calling program changes back to 0) bsf TRISIO, 0 ; make pin 7 GP0 an input return tone_on bsf STATUS, RP0 bcf TRISIO, 0 ; make pin 7 (GP0) an output return END ; directive 'end of program'