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

📄 usbjoy32.asm

📁 HID规范和一个基于CYPRESS FX2 的例子
💻 ASM
📖 第 1 页 / 共 4 页
字号:
	mov a, 00h
	iowr Global_Interrupt		;Disable global interrupts

	mov a, 00h
	mov [rollover_bit], a		;rollover_bit set to 00h, but may need to change to 01h, if TTL value is < CMOS value

	iord Capture_TimerA_Falling	;Read CaptureTimerA 8 bits, CMOS captured time value (always earlier and usually the smaller value)
	mov [capture_a_temp], a
	iord Capture_TimerB_Falling	;Read CaptureTimerB 8 bits, TTL captured time value (always earlier and usually the larger value)
	mov [capture_b_temp], a

	sub a, [capture_a_temp]		;Subtract CMOS time from TTL time to see if timer rolled over
	jnc .no_timer_rollover		;If TTL value is > CMOS value, the timer did not roll over and we keep rollover_bit = 00h

	mov a, 01h			;In the exception that TTL time value is < CMOS time value, we make rollover_bit = 01h
	mov [rollover_bit], a	
	
	.no_timer_rollover:
        mov a, FEh			;Recharges the capacitor, P0.0 is Hi-Z CMOS always, others set to Strong Drive CMOS
        iowr Port0_Mode0
        iowr Port0_Mode1
        iowr Port0
                
	SUB16x16 rollover_bit, capture_b_temp, 00h, capture_a_temp, always_zero, z_R2_diff1_new

        mov a, 05h          		;Re-Enable 1ms,USBReset interrupts
        iowr Global_Interrupt   

	jmp TaskLoop

;==========================================================
;	Task 7:
;	{
;	if (button state has been stable for 5ms)
;		save button state
;	}
;==========================================================

Task7:
	iord 	Port1
	and 	A, ffh
	cmp 	A, [button_debounce]
	jnz 	TaskLoop

	xor 	A, ffh
	mov 	[new_buttons], A
	mov	[hat_buttons], a
	mov	[fire_buttons], a

	mov a, 0fh
	and [hat_buttons], a	; hat buttons byte becomes 0000xxxx 

	mov a, f0h
	and [fire_buttons], a	; fire buttons byte becomes xxxx0000  

	jmp 	TaskLoop

;=========================================================
;	Task 8:	Read Resistor Value for x_R1
;=========================================================

Task8:

	mov a, 30h
        iowr Global_Interrupt		;Enable only Capture interrupts

     ;**********************************************
     ;P0.0 = CMOS Hi-Z
     ;P0.1 = TTL Hi-Z
     ;P0.2 = High Sink
     ;P0.3/P0.4/P0.5/P0.6/P0.7 = CMOS Hi-Z
     ;Port0_Mode0 = 00000100
     ;Port0_Mode1 = 00000100
     ;Port0       = 00000010
     ;**********************************************

        mov a, 04h			;Set port and port mode values to discharge capacitor
        iowr Port0_Mode0		;See table above
        iowr Port0_Mode1
        mov a, 02h
        iowr Port0

	mov a, 00h			;wait about 500usec (6000 cycles) for Capture_timer A & B ISRs, then recharge cap and do math below
	mov [discharge_cnt], a
	.discharge_loop:
	    inc [discharge_cnt]
	    mov a, CCh
	    cmp a, [discharge_cnt]
	    iowr Watchdog
	    jnz .discharge_loop

	mov a, 00h
	iowr Global_Interrupt		;Disable global interrupts

	mov a, 00h
	mov [rollover_bit], a		;rollover_bit set to 00h, but may need to change to 01h, if TTL value is < CMOS value

	iord Capture_TimerA_Falling	;Read CaptureTimerA 8 bits, CMOS captured time value (always earlier and usually the smaller value)
	mov [capture_a_temp], a
	iord Capture_TimerB_Falling	;Read CaptureTimerB 8 bits, TTL captured time value (always earlier and usually the larger value)
	mov [capture_b_temp], a

	sub a, [capture_a_temp]		;Subtract CMOS time from TTL time to see if timer rolled over
	jnc .no_timer_rollover		;If TTL value is > CMOS value, the timer did not roll over and we keep rollover_bit = 00h

	mov a, 01h			;In the exception that TTL time value is < CMOS time value, we make rollover_bit = 01h
	mov [rollover_bit], a	
	
	.no_timer_rollover:
        mov a, FEh			;Recharges the capacitor, P0.0 is Hi-Z CMOS always, others set to Strong Drive CMOS
        iowr Port0_Mode0
        iowr Port0_Mode1
        iowr Port0    
                
	SUB16x16 rollover_bit, capture_b_temp, 00h, capture_a_temp, always_zero, x_R1_diff2_new

	mov a, 05h          		;Re-Enable 1ms,USBReset interrupts
        iowr Global_Interrupt

        jmp TaskLoop

