;****************************************************************************************** ; PIC12F1822 Sample Program ; Project : ; Program Name : ; Programer : ; Create date : Version 1.0 H23.02.20 ; History : ; Laungage : MPLAB MPASM ;****************************************************************************************** ; (1)Description ; OSC : Internal OSC 16MHz ; I/O Port : RA(0) DACOUT ;****************************************************************************************** LIST P=PIC12F1822,ST=OFF,R=DEC INCLUDE "P12F1822.inc" __CONFIG _CONFIG1 , _FOSC_INTOSC & _WDTE_OFF & _PWRTE_OFF & _MCLRE_OFF & _CP_OFF & _CPD_OFF & _BOREN_OFF & _CLKOUTEN_OFF & _IESO_OFF & _FCMEN_OFF __CONFIG _CONFIG2 , _WRT_OFF & _PLLEN_OFF & _STVREN_OFF & _BORV_25 & _LVP_OFF ; ; Define GENERAL PURPOSE RAM AREA (80 bytes maximum) ; TMP_G EQU 20H WAT1 EQU 21H WAT2 EQU 22H WAT3 EQU 23H ; ; Define Common RAM AREA (16 bytes) ; TMP_C EQU 70H ; Define Macro SELBANK MACRO #BANK_NO MOVLW #BANK_NO MOVWF BSR ENDM MPUTDAC MACRO #DACDAT MOVLW #DACDAT CALL PUTDAC CALL WAT ENDM ORG 0h GOTO START ORG 4h GOTO START START ; ***************************** ; 1.Initialized Segment ; ***************************** ; (1)Define OSC (16MHz, IntOSC) SELBANK 1 ; SET BANK1 MOVLW B'11111010' MOVWF OSCCON ; (2)Define I/O port SELBANK 1 ; SET BANK1 MOVLW B'11111111' MOVWF TRISA ; (3)Initialized D/A Converter CALL INITDAC SELBANK 0 ; SET BANK0 ; ***************************** ; 2.Program main ; ***************************** ; (1)Main loop MAIN MPUTDAC 0 MPUTDAC 1 MPUTDAC 2 MPUTDAC 3 MPUTDAC 4 MPUTDAC 5 MPUTDAC 6 MPUTDAC 7 MPUTDAC 8 MPUTDAC 9 MPUTDAC 10 MPUTDAC 11 MPUTDAC 12 MPUTDAC 13 MPUTDAC 14 MPUTDAC 15 MPUTDAC 16 MPUTDAC 17 MPUTDAC 18 MPUTDAC 19 MPUTDAC 20 MPUTDAC 21 MPUTDAC 22 MPUTDAC 23 MPUTDAC 24 MPUTDAC 25 MPUTDAC 26 MPUTDAC 27 MPUTDAC 28 MPUTDAC 29 MPUTDAC 30 MPUTDAC 31 GOTO MAIN ;****************************************************************************************** ; Program : INITDAC ; Title : ; Input : ; Output : ;****************************************************************************************** INITDAC ; ******************************************************* ; 1.Define DACCON0: VOLTAGE REFERENCE CONTROL REGISTER 0 ; DACCON0(7) DACEN: DAC Enable bit ; 1 = DAC is enabled ; 0 = DAC is disabled ; DACCON0(6)DACLPS: DAC Low-Power Voltage State Select bit ; 1 = DAC Positive reference source selected ; 0 = DAC Negative reference source selected ; DACCON0(5) DACOE: DAC Voltage Output Enable bit ; 1 = DAC voltage level is also an output on the DACOUT pin ; 0 = DAC voltage level is disconnected from the DACOUT pin ; DACCON0(4) Unimplemented: Read as e0f ; DACCON0(3-2) DACPSS<1:0>: DAC Positive Source Select bits ; 00 = VDD ; 01 = VREF+ ; 10 = FVR Buffer2 output ; 11 = Reserved, do not use ; DACCON0(1-0) Unimplemented: Read as e0f ; ******************************************************* SELBANK 2 ; SET BANK2 MOVLW B'10100000' MOVWF DACCON0 ; ******************************************************* ; 2.DACCON1: VOLTAGE REFERENCE CONTROL REGISTER 1 ; DACCON1(7-5) Unimplemented: Read as e0f ; DACCON1(4-0) DACR<4:0>: DAC Voltage Output Select bits ; VOUT = ((VSRC+) - (VSRC-))*(DACR<4:0>/(2**5)) + VSRC- ; ******************************************************* SELBANK 2 ; SET BANK2 MOVLW B'00000000' MOVWF DACCON1 ; ******************************************************* ; 3.FVRCON: FIXED VOLTAGE REFERENCE CONTROL REGISTER ; FVRCON(7) FVREN: Fixed Voltage Reference Enable bit ; 0 = Fixed Voltage Reference is disabled ; 1 = Fixed Voltage Reference is enabled ; FVRCON(6) FVRRDY: Fixed Voltage Reference Ready Flag bit (1) ; 0 = Fixed Voltage Reference output is not ready or not enabled ; 1 = Fixed Voltage Reference output is ready for use ; FVRCON(5-4) Reserved: Read as e0f. Maintain these bits clear. ; FVRCON(3-2) CDAFVR<1:0>: Comparator and DAC Fixed Voltage Reference Selection bits ; 00 = Comparator and DAC Fixed Voltage Reference Peripheral output is off ; 01 = Comparator and DAC Fixed Voltage Reference Peripheral output is 1x (1.024V) ; 10 = Comparator and DAC Fixed Voltage Reference Peripheral output is 2x (2.048V) (2) ; 11 = Comparator and DAC Fixed Voltage Reference Peripheral output is 4x (4.096V) (2) ; FVRCON(1-0) ADFVR<1:0>: ADC Fixed Voltage Reference Selection bits ; 00 = ADC Fixed Voltage Reference Peripheral output is off ; 01 = ADC Fixed Voltage Reference Peripheral output is 1x (1.024V) ; 10 = ADC Fixed Voltage Reference Peripheral output is 2x (2.048V) (2) ; 11 = ADC Fixed Voltage Reference Peripheral output is 4x (4.096V) (2) ; ; Note 1: FVRRDY is always e1f on devices with the LDO (PIC12F1822/16F1823). ; 2: Fixed Voltage Reference output cannot exceed VDD. ; ******************************************************* SELBANK 2 ; SET BANK2 MOVLW B'000000100' MOVWF FVRCON SELBANK 0 ; SET BANK0 RETURN ;****************************************************************************************** ; Program : PUTDAC ; Title : ; Input : W-reg (Decimal 0-31) ; Output : ;****************************************************************************************** PUTDAC MOVWF TMP_C SELBANK 2 ; SET BANK2 MOVFW TMP_C MOVWF DACCON1 SELBANK 0 ; SET BANK0 RETURN ;****************************************************************************************** ; Program : WAT ; Title : ; Input : ; Output : ;****************************************************************************************** WAT MOVLW H'0A' ; Decimal 10 MOVWF WAT2 WATB MOVLW H'64' ; Decimal 100 MOVWF WAT1 WATA NOP NOP NOP NOP NOP NOP DECFSZ WAT1,F GOTO WATA DECFSZ WAT2,F GOTO WATB RETURN END