⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 serial.asm

📁 uart-I2C, working on PIC18F2420
💻 ASM
📖 第 1 页 / 共 5 页
字号:
	incf	current_channel,f
	call	wait_4
	call	send2TDA			;ch 5
	incf	current_channel,f
	call	wait_4
	call	send2TDA			;ch 6

	movlw	0x03					;sub addr 0 - Left Volume
	movwf	current_subadd
	movlw	0x39					;mic, gain = 14dB, mute = off, in=in1
	movwf	current_data
	movlw	1
	movwf	current_channel
	call	send2TDA			;ch 1
	incf	current_channel,f
	call	wait_4
	call	send2TDA			;ch 2
	incf	current_channel,f
	call	wait_4
	call	send2TDA			;ch 3
	incf	current_channel,f
	call	wait_4
	call	send2TDA			;ch 4
	incf	current_channel,f
	call	wait_4
	call	send2TDA			;ch 5
	incf	current_channel,f
	call	wait_4
	call	send2TDA			;ch 6

	movlw	0x04					;sub addr 0 - Left Volume
	movwf	current_subadd
	movlw	0x39					;mic, gain = 14dB, mute = off, in=in1
	movwf	current_data
	movlw	1
	movwf	current_channel
	call	send2TDA			;ch 1
	incf	current_channel,f
	call	wait_4
	call	send2TDA			;ch 2
	incf	current_channel,f
	call	wait_4
	call	send2TDA			;ch 3
	incf	current_channel,f
	call	wait_4
	call	send2TDA			;ch 4
	incf	current_channel,f
	call	wait_4
	call	send2TDA			;ch 5
	incf	current_channel,f
	call	wait_4
	call	send2TDA			;ch 6
;don't forget to clear the state....
	bcf		FLAG2,b_tdaonly

;mic should now be working.....
mic_loop
	btfsc	PAGE_IN
	bra		mic_loop

;now restore - this will be longer......

;TDA1 - Input select
	movlw	TDA_ADDRESS				;0x088
	movwf	I2CBUF	
	call	START_TDA1				;start condition to TDA1
	call	SEND_TDA1				;send address to TDA1
	bcf		FLAG3,b_ACKERROR		;we will ignore any ack errors for now
	movlw	0x00					;Input subaddress
	movwf	I2CBUF
	call	SEND_TDA1				;send subaddress
	bcf		FLAG3,b_ACKERROR		;ignore ack

	movf	current_input,w			;get current input
	addlw	0x20					;shut off mic

	movwf	I2CBUF
	call	SEND_TDA1
	bcf		FLAG3,b_ACKERROR
	call	STOP_TDA1

;TDA1 - Vol Left
	movlw	TDA_ADDRESS				;0x088
	movwf	I2CBUF	
	call	START_TDA1				;start condition to TDA1
	call	SEND_TDA1				;send address to TDA1
	bcf		FLAG3,b_ACKERROR		;we will ignore any ack errors for now
	movlw	0x03					;Input subaddress
	movwf	I2CBUF
	call	SEND_TDA1				;send subaddress
	bcf		FLAG3,b_ACKERROR		;ignore ack

	movf	vl_1,w

	movwf	I2CBUF
	call	SEND_TDA1
	bcf		FLAG3,b_ACKERROR
	call	STOP_TDA1

;TDA1 - Vol Right
	movlw	TDA_ADDRESS				;0x088
	movwf	I2CBUF	
	call	START_TDA1				;start condition to TDA1
	call	SEND_TDA1				;send address to TDA1
	bcf		FLAG3,b_ACKERROR		;we will ignore any ack errors for now
	movlw	0x04					;Input subaddress
	movwf	I2CBUF
	call	SEND_TDA1				;send subaddress
	bcf		FLAG3,b_ACKERROR		;ignore ack

	movf	vl_1,w

	movwf	I2CBUF
	call	SEND_TDA1
	bcf		FLAG3,b_ACKERROR
	call	STOP_TDA1