;=========================================================
;	Task 9: Read Resistor Value for x_R2
;=========================================================

Task9:

	mov a, 30h
        iowr Global_Interrupt		;Enable only Capture interrupts

     ;**********************************************
     ;P0.0 = CMOS Hi-Z
     ;P0.1 = TTL Hi-Z
     ;P0.2 = CMOS Hi-Z
     ;P0.3 = High Sink
     ;P0.4/P0.5/P0.6/P0.7 = CMOS Hi-Z
     ;Port0_Mode0 = 00001000
     ;Port0_Mode1 = 00001000
     ;Port0       = 00000010
     ;**********************************************
        mov a, 08h			;Set port and port mode values to discharge capacitor
        iowr Port0_Mode0		;See table above
        iowr Port0_Mode1
        mov a, 02h
        iowr Port0
        
	mov a, 00h			;wait about 500usec (6000 cycles) for Capture_timer A & B ISRs, then recharge cap and do math below
	mov [discharge_cnt], a
	.discharge_loop:
	    inc [discharge_cnt]
	    mov a, CCh
	    cmp a, [discharge_cnt]
	    iowr Watchdog
	    jnz .discharge_loop 

	mov a, 00h
	iowr Global_Interrupt		;Disable global interrupts

	mov a, 00h
	mov [rollover_bit], a		;rollover_bit set to 00h, but may need to change to 01h, if TTL value is < CMOS value

	iord Capture_TimerA_Falling	;Read CaptureTimerA 8 bits, CMOS captured time value (always earlier and usually the smaller value)
	mov [capture_a_temp], a
	iord Capture_TimerB_Falling	;Read CaptureTimerB 8 bits, TTL captured time value (always earlier and usually the larger value)
	mov [capture_b_temp], a

	sub a, [capture_a_temp]		;Subtract CMOS time from TTL time to see if timer rolled over
	jnc .no_timer_rollover		;If TTL value is > CMOS value, the timer did not roll over and we keep rollover_bit = 00h

	mov a, 01h			;In the exception that TTL time value is < CMOS time value, we make rollover_bit = 01h
	mov [rollover_bit], a	
	
	.no_timer_rollover:
        mov a, FEh			;Recharges the capacitor, P0.0 is Hi-Z CMOS always, others set to Strong Drive CMOS
        iowr Port0_Mode0
        iowr Port0_Mode1
        iowr Port0    
                
	SUB16x16 rollover_bit, capture_b_temp, 00h, capture_a_temp, always_zero, x_R2_diff2_new

	; perform calculations to average the joystick readings (R_1st + R_2nd)/2

	AVERAGE8x8 x_R1_diff1_new, x_R1_diff2_new, x_R1_diff1_old, x_R1_diff2_old, RunningCarry, RunningSum, x_R1_average
	AVERAGE8x8 x_R2_diff1_new, x_R2_diff2_new, x_R2_diff1_old, x_R2_diff2_old, RunningCarry, RunningSum, x_R2_average

	; perform calculations below to average and normalize the joystick position for more accuracy (R1*256)/(R1+R2)

	ADD8x8wCarry x_R1_average, x_R2_average, divider_bottom_hi, divider_bottom_lo, x_R12_Sum_MSB,  x_R12_Sum_LSB
	mov a, [x_R1_average]		;Top of division equation gets R1, then it is "multiplied" by 256
	mov [divider_top_hi], a
	mov a, 00h
	mov [divider_top_lo], a		;This is the LSByte of R1 in div equation, effectively "multiplies" it by 256

	DIV16x16 divider_bottom_hi, divider_bottom_lo, divider_top_hi, divider_top_lo	; (R1*256)/(R1+R2)
	mov a, [divider_top_lo]		;Divider_top_lo gets re-written with the result, it's actually the joystick X position
	mov [x_position], a

        mov a, 05h          		;Re-Enable 1ms,USBReset interrupts
        iowr Global_Interrupt

        jmp TaskLoop


;========================================================
;	Task 	10:	do nothing
;========================================================

Task10:
	jmp 	TaskLoop

;=========================================================
;	Task 11: Read Resistor Value for y_R1
;=========================================================

