;
;
;	Digital multi mater "MODOKI" 0.1.0
;		for valiable power supply
;								2006.01.21
;
		INCLUDE		P16F819.INC
TRUE		EQU   1
FALSE		EQU   0
;P16C5X      SET    FALSE       ; If P16C5X, use INHX8M file format.
;P16CXX      SET    TRUE        ; If P16CXX, use INHX8M file format.
;P17CXX      SET    FALSE       ; If P17CXX, the INHX32 file format is required
;                              ;            in the LIST directive
;RESET_V     SET    0x0000      ; Default Reset Vector address of 0h
                               ;   (16Cxx and 17Cxx devices)
P16_MAP1    SET    FALSE       ; FOR 16C60/61/70/71/84 Memory Map
P16_MAP2    SET    TRUE	       ; For all other 16Cxx Memory Maps
		INCLUDE <MATH16.INC>	; PIC16 math library definitions
		ERRORLEVEL	-302,-306
		__CONFIG	_CP_OFF & _CCP1_RB2 & _DEBUG_OFF & _WRT_ENABLE_OFF & _CPD_OFF & _LVP_OFF & _BODEN_ON & _MCLR_OFF & _PWRTE_OFF & _WDT_OFF & _INTRC_IO

;===============================
;         Define
;===============================
LCD_SIG_RS	EQU		4			; PORTA bit4
LCD_SIG_RW	EQU		6			; PORTA bit6
LCD_SIG_E	EQU		7			; PORTA bit7

;===============================
;         Work area
;===============================
		CBLOCK	0x0040
ISR_W
ISR_S
WAITCTR
WAITCTR2
LCD_STATUS
LCD_WORK
LCD_WORK2
AN0VALH
AN0VALL
AN1VALH
AN1VALL
AN2VALH
AN2VALL
AN0VAL
AN1VAL
AN2VAL
ANYCMD
ANYVAL1
ANYVAL2
ANYVAL3
ANYVALS
ANYVALH
ANYVALL
ANXVAL
DECVAL0
DECVAL1
DECVAL2
DECVAL3
DECVAL4
DECVAL5
DECVAL6
DECVAL7
DECVAL8
DECVAL9
DECVALC
		ENDC

;===============================
;         Vector area
;===============================
		ORG		0x0000
		GOTO  	START

		ORG		0x0004
		GOTO  	ISR


;===============================
;     Table area (5-255word)
;===============================
		ORG		0x005
HEXCHAR1
		ANDLW	B'00001111'
		ADDWF	PCL,F
		RETLW	"0"
		RETLW	"1"
		RETLW	"2"
		RETLW	"3"
		RETLW	"4"
		RETLW	"5"
		RETLW	"6"
		RETLW	"7"
		RETLW	"8"
		RETLW	"9"
		RETLW	"A"
		RETLW	"B"
		RETLW	"C"
		RETLW	"D"
		RETLW	"E"
		RETLW	"F"



		ORG	0x0100

;===============================
;   Intrrupt service routine
;===============================
ISR
		MOVWF	ISR_W			; House keep
		SWAPF	STATUS,W
		CLRF	STATUS
		MOVWF	ISR_S
ISR10
		BTFSC	INTCON,RBIF
		GOTO	ISR_RBIF
		BTFSC	INTCON,INTF
		GOTO	ISR_INTF
		BTFSC	INTCON,TMR0IF
		GOTO	ISR_TMR0IF
		BTFSC	PIR1,TMR1IF
		GOTO	ISR_TMR1IF
		BTFSC	PIR1,TMR2IF
		GOTO	ISR_TMR2IF
		BTFSC	PIR1,CCP1IF
		GOTO	ISR_CCP1IF
		BTFSC	PIR1,SSPIF
		GOTO	ISR_SSPIF
		BTFSC	PIR1,ADIF
		GOTO	ISR_ADIF
		BTFSC	PIR2,EEIF
		GOTO	ISR_EEIF
ISR20							; Other interrupt
		GOTO	ISR90


ISR_RBIF
		BCF		INTCON,RBIF
		GOTO	ISR90
ISR_INTF
		BCF		INTCON,INTF
		GOTO	ISR90
ISR_TMR0IF
		BCF		INTCON,TMR0IF
		GOTO	ISR90
ISR_TMR1IF
		BCF		PIR1,TMR1IF
		GOTO	ISR90