;TDA2 - Input select
	movlw	TDA_ADDRESS				;0x088
	movwf	I2CBUF	
	call	START_TDA2				;start condition to TDA1
	call	SEND_TDA2				;send address to TDA1
	bcf		FLAG3,b_ACKERROR		;we will ignore any ack errors for now
	movlw	0x00					;Input subaddress
	movwf	I2CBUF
	call	SEND_TDA2				;send subaddress
	bcf		FLAG3,b_ACKERROR		;ignore ack

	movf	current_input,w			;get current input
	addlw	0x20					;shut off mic

	movwf	I2CBUF
	call	SEND_TDA2
	bcf		FLAG3,b_ACKERROR
	call	STOP_TDA2

;TDA2 - Vol Left
	movlw	TDA_ADDRESS				;0x088
	movwf	I2CBUF	
	call	START_TDA2				;start condition to TDA1
	call	SEND_TDA2				;send address to TDA1
	bcf		FLAG3,b_ACKERROR		;we will ignore any ack errors for now
	movlw	0x03					;Input subaddress
	movwf	I2CBUF
	call	SEND_TDA2				;send subaddress
	bcf		FLAG3,b_ACKERROR		;ignore ack

	movf	vl_2,w

	movwf	I2CBUF
	call	SEND_TDA2
	bcf		FLAG3,b_ACKERROR
	call	STOP_TDA2

;TDA2 - Vol Right
	movlw	TDA_ADDRESS				;0x088
	movwf	I2CBUF	
	call	START_TDA2				;start condition to TDA1
	call	SEND_TDA2				;send address to TDA1
	bcf		FLAG3,b_ACKERROR		;we will ignore any ack errors for now
	movlw	0x04					;Input subaddress
	movwf	I2CBUF
	call	SEND_TDA2				;send subaddress
	bcf		FLAG3,b_ACKERROR		;ignore ack

	movf	vl_2,w

	movwf	I2CBUF
	call	SEND_TDA2
	bcf		FLAG3,b_ACKERROR
	call	STOP_TDA2


;TDA3 - Input select
	movlw	TDA_ADDRESS				;0x088
	movwf	I2CBUF	
	call	START_TDA3				;start condition to TDA1
	call	SEND_TDA3				;send address to TDA1
	bcf		FLAG3,b_ACKERROR		;we will ignore any ack errors for now
	movlw	0x00					;Input subaddress
	movwf	I2CBUF
	call	SEND_TDA3				;send subaddress
	bcf		FLAG3,b_ACKERROR		;ignore ack

	movf	current_input,w			;get current input
	addlw	0x20					;shut off mic

	movwf	I2CBUF
	call	SEND_TDA3
	bcf		FLAG3,b_ACKERROR
	call	STOP_TDA3

;TDA3 - Vol Left
	movlw	TDA_ADDRESS				;0x088
	movwf	I2CBUF	
	call	START_TDA3				;start condition to TDA1
	call	SEND_TDA3				;send address to TDA1
	bcf		FLAG3,b_ACKERROR		;we will ignore any ack errors for now
	movlw	0x03					;Input subaddress
	movwf	I2CBUF
	call	SEND_TDA3				;send subaddress
	bcf		FLAG3,b_ACKERROR		;ignore ack

	movf	vl_3,w

	movwf	I2CBUF
	call	SEND_TDA3
	bcf		FLAG3,b_ACKERROR
	call	STOP_TDA3

;TDA3 - Vol Right
	movlw	TDA_ADDRESS				;0x088
	movwf	I2CBUF	
	call	START_TDA3				;start condition to TDA1
	call	SEND_TDA3				;send address to TDA1
	bcf		FLAG3,b_ACKERROR		;we will ignore any ack errors for now
	movlw	0x04					;Input subaddress
	movwf	I2CBUF
	call	SEND_TDA3				;send subaddress
	bcf		FLAG3,b_ACKERROR		;ignore ack

	movf	vl_3,w

	movwf	I2CBUF
	call	SEND_TDA3
	bcf		FLAG3,b_ACKERROR
	call	STOP_TDA3