Task11:

	mov a, 30h
        iowr Global_Interrupt		;Enable only Capture interrupts

     ;**********************************************
     ;P0.0 = CMOS Hi-Z
     ;P0.1 = TTL Hi-Z
     ;P0.4 = High Sink
     ;P0.2/P0.3/P0.5/P0.6/P0.7 = CMOS Hi-Z
     ;Port0_Mode0 = 00010000
     ;Port0_Mode1 = 00010000
     ;Port0       = 00000010
     ;**********************************************

        mov a, 10h			;Set port and port mode values to discharge capacitor
        iowr Port0_Mode0		;See table above
        iowr Port0_Mode1
        mov a, 02h
        iowr Port0

	mov a, 00h			;wait about 500usec (6000 cycles) for Capture_timer A & B ISRs, then recharge cap and do math below
	mov [discharge_cnt], a
	.discharge_loop:
	    inc [discharge_cnt]
	    mov a, CCh
	    cmp a, [discharge_cnt]
	    iowr Watchdog
	    jnz .discharge_loop 

	mov a, 00h
	iowr Global_Interrupt		;Disable global interrupts

	mov a, 00h
	mov [rollover_bit], a		;rollover_bit set to 00h, but may need to change to 01h, if TTL value is < CMOS value

	iord Capture_TimerA_Falling	;Read CaptureTimerA 8 bits, CMOS captured time value (always earlier and usually the smaller value)
	mov [capture_a_temp], a
	iord Capture_TimerB_Falling	;Read CaptureTimerB 8 bits, TTL captured time value (always earlier and usually the larger value)
	mov [capture_b_temp], a

	sub a, [capture_a_temp]		;Subtract CMOS time from TTL time to see if timer rolled over
	jnc .no_timer_rollover		;If TTL value is > CMOS value, the timer did not roll over and we keep rollover_bit = 00h

	mov a, 01h			;In the exception that TTL time value is < CMOS time value, we make rollover_bit = 01h
	mov [rollover_bit], a	
	
	.no_timer_rollover:
        mov a, FEh			;Recharges the capacitor, P0.0 is Hi-Z CMOS always, others set to Strong Drive CMOS
        iowr Port0_Mode0
        iowr Port0_Mode1
        iowr Port0    
        
	SUB16x16 rollover_bit, capture_b_temp, 00h, capture_a_temp, always_zero, y_R1_diff2_new

        mov a, 05h          		;Re-Enable 1ms,USBReset interrupts
        iowr Global_Interrupt   

        jmp TaskLoop


;===========================================================
;	
;	Task 12:  Read Resistor Value for y_R2
;============================================================

