;**************************************************************************
; dtmf001.asm
;
; PROGRAM:    Simple Parrallel to Serial to take output from 
;			Holtek HT9170B/HT9170D DTMF detector
;
; AUTHOR:		Doug Rice, 
;			doug.h.rice@btinternet.com
;
; WEBSITE:	http://www.btinternet.com/~doug.h.rice or http://www.dougrice.plus.com/
;
; COPYRIGHT: 2004 10th September 2004.
;
; Version:   V1.0c
;
; Changes:
;
;
;
; DESCRIPTION: 
;	simple serial project that receives serial commands
;
; AUTHOR:  Douglas Rice
; Copyright 2004
;
;	Osc: 4MHz for 16F628
;		3.579545MHz for 16F84
;
;	I/O used:-
;
;	Port A	
;
;	PA1		Serial I/O connect to RS232 RX and via 4k7 to RS232 TX	
;
;	PA 0 	HT9170B/HT9170D EST pin
;
;	PB0		HT9170B/HT9170D D0
;	PB1		HT9170B/HT9170D D1
;	PB2		HT9170B/HT9170D D2
;	PB3		HT9170B/HT9170D D3
;
; Holtek HT9170B/HT9170D
;
;DTMF data output table
;Low Group (Hz) High Group (Hz) Digit OE D3 D2 D1 D0
;697 1209 1 H L L L H
;697 1336 2 H L L H L
;697 1477 3 H L L H H
;770 1209 4 H L H L L
;770 1336 5 H L H L H
;770 1477 6 H L H H L
;852 1209 7 H L H H H
;852 1336 8 H H L L L
;852 1477 9 H H L L H
;941 1336 0 H H L H L
;941 1209 * H H L H H
;941 1477 # H H H L L
;697 1633 A H H H L H
;770 1633 B H H H H L
;852 1633 C H H H H H
;941 1633 D H L L L L
;       ANY L Z Z Z Z
;Note: Z High impedance; ANY Any digit
;
; REVISIONS:
;	
;
;**************************************************************************


;--------------------------------------------------------------------------
; Sec 0.  #Defines tofor different chips
;--------------------------------------------------------------------------

; uncomment only one of these
;
;#define MHZ_4			1
#define MHZ_3_579545	1



; uncomment only one of these
;

#define pic16F84 		1
;#define pic16F628 	1
;#define pic16F876 	1
;#define pic12F675 	1


;--------------------------------------------------------------------------
; Sec 0.1 #Defines for 16F84
;--------------------------------------------------------------------------




ifdef pic16F84
	INCLUDE "p16F84.inc"       

        LIST P=16F84, R=DEC
	__idlocs  0x0084
	__config  _XT_OSC & _PWRTE_ON & _WDT_OFF
	
	cblock 	0x0c
	endc
	

PORT_RS232	  EQU  PORTA 	    
TRIS_RS232	  EQU  TRISA 	    
RS232tx         EQU  0	; Serial Out
RS232rx         EQU  0	; Serial In needs to be equal to BUTTON_RS232

	
	endif	

;--------------------------------------------------------------------------
; Sec 0.2 #Defines for 16F628
;--------------------------------------------------------------------------

ifdef pic16F628
#define 	buildPWM	1

	INCLUDE "p16F628.inc"       
        LIST P=16F628, R=DEC
	__idlocs  0x0628
	__CONFIG _CP_OFF & _WDT_OFF & _BODEN_OFF & _PWRTE_ON & _INTRC_OSC_CLKOUT  & _MCLRE_OFF & _LVP_OFF
	
	cblock 0x20
	endc

; *********** I/O EQUATES **************

PORT_RS232	  EQU  PORTA 	    
TRIS_RS232	  EQU  TRISA 	    
RS232tx         EQU  0	; Serial Out
RS232rx         EQU  0	; Serial In needs to be equal to BUTTON_RS232


	endif

;--------------------------------------------------------------------------
; Sec 0.3 #Defines for 16F876
;--------------------------------------------------------------------------