ISR_TMR2IF
		BCF		PIR1,TMR2IF
		GOTO	ISR90
ISR_CCP1IF
		BCF		PIR1,CCP1IF
		GOTO	ISR90
ISR_SSPIF
		BCF		PIR1,SSPIF
		GOTO	ISR90
ISR_ADIF
		BCF		PIR1,ADIF
		GOTO	ISR90
ISR_EEIF
		BCF		PIR2,EEIF
		GOTO	ISR90

ISR90
		SWAPF	ISR_S,W			; House keep
		MOVWF	STATUS
		SWAPF	ISR_W,F
		SWAPF	ISR_W,W
		RETFIE

;===============================
;         Init
;===============================
START
;
; === Initialize ===
;
;		*** System clock ***
;
;				  IOC - Interrupt-on-change GPIO register
;				  -000-0--  Initial
;				  .XXX....  IRCF: Internal oscillator
;				  .000....     31.25KHz
;				  .001....     125KHz
;				  .010....     250KHz
;				  .011....     500KHz
;				  .100....     1MHz
;				  .101....     2MHz
;				  .110....     4MHz
;				  .111....     8MHz
;				  .....X..  IOFS :INTOSC Frequency stable bit
;				  .....1..     stable
;				  .....0..     not stable
		MOVLW	B'01100000'		; 4MHz
		BSF		STATUS,RP0		; Bank 1 switch
		BCF		STATUS,RP1		;
		MOVWF	OSCCON			; OSC Control

;		*** System option ***
;
;				  OPTION_REG - Option register
;				  11111111  Initial
;				  X.......  RBPU: GPIO Pull-up enable
;				  1.......     disabled
;				  0.......     enable
;				  .X......  INTEDG: Interrupt edge select
;				  .1......     riging edge
;				  .0......     failling edge
;				  ..X.....  T0CS: TMR0 clock source select
;				  ..1.....     Transition on GP2/T0CKI pin
;				  ..0.....     Internal instruction cycle
;				  ...X....  T0SE: TMR0 source edge select
;				  ...1....     high-to-low edge
;				  ...0....     low-to-high edge
;				  ....X...  PSA: Prescaler assignment
;				  ....1...     WDT
;				  ....0...     Timer0
;				  .....XXX  PS:  Prescaler assignment
;				  .....000     TMR0 rate 1:2 / WDT rate 1:1
;				  .....001               1:4 /          1:2
;				  .....010               1:8 /          1:4
;				  .....011              1:16 /          1:8
;				  .....100              1:32 /         1:16
;				  .....101              1:64 /         1:32
;				  .....110             1:128 /         1:64
;				  .....111             1:256 /        1:128
		MOVLW	B'11011111'		; Timer0
		BSF		STATUS,RP0		; Bank 1 switch
		BCF		STATUS,RP1		;
		MOVWF	OPTION_REG

;		*** A/D Converter ***
;
;				  ADCON0 - A/D control register 0
;				  000000-0  Initial
;				  XX......  ADCS1: A/D converter clock select
;				  00......    ADCS2=0 Fosc/2   ADCS2=1 Fosc/4
;				  01......            Fosc/8           Fosc/16
;				  10......            Fosc/32          Fosc/64
;				  11......            FRC              FRC
;				  ..XXX...  CHS: Analog channel select bits
;				  ..000...    Channel 0 AN0
;				  ..001...    Channel 1 AN1
;				  ..010...    Channel 2 AN2
;				  ..011...    Channel 3 AN3
;				  ..100...    Channel 4 AN4
;				  .....X..  GO/DONE: A/D conversion status bit
;				  .....1..    ADON=1 in progress
;				  .....0..           not in progress
;				  .......X  ADON: A/D on bit
;				  .......1    module is operating
;				  .......0    module is shutoff
		MOVLW	B'11000000'		; FRC AN0
		BCF		STATUS,RP0		; Bank 0 switch
		BCF		STATUS,RP1		;
		MOVWF	ADCON0			;