;TDA4 - Input select
	movlw	TDA_ADDRESS				;0x088
	movwf	I2CBUF	
	call	START_TDA4				;start condition to TDA1
	call	SEND_TDA4				;send address to TDA1
	bcf		FLAG3,b_ACKERROR		;we will ignore any ack errors for now
	movlw	0x00					;Input subaddress
	movwf	I2CBUF
	call	SEND_TDA4				;send subaddress
	bcf		FLAG3,b_ACKERROR		;ignore ack

	movf	current_input,w			;get current input
	addlw	0x20					;shut off mic

	movwf	I2CBUF
	call	SEND_TDA4
	bcf		FLAG3,b_ACKERROR
	call	STOP_TDA4

;TDA4 - Vol Left
	movlw	TDA_ADDRESS				;0x088
	movwf	I2CBUF	
	call	START_TDA4				;start condition to TDA1
	call	SEND_TDA4				;send address to TDA1
	bcf		FLAG3,b_ACKERROR		;we will ignore any ack errors for now
	movlw	0x03					;Input subaddress
	movwf	I2CBUF
	call	SEND_TDA4				;send subaddress
	bcf		FLAG3,b_ACKERROR		;ignore ack

	movf	vl_4,w

	movwf	I2CBUF
	call	SEND_TDA4
	bcf		FLAG3,b_ACKERROR
	call	STOP_TDA4

;TDA4 - Vol Right
	movlw	TDA_ADDRESS				;0x088
	movwf	I2CBUF	
	call	START_TDA4				;start condition to TDA1
	call	SEND_TDA4				;send address to TDA1
	bcf		FLAG3,b_ACKERROR		;we will ignore any ack errors for now
	movlw	0x04					;Input subaddress
	movwf	I2CBUF
	call	SEND_TDA4				;send subaddress
	bcf		FLAG3,b_ACKERROR		;ignore ack

	movf	vl_4,w

	movwf	I2CBUF
	call	SEND_TDA4
	bcf		FLAG3,b_ACKERROR
	call	STOP_TDA4

;TDA5 - Input select
	movlw	TDA_ADDRESS				;0x088
	movwf	I2CBUF	
	call	START_TDA5				;start condition to TDA1
	call	SEND_TDA5				;send address to TDA1
	bcf		FLAG3,b_ACKERROR		;we will ignore any ack errors for now
	movlw	0x00					;Input subaddress
	movwf	I2CBUF
	call	SEND_TDA5				;send subaddress
	bcf		FLAG3,b_ACKERROR		;ignore ack

	movf	current_input,w			;get current input
	addlw	0x20					;shut off mic

	movwf	I2CBUF
	call	SEND_TDA5
	bcf		FLAG3,b_ACKERROR
	call	STOP_TDA5

;TDA5 - Vol Left
	movlw	TDA_ADDRESS				;0x088
	movwf	I2CBUF	
	call	START_TDA5				;start condition to TDA1
	call	SEND_TDA5				;send address to TDA1
	bcf		FLAG3,b_ACKERROR		;we will ignore any ack errors for now
	movlw	0x03					;Input subaddress
	movwf	I2CBUF
	call	SEND_TDA5				;send subaddress
	bcf		FLAG3,b_ACKERROR		;ignore ack

	movf	vl_5,w

	movwf	I2CBUF
	call	SEND_TDA5
	bcf		FLAG3,b_ACKERROR
	call	STOP_TDA5

;TDA5 - Vol Right
	movlw	TDA_ADDRESS				;0x088
	movwf	I2CBUF	
	call	START_TDA5				;start condition to TDA1
	call	SEND_TDA5				;send address to TDA1
	bcf		FLAG3,b_ACKERROR		;we will ignore any ack errors for now
	movlw	0x04					;Input subaddress
	movwf	I2CBUF
	call	SEND_TDA5				;send subaddress
	bcf		FLAG3,b_ACKERROR		;ignore ack

	movf	vl_5,w

	movwf	I2CBUF
	call	SEND_TDA5
	bcf		FLAG3,b_ACKERROR
	call	STOP_TDA5	