Task12:

	mov a, 30h
        iowr Global_Interrupt		;Enable only Capture interrupts

     ;**********************************************
     ;P0.0 = CMOS Hi-Z
     ;P0.1 = TTL Hi-Z
     ;P0.5 = High Sink
     ;P0.2/P0.3/P0.4/P0.6/P0.7 = CMOS Hi-Z
     ;Port0_Mode0 = 00100000
     ;Port0_Mode1 = 00100000
     ;Port0       = 00000010
     ;**********************************************
        mov a, 20h			;Set port and port mode values to discharge capacitor
        iowr Port0_Mode0		;See table above
        iowr Port0_Mode1
        mov a, 02h
        iowr Port0

	mov a, 00h			;wait about 500usec (6000 cycles) for Capture_timer A & B ISRs, then recharge cap and do math below
	mov [discharge_cnt], a
	.discharge_loop:
	    inc [discharge_cnt]
	    mov a, CCh
	    cmp a, [discharge_cnt]
	    iowr Watchdog
	    jnz .discharge_loop 

	mov a, 00h
	iowr Global_Interrupt		;Disable global interrupts

	mov a, 00h
	mov [rollover_bit], a		;rollover_bit set to 00h, but may need to change to 01h, if TTL value is < CMOS value

	iord Capture_TimerA_Falling	;Read CaptureTimerA 8 bits, CMOS captured time value (always earlier and usually the smaller value)
	mov [capture_a_temp], a
	iord Capture_TimerB_Falling	;Read CaptureTimerB 8 bits, TTL captured time value (always earlier and usually the larger value)
	mov [capture_b_temp], a

	sub a, [capture_a_temp]		;Subtract CMOS time from TTL time to see if timer rolled over
	jnc .no_timer_rollover		;If TTL value is > CMOS value, the timer did not roll over and we keep rollover_bit = 00h

	mov a, 01h			;In the exception that TTL time value is < CMOS time value, we make rollover_bit = 01h
	mov [rollover_bit], a	
	
	.no_timer_rollover:
        mov a, FEh			;Recharges the capacitor, P0.0 is Hi-Z CMOS always, others set to Strong Drive CMOS
        iowr Port0_Mode0
        iowr Port0_Mode1
        iowr Port0    
                
	SUB16x16 rollover_bit, capture_b_temp, 00h, capture_a_temp, always_zero, y_R2_diff2_new

	; perform calculations to average the joystick readings (R_1st + R_2nd)/2

	AVERAGE8x8 y_R1_diff1_new, y_R1_diff2_new, y_R1_diff1_old, y_R1_diff2_old, RunningCarry, RunningSum, y_R1_average
	AVERAGE8x8 y_R2_diff1_new, y_R2_diff2_new, y_R2_diff1_old, y_R2_diff2_old, RunningCarry, RunningSum, y_R2_average

	; perform calculations below to average and normalize the joystick position for more accuracy (R1*256)/(R1+R2)

	ADD8x8wCarry y_R1_average, y_R2_average, divider_bottom_hi, divider_bottom_lo, y_R12_Sum_MSB,  y_R12_Sum_LSB
	mov a, [y_R1_average]		;Top of division equation gets R1, then it is "multiplied" by 256
	mov [divider_top_hi], a
	mov a, 00h
	mov [divider_top_lo], a		;This is the LSByte of R1 in div equation, effectively "multiplies" it by 256

	DIV16x16 divider_bottom_hi, divider_bottom_lo, divider_top_hi, divider_top_lo	; (R1*256)/(R1+R2)
	mov a, [divider_top_lo]		; this is actually the joystick y position
	mov [y_position], a

        mov a, 05h          		;Re-Enable 1ms,USBReset interrupts
        iowr Global_Interrupt   

	jmp 	TaskLoop

;========================================
;	Task 13: Read Resistor Value for z_R1
;========================================

Task13:

	mov a, 30h
        iowr Global_Interrupt		;Enable only Capture interrupts

     ;**********************************************
     ;P0.0 = CMOS Hi-Z
     ;P0.1 = TTL Hi-Z
     ;P0.6 = High Sink
     ;P0.2/P0.3/P0.4/P0.5/P0.7 = CMOS Hi-Z
     ;Port0_Mode0 = 01000000
     ;Port0_Mode1 = 01000000
     ;Port0       = 00000010
     ;**********************************************

        mov a, 40h			;Set port and port mode values to discharge capacitor
        iowr Port0_Mode0		;See table above
        iowr Port0_Mode1
        mov a, 02h
        iowr Port0

	mov a, 00h			;wait about 500usec (6000 cycles) for Capture_timer A & B ISRs, then recharge cap and do math below
	mov [discharge_cnt], a
	.discharge_loop:
	    inc [discharge_cnt]
	    mov a, CCh
	    cmp a, [discharge_cnt]
	    iowr Watchdog
	    jnz .discharge_loop 

	mov a, 00h
	iowr Global_Interrupt		;Disable global interrupts

	mov a, 00h
	mov [rollover_bit], a		;rollover_bit set to 00h, but may need to change to 01h, if TTL value is < CMOS value

	iord Capture_TimerA_Falling	;Read CaptureTimerA 8 bits, CMOS captured time value (always earlier and usually the smaller value)
	mov [capture_a_temp], a
	iord Capture_TimerB_Falling	;Read CaptureTimerB 8 bits, TTL captured time value (always earlier and usually the larger value)
	mov [capture_b_temp], a

	sub a, [capture_a_temp]		;Subtract CMOS time from TTL time to see if timer rolled over
	jnc .no_timer_rollover		;If TTL value is > CMOS value, the timer did not roll over and we keep rollover_bit = 00h

	mov a, 01h			;In the exception that TTL time value is < CMOS time value, we make rollover_bit = 01h
	mov [rollover_bit], a	
	
	.no_timer_rollover:
        mov a, FEh			;Recharges the capacitor, P0.0 is Hi-Z CMOS always, others set to Strong Drive CMOS
        iowr Port0_Mode0
        iowr Port0_Mode1
        iowr Port0    
        
	SUB16x16 rollover_bit, capture_b_temp, 00h, capture_a_temp, always_zero, z_R1_diff2_new

        mov a, 05h          		;Re-Enable 1ms,USBReset interrupts

⌨️ 快捷键说明

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