ifdef pic16F876
#define 	buildPWM	1

	INCLUDE "p16F876.inc"       
        LIST P=16F876, R=DEC
	__idlocs  0x0876
	__CONFIG _CP_OFF & _DEBUG_OFF & _WRT_ENABLE_ON & _WDT_OFF & _BODEN_OFF & _PWRTE_ON  & _HS_OSC & _LVP_OFF
	
	cblock 0x20
	endc

; *********** I/O EQUATES **************

PORT_RS232	  EQU  PORTA 	    
TRIS_RS232	  EQU  TRISA 	    
RS232tx         EQU  0	; Serial Out
RS232rx         EQU  0	; Serial In needs to be equal to BUTTON_RS232


endif


;--------------------------------------------------------------------------
; Sec 0.4 #Defines for 16F675
;--------------------------------------------------------------------------

ifdef pic12F675

	INCLUDE "p12F675.inc"       
        LIST P=16F675, R=DEC
	__idlocs  0x0675
	__config  _PWRTE_ON & _WDT_OFF & _BODEN_OFF & _INTRC_OSC_NOCLKOUT & _MCLRE_OFF 

	
	cblock 0x20
	endc

; *********** I/O EQUATES **************

PORT_RS232  EQU  GPIO 	    
TRIS_RS232  EQU  TRISIO 	    
RS232tx     EQU  5	; Serial Out
RS232rx     EQU  5	; Serial In needs to be equal to BUTTON_RS232

PORTA		EQU  GPIO 	    		
PORTB		EQU  GPIO 	    		

TRISA		EQU  TRISIO 	    
TRISB		EQU  TRISIO 	    



endif



;--------------------------------------------------------------------------
; Sec 1. Equates and Constants
;--------------------------------------------------------------------------
; The General Purpose Registers start at the end of the 
; Special Purpose Registers.

; IPMentState values
; in normal running or in time setting modes
;

; DoTimeSlice bits
; these bits are set to schedule a timer chain event
DTsS4event	EQU	0	; 



; IPtrigMenu
DTSFast			EQU	0

;--------------------------------------------------------------------------
; Sec 1.1 Button and LED 
;--------------------------------------------------------------------------

BUTTON_RS232	EQU	RS232rx	; EDG  - Bottom Buttom
BUTTON_DOWN	EQU	6	; EDG  - Bottom Buttom
BUTTON_TONE		EQU	1	; EDG  - Bottom Buttom

;--------------------------------------------------------------------------
; Sec 2.0 Variables
;--------------------------------------------------------------------------
; Variables start 0x0C
;

;	DoTimeSlice Bits, to schedule, set bit

	cblock 	
	
	
DoTimeSlice

IPnew
IPlast
IPbuttonEvent

IPnewFast
IPlastFast
IPbuttonEventUp

IPtrigMenu

DT_PORTB

; variables used by Serial input and output routines
RStxTemp
RSloopCnt
RSdelayCnt
RSin
RSout
ENDC


	



;--------------------------------------------------------------------------
; Sec 3. MACROS
;--------------------------------------------------------------------------


TEST_STRADDLE	MACRO	START
	if high( $ ) != high( START ) 
	    Error "Table straddles Page Boundary " + Start
	endif
	endm

;--------------------------------------------------------------------------
; Sec 4. Program Code
;--------------------------------------------------------------------------
	ORG     0       
	GOTO    Start

	ORG 	4
	RETFIE
	GOTO 	Intrtn

;--------------------------------------------------------------------------
; Sec 4.1	Main Program Init Code
;--------------------------------------------------------------------------

Start
	GOTO 	$+1

	CALL 	INinit
	CALL	INsayHello	
	
	GOTO	MainLoop


;--------------------------------------------------------------------------
; Sec 4.2	Main Program 
;--------------------------------------------------------------------------

MainLoop	
		CALL	IPtimesliceFast
		
		; test for rising edge
