;****************************************************************************************** ; PIC12F1822 Sample Program ; Project : ; Program Name : ; Programer : ; Create date : Version 1.0 H23.08.20 ; History : ; Laungage : MPLAB MPASM ;****************************************************************************************** ; Description ; (1)Sysytem Clock ; Internal OSC 16MHz ; (2)I/O Port ; PORTA(1/C1IN0-) : Voltage check ; PORTA(2) : Switching transistor driver ;****************************************************************************************** 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 VCHKBIT EQU H'0001' TRBIT EQU H'0002' ; ; Define GENERAL PURPOSE RAM AREA (80 bytes maximum) ; TMP_G EQU 20H ; ; Define Common RAM AREA (16 bytes) ; TMP_C EQU 70H F_STATUS EQU 71H ; ; Define Macro ; SELBANK MACRO #BANK_NO MOVLW #BANK_NO MOVWF BSR ENDM ; TRON MACRO BSF PORTA,TRBIT ENDM ; TROFF MACRO BCF PORTA,TRBIT 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 ANSELA (ALL Digital Port) SELBANK 3 ; SET BANK3 CLRF ANSELA ; (3)Define I/O port SELBANK 0 ; SET BANK0 CLRF PORTA SELBANK 1 ; SET BANK1 MOVLW B'00111011' MOVWF TRISA ; (4)Initialized comparator module CALL INITCMP SELBANK 0 ; SET BANK0 ; ***************************** ; 2.Program main ; ***************************** MAIN ; (1)Check Voltage Level SELBANK 2 BTFSC CM1CON0,C1OUT GOTO MAIN1 ; SELBANK 0 GOTO MAIN ; (2)Switching Program MAIN1 SELBANK 0 CALL PRG01 GOTO MAIN ; ;****************************************************************************************** ; Function Name : PRG01 ; Title : Switching Program ; Input : ; Output : ;****************************************************************************************** PRG01 ; (1)Switching-TR ON TRON ; (2)Interval Timer CALL WAT5U CALL WAT5U CALL WAT5U CALL WAT5U CALL WAT5U ; (3)Switching-TR OFF TROFF ; (4)Interval Timer CALL WAT5U CALL WAT5U RETURN ; ;****************************************************************************************** ; Function Name : WAT5U ; Title : Wait 5[uS] / 200KHz ; Input : ; Output : ; Description : System clock 16MHz ; 16MHz / 4(PIC) / 200KHz ==> 20(STEP) ;****************************************************************************************** WAT5U NOP NOP NOP NOP NOP NOP NOP NOP NOP NOP NOP NOP NOP NOP NOP NOP NOP NOP RETURN ; ;****************************************************************************************** ; Function Name : INITCMP ; Title : Initialized comparator module ; Input : ; Output : ; Description : C1VP:FVR / C1VN:C1IN0- ;****************************************************************************************** INITCMP ; ******************************************************* ; 1.Analog Channel Select ; ******************************************************* SELBANK 3 MOVLW B'00000010' ; Select Analog PIN ANS(1) MOVWF ANSELA ; ******************************************************* ; 2.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 MOVLW B'11001000' ; Comparator and DAC Fixed Voltage MOVWF FVRCON ; ******************************************************* ; 3.CM1CON0: COMPARATOR 1 CONTROL REGISTER 0 ; CM1CON0[7] CxON: Comparator Enable bit ; 1 = Comparator is enabled and consumes no active power ; 0 = Comparator is disabled ; CM1CON0[6] CxOUT: Comparator Output bit ; If CxPOL = 1 (inverted polarity): ; 1 = CxVP < CxVN ; 0 = CxVP > CxVN ; If CxPOL = 0 (non-inverted polarity): ; 1 = CxVP > CxVN ; 0 = CxVP < CxVN ; CM1CON0[5] CxOE: Comparator Output Enable bit ; 1 = CxOUT is present on the CxOUT pin. Requires that the associated TRIS bit be cleared to actually ; drive the pin. Not affected by CxON. ; 0 = CxOUT is internal only ; CM1CON0[4] CxPOL: Comparator Output Polarity Select bit ; 1 = Comparator output is inverted ; 0 = Comparator output is not inverted ; CM1CON0[3] Unimplemented: Read as e0f ; CM1CON0[2] CxSP: Comparator Speed/Power Select bit ; 1 = Comparator operates in normal power, higher speed mode ; 0 = Comparator operates in low-power, low-speed mode ; CM1CON0[1] CxHYS: Comparator Hysteresis Enable bit ; 1 = Comparator hysteresis enabled ; 0 = Comparator hysteresis disabled ; CM1CON0[0] CxSYNC: Comparator Output Synchronous Mode bit ; 1 = Comparator output to Timer1 and I/O pin is synchronous to changes on Timer1 clock source. ; Output updated on the falling edge of Timer1 clock source. ; 0 = Comparator output to Timer1 and I/O pin is asynchronous. ; ******************************************************* SELBANK 2 MOVLW B'10000100' ; MOVWF CM1CON0 ; ******************************************************* ; 3.CM1CON1: COMPARATOR CX CONTROL REGISTER 1 ; CM1CON1[7] CxINTP: Comparator Interrupt on Positive Going Edge Enable bits ; 1 = The CxIF interrupt flag will be set upon a positive going edge of the CxOUT bit ; 0 = No interrupt flag will be set on a positive going edge of the CxOUT bit ; CM1CON1[6] CxINTN: Comparator Interrupt on Negative Going Edge Enable bits ; 1 = The CxIF interrupt flag will be set upon a negative going edge of the CxOUT bit ; 0 = No interrupt flag will be set on a negative going edge of the CxOUT bit ; CM1CON1[5-4] CxPCH<1:0>: Comparator Positive Input Channel Select bits ; 00 = CxVP connects to CxIN+ pin ; 01 = CxVP connects to DAC Voltage Reference ; 10 = CxVP connects to FVR Voltage Reference ; CM1CON1[3-2] Unimplemented: Read as e0f ; CM1CON1[1-0] CxNCH<1:0>: Comparator Negative Input Channel Select bits ; PIC12F/LF1822: ; 0 = C1VN connects to C1IN0- pin ; 1 = C1VN connects to C1IN1- pin ; PIC16F/LF1823: ; 00 = CxVN connects to C12IN0- pin ; 01 = CxVN connects to C12IN1- pin ; 10 = CxVN connects to C12IN2- pin ; 11 = CxVN connects to C12IN3- pin ; ******************************************************* SELBANK 2 MOVLW B'00100000' ; FVR / C1IN0- MOVWF CM1CON1 ; ******************************************************* ; 3.CMOUT: COMPARATOR OUTPUT REGISTER ; CMOUT[7-2] Unimplemented: Read as e0f ; CMOUT[1] MC2OUT: Mirror Copy of C2OUT bit(1) ; CMOUT[0] MC1OUT: Mirror Copy of C1OUT bit ; Note 1: PIC16F/LF1823 only. ; ******************************************************* SELBANK 2 MOVLW B'00000000' ; MOVWF CMOUT SELBANK 0 RETURN END