;****************************************************************************************** ; PIC12F1822 Sample Program ; Project : Laser Diode APC(Automatic Power Control) Sample Program ; Program Name : ; Programer : ; Create date : Version 1.0 H24.01.10 ; History : ; Laungage : MPLAB MPASM ;****************************************************************************************** ; Description ; (1)Sysytem Clock ; Internal OSC 16MHz ; (2)I/O Port ; PORTA(0/C1IN+) : Voltage Reference 0.2[V] ; PORTA(1/C1IN0-) : Laser Diode DL-3247 (PD) ; PORTA(2/C1OUT) : Laser Diode DL-3247 (LD) ; PORTA(3) : No define ; PORTA(4) : No define ; PORTA(5) : No define ;****************************************************************************************** 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 Parameter ; VREFBIT EQU H'0000' VPDBIT EQU H'0001' OUTLDBIT 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 ; ; Define Macro ; SELBANK MACRO #BANK_NO MOVLW #BANK_NO MOVWF BSR ENDM ; TRON MACRO BSF PORTA,OUTLDBIT NOP ENDM ; TROFF MACRO BCF PORTA,OUTLDBIT NOP 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 ; (5)Wait CALL TIMER ; ***************************** ; 2.Program main ; ***************************** MAIN ; (1)Check PD ; SELBANK 2 BTFSC CM1CON0,C1OUT GOTO MAIN2 GOTO MAIN1 ; (2)PD Low Level MAIN1 SELBANK 0 TRON GOTO MAIN ; (3)PD Hight Level MAIN2 SELBANK 0 TROFF GOTO MAIN ; ;****************************************************************************************** ; Function Name : INITCMP ; Title : Initialized comparator module ; Input : ; Output : ; Description : C1VP:C1IN+ / C1VN:C1IN0- ;****************************************************************************************** INITCMP ; ******************************************************* ; 1.Analog Channel Select ; ******************************************************* SELBANK 3 MOVLW B'00000011' ; Select Analog PIN ANS(0) and ANS(1) MOVWF ANSELA ; ******************************************************* ; 2.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'10010100' 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'00000000' ; FVR / C1IN0- MOVWF CM1CON1 ; ******************************************************* ; 4.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 ;************************************************************************************ ; Function Name : TIMER ; Title : Wait 0.001[S] ; Input : ; Output : ;************************************************************************************ TIMER ; ******************************************************* ; 1.Define T1CON(TIMER1 CONTROL REGISTER) Register ; T1CON(7-6) TMR1CS1,TMR1CS0: Timer1 Clock Source Select bits ; 11 =Timer1 clock source is Capacitive Sensing Oscillator (CAPOSC) ; 10 =Timer1 clock source is pin or oscillator: ; If T1OSCEN = 0: ; External clock from T1CKI pin (on the rising edge) ; If T1OSCEN = 1: ; Crystal oscillator on T1OSI/T1OSO pins ; 01 =Timer1 clock source is system clock (FOSC) ; 00 =Timer1 clock source is instruction clock (FOSC/4) ; T1CON(5-4) T1CKPS1,T1CKPS0: 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) T1OSCEN: LP Oscillator Enable Control bit ; 1 = Dedicated Timer1 oscillator circuit enabled ; 0 = Dedicated Timer1 oscillator circuit disabled ; T1CON(2) T1SYNC: Timer1 External Clock Input Synchronization Control bit ; TMR1CS1,TMR1CS0 = 1X ; 1 = Do not synchronize external clock input ; 0 = Synchronize external clock input with system clock (FOSC) ; TMR1CS1,TMR1CS0 = 0X ; This bit is ignored. Timer1 uses the internal clock when TMR1CS<1:0> = 1X. ; T1CON(1) Unimplemented: Read as e0f ; T1CON(0) TMR1ON: Timer1 On bit ; 1 = Enables Timer1 ; 0 = Stops Timer1 ; Clears Timer1 Gate flip-flop ; ******************************************************* SELBANK 0 ; SET BANK0 MOVLW B'00000001' 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 ; Timer1 gate flip-flop toggles on every rising edge. ; 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 ; This bit is automatically cleared when T1GSPM is cleared. ; 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) T1GSS1,T1GSS0: Timer1 Gate Source Select bits ; 00 = Timer1 Gate pin ; 01 = Timer0 overflow output ; 10 = Comparator 1 optionally synchronized output (SYNCC1OUT) ; 11 = Comparator 2 optionally synchronized output (SYNCC2OUT) ; ******************************************************* 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 ; TMR1 data ( OSC:16MHz, FOSC/4, 1:1 Prescale value, 0.001[S];1000Hz ) ; 1mS(1000Hz): 16MHz / 4(FOSC/4) / 1(Prescale) / 1000(Hz)==> 0d4,000(Count) ==> 0x0FA0(Count) ; TMR1 = 0xFFFF - 0x0FA0 ==> 0xF05F ; TMR1 data ( OSC:32MHz, FOSC/4, 1:2 Prescale value, 0.001[S];1000Hz ) ; 1mS(1000Hz): 32MHz / 4(FOSC/4) / 2(Prescale) / 1000(Hz)==> 0d4,000(Count) ==> 0x0FA0(Count) ; TMR1 = 0xFFFF - 0x0FA0 ==> 0xF05F MOVLW H'F0' MOVWF TMR1H MOVLW H'5F' MOVWF TMR1L ; ******************************************************* ; 4.Timer ; ******************************************************* BCF PIR1,TMR1IF BTFSS PIR1,TMR1IF GOTO $-1 RETURN END