;****************************************************************************************** ; PIC12F1822 Sample Program ; Project : ; Program Name : ; Programer : ; Create date : Version 1.0 H23.10.24 ; History : ; Laungage : MPLAB MPASM ;****************************************************************************************** ; Description ; (1)Sysytem Clock ; Internal OSC 16MHz ; (2)I/O Port ; PORTA(0) : LED 0 ; PORTA(1) : LED 1 ; PORTA(2) : LED 2 ; PORTA(4/AN3) : A/D Converter ;****************************************************************************************** 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 ; LED0BIT EQU H'0000' LED1BIT EQU H'0001' LED2BIT EQU H'0002' PI_VLT0 EQU D'180' ; 11.6V(180:11.6V*255/4/4.096) PI_VLT1 EQU D'187' ; 12.0V(187:12.0V*255/4/4.096) PI_VLT2 EQU D'193' ; 12.4V(193:12.4V*255/4/4.096) PI_VLT3 EQU D'199' ; 12.8V(199:12.8V*255/4/4.096) ; ; Define GENERAL PURPOSE RAM AREA (80 bytes maximum) ; TMP_G EQU 20H LPWAT1 EQU 21H ; ; Define Common RAM AREA (16 bytes) ; TMP_C EQU 70H ADCDAT EQU 71H ; ; Define Macro ; SELBANK MACRO #BANK_NO MOVLW #BANK_NO MOVWF BSR ENDM ; LED0ON MACRO BSF PORTA,LED0BIT ENDM ; LED1ON MACRO BSF PORTA,LED1BIT ENDM ; LED2ON MACRO BSF PORTA,LED2BIT ENDM ; LED0OFF MACRO BCF PORTA,LED0BIT ENDM ; LED1OFF MACRO BCF PORTA,LED1BIT ENDM ; LED2OFF MACRO BCF PORTA,LED2BIT ENDM ; ORG 0h GOTO START ORG 4h GOTO START START ; ***************************** ; 1.Initialize Segment ; ***************************** ; (1)Define OSC IntOSC: 16MHz 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'00111000' MOVWF TRISA ; (4)Initialized A/D coverter module CALL INTADC SELBANK 0 ; SET BANK0 ; ***************************** ; 2.Program main ; ***************************** MAIN ; (1)Get A/D data CALL GETADC ; (2)LED ON CALL PRG01 CALL WAT ; (3)LED Clear CALL LEDOFF GOTO MAIN ;****************************************************************************************** ; Program : PRG01 ; Title : ; Input : ; Output : ;****************************************************************************************** PRG01 ; 1.電圧がVOLT3より高い場合、全てのLEDを点灯する MOVFW ADCDAT MOVWF TMP_G MOVLW PI_VLT3 SUBWF TMP_G,F BTFSS STATUS,C GOTO PRG0102 LED2ON LED1ON LED0ON RETURN ; 2.電圧がVOLT2より高い場合、LED2を点灯する PRG0102 MOVFW ADCDAT MOVWF TMP_G MOVLW PI_VLT2 SUBWF TMP_G,F BTFSS STATUS,C GOTO PRG0101 LED2ON RETURN ; 3.電圧がVOLT1より高い場合、LED1を点灯する PRG0101 MOVFW ADCDAT MOVWF TMP_G MOVLW PI_VLT1 SUBWF TMP_G,F BTFSS STATUS,C GOTO PRG0100 LED1ON RETURN ; 4.電圧がVOLT0より高い場合、LED0を点灯する PRG0100 MOVFW ADCDAT MOVWF TMP_G MOVLW PI_VLT0 SUBWF TMP_G,F BTFSS STATUS,C GOTO PRG01OFF LED0ON RETURN ; 5.電圧がVOLT0以下の場合、LEDは点灯しない PRG01OFF RETURN ;****************************************************************************************** ; Program : LEDOFF ; Title : ; Input : ; Output : ;****************************************************************************************** LEDOFF LED0OFF LED1OFF LED2OFF RETURN ;****************************************************************************************** ; Function Name : WAT ; Title : Timer ; Input : ; Output : ;****************************************************************************************** WAT MOVLW H'0A' MOVWF LPWAT1 WATA NOP NOP NOP NOP NOP NOP NOP NOP DECFSZ LPWAT1,F GOTO WATA RETURN ;****************************************************************************************** ; Program : INTADC ; Title : ; Input : ; Output : ;****************************************************************************************** INTADC ; ******************************************************* ; 1.Analog Channel Select ; ******************************************************* SELBANK 3 MOVLW B'00010000' ; Select Analog PIN ANS(3) 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 ‘0’. 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 ‘1’ on devices with the LDO (PIC12F1822/16F1823). ; 2: Fixed Voltage Reference output cannot exceed VDD. ; ******************************************************* SELBANK 2 MOVLW B'11000011' ; ADC Fixed Voltage Reference Peripheral output is 4x (4.096V) MOVWF FVRCON ; ******************************************************* ; 3.Define ADCON0(A/D CONTROL REGISTER 0) Register ; ADCON0(7) Unimplemented: Read as ‘0’ ; ADCON0(6-2) CHS<4:0>: Analog Channel Select bits ; 00000 = AN0 ; 00001 = AN1 ; 00010 = AN2 ; 00011 = AN3 ; 00100 = AN4(1) ※PIC16F/LF1823 only. For PIC12F/LF1822 it is “Reserved. No channel connected” ; 00101 = AN5(1) ※PIC16F/LF1823 only. For PIC12F/LF1822 it is “Reserved. No channel connected” ; 00110 = AN6(1) ※PIC16F/LF1823 only. For PIC12F/LF1822 it is “Reserved. No channel connected” ; 00111 = AN7(1) ※PIC16F/LF1823 only. For PIC12F/LF1822 it is “Reserved. No channel connected” ; 10001 = Reserved ; ・ ; ・ ; ・ ; 11101 = Reserved ; 11110 = DAC ; 11111 = FVR ; ADCON0(1) GO/DONE: A/D Conversion Status bit ; 1 = A/D conversion cycle in progress. Setting this bit starts an A/D conversion cycle. ; This bit is automatically cleared by hardware when the A/D conversion has completed. ; 0 = A/D conversion completed/not in progress ; ADCON0(0) ADON: ADC Enable bit ; 1 = ADC is enabled ; 0 = ADC is disabled and consumes no operating current ; ******************************************************* SELBANK 1 MOVLW B'00001101' MOVWF ADCON0 ; ******************************************************* ; 4.Define ADCON1(A/D CONTROL REGISTER 1) Register ; ADCON1(7) ADFM: A/D Result Format Select bit ; 1 = Right justified. Six Most Significant bits of ADRESH are set to ‘0’ when the conversion result is loaded. ; 0 = Left justified. Six Least Significant bits of ADRESL are set to ‘0’ when the conversion result is loaded. ; ADCON1(6-4) ADCS<2:0>: A/D Conversion Clock Select bits ; 000 = FOSC/2 ; 001 = FOSC/8 ; 010 = FOSC/32 ; 011 = FRC (clock supplied from a dedicated RC oscillator) ; 100 = FOSC/4 ; 101 = FOSC/16 ; 110 = FOSC/64 ; 111 = FRC (clock supplied from a dedicated RC oscillator) ; ADCON1(3-2) Unimplemented: Read as ‘0’ ; ADCON1(1-0) ADPREF<1:0>: A/D Positive Voltage Reference Configuration bits ; 00 = VREF+ is connected to AVDD ; 01 = Reserved ; 10 = VREF+ is connected to external VREF+ ; 11 = VREF+ is connected to internal fixed voltage reference ; ******************************************************* SELBANK 1 MOVLW B'00100011' MOVWF ADCON1 SELBANK 0 RETURN ;****************************************************************************************** ; Function Name : GETADC ; Title : ; Input : ; Output : ADCDAT ;****************************************************************************************** GETADC: ; ***************** ; 1.Start A/D Converter ; ***************** ; (1)GO/DONE: A/D Conversion Status bit SELBANK 1 BSF ADCON0,GO_NOT_DONE ; ***************** ; 2.Get data (8 bit) ; ***************** ; (1)ADCの開始フラグが完了するまで待つ SELBANK 1 BTFSC ADCON0,GO_NOT_DONE GOTO $-1 ; (2)結果を代入 SELBANK 1 MOVF ADRESH,W MOVWF ADCDAT SELBANK 0 RETURN END