;TDA6 - Input select
	movlw	TDA_ADDRESS				;0x088
	movwf	I2CBUF	
	call	START_TDA6				;start condition to TDA1
	call	SEND_TDA6				;send address to TDA1
	bcf		FLAG3,b_ACKERROR		;we will ignore any ack errors for now
	movlw	0x00					;Input subaddress
	movwf	I2CBUF
	call	SEND_TDA6				;send subaddress
	bcf		FLAG3,b_ACKERROR		;ignore ack

	movf	current_input,w			;get current input
	addlw	0x20					;shut off mic

	movwf	I2CBUF
	call	SEND_TDA6
	bcf		FLAG3,b_ACKERROR
	call	STOP_TDA6

;TDA6 - Vol Left
	movlw	TDA_ADDRESS				;0x088
	movwf	I2CBUF	
	call	START_TDA6				;start condition to TDA1
	call	SEND_TDA6				;send address to TDA1
	bcf		FLAG3,b_ACKERROR		;we will ignore any ack errors for now
	movlw	0x03					;Input subaddress
	movwf	I2CBUF
	call	SEND_TDA6				;send subaddress
	bcf		FLAG3,b_ACKERROR		;ignore ack

	movf	vl_6,w

	movwf	I2CBUF
	call	SEND_TDA6
	bcf		FLAG3,b_ACKERROR
	call	STOP_TDA6

;TDA6 - Vol Right
	movlw	TDA_ADDRESS				;0x088
	movwf	I2CBUF	
	call	START_TDA6				;start condition to TDA1
	call	SEND_TDA6				;send address to TDA1
	bcf		FLAG3,b_ACKERROR		;we will ignore any ack errors for now
	movlw	0x04					;Input subaddress
	movwf	I2CBUF
	call	SEND_TDA6				;send subaddress
	bcf		FLAG3,b_ACKERROR		;ignore ack

	movf	vl_6,w

	movwf	I2CBUF
	call	SEND_TDA6
	bcf		FLAG3,b_ACKERROR
	call	STOP_TDA6

	return



I2C_get
start_con

	incf	i2c_counter	;test buffer for monitoring how many times I am called here
	clrf	KEYBUFF		;clear input buffer


	movlw	d'8'		;8 loops
	movwf	rec_count	;into variable
M1
	btfsc	SCK1		;check for SCA to drop...
	goto	M1			;nope, keep waiting

;clock has dropped, we are at "00". Data can do what it wants.
;we need clock to go high to start getting data
M2
	btfss	SCK1		;look for clock to go back high
	goto	M2			;loop until it does

	btfsc	SDA1			;we have clock high, is bit a 1?
	bsf		KEYBUFF,0		;set bit if it is
	decfsz	rec_count,F		;either way dec counter, look for 8th bit
	bra		M3				;if not done, head for rotate...
	bra		do_ack			;we have the eighth bit, jump out
M3
	rlncf		KEYBUFF,F		;rotate recbuff to the left
M3_0
	btfsc	SCK1				;look for clock to drop again
	bra		M3_0				;circle until....
	bra		M2					;then head back to front of loop

do_ack
	btfsc	SCK1				;wait for clock to drop
	bra		do_ack