;
;				  ADCON1 - A/D control register 1
;				  00--0000  Initial
;				  X.......  ADFM: A/D result format select bit
;				  1.......    Right justified
;				  0.......    Left justified
;				  .X......  ADCS2: A/D clock divied by 2 select bit
;				  .1......    Enabled
;				  .0......    Disabled
;				  ....XXXX  PCFG: A/D port configuration control bits
;				  ........    AN4 AN3 AN2 AN1 AN0 Vref+ Vref- C/R   (A=Analog D=Digital I/O)
;				  ....0000     A   A   A   A   A   AVdd  AVss 5/0
;				  ....0001     A   +   A   A   A   AN3   AVss 4/1
;				  ....0010     A   A   A   A   A   AVdd  AVss 5/0
;				  ....0011     A   +   A   A   A   AN3   AVss 4/1
;				  ....0100     D   A   D   A   A   AVdd  AVss 3/0
;				  ....0101     D   +   D   A   A   AN3   AVss 2/1
;				  ....011x     D   D   D   D   D   AVdd  AVss 0/0
;				  ....1000     A   +   -   A   A   AN3   AN2  3/2
;				  ....1001     A   A   A   A   A   AVdd  AVss 5/0
;				  ....1010     A   +   A   A   A   AN3   AVss 4/1
;				  ....1011     A   +   -   A   A   AN3   AN2  3/2
;				  ....1100     A   +   -   A   A   AN3   AN2  3/2
;				  ....1101     D   +   -   A   A   AN3   AN2  2/2
;				  ....1110     D   D   D   D   A   AVdd  AVss 1/0
;				  ....1111     D   +   -   D   A   AN3   AN2  1/2
		MOVLW	B'10000100'		; AN3+1+0 analog port, Vref=Vdd:Vss
		BSF		STATUS,RP0		; Bank 1 switch
		BCF		STATUS,RP1		;
		MOVWF	ADCON1			;

;		*** GPIO port ***
;
;				  PORTA - General purpose I/O register A
;                 xxx00000  Initial
;				  X.......  RA7: OSC1/CLKI
;				  .X......  RA6: OSC2/CLKO
;				  ..X.....  RA5: MCLR/Vpp
;				  ...X....  RA4: AN4/T0CKI
;				  ....X...  RA3: AN3/Vref+
;				  .....X..  RA2: AN2/Vref-
;				  ......X.  RA1: AN1
;				  .......X  RA0: AN0
		MOVLW	B'00000000'		; Initial all off
		BCF		STATUS,RP0		; Bank 0 switch
		BCF		STATUS,RP1		;
		MOVWF	PORTA
;
;				  PORTB - General purpose I/O register B
;                 xxxxxxxx  Initial
;				  X.......  RB7: T1OSI/PGD
;				  .X......  RB6: T1OSO/T1CKI/PGC
;				  ..X.....  RB5: SS
;				  ...X....  RB4: SCK/SCL
;				  ....X...  RB3: CCP1/PGM
;				  .....X..  RB2: SDO/CCP1
;				  ......X.  RB1: SDI/SDA
;				  .......X  RB0: INT
		MOVLW	B'00000000'		; Initial all off
		MOVWF	PORTB			; 
;
;				  TRISA - GPIO tri-state register A
;                 11111111  Initial
;				  X.......  TRISA7: RA7 1=input 0=output
;				  .X......  TRISA6: RA6 1=input 0=output
;				  ..1.....  TRISA5: RA5 1=input only
;				  ...X....  TRISA4: RA4 1=input 0=output
;				  ....X...  TRISA3: RA3 1=input 0=output
;				  .....X..  TRISA2: RA2 1=input 0=output
;				  ......X.  TRISA1: RA1 1=input 0=output
;				  .......X  TRISA0: RA0 1=input 0=output
		MOVLW	B'00101111'		; RA7,6,4=output
		BSF		STATUS,RP0		; Bank 1 switch
		BCF		STATUS,RP1		;
		MOVWF	TRISA			;
;
;				  TRISB - GPIO tri-state register B
;                 11111111  Initial
;				  X.......  TRISB7: RB7 1=input 0=output
;				  .X......  TRISB6: RB6 1=input 0=output
;				  ..X.....  TRISB5: RB5 1=input 0=output
;				  ...X....  TRISB4: RB4 1=input 0=output
;				  ....X...  TRISB3: RB3 1=input 0=output
;				  .....X..  TRISB2: RB2 1=input 0=output
;				  ......X.  TRISB1: RB1 1=input 0=output
;				  .......X  TRISB0: RB0 1=input 0=output
		MOVLW	B'00000000'		; Dynamic change
		MOVWF	TRISB			;

;		*** Timer 0 ***
;
;		TMR0 30.27Hz=33.0ms (31KHz clock)
;		TMR0 122.07Hz=8.2ms (125KHz clock)
;		TMR0 244.14Hz=4.1ms (250KHz clock)
;		TMR0 488.28Hz=2.0ms (500KHz clock)
;		TMR0 976.56Hz=1.0ms (1MHz clock)


;		*** Timer 1 ***
;
TMR1V	EQU		D'819'			; 100ms, Fosc(32k)/4 unit
TMR1VH	EQU		(-TMR1V >> 8) & H'FF'
TMR1VL	EQU		-TMR1V & H'FF'

		BCF		STATUS,RP0		; Bank 0 swtch
		BCF		STATUS,RP1		;
		MOVLW	TMR1VH			; 
		MOVWF	TMR1H			; 
		MOVLW	TMR1VL			; 
		MOVWF	TMR1L			; 
;				  T1CON - Timer1 control register
;                 00000000  Initial
;				  ..XX....  T1CKPS: Input clock prescale
;				  ..11....     1:8
;				  ..10....     1:4
;				  ..01....     1:2
;				  ..00....     1:1
;				  ....X...  T1OSCEN:Timer1 oscillator enable control bit
;				  ....1...     enabled 
;				  ....0...     shutoff
;				  .....X..  T1SYNC: External clock synchronization control
;				  .....1..     Do not sync  TMR1CS=1 
;				  .....0..     sync         TMR1CS=1
;				  ......X.  TMR1CS: Clock source select
;				  ......1.     External - T1CKI pin
;				  ......0.     Internal - Fosc/4
;				  .......X  TMR1ON: Timer1 on bit
;				  .......1     enable
;				  .......0     stop
		MOVLW	B'00000000'		; off
		BCF		STATUS,RP0		; Bank 0 switch
		BCF		STATUS,RP1		;
		MOVWF	T1CON

;		*** Timer 2 ***
;
		BCF		STATUS,RP0		; Bank 0 switch
		BCF		STATUS,RP1		;
		MOVLW	D'0'			;
		MOVWF	TMR2
;
		BSF		STATUS,RP0		; Bank 1 switch
		BCF		STATUS,RP1		;
		MOVLW	D'255'			;
		MOVWF	PR2
;
;				  T2CON - Timer2 control register
;                 00000000  Initial
;				  .XXXX...  TOUTPS: Timer2 output postscale select bits
;				  .0000...     1:1
;				  .0001...     1:2
;				     :
;				  .1111...     1:16
;				  .....X..  TMR2ON: Timer2 on bit
;				  .....1..     on
;				  .....0..     off
;				  ......XX  T2CKPS: Timer2 clock prescale select bits
;				  ......00     Prescaler is 1
;				  ......01     Prescaler is 4
;				  ......1x     Prescaler is 16
		MOVLW	B'00000000'		; off
		BCF		STATUS,RP0		; Bank 0 switch
		BCF		STATUS,RP1		;
		MOVWF	T2CON

;		*** CCP ***
;
CCPR1V	EQU		D'0'			; 
CCPR1VH	EQU		(-CCPR1V >> 8) & H'FF'
CCPR1VL	EQU		-CCPR1V & H'FF'

		BCF		STATUS,RP0		; Bank 0 swtch
		BCF		STATUS,RP1		;
		MOVLW	CCPR1VH			; 
		MOVWF	CCPR1H			; 
		MOVLW	CCPR1VL			; 
		MOVWF	CCPR1L			; 
;
;				  CCP1CON - CCP control register 1
;                 00000000  Initial
;				  ..XX....  CCP1X, CCP1Y: PWM Least significant bits
;				  ....XXXX  CCP1M: CCP1 Mode select bits
;				  ....0000     disable(reset)
;				  ....0100     Capture mode, every failing edge
;				  ....0101     Capture mode, every riging edge
;				  ....0110     Capture mode, every 4th edge
;				  ....0111     Capture mode, every 16th edge
;				  ....1000     Compare mode, set output match
;				  ....1001     Compare mode, clear output match
;				  ....1010     Compare mode, generate software intrrupt on match
;				  ....1011     Compare mode, trigger special event
;				  ....110x     PWM mode active high
;				  ....111x     PWM mode active low
		MOVLW	B'00000000'		; off
		BCF		STATUS,RP0		; Bank 0 switch
		BCF		STATUS,RP1		;
		MOVWF	CCP1CON