;		BTFSC	IPbuttonEventUp,BUTTON_RS232	; RS232 input start 
;		CALL	IPrs232


		BTFSC	IPbuttonEventUp,BUTTON_TONE		
		CALL	DTtone
		
		MOVF	DoTimeSlice,w
		BZ		MainLoop

;		BTFSC	DoTimeSlice,RUN_BUFF
;		CALL	DLrunBuff

		GOTO 	MainLoop
		


;--------------------------------------------------------------------------
; Sec 5.	Subroutines, procedures and functions
;--------------------------------------------------------------------------


;--------------------------------------------------------------------------
; Sec 5.1	Button Poll Routine
;--------------------------------------------------------------------------
		
IPtimeslice
		;	--_____	button press
		;	----___	
		
		;	__-----  /IPnew
		;	----___  IPlast
		;	__--___  
		;
		;
		;
		; This reads all Port A inputs and looks for Press
		MOVFW	IPnew
		MOVWF	IPlast
		MOVFW	PORTA
		MOVWF	IPnew
		; IP last contains new setting, IPlast contains previous
		; look for falling edges
		COMF	IPnew,W
		ANDWF	IPlast,W
		; now force IPbuttonEvent bits to high for new pressed button
		; the service routine should reset the bit to clear the event.
		IORWF	IPbuttonEvent,F
		RETURN

IPtimesliceFast
		; Copy old inputs 
		MOVFW	IPnewFast
		MOVWF	IPlastFast
		; read new inputs
		MOVFW	PORT_RS232
		MOVWF	IPnewFast

		; Look for rising edges
		COMF	IPlastFast,W
		ANDWF	IPnewFast,W
		IORWF	IPbuttonEventUp,F

		RETURN
;--------------------------------------------------------------------------
; Sec 5.2	
;--------------------------------------------------------------------------

DTtone
		BCF IPbuttonEventUp,BUTTON_TONE
		; Tone detected
		; read PORT B
		MOVFW	PORTB
		MOVWF	DT_PORTB
		
;		MOVLW	'/'
;		CALL 	RStxChar
		
		CALL	DTgetChar
		MOVWF	DT_PORTB
		
		CALL 	RStxChar
		
		MOVLW	0x0D
		CALL 	RStxChar
		
		MOVLW	0x0A
		CALL 	RStxChar
	
		RETURN
			

		
DTgetChar
		MOVFW	DT_PORTB
		ANDLW	0x0F
		ADDWF	PCL,F

		RETLW	'D'	;	
		RETLW	'1'	
		RETLW	'2'
		RETLW	'3'	

		RETLW	'4'
		RETLW	'5'
		RETLW	'6'
		RETLW	'7'

		RETLW	'8'
		RETLW	'9'
		RETLW	'0'
		RETLW	'*'

		RETLW	'#'
		RETLW	'A'		
		RETLW	'B'
		RETLW	'C'
	TEST_STRADDLE		DTgetChar


;--------------------------------------------------------------------------
; Sec 5.2	.1 run process on request
;--------------------------------------------------------------------------

DLrunBuff
;	BCF	DoTimeSlice,RUN_BUFF
;	MOVFW	'#'
;	CALL 	RStxChar

	
;	MOVFW	RSin			
;	CALL	RSbuffInput
;	return
	
	


;--------------------------------------------------------------------------
; Sec 5.3	 Button Input functions
;--------------------------------------------------------------------------


IPdownPressed 
	BCF IPbuttonEvent,BUTTON_DOWN
	return


;--------------------------------------------------------------------------
; Sec 5.4	 Clock Chain Routine
;--------------------------------------------------------------------------
		
CLtimeslice
		;
		BTFSS	INTCON,T0IF
		RETURN

		; TMR0 timeout
		BCF		INTCON,T0IF
		BSF		DoTimeSlice,DTsS4event
		
		CALL	IPtimeslice
		RETURN


;--------------------------------------------------------------------------
; Sec 5.5 	Serial Input Routine
;--------------------------------------------------------------------------

		

;--------------------------------------------------------------------------
; Sec 5.6 	Serial Output Routines
;--------------------------------------------------------------------------