;clock is low. Data is xxx
;we need clock to go high again (data is a don't care)
do_ack_2
	btfss	SCK1
	goto	do_ack_2

sda_done
	btfss	SDA1
	goto	sda_done

;okay, we have had a stop condition. which byte was this? Since we want to reuse the above routine, 
;makes sense to keep track of bytes, so a "mini" state machine can be used here
	btfsc	FLAG2,b_i2c_addr		;have we recvd addr yet?
	bra		i2c_got_data
;address byte received, is it me? Is it "all"?
	movf	KEYBUFF,w
	movwf	current_channel
	bsf		FLAG2,b_i2c_addr		;set addr recvd bit
	return

i2c_got_data
;okay, now we have data, lets decode it using a small lookup
	bcf		FLAG2,b_i2c_addr		;set addr recvd bit	
;added in 3.05, 21 Oct 07. Should have done this a long time ago.
;if the keypad does not give a value between 1-6, then it is no good..
	movlw	0x06
	cpfsgt	current_channel
	bra		i2c_0
	call	String_ERROR5

i2c_0
;short term, echo characters out through UART
	movf	current_channel,w
	movwf	SENDBUFF
	call	tx_dec		;transmit the dec byte
	movf	KEYBUFF,w
	movwf	SENDBUFF
	call	tx_dec

;send a comma here for delimination between commands....
	movlw	";"
tx_comma1
	btfss	PIR1,TXIF	;Check the tx interrupt flag to make sure 
    goto	tx_comma1		;the buffer is empty.

tx_comma2
	btfss	TXSTA,TRMT	;Check the Transmit Register
	goto	tx_comma2
	movwf	TXREG		;Send comma out to the TXREG

;this return is JUST to test what the keypad is sending - no action taken,
; just echo. Comment out in working firmware
;	return

	nop
	movf	PCL,w					;false read to update PCU,PCH
	rlncf	KEYBUFF,w
	addwf	PCL,F					;jump to correct command

	nop								;zero not allowed - treat as 1
	bra		a_k_vol_down			;command = 1
	bra		a_k_vol_up				;command = 2
	bra		a_dumb					;command = 3
	bra		a_k_input1				;command = 4
	bra		a_k_input2				;command = 5
	bra		a_k_input3				;command = 6
	bra		a_k_input4				;command = 7
	bra		a_k_input5				;command = 8
	bra		a_k_input6				;command = 9
	bra		a_k_input7				;command = 10
	bra		a_k_unmute				;command = 11
	bra		a_k_mute				;command = 12
	bra		a_dumb					;command = 13
	bra		a_k_zone_off			;command = 14
	bra		a_k_all_off				;command = 15
	bra		a_k_zone_on				;command = 16
	bra		a_dumb					;command = 17
	bra		a_k_all_in1				;command = 18
	bra		a_k_all_in2				;command = 19
	bra		a_k_all_in3				;command = 20
	bra		a_k_all_in4				;command = 21
	bra		a_k_all_in5				;command = 22
	bra		a_k_all_in6				;command = 23
	bra		a_k_all_in7				;command = 24
	bra		a_k_all_vol_dn			;command = 25 all vol down
	bra		a_k_all_vol_up			;command = 26 all vol up
	bra		a_k_bass_up				;command = 27
	bra		a_k_bass_dn				;command = 28
	bra		a_k_vol_jump_up			;command = 29
	bra		a_k_vol_jump_down		;command = 30
	bra 	a_k_treb_up				;command = 31
	bra		a_k_treb_dn				;command = 32
	bra		a_k_all_mute			;command = 33 all mute
	bra		a_k_all_unmute			;command = 34 all unmute
	bra		a_k_bal_up				;command = 35		
	bra		a_k_bal_dn				;command = 36
	bra		a_k_party				;command = 37 = party mode!
	bra		a_k_all_vol_jup			;command = 38 = all vol jump up
	bra		a_k_all_vol_jdn			;command = 39 = all vol jump dn

;we are branching over boundaries above, now we have to just
;branch to here and then use a "goto"
a_k_vol_down			;command = 1
	goto	k_vol_down
a_k_vol_up				;command = 2
	goto	k_vol_up
a_dumb
	goto	dumb		;command = 3
a_k_input1
	goto	k_input1	;command = 4
a_k_input2
	goto	k_input2	;command = 5
a_k_input3
	goto	k_input3	;command = 6
a_k_input4
	goto	k_input4	;command = 7
a_k_input5
	goto	k_input5	;command = 8
a_k_input6
	goto	k_input6	;command = 9
a_k_input7
	goto	k_input7	;command = 10
a_k_unmute
	goto	k_unmute	;command = 11
a_k_mute
	goto	k_mute		;command = 12
;a_dumb
;	goto	dumb		;command = 13
a_k_zone_off
	goto	k_zone_off	;command = 14
a_k_all_off
	goto	k_all_off	;command = 15
a_k_zone_on
	goto	k_zone_on	;command = 16
;a_dumb
;	goto	dumb		;command = 17
a_k_all_in1
	goto	k_all_in1	;command = 18
a_k_all_in2
	goto	k_all_in2	;command = 19
a_k_all_in3
	goto	k_all_in3	;command = 20
a_k_all_in4
	goto	k_all_in4	;command = 21
a_k_all_in5
	goto	k_all_in5	;command = 22
a_k_all_in6
	go

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -