;****************************************************************************************** ; PIC12F1501 Sample Program ; Project : ; Program Name : ; Programer : ; Create date : Version 1.0 H26.11.1 ; History : ; Laungage : MPLAB MPASM ; ;****************************************************************************************** ; Description ; OSC : Internal OSC 16MHz ; Assign I/O pins ; RA1 : NCO1, Numerically Controlled Oscillator output. ;****************************************************************************************** LIST P=PIC12F1501,ST=OFF,R=DEC INCLUDE "P12F1501.inc" __CONFIG _CONFIG1 , _FOSC_INTOSC & _WDTE_OFF & _PWRTE_OFF & _MCLRE_OFF & _CP_OFF & _BOREN_OFF & _CLKOUTEN_OFF __CONFIG _CONFIG2 , _WRT_OFF & _STVREN_OFF & _BORV_HI & _LPBOR_OFF & _LVP_OFF ; ; Define GENERAL PURPOSE RAM AREA (80 bytes maximum) 20h-6fh ; TMP_G EQU 20H ; ; Define Common RAM AREA (16 bytes) 70h-7fh ; TMP_C EQU 70H C_TMP1 EQU 71H PLAY_C EQU 72H VOL_C EQU 73H ; ; Define Macro ; SELBANK MACRO #BANK_NO MOVLW #BANK_NO MOVWF BSR ; The active bank is selected by writing the bank number into the Bank Select Register (BSR) ENDM WAIT01 MACRO #TIME_NO MOVLW #TIME_NO CALL WAIT_01S ENDM PLAYSTP MACRO CALL STOP_1 ENDM PLAYDO2 MACRO MOVLW 34 CALL PLAY_1 ENDM PLAYRE2 MACRO MOVLW 38 CALL PLAY_1 ENDM PLAYMI2 MACRO MOVLW 43 CALL PLAY_1 ENDM PLAYFA2 MACRO MOVLW 46 CALL PLAY_1 ENDM PLAYSO2 MACRO MOVLW 51 CALL PLAY_1 ENDM PLAYRA2 MACRO MOVLW 58 CALL PLAY_1 ENDM PLAYSI2 MACRO MOVLW 65 CALL PLAY_1 ENDM PLAYDO3 MACRO MOVLW 69 CALL PLAY_1 ENDM SETDAC MACRO #DAC_DATA ; 0 <= DAC_DATA <= 31 MOVLW #DAC_DATA MOVWF DACCON1 ENDM VOL MACRO #DAC_DATA ; 0 <= DAC_DATA <= 31 MOVLW #DAC_DATA CALL VOLUME ENDM ; ; Program Start ; ORG 0h GOTO START ORG 4h GOTO START START ; ***************************** ; 1.Initialized ; ***************************** ; (1)Define OSC (16MHz, IntOSC) SELBANK 1 ; SET BANK1 MOVLW B'01111010' MOVWF OSCCON ; (2)Define ANSELA Register ; The ANSELA register is used to configure the Input mode of an I/O pin to analog. SELBANK 3 CLRF ANSELA ; All Degital Port ; (3)Define I/O port SELBANK 1 ; SET BANK1 MOVLW B'11111101' MOVWF TRISA ; (4)Initialized DAC CALL INITDAC ; (5)Initialized NCO Module CALL INITNCO SELBANK 0 ; ***************************** ; 2.Program main ; ***************************** ; (1)Main loop MAIN ; VOL 15 CALL DEMO01 VOL 30 CALL DEMO02 GOTO MAIN ;******************************************************************************************* ; Function Name : DEMO01@@@@ ; Function : This demo program to play the do-re-mi ; Input : ; Output : ; Description : ;******************************************************************************************* DEMO01 VOL 15 PLAYDO2 VOL 30 PLAYRE2 VOL 15 PLAYMI2 VOL 30 PLAYFA2 VOL 15 PLAYSO2 VOL 30 PLAYRA2 VOL 15 PLAYSI2 VOL 30 PLAYDO3 ; PLAYSTP PLAYSTP RETURN ;******************************************************************************************* ; Function Name : DEMO02@@@@ ; Function : ; Input : ; Output : ; Description : ;******************************************************************************************* DEMO02 PLAYDO2 PLAYDO2 PLAYRE2 PLAYRE2 PLAYMI2 PLAYMI2 PLAYRE2 PLAYSTP PLAYMI2 PLAYMI2 PLAYSO2 PLAYSO2 PLAYRA2 PLAYRA2 PLAYSO2 PLAYSTP PLAYRA2 PLAYRA2 PLAYSO2 PLAYSO2 PLAYMI2 PLAYDO2 PLAYRE2 PLAYSTP PLAYSO2 PLAYSO2 PLAYMI2 PLAYDO2 PLAYRE2 PLAYMI2 PLAYDO2 PLAYSTP RETURN ;******************************************************************************************* ; Function Name : VOLUME@@@@ ; Function : ; Input : ; Output : ; Description : ;******************************************************************************************* VOLUME MOVWF VOL_C SELBANK 2 MOVFW VOL_C MOVWF DACCON1 RETURN ;******************************************************************************************* ; Function Name : STOP_1@@@@ ; Function : ; Input : ; Output : ; Description : ;******************************************************************************************* STOP_1 ; 1.Wait WAIT01 4 RETURN ;******************************************************************************************* ; Function Name : PLAY_1@@@@ ; Function : ; Input : ; Output : ; Description : ;******************************************************************************************* PLAY_1 ; 1.Move W-reg value MOVWF PLAY_C ; 2.Set NCO1INCL register SELBANK 9 MOVF PLAY_C,W MOVWF NCO1INCL ; 3.NCO ON SELBANK 9 MOVLW B'11000000' MOVWF NCO1CON ; 4.Wait WAIT01 1 ; 5.NCO OFF SELBANK 9 MOVLW B'10000000' MOVWF NCO1CON ; 6.Wait WAIT01 3 RETURN ;****************************************************************************************** ; Program : INITDAC ; Title : ; Input : ; Output : ;****************************************************************************************** INITDAC ; ******************************************************* ; 1.Define FVRCON: FIXED VOLTAGE REFERENCE CONTROL REGISTER ; FVRCON[7] FVREN: Fixed Voltage Reference Enable bit ; 1 = Fixed Voltage Reference is enabled / 0 = Fixed Voltage Reference is disabled ; FVRCON[6] FVRRDY: Fixed Voltage Reference Ready Flag bit ; 1 = Fixed Voltage Reference output is ready for use / 0 = Fixed Voltage Reference output is not ready or not enabled ; FVRCON[5] TSEN: Temperature Indicator Enable bit ; 1 = Temperature Indicator is enabled / 0 = Temperature Indicator is disabled ; FVRCON[4] TSRNG: Temperature Indicator Range Selection bit ; 1 = VOUT = VDD - 4VT (High Range) ; 0 = VOUT = VDD - 2VT (Low Range) ; FVRCON[3-2] CDAFVR<1:0>: Comparator Fixed Voltage Reference Selection bits ; 11 = Comparator Fixed Voltage Reference Peripheral output is 4x (4.096V) ; 10 = Comparator Fixed Voltage Reference Peripheral output is 2x (2.048V) ; 01 = Comparator Fixed Voltage Reference Peripheral output is 1x (1.024V) ; 00 = Comparator Fixed Voltage Reference Peripheral output is off ; FVRCON[1-0] ADFVR<1:0>: ADC Fixed Voltage Reference Selection bit ; 11 = ADC Fixed Voltage Reference Peripheral output is 4x (4.096V) ; 10 = ADC Fixed Voltage Reference Peripheral output is 2x (2.048V) ; 01 = ADC Fixed Voltage Reference Peripheral output is 1x (1.024V) ; 00 = ADC Fixed Voltage Reference Peripheral output is off ; ******************************************************* ; SELBANK 2 ; MOVLW B'00000000' ; MOVWF FVRCON ; ******************************************************* ; 2.Define DACCON0: VOLTAGE REFERENCE CONTROL REGISTER 0 ; DACCON0[7] DACEN: DAC Enable bit ; 1 = DAC is enabled / 0 = DAC is disabled ; DACCON0[6] Unimplemented: Read as e0f ; DACCON0[5] DACOE1: DAC Voltage Output Enable bit ; 1 = DAC voltage level is also an output on the DACOUT1 pin ; 0 = DAC voltage level is disconnected from the DACOUT1 pin ; DACCON0[4] DACOE2: DAC Voltage Output Enable bit ; 1 = DAC voltage level is also an output on the DACOUT2 pin ; 0 = DAC voltage level is disconnected from the DACOUT2 pin ; DACCON0[3] Unimplemented: Read as e0f ; DACCON0[2] DACPSS: DAC Positive Source Select bit ; 1 = VREF+ pin ; 0 = VDD ; DACCON0[1-0] Unimplemented: Read as e0f ; ******************************************************* SELBANK 2 MOVLW B'10100000' MOVWF DACCON0 ; ******************************************************* ; 3.Define DACCON1: VOLTAGE REFERENCE CONTROL REGISTER 1 ; DACCON1[7-5] Unimplemented: Read as e0f ; DACCON1[4-0] DACR<4:0>: DAC Voltage Output Select bits ; ******************************************************* SELBANK 2 MOVLW B'00000000' MOVWF DACCON1 SELBANK 0 ; SET BANK0 RETURN ;******************************************************************************************* ; Function Name : INITNCO@@@@ ; Function : Initialize NCO Module ; Input : ; Output : ; Description : ;******************************************************************************************* INITNCO ; **************************************************************** ; 0.APFCON: ALTERNATE PIN FUNCTION CONTROL REGISTER ; APFCON[7] CWG1BSEL: Pin Selection bit ; 1 = CWG1B function is on RA4 / 0 = CWG1B function is on RA0 ; APFCON[6] CWG1ASEL: Pin Selection bit ; 1 = CWG1A function is on RA5 / 0 = CWG1A function is on RA2 ; APFCON[5-4] Unimplemented: Read as e0f ; APFCON[3] T1GSEL: Pin Selection bit ; 1 = T1G function is on RA3 / 0 = T1G function is on RA4 ; APFCON[2] Unimplemented: Read as e0f ; APFCON[1] CLC1SEL: Pin Selection bit ; 1 = CLC1 function is on RA4 / 0 = CLC1 function is on RA2 ; APFCON[0] NCO1SEL: Pin Selection bit ; 1 = NCO1 function is on RA5 / 0 = NCO1 function is on RA1 ; **************************************************************** SELBANK 2 MOVLW B'00000000' MOVWF APFCON ; **************************************************************** ; 1.NCOxCON:NCOx CONTROL REGISTER ; NCOxCON[7] NxEN: NCOx Enable bit ; 1 = NCOx module is enabled / 0 = NCOx module is disabled ; NCOxCON[6] NxOE: NCOx Output Enable bit ; 1 = NCOx output pin is enabled / 0 = NCOx output pin is disabled ; NCOxCON[5] NxOUT: NCOx Output bit ; 1 = NCOx output is high / 0 = NCOx output is low ; NCOxCON[4] NxPOL: NCOx Polarity bit ; 1 = NCOx output signal is active-high / 0 = NCOx output signal is active-low ; NCOxCON[3-1] Unimplemented: Read as e0f. ; NCOxCON[0] NxPFM: NCOx Pulse Frequency Mode bit ; 1 = NCOx operates in Pulse Frequency mode / 0 = NCOx operates in Fixed Duty Cycle mode ; **************************************************************** SELBANK 9 ; MOVLW B'11000000' MOVLW B'10000000' MOVWF NCO1CON ; **************************************************************** ; 2.NCOxCLK: NCOx INPUT CLOCK CONTROL REGISTER ; NCOxCLK[7-5] NxPWS<2:0>: NCOx Output Pulse Width Select bits(1, 2) ; 111 = 128 NCOx clock periods ; 110 = 64 NCOx clock periods ; 101 = 32 NCOx clock periods ; 100 = 16 NCOx clock periods ; 011 = 8 NCOx clock periods ; 010 = 4 NCOx clock periods ; 001 = 2 NCOx clock periods ; 000 = 1 NCOx clock periods ; NCOxCLK[4-2] Unimplemented: Read as e0f ; NCOxCLK[1-0] NxCKS<1:0>: NCOx Clock Source Select bits ; 11 = NCO1CLK ; 10 = LC1OUT ; 01 = FOSC ; 00 = HFINTOSC (16 MHz) ; **************************************************************** SELBANK 9 MOVLW B'00000001' MOVWF NCO1CLK ; **************************************************************** ; 3.NCOxACCU: NCOx ACCUMULATOR REGISTER ; NCOxACCL: NCOx ACCUMULATOR REGISTER ? LOW BYTE ; NCOxACCL[7:0] NCOxACCL<7:0> NCOx Accumulator, low byte ; NCOxACCH: NCOx ACCUMULATOR REGISTER ? HIGH BYTE ; NCOxACCH[7:0] NCOxACC<15:8> NCOx Accumulator, high byte ; NCOxACCU: NCOx ACCUMULATOR REGISTER ? UPPER BYTE ; NCOxACCU[7-4] Unimplemented: Read as e0f ; NCOxACCU[3-0] NCOxACC<19:16> NCOx Accumulator, upper byte ; **************************************************************** SELBANK 9 MOVLW H'00' MOVWF NCO1ACCU MOVLW H'00' MOVWF NCO1ACCH MOVLW H'00' MOVWF NCO1ACCL ; **************************************************************** ; 4.NCOxINC: NCOx INCREMENT REGISTER ; NCOxINCL: NCOx INCREMENT REGISTER ? LOW BYTE ; NCOxINCL[7-0] NCOxINC<7:0>: NCOx Increment, low byte ; NCOxINCH: NCOx INCREMENT REGISTER ? HIGH BYTE ; NCOxINCL[7-0] NCOxINC<15:8>: NCOx Increment, high byte ; **************************************************************** ; F(Hz)=16MHz*(NCO1INCL)/(2^21) ==> F(Hz)=(NCO1INCL)*7.629394531 ==> NCO1INCL=F(Hz)/7.629394531 ; F(Hz):50Hz 50Hz /7.629394531=>0d6.5536 =>0x6 ; F(Hz):100Hz 100Hz /7.629394531=>0d13.1072=>0xD ; F(Hz):1KHz 1KHz /7.629394531=>0d131.072=>0x83 ; F(Hz):10KHz 10KHz /7.629394531=>0d1310.72=>0x51F ; F(Hz):100KHz 100KHz/7.629394531=>0d13107.2=>0x3333 ; F(Hz):500KHz 500KHz/7.629394531=>0d65536 =>0x10000 ; ; Ra:220.00Hz/7.629394531=>29 ; Si:246.94Hz/7.629394531=>32 ; Do:261.63Hz/7.629394531=>34 ; Re:293.66Hz/7.629394531=>38 ; Mi:329.63Hz/7.629394531=>43 ; Fa:349.23Hz/7.629394531=>46 ; So:392.00Hz/7.629394531=>51 ; Ra:440.00Hz/7.629394531=>58 ; Si:493.89Hz/7.629394531=>65 ; Do:523.25Hz/7.629394531=>69 SELBANK 9 MOVLW H'00' MOVWF NCO1INCH MOVLW H'83' MOVWF NCO1INCL RETURN ;****************************************************************************************** ; Function Name : Wait_01S ; Title : This function will wait n times of 0.1 seconds ; Input : W-reg (n) ; Output : ;****************************************************************************************** WAIT_01S MOVWF C_TMP1 WAIT_01S_1 CALL WAIT_100MS DECFSZ C_TMP1,F GOTO WAIT_01S_1 RETURN ;****************************************************************************************** ; Function Name : Wait_100mS ; Title : This function will wait 100 mS ; Input : ; Output : ;****************************************************************************************** WAIT_100MS ; ******************************************************* ; 1.Define T1CON(TIMER1 CONTROL REGISTER) Register ; T1CON(7-6) TMR1CS<1:0>: Timer1 Clock Source Select bits ; 11 =Timer1 clock source is LFINTOSC ; 10 =Timer1 clock source is T1CKI pin (on rising edge) ; 01 =Timer1 clock source is system clock (FOSC) ; 00 =Timer1 clock source is instruction clock (FOSC/4) ; T1CON(5-4) T1CKPS<1:0>: Timer1 Input Clock Prescale Select bits ; 11 = 1:8 Prescale value ; 10 = 1:4 Prescale value ; 01 = 1:2 Prescale value ; 00 = 1:1 Prescale value ; T1CON(3) Unimplemented: Read as e0f ; T1CON(2) T1SYNC: Timer1 Synchronization Control bit ; 1 = Do not synchronize asynchronous clock input ; 0 = Synchronize asynchronous clock input with system clock (FOSC) ; T1CON(1) Unimplemented: Read as e0f ; T1CON(0) TMR1ON: Timer1 On bit ; 1 = Enables Timer1 ; 0 = Stops Timer1 and clears Timer1 gate flip-flop ; ******************************************************* SELBANK 0 ; SET BANK0 MOVLW B'00110001' MOVWF T1CON ; ******************************************************* ; 2.Define T1GCON (TIMER1 GATE CONTROL REGISTER) Register ; T1GCON(7) TMR1GE: Timer1 Gate Enable bit ; If TMR1ON = 0: This bit is ignored ; If TMR1ON = 1: ; 1 = Timer1 counting is controlled by the Timer1 gate function ; 0 = Timer1 counts regardless of Timer1 gate function ; T1GCON(6) T1GPOL: Timer1 Gate Polarity bit ; 1 = Timer1 gate is active-high (Timer1 counts when gate is high) ; 0 = Timer1 gate is active-low (Timer1 counts when gate is low) ; T1GCON(5) T1GTM: Timer1 Gate Toggle Mode bit ; 1 = Timer1 Gate Toggle mode is enabled ; 0 = Timer1 Gate Toggle mode is disabled and toggle flip-flop is cleared ; T1GCON(4) T1GSPM: Timer1 Gate Single-Pulse Mode bit ; 1 = Timer1 Gate Single-Pulse mode is enabled and is controlling Timer1 gate ; 0 = Timer1 Gate Single-Pulse mode is disabled ; T1GCON(3) T1GGO/DONE: Timer1 Gate Single-Pulse Acquisition Status bit ; 1 = Timer1 gate single-pulse acquisition is ready, waiting for an edge ; 0 = Timer1 gate single-pulse acquisition has completed or has not been started ; T1GCON(2) T1GVAL: Timer1 Gate Current State bit ; Indicates the current state of the Timer1 gate that could be provided to TMR1H:TMR1L. ; Unaffected by Timer1 Gate Enable (TMR1GE). ; T1GCON(1-0) T1GSS<1:0>: Timer1 Gate Source Select bits ; 11 = Reserved ; 10 = Comparator 1 optionally synchronized output (sync_C1OUT) ; 01 = Timer0 overflow output ; 00 = Timer1 gate pin ; ******************************************************* MOVLW B'00000000' MOVWF T1GCON ; ******************************************************* ; 3.Define TMR1 Register ; ******************************************************* ; TMR1 data ( OSC:16MHz, FOSC/4, 1:8 Prescale value, 0.01[S];100Hz ) ; 10mS(100Hz): 16MHz / 4(FOSC/4) / 8(Prescale) / 100(100Hz)==> 0d5,000(Count) ==> 0x1388(Count) ; TMR1 = 0xFFFF - 0x1388 ==> 0xEC77 ; TMR1 data ( OSC:16MHz, FOSC/4, 1:8 Prescale value, 0.1[S];10Hz ) ; 100mS(10Hz): 16MHz / 4(FOSC/4) / 8(Prescale) / 10(10Hz) ==> 0d50,000(Count) ==> 0xC350(Count) ; TMR1 = 0xFFFF - 0xC350 ==> 0x3CAF MOVLW H'3C' MOVWF TMR1H MOVLW H'AF' MOVWF TMR1L ; ******************************************************* ; 4.Timer ; ******************************************************* BCF PIR1,TMR1IF BTFSS PIR1,TMR1IF GOTO $-1 RETURN END