
; CC5X Version 3.8, Copyright (c) B Knudsen Data
; C compiler for the PICmicro family
; ************   1. Sep 2022  19:48  *************

        processor  OB36A01A1
        radix  DEC

STATUS      EQU   0x03
PORTA       EQU   0x05
Carry       EQU   0
Zero_       EQU   2
RP0         EQU   5
RP1         EQU   6
ANSELA      EQU   0x07
ANSELB      EQU   0x08
PAM0        EQU   0x0F
PAM1        EQU   0x10
PBIM        EQU   0x11
ADCCON      EQU   0x18
ADCCS       EQU   0x19
ADCDH       EQU   0x1A
ADCDL       EQU   0x1B
SCON0       EQU   0x8E
SCON1       EQU   0x8F
SRELL       EQU   0x90
SRELH       EQU   0x91
SBUF        EQU   0x92
PB_0        EQU   0
ADCIF       EQU   6
TXIF        EQU   1
svrWREG     EQU   0x20
svrSTATUS   EQU   0x21
Data        EQU   0x7F
Count       EQU   0x22
i           EQU   0x23
Result      EQU   0x24
Above       EQU   0x26
Below       EQU   0x28
StandardVol EQU   0x2A
C1cnt       EQU   0x2C
C3cnt       EQU   0x2C

        GOTO main

  ; FILE .\ADC.c
                        ;#include "int16CXX.h"
                        ;
                        ;#pragma interruptSaveCheck  n
                        ;
                        ;
                        ;////////////////////////////////////////////////////////////////////////////////
                        ;// Interrupt ISR
                        ;////////////////////////////////////////////////////////////////////////////////
                        ;void Sub_WATCHDOG();
                        ;
                        ;void Sub_ALLINT(void);
                        ;
                        ;
                        ;#pragma unlockISR   // If the ISR address!=0x04, you will do this.
                        ;#pragma origin 4    // Define the following code at flash address 0x04
        ORG 0x0004
                        ;interrupt INTR_ISR( void)
                        ;{
INTR_ISR
                        ;    Sub_ALLINT();    
        CALL  Sub_ALLINT
                        ;}
        RETFIE
                        ;
                        ;/*******************************************************************************/
                        ;/*******************************************************************************/
                        ;void Sub_ALLINT()
                        ;{
Sub_ALLINT
                        ;    int_save_registers    // W, STATUS (and PCLATH)
        MOVWF svrWREG
        SWAPF STATUS,W
        BCF   0x03,RP0
        BCF   0x03,RP1
        MOVWF svrSTATUS
                        ;
                        ;   
                        ;    int_restore_registers // W, STATUS (and PCLATH)
        SWAPF svrSTATUS,W
        MOVWF STATUS
        SWAPF svrWREG,1
        SWAPF svrWREG,W
                        ;}
        RETURN
                        ;
                        ;/*******************************************************************************/
                        ;/*******************************************************************************/
                        ;void Initiate_UART()
                        ;{	
Initiate_UART
                        ;	SCON0=0x80;
        MOVLW 128
        BSF   0x03,RP0
        MOVWF SCON0
                        ;	SCON1=0x00;
        CLRF  SCON1
                        ;
                        ;	//9600
                        ;	SRELH=0x03;
        MOVLW 3
        MOVWF SRELH
                        ;	SRELL=0xF3;				
        MOVLW 243
        MOVWF SRELL
                        ;}
        RETURN
                        ;
                        ;/*******************************************************************************/
                        ;/*******************************************************************************/
                        ;void Send_Data(uns8 Data)
                        ;{
Send_Data
        BCF   0x03,RP0
        MOVWF Data
                        ;	SBUF=Data;
        MOVF  Data,W
        BSF   0x03,RP0
        MOVWF SBUF
                        ;	
                        ;	while(TXIF==0);		
m001    BSF   0x03,RP0
        BTFSS 0x8E,TXIF
        GOTO  m001
                        ;			
                        ;	TXIF=0;		
        BSF   0x03,RP0
        BCF   0x8E,TXIF
                        ;}
        RETURN
                        ;
                        ;/*******************************************************************************/
                        ;// Main
                        ;/*******************************************************************************/
                        ;void main() 
                        ;{   
main
                        ;	
                        ;	uns8 Count,i;
                        ;	uns16 Result,Above,Below,StandardVol;
                        ;
                        ;	//initiate GPIO
                        ;	ANSELA=0x00;
        BCF   0x03,RP0
        CLRF  ANSELA
                        ;	ANSELB=0x00;
        CLRF  ANSELB
                        ;	PAM0=0xff;
        MOVLW 255
        MOVWF PAM0
                        ;	PAM1=0xff;
        MOVLW 255
        MOVWF PAM1
                        ;	PBIM=0x03;
        MOVLW 3
        MOVWF PBIM
                        ;	
                        ;	PORTA=0xff;
        MOVLW 255
        MOVWF PORTA
                        ;	PB_0=1;	
        BSF   0x06,PB_0
                        ;	
                        ; 	Initiate_UART();
        CALL  Initiate_UART
                        ;	
                        ;	
                        ;	//-------------------------------------------------
                        ;	ADCCON = 0x4C;	//ADC ref. voltage=VDD + channel 4 (PB0)
        MOVLW 76
        BCF   0x03,RP0
        MOVWF ADCCON
                        ;	ADCCS =0x1F;
        MOVLW 31
        MOVWF ADCCS
                        ;	
                        ;	ANSELB = ANSELB |0x01;
        BSF   ANSELB,0
                        ;
                        ;	//-------------------------------------------------
                        ;	StandardVol=0x800;
        CLRF  StandardVol
        MOVLW 8
        MOVWF StandardVol+1
                        ;	
                        ;	
                        ;	//取8筆求平均
                        ;	for(Count=0;Count<16;Count++)
        CLRF  Count
m002    MOVLW 16
        BCF   0x03,RP0
        SUBWF Count,W
        BTFSC 0x03,Carry
        GOTO  m009
                        ;	{
                        ;		ADCCON |=0x80;		
        BSF   ADCCON,7
                        ;		
                        ;		while(ADCIF==0);
m003    BCF   0x03,RP0
        BTFSS 0x0C,ADCIF
        GOTO  m003
                        ;		
                        ;		ADCIF=0;		
        BCF   0x03,RP0
        BCF   0x0C,ADCIF
                        ;		
                        ;		Result =ADCDH;			
        MOVF  ADCDH,W
        MOVWF Result
        CLRF  Result+1
                        ;		Result <<=8;			
        MOVF  Result,W
        MOVWF Result+1
        CLRF  Result
                        ;		Result += ADCDL;
        MOVF  ADCDL,W
        ADDWF Result,1
        BTFSC 0x03,Carry
        INCF  Result+1,1
                        ;				
                        ;			
                        ;	
                        ;		//get the difference of startard voltage (1V, 0.5V)
                        ;		if(Result>StandardVol)	//>1V or 0.5V
        MOVF  Result+1,W
        SUBWF StandardVol+1,W
        BTFSS 0x03,Carry
        GOTO  m004
        BTFSS 0x03,Zero_
        GOTO  m005
        MOVF  Result,W
        SUBWF StandardVol,W
        BTFSC 0x03,Carry
        GOTO  m005
                        ;		{
                        ;			 Result= Result - StandardVol;
m004    BCF   0x03,RP0
        MOVF  StandardVol+1,W
        SUBWF Result+1,1
        MOVF  StandardVol,W
        SUBWF Result,1
        BTFSS 0x03,Carry
        DECF  Result+1,1
                        ;			 
                        ;			Above = Above + Result; 	
        MOVF  Result+1,W
        ADDWF Above+1,1
        MOVF  Result,W
        ADDWF Above,1
        BTFSC 0x03,Carry
        INCF  Above+1,1
                        ;		}
                        ;		else
        GOTO  m006
                        ;		{
                        ;			Result = StandardVol - Result;				
m005    BCF   0x03,RP0
        MOVF  Result+1,W
        SUBWF StandardVol+1,W
        MOVWF Result+1
        MOVF  Result,W
        SUBWF StandardVol,W
        MOVWF Result
        BTFSS 0x03,Carry
        DECF  Result+1,1
                        ;			Below = Below + Result;
        MOVF  Result+1,W
        ADDWF Below+1,1
        MOVF  Result,W
        ADDWF Below,1
        BTFSC 0x03,Carry
        INCF  Below+1,1
                        ;		}
                        ;		
                        ;		for(i=0;i<0xff;i++);	//delay				
m006    BCF   0x03,RP0
        CLRF  i
m007    BCF   0x03,RP0
        INCF  i,W
        BTFSC 0x03,Zero_
        GOTO  m008
        INCF  i,1
        GOTO  m007
                        ;	}
m008    BCF   0x03,RP0
        INCF  Count,1
        GOTO  m002
                        ;	
                        ;	//---------------------------------------------------------------				
                        ;	//求平均
                        ;	if(Above>Below)
m009    BCF   0x03,RP0
        MOVF  Above+1,W
        SUBWF Below+1,W
        BTFSS 0x03,Carry
        GOTO  m010
        BTFSS 0x03,Zero_
        GOTO  m012
        MOVF  Above,W
        SUBWF Below,W
        BTFSC 0x03,Carry
        GOTO  m012
                        ;	{
                        ;		Result = Above-Below;									 				
m010    BCF   0x03,RP0
        MOVF  Below+1,W
        SUBWF Above+1,W
        MOVWF Result+1
        MOVF  Below,W
        SUBWF Above,W
        MOVWF Result
        BTFSS 0x03,Carry
        DECF  Result+1,1
                        ;		Result=Result >>4;	
        MOVLW 4
        MOVWF C1cnt
m011    BCF   0x03,Carry
        BCF   0x03,RP0
        RRF   Result+1,1
        RRF   Result,1
        DECFSZ C1cnt,1
        GOTO  m011
                        ;		Result = Result + StandardVol;
        MOVF  StandardVol+1,W
        ADDWF Result+1,1
        MOVF  StandardVol,W
        ADDWF Result,1
        BTFSC 0x03,Carry
        INCF  Result+1,1
                        ;	}
                        ;	else
        GOTO  m014
                        ;	{
                        ;		Result =Below-Above;
m012    BCF   0x03,RP0
        MOVF  Above+1,W
        SUBWF Below+1,W
        MOVWF Result+1
        MOVF  Above,W
        SUBWF Below,W
        MOVWF Result
        BTFSS 0x03,Carry
        DECF  Result+1,1
                        ;		Result=Result >>4;	
        MOVLW 4
        MOVWF C3cnt
m013    BCF   0x03,Carry
        BCF   0x03,RP0
        RRF   Result+1,1
        RRF   Result,1
        DECFSZ C3cnt,1
        GOTO  m013
                        ;		Result = StandardVol-Result;
        MOVF  Result+1,W
        SUBWF StandardVol+1,W
        MOVWF Result+1
        MOVF  Result,W
        SUBWF StandardVol,W
        MOVWF Result
        BTFSS 0x03,Carry
        DECF  Result+1,1
                        ;	}		
                        ;	while(1)
                        ;	{
                        ;		;
                        ;	}
m014    GOTO  m014

        END


; *** KEY INFO ***

; 0x0006   10 word(s)  0 % : Sub_ALLINT
; 0x0004    2 word(s)  0 % : INTR_ISR
; 0x0010    9 word(s)  0 % : Initiate_UART
; 0x0019   11 word(s)  1 % : Send_Data
; 0x0024  155 word(s) 15 % : main

; RAM usage: 14 bytes (14 local), 50 bytes free
; Maximum call level: 1 (+2 for interrupt)
; Total of 188 code words (18 %)