;		*** Interrupt ***
;
;				  PIE1 - Peripheral interrupt enable register 1
;                 -0--0000  Initial
;							0=disable 1=enable
;				  .X......  ADIE  : A/D Converter interrupt
;				  ....X...  SSPIE : Synchronous serial port interrupt
;				  .....X..  CCP1IE: CCP1 interrupt
;				  ......X.  TMR2IE: Timer2 to PR2 match
;				  .......X  TMR1IE: Timer1 overflow
		MOVLW	B'00000000'		; Off
		BSF		STATUS,RP0		; Bank 1 switch
		BCF		STATUS,RP1		;
		MOVWF	PIE1
;
;				  PIE2 - Peripheral interrupt enable register 2
;                 ---0----  Initial
;							0=disable 1=enable
;				  ...X....  EEIE  : EEPROM Write complete
		MOVLW	B'00000000'		; Off
		BSF		STATUS,RP0		; Bank 1 switch
		BCF		STATUS,RP1		;
		MOVWF	PIE2
;
;				  INTCON - Interrupt control register
;                 0000000x  Initial
;							0=disable 1=enable
;				  X.......  GIE   : Global interrupt
;				  .X......  PEIE  : Peripheral intterupt -> PIE1
;				  ..X.....  TMR0IE: TMR0 Overflow interrupt
;				  ...X....  INTE  : P2/INT external interrupt
;				  ....X...  RBIE  : RB Change interrupt -> IOC
;							0=not(Appl reset) 1=occurred
;				  .....X..  TMR0IF: TMR0 Overflow interrupt flag
;				  ......X.  INTF  : GP2/INT external interrupt flag
;				  .......X  RBIF  : RB Change interrupt flag
		MOVLW	B'00000000'		; off
		BCF		STATUS,RP0		; Bank 0 swtch
		BCF		STATUS,RP1		;
		MOVWF	INTCON

;		*** Ready ***
;
		BCF		STATUS,RP0		; Bank 0 swtch
		BCF		STATUS,RP1		;

;		*** LCD Initalyze ***
;
		MOVLW	D'255'		; 255ms
		CALL	WAIT
		CALL	LCD_INIT

;===============================
;     Main
;===============================
MAIN
		MOVLW	0x80			; Line 0 Culumn 0
		CALL	LCD_PUTCMD
		MOVLW	"I"
		CALL	LCD_PUTCHAR
		MOVLW	"n"
		CALL	LCD_PUTCHAR
		MOVLW	"p"
		CALL	LCD_PUTCHAR
		MOVLW	"u"
		CALL	LCD_PUTCHAR
		MOVLW	"t"
		CALL	LCD_PUTCHAR

		MOVLW	0x8e			; Line 0 Culumn 11
		CALL	LCD_PUTCMD
		MOVLW	"V"
		CALL	LCD_PUTCHAR

		MOVLW	0xc0			; Line 1 Culumn 0
		CALL	LCD_PUTCMD
		MOVLW	"O"
		CALL	LCD_PUTCHAR
		MOVLW	"u"
		CALL	LCD_PUTCHAR
		MOVLW	"t"
		CALL	LCD_PUTCHAR
		MOVLW	"p"
		CALL	LCD_PUTCHAR
		MOVLW	"u"
		CALL	LCD_PUTCHAR
		MOVLW	"t"
		CALL	LCD_PUTCHAR

		MOVLW	0xce			; Line 1 Culumn 11
		CALL	LCD_PUTCMD
		MOVLW	"V"
		CALL	LCD_PUTCHAR

;		MOVLW	0x8c			; Line 0 Culumn 12
;		CALL	LCD_PUTCMD
;		MOVLW	"A"
;		CALL	LCD_PUTCHAR
;		MOVLW	"N"
;		CALL	LCD_PUTCHAR
;		MOVLW	"2"
;		CALL	LCD_PUTCHAR