RSwrtHexNibble	; currently only displays 0..9, A..F
	ANDLW	0x0F
	ADDLW	0x06	; is it A..F, if so trigger a digit overflow
	SKPNDC
	ADDLW	7; subtract 10, then add 'A'-'0'
	ADDLW	0x30-6	; Subtract extra 6 added to cause DC
	MOVWF	RSout
	GOTO	RStxChar

RStxChar
	;
	; Output Start Bit
	; Start bit is low, data bits are inverted
	;
	
	movwf	RStxTemp
	comf	RStxTemp,f
	BCF		PORT_RS232,RS232tx

	; Make RS232 pin an ouput while transmitting character
	BANKSEL  TRIS_RS232		
	BCF		TRIS_RS232,RS232tx
	BANKSEL  PORT_RS232		

	; 1200 baud output
	; move the char into RStxtemp, it is destroyed.

	;Send Start Bit
	BSF		PORT_RS232,RS232tx

	call	RSdelayBit
	; prepare to send ot 8 bits
	movlw	8
	movwf	RSloopCnt

RStxCharLp1
	
	; After start bit, which is 1, then test each bit
	; I need to set PA:0 to same as LSB RStxTemp
	; this compares PA:0 with RStxTemp so see if it needs toggling
	; The xorwf PORT_RS232,f causes the bit to be toggled if required.
	
	RRF		RStxTemp,f
	movfw	STATUS
	andlw	1 << C			; mask off bit
	addlw   ( 1 << RS232tx) -1	; shift bit by using a ripple carry
	xorwf	PORT_RS232,W			; Doeas output need toggling ?
	andlw	1 << RS232tx		; Mask off output pin	
	xorwf	PORT_RS232,f			; Toggle output pin if needed	
	call	RSdelayBit			; delay bit
	
	
	decfsz	RSloopCnt
	goto	RStxCharLp1

	; now clock out 3 stop bits
	BCF		PORT_RS232,RS232tx
	call	RSdelayBit	; apply stop bit
	call	RSdelayBit	; apply stop bit
	call	RSdelayBit	; apply stop bit

	; Make RS232 pin an input so that main loop can test for start bit
	BANKSEL  TRIS_RS232		
;	BSF		TRIS_RS232,RS232tx
	BCF		TRIS_RS232,RS232tx
	BANKSEL  PORT_RS232		

	RETURN


; timing functions to delay serial routines for a bit or half bit.

RSdelayHalfBit		

ifdef  MHZ_4			
		MOVLW	0x1D		; shorten delay as edge detector takes about 22 us for 4800 baud
	endif

ifdef  MHZ_3_579545	
		MOVLW	0x1D	 -( (208-186)/3)-1	; shorten delay as edge detector takes about 22 us for 4800 baud
	endif

		
		GOTO	RSdelayBit0			

RSdelayBit		

		; delay = clkFreq*(cyclesCode+cyclesLoop*nTimes)
		; Given delay and cyclesCode and cyclesLoop work out nTimes
		;
		; ( ( delay/clfFreq)-cyclesCode ) = cyclesLoop*nTimes
		; 
		; ( ( delay/clfFreq)-cyclesCode ) /cyclesLoop = nTimes
		;
		 

         ; for 4800 baud this delay is 200uS
         ; each bit takes 208.333333333333333333333333333333 uS
         ;
         ; for 4Mhz Clock  need 208.3 cycles
         ;
         ; for 3.579545Mhz need 186.434635416666666666666666666667 cycles
         ;
         ;
         ; Using the stop watch 

ifdef  MHZ_4			
		NOP
         ; want to make loop delay 208 us or 208 cycles
		MOVLW	0x40	 ; for 4800 baud	
	endif

ifdef  MHZ_3_579545	
		;
         ; need 186.4 instead of 208 cycles.
         ;
		MOVLW	0x40	 -( (208-186)/3)-1 ; for 4800 baud	
	endif

RSdelayBit0			
		MOVWF	RSdelayCnt
RSdelayBit1	DECFSZ	RSdelayCnt		; 1
		GOTO	RSdelayBit1		; 2 clk
		CLRWDT
		RETURN



;--------------------------------------------------------------------------
; Sec 5.7 	Serial Output Format Routines
;--------------------------------------------------------------------------




;--------------------------------------------------------------------------
; Sec 5.9 Initilization code - 
;--------------------------------------------------------------------------


INinit
	; Enable Interupts
	MOVLW	H'00'
	MOVWF	INTCON

	CLRF PORT_RS232 ;Initialize PORT_RS232 by setting
	;output data latches
	
	ifdef pic12F675
	; get the Oscillator calibration

	;MOVLW	0x6C		; load the Calib
	BSF		STATUS,RP0
	CALL 	OscCalValue
	MOVWF	OSCCAL
	
	BANKSEL 	CMCON
	MOVLW 	0X07 ;Turn comparators off and
	MOVWF 	CMCON ;enable pins for I/O
	
	BANKSEL 	ANSEL
	CLRF	ANSEL	

	endif



	ifdef pic16F628
	BANKSEL CMCON
	MOVLW 0X07 ;Turn comparators off and
	MOVWF CMCON ;enable pins for I/O

	endif

	ifdef pic16F876
     ;make AtoD inputs digital
	BANKSEL ADCON1
	MOVLW 0X07 ;Turn comparators off and
	MOVWF ADCON1 ;enable pins for I/O
	endif




	banksel	TRISB
	MOVLW	0XFF
	MOVWF	TRISB
	;BSF		TRIS_RS232, RS232tx 	; Make PORT_RS232 INPUT.
	BCF		TRIS_RS232, RS232tx 	; Make PORT_RS232 OUTPUT.

	MOVLW	0x10
	MOVWF	RSloopCnt
INinit1
	call	RSdelayBit
	DECFSZ	RSloopCnt
	GOTO	INinit1	


	; Change Prescaler
	CLRWDT

	; Configure Tmr 0
	BSF		STATUS,RP0
	; Set up prescaller for 8192 / 16 = 512 ticks perseconds.

	MOVLW	0x0 + 2 	; For Tmr0 0=/2, 1=/4, 2=/8,3=/16
	MOVWF	OPTION_REG
	BCF		STATUS,RP0

	CLRF	DoTimeSlice	
	CLRF	IPbuttonEvent
	CLRF	IPbuttonEventUp
	return

INsayHello	
	; print out power on message

	MOVLW	0x17			;	XON
	CALL 	RStxChar		

	
	MOVLW	0x0D			;	CR
	CALL 	RStxChar		
		
	MOVLW	0x0A			;	LF
	CALL 	RStxChar

	MOVLW	'D'
	CALL 	RStxChar
		
	MOVLW	'T'
	CALL 	RStxChar
		
	MOVLW	'M'
	CALL 	RStxChar
	
	MOVLW	'F'
	CALL 	RStxChar

	MOVLW	0x0D
	CALL 	RStxChar
		
	MOVLW	0x0A
	CALL 	RStxChar
	return



;--------------------------------------------------------------------------
; Sec 6.0		Interrupt Routines
;--------------------------------------------------------------------------

Intrtn		
		RETFIE

;--------------------------------------------------------------------------
; Program End
;--------------------------------------------------------------------------
	ifdef pic12F675
	
	org	0x3FF
OscCalValue	
;	RETLW	0x6C		; 12F675 chip A
	RETLW	0x7C		; 12F675 chip B
	
	endif
LastProgWord


;				osc cal		i/d	fuses(bandgap )
;12F675 chip A	   346C		0fc3 did not record
;12F675 chip b	   347C		0fc3 11FF


;--------------------------------------------------------------------------
; Sec 7.0		EEPROM data
;--------------------------------------------------------------------------
		ORG	0x2100
		; first 2 on and off are general ones
		DE	.100,.50  ; 4

	cblock 	
LastVar
	endc
               END