MAINLOOP
		CLRWDT

		MOVLW	D'255'		; 255ms
		CALL	WAIT

		CALL	AN0IN
		CALL	AN1IN

		MOVF	AN0VALH,W
		MOVWF	AARGB0
		MOVF	AN0VALL,W
		MOVWF	AARGB1
		CALL	ADVALCNV	; A/D scale -> mV unit
		MOVLW	0x87		; Line 0 Culumn 7
		CALL	LCD_PUTCMD
		CALL	BINDEC		; bin -> decimal char
		CALL	PUTDEC		; display

		MOVF	AN1VALH,W
		MOVWF	AARGB0
		MOVF	AN1VALL,W
		MOVWF	AARGB1
		CALL	ADVALCNV	; A/D scale -> mV unit
		MOVLW	0xc7		; Line 1 Culumn 7
		CALL	LCD_PUTCMD
		CALL	BINDEC		; bin -> decimal char
		CALL	PUTDEC		; display

		GOTO	MAINLOOP

;=--------------------------
		CALL	AN0IN
		CALL	AN1IN
		CALL	AN2IN

		MOVLW	0xc0		; Line 1 Culumn 0
		CALL	LCD_PUTCMD
;		MOVF	AN0VAL,W
;		CALL	LCD_PUTHEX
		MOVF	AN0VALH,W
		CALL	LCD_PUTHEX
		MOVF	AN0VALL,W
		CALL	LCD_PUTHEX

		MOVLW	0xc6		; Line 1 Culumn 6
		CALL	LCD_PUTCMD
;		MOVF	AN1VAL,W
;		CALL	LCD_PUTHEX
		MOVF	AN1VALH,W
		CALL	LCD_PUTHEX
		MOVF	AN1VALL,W
		CALL	LCD_PUTHEX

		MOVLW	0xcc		; Line 1 Culumn 12
		CALL	LCD_PUTCMD
;		MOVF	AN2VAL,W
;		CALL	LCD_PUTHEX
		MOVF	AN2VALH,W
		CALL	LCD_PUTHEX
		MOVF	AN2VALL,W
		CALL	LCD_PUTHEX


;===============================
;     Decimal display  xx.xxx
;===============================
PUTDEC
		MOVLW	DECVAL0
		MOVWF	FSR				;pointer = address of smallest digit
		MOVLW	D'7'			;load counter with the number of
		MOVWF	DECVALC			;significant figures the decimal number	
PUTDEC10
		MOVF	INDF,W
		SUBLW	"0"
		BTFSS	STATUS,Z
		GOTO	PUTDEC20
		MOVLW	" "
		MOVWF	INDF
		INCF	FSR,F
		DECFSZ	DECVALC,F
		goto	PUTDEC10
PUTDEC20
		MOVF	DECVAL5,W
		CALL	LCD_PUTCHAR
		MOVF	DECVAL6,W
		CALL	LCD_PUTCHAR
		MOVLW	"."
		CALL	LCD_PUTCHAR
		MOVF	DECVAL7,W
		CALL	LCD_PUTCHAR
		MOVF	DECVAL8,W
		CALL	LCD_PUTCHAR
		MOVF	DECVAL9,W
		CALL	LCD_PUTCHAR

		RETURN

;===============================
;     Bin to Decimal 
;		AARGB0:3 -> DECVAL0:9
;===============================
BINDEC
		movlw	DECVAL9
		movwf	FSR				;pointer = address of smallest digit
		movlw	D'10'			;load counter with the number of
		movwf	DECVALC			;significant figures the decimal number	

BINDEC10
		clrf	BARGB0			;Make the divisor 10.  
		movlw	d'10'
		movwf	BARGB1

		call	FXD3216U		;divide (32 bit fixed) / 10 (to get remainder)
	
		movf	REMB1,w			;put remainder in w register
		movwf	INDF			;put number into appropriate digit position

		movlw	"0"
		addwf	INDF,f			;add 30H to decimal number to convert to ASCII

		decf	FSR,f			;move pointer to next digit 	

		decfsz	DECVALC,f
		goto	BINDEC10

		RETURN

;===============================
;     A/D value -> Vol
;		AARGB0:1 * 200000 / 1024 -> AARGB0:3
;===============================
ADVALCNV

ADCNV1	EQU		D'20000'		; 20V * 10^3
ADCNV1H	EQU		(ADCNV1 >> 8) & H'FF'
ADCNV1L	EQU		ADCNV1 & H'FF'
		MOVLW	ADCNV1H
		MOVWF	BARGB0
		MOVLW	ADCNV1L
		MOVWF	BARGB1
		CALL	FXM1616S

ADCNV2	EQU		D'1024'
ADCNV2H	EQU		(ADCNV2 >> 8) & H'FF'
ADCNV2L	EQU		ADCNV2 & H'FF'
		MOVLW	ADCNV2H
		MOVWF	BARGB0
		MOVLW	ADCNV2L
		MOVWF	BARGB1
		CALL	FXD3216S

		RETURN

;===============================
;     Analog port sampling
;===============================
AN0IN
		MOVLW	B'11000001'		; FRC, AN0
;		CALL	ANYIN
;		MOVWF	AN0VAL
		CALL	ANXIN
		MOVWF	AN0VALH
		BSF		STATUS,RP0		; Bank 1 switch
		MOVF	ADRESL,W
		BCF		STATUS,RP0		; Bank 0 switch
		MOVWF	AN0VALL
		RETURN
AN1IN
		MOVLW	B'11001001'		; FRC, AN1
;		CALL	ANYIN
;		MOVWF	AN1VAL
		CALL	ANXIN
		MOVWF	AN1VALH
		BSF		STATUS,RP0		; Bank 1 switch
		MOVF	ADRESL,W
		BCF		STATUS,RP0		; Bank 0 switch
		MOVWF	AN1VALL
		RETURN
AN2IN
		MOVLW	B'11010001'		; FRC, AN2
;		CALL	ANYIN
;		MOVWF	AN2VAL
		CALL	ANXIN
		MOVWF	AN2VALH
		BSF		STATUS,RP0		; Bank 1 switch
		MOVF	ADRESL,W
		BCF		STATUS,RP0		; Bank 0 switch
		MOVWF	AN2VALL
		RETURN

; Simple sampling
ANXIN
		MOVWF	ADCON0			; Write sampling start command
;		NOP
;		NOP
		MOVLW	D'6'
		CALL	WAIT

		BSF		ADCON0,GO		; Start conversion
		BTFSC	ADCON0,GO		; Is conversion done?
		GOTO	$-1				; No, test again
		MOVF	ADRESH,W		; Read upper 8 bits, W is value
;		MOVWF	ANXVAL			; Save
;		MOVLW	B'00000000'		; ADC OFF
;		MOVWF	ADCON0			;
;		MOVF	ANXVAL,W		; Recall
		RETURN

;===============================
;        LCD put display hex
;===============================
LCD_PUTHEX
		MOVWF	LCD_WORK2
		SWAPF	LCD_WORK2,W
		CALL	HEXCHAR1
		CALL	LCD_PUTCHAR

		MOVF	LCD_WORK2,W
		CALL	HEXCHAR1
		CALL	LCD_PUTCHAR

		RETURN

;===============================
;        LCD initalyze
;===============================
LCD_INIT
; Register mode set
		BCF		STATUS,RP0		; Bank 0 switch
		BCF		STATUS,RP1		;
		BCF		PORTA,LCD_SIG_RS; Register
; wait 15ms after power on
		MOVLW	D'15'
		CALL	WAIT
; Command write
		MOVLW	B'00110000'		; Initial command
		CALL	LCD_WRITE
; wait 4.1ms
		MOVLW	D'5'
		CALL	WAIT
; Command write
		MOVLW	B'00110000'		; Initial command
		CALL	LCD_WRITE
; wait 100us
		MOVLW	D'1'
		CALL	WAIT
; Command write
		MOVLW	B'00110000'		; Initial command
		CALL	LCD_WRITE
; init data set
		MOVLW	B'00111000'		; DL=1:8bit N=1:2lines F=0:5x7dot
		CALL	LCD_PUTCMD
		MOVLW	B'00001100'		; Display on/off D=1:char C=0:cursor B=0:blink
		CALL	LCD_PUTCMD
		MOVLW	B'00000001'		; Clear Display
		CALL	LCD_PUTCMD
		MOVLW	B'00000110'		; Entry mode set
		CALL	LCD_PUTCMD
		
		RETURN

;===============================
;        LCD put 1 charactor
;===============================
LCD_PUTCHAR
; Busy check
		MOVWF	LCD_WORK
		CALL	LCD_BUSYWAIT
; Data mode set
		BCF		STATUS,RP0		; Bank 0 switch
		BCF		STATUS,RP1		;
		BSF		PORTA,LCD_SIG_RS; Register select
; Port write
		MOVF	LCD_WORK,W
		CALL	LCD_WRITE

		RETURN

;===============================
;        LCD put command
;===============================
LCD_PUTCMD
; Busy check
		MOVWF	LCD_WORK
		CALL	LCD_BUSYWAIT
; Command mode set
		BCF		STATUS,RP0		; Bank 0 switch
		BCF		STATUS,RP1		;
		BCF		PORTA,LCD_SIG_RS; Register select
; Port write
		MOVF	LCD_WORK,W
		CALL	LCD_WRITE

		RETURN

;===============================
;        LCD busy wait
;===============================
LCD_BUSYWAIT
; Command mode set
		BCF		STATUS,RP0		; Bank 0 switch
		BCF		STATUS,RP1		;
		BCF		PORTA,LCD_SIG_RS; Register select
LCD_BUSYWAIT10
		CALL	LCD_READ
		MOVWF	LCD_STATUS
		BTFSC	LCD_STATUS,7
		GOTO	LCD_BUSYWAIT10
		RETURN

;===============================
;        LCD Port read
;===============================
LCD_READ
; port direction unset
		MOVLW	B'11111111'		; port input mode
		BSF		STATUS,RP0		; Bank 1 switch
		BCF		STATUS,RP1		;
		MOVWF	TRISB			;
; read enable storobe
		BCF		STATUS,RP0		; Bank 0 switch
		BCF		STATUS,RP1		;
		BSF		PORTA,LCD_SIG_RW; R 
		NOP
		BSF		PORTA,LCD_SIG_E	; H
		NOP
		MOVF	PORTB,W			; Read port data -> W reg
		BCF		PORTA,LCD_SIG_E	; L

		RETURN

;===============================
;        LCD Port write
;===============================
LCD_WRITE
; data set for 
		BCF		STATUS,RP0		; Bank 0 switch
		BCF		STATUS,RP1		;
		MOVWF	PORTB			; 
; port direction set
		MOVLW	B'00000000'		; output mode
		BSF		STATUS,RP0		; Bank 1 switch
		BCF		STATUS,RP1		;
		MOVWF	TRISB			;
; write enable storobe
		BCF		STATUS,RP0		; Bank 0 switch
		BCF		STATUS,RP1		;
		BCF		PORTA,LCD_SIG_RW; W
		NOP
		BSF		PORTA,LCD_SIG_E	; H
		NOP
		BCF		PORTA,LCD_SIG_E	; L

		RETURN

;===============================
;        WAIT (1 ms unit)
;===============================
WAIT
		MOVWF	WAITCTR
WAIT10
;		MOVLW	D'17'			; by 32KHz clock 10ms
;		MOVLW	D'68'			; by 125KHz clock 10ms (not adjust)
;		MOVLW	D'136'			; by 250KHz clock 10ms (not adjust)
;		MOVLW	D'11'			; by 250KHz clock 1ms
;		MOVLW	D'23'			; by 500KHz clock 1ms (not adjust)
;		MOVLW	D'46'			; by 1MHz clock 1ms (not adjust)
		MOVLW	D'250'			; by 4MHz clock 1ms (not adjust)
		MOVWF	WAITCTR2
WAIT20
		CLRWDT					; WDT cancel
		DECFSZ	WAITCTR2,F
		GOTO	WAIT20

		DECFSZ	WAITCTR,F
		GOTO	WAIT10
		RETURN

;===============================
;        LIBRALY
;===============================

;		INCLUDE <FXM22.A16>

;		INCLUDE <FXM24.A16>

;		INCLUDE <FXM26.A16>

;		INCLUDE <FXM44.A16>

;		INCLUDE <FXM46.A16>

		INCLUDE <FXM66.A16>		; 16 x 16

;		INCLUDE <FXM68.A16>

;		INCLUDE <FXM88.A16>

;		INCLUDE <FXD22.A16>

;		INCLUDE <FXD24.A16>

		INCLUDE <FXD26.A16>		; 32 / 16

;		INCLUDE <FXD44.A16>

;		INCLUDE <FXD46.A16>

;		INCLUDE <FXD66.A16>		; 16 / 16

;		INCLUDE <FXD68.A16>

;		INCLUDE <FXD88.A16>


		END
