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

📄 rs232.asm

📁 用CY7C630芯片做USB转串口的汇编原程序,里面有编译生成的hex文件.写入芯片就可使用.最高波特率为24
💻 ASM
📖 第 1 页 / 共 2 页
字号:
  ;******************************************************************
  ;  RS232的传送与接收子程序 修改自serialb.asm
  ;******************************************************************
  
  ;label:          XPAGEON
  
  ;
  ; Variable memory allocations
  innerDelayOneCount:       equ 3Ch ; Delay One: Inner delay count 
  outerDelayOneCount:       equ 3Dh ; Delay One: Outer delay count 
  innerDelayCount:          equ 3Eh ; Delay: Inner delay count 
  outerDelayCount:          equ 3Fh ; Delay: Outer delay count 
  rxEnable:                 equ 40h ; Receiver Enabled Flag
  rxBitCount:               equ 42h ; Recieve Bits Count
  rxFrameCount:             equ 4dh ; Recieve Frame Count
  rxData:		          equ 46h ; Receive Data Holding Register
  rxBufPtr:                 equ 48h ; Receive Buffer Pointer
  rxFrameFlag:              equ 4Ah ; Framing Error Flag (0=No,1=Yes)
  rxPassCount:              equ 4Bh ; Transmit Buffer Pointer
  rxBuf:		          equ 50h ; Recieve Buffer Start
  rxBufEnd:                 equ 5Fh ; Recieve Buffer End
  txEnable:                 equ 4eh ; Transmiter Enabled Flag
  txBitCount:               equ 43h ; Transmit Bits Count
  txFrameCount:             equ 45h ; Transmit Frame Count
  txData:		          equ 47h ; Transmit Data Holding Register
  txBufPtr:                 equ 49h ; Transmit Buffer Pointer
  txBuf:		          equ 4Ch ; Transmit Buffer Start (4 Bytes)
  ;
  ; Constant declarations
  ; Transmit counter values
  txStart:	  equ 00h ; Start bit begin
  txBit0:	    equ 03h ; Start bit end
  txBit1:	    equ 07h ; Data bit 0 end
  txBit2:	    equ 0Ah ; Data bit 1 end
  txBit3:	    equ 0Dh ; Data bit 2 end
  txBit4:	    equ 10h ; Data bit 3 end
  txBit5:	    equ 14h ; Data bit 4 end
  txBit6:	    equ 17h ; Data bit 5 end
  txBit7:	    equ 1Ah ; Data bit 6 end
  txStop:	    equ 1Dh ; Data bit 7 end
  txComp:	    equ 20h ; Stop bit end
  ;
  txDataMask: equ FEh ; Mask to preserve inputs.
  ;
  ; Receive counter values
  rxStart:    equ 01h ; Start bit center
  rxBit0:	    equ 05h ; Data bit 0 center
  rxBit1:	    equ 09h ; Data bit 1 center
  rxBit2:	    equ 0Ch ; Data bit 2 center
  rxBit3:	    equ 0Fh ; Data bit 3 center
  rxBit4:	    equ 12h ; Data bit 4 center
  rxBit5:	    equ 16h ; Data bit 5 center
  rxBit6:	    equ 19h ; Data bit 6 center
  rxBit7:	    equ 1Ch ; Data bit 7 center
  rxStop:	    equ 1Fh ; Data bit 7 center
  rxComp:	    equ 22h ; Stop bit center
  lastrxBit:  equ 07h ; Last receive data bit count
  ;4
  ; Interrupt masks
  GPIO_intMask:	  equ 40h ; Mask for Port 0 GPIO interrupts.
  128us_intMask:    equ 02h ; Mask to enable 128us only.
  ;
  ; ASCII Characters for commands
  Car_Ret:          equ 0Dh ; Carriage Return Terminator
  Ln_Fd:            equ 0Ah ; Line Feed Terminator
  Asc_Space:        equ 20h ; ASCII Space Character
  ;
  ;************************************************************************
  ; If we are recieving a byte of data from the serial channel we need to
  ; go to the receive subroutine.
  
  Serial_ISR:
  	push	A                     ;
  	push X                      ;
  ;	mov	A, [rxEnable]           ; Load the receive enable flag.
  ;	cmp	A, 0 			              ; Check for receive in progress.
  ;	jnz	Increment_rxFrameCount  ; Yes, go receive.
  ;************************************************************************
  ; If we are sending a byte of data to the serial channel we need to
  ; go to the transmit subroutine.
  ;
  	mov	A, [txEnable]           ; Check for transmit in progress.
  	cmp	A, 0                    ;
  	jnz	Increment_txFrameCount  ; Yes, go transmit.
  	jmp	done_Serial             ; Default
  ;
  ; We are transmitting.
  Increment_txFrameCount:
  	inc	[txFrameCount]          ; Adjust frame count.
  	mov	A, [txFrameCount]       ; Put it in the accumulator
  	mov	[txBitCount], A         ; Save it as the new bit count
  	jmp	done_Serial             ; Finished
  ;
  ; We are receiving.
  ;Increment_rxFrameCount:
  	inc	[rxFrameCount]          ; Adjust frame count.
  	call	rxRoutine             ; Go get a character
  	pop X                       ;
  	mov	A, 00h                  ; Disable interrupts and return.
  	ipret	Global_Interrupt      ;
  ;
  ; Finish the interrupt handling
  done_Serial:
  	pop X
  	mov	A, 128us_intMask        ; Load 128us ISR Enable value
  	ipret	Global_Interrupt      ; Return and enable 128us ISR
  ;
  ;************************************************************************
  ; The GPIO interrupt will occur at the start of a receive data byte.
  ; The low going start bit will trigger the GPIO_ISR.
  ;
  GPIO_ISR:
  	push	A	; save the accumulator to stack
  	push	X	; save X on stack
  
  ; Three steps for detecting the start bit:
  ; 1.) Check that the receiver generated the interrupt.
  ;       iord    [Port0_Data]    
  ;       and     A, 80h          
  ;       jz      GPIO_ISR_Done   
  ;       jnz     GPIO_ISR_Done   
  
  ; 2.) Set the receive enable bit.
  	mov	A, 01h		          ; Load the accumulator.
          mov     [rxEnable], A             ; write to the receive enable flag.
  ;
  ; 3.) Clear the receive data register.
  	mov	A, 00h		          ; Clear the accumulator.
          mov     [rxData], A               ; Clear the receive data register.
  GPIO_ISR_Done:
          pop     x                         ; Restore the index register.
  	mov	A, 128us_intMask	  ; Load the 128us interrupt mask.
          ipret   Global_Interrupt          ; Return to caller.
                                            ; 128us interrupt enabled.
  ;
  ;************************************************************************
  ; During serial transfers data bit 0 is transmitted first.
  ; We will use Port 0 Bit 7 for receive and Bit 0 for transmit.
  ; Data will always be right shifted for either transmit or receive.
  ; Port 0 Bit 7 will be a falling edge sensitive GPIO_ISR input.
  ; Port 0 bits 6-0 and Port 1 bits 3-0 will be outputs.
  ;
  SerialInitialize:
  	push	A			        ; Save the accumulator.
  	push	X			        ; Save the index.
  	mov	A, FFh		      ; load accumulator with ones
  	iowr	Port0_Data		; output ones to port 0
  	iowr	Port1_Data		; output ones to port 1
  
  	mov	A, 00h		; load accumulator with zeros
  	iowr	Port0_Pullup	; enable port 0 pullups
  	iowr	Port1_Pullup	; enable port 1 pullups
  ;
  	iowr	Port0_Interrupt	; disable port 0 interrupts
  	iowr	Port1_Interrupt	; disable port 1 interrupts
  ;
  	mov	A, 08h		      ; load accumulator with med sink
  	iowr	Port0_Isink0	; minimum sink current Port0 bit 0
  ;
  ;	iowr	Watchdog		  ; clear watchdog timer
  ;
  ; Clear the serial channel counters.
  	mov	A, 00h
  	mov	[rxEnable], A	    ; Clear rxEnable Flag
  	mov	[rxBitCount], A	  ; Clear rx bit count.
  	mov	[rxFrameCount], A	; Clear rx frame counter.
  	mov	[rxBufPtr], A	    ; Clear rx buffer Pointer.
  	mov	[txEnable], A	    ; Clear txEnable Flag.
  	mov	[txBitCount], A	  ; Clear tx bit count.
  	mov	[txFrameCount], A	; Clear tx frame counter.
  	mov	[txBufPtr], A	    ; Clear tx buffer Pointer.
  
  	mov	A, 81h		        ; Enable port0 bit7 as input.
  	iowr	Port0_Data		  ; All other bits are outputs.
  	mov	A, 7Eh		        ; Select falling edge interrupt
  	iowr	Port0_Pullup	  ; on port0 bit7.
  	mov	A, 00h		        ;
  	iowr	Port0_Interrupt	; Disable port 0 bit 7 interrupt.
  
  	mov	[txBufPtr], A	      ; Reset tx buffer pointer.
  ; 	mov	[interrupt_mask], A ; Default all interrupts to disabled.
  	pop	X			              ; Restore the index.
  	pop	A			              ; Restore the accumulator.
  	ret				              ; Return to caller.
  
  ;************************************************************************
  ; TX_Data processing:
  ; This routine will write a byte of data.
  ; 1.) Send the active low Start bit.
  ; 2.) Send eight variable data bits.
  ; 3.) Send the active high Start bit.
  ; 4.) Stay in transmit until complete.
  ;************************************************************************
  ;
  txRoutine:
  ;  Prepare for the transmit.
          push    A                         ; save accumulator.
          mov     A, 01h                    ; Load txEnable Flag.
          mov     [txEnable], A             ; Store txEnable Flag.
          mov     A, [txFrameCount]         ; Get frame count.
          mov     [txBitCount], A           ; Save bit count.
  ;
  sendStart:
  ;  Write out the start bit. (active low)
  	mov	A, FEh		          ; Load tx Start bit.
          iowr    Port0_Data                ; Send tx Start bit.
          mov     A, 02h                    ; Load 128us ISR Enable value.
          iowr    Global_Interrupt          ; Enable 128us ISR.
  ;
  ;  Check the bit count and send a bit if required.
  check_tx_bit:
  	mov	A, [txBitCount]	; Get frame count
  	cmp	A, txBit0		; tx bit 0 at frame count=03h
  	jz	sendtxBit		; Go send data bit
  	cmp	A, txBit1		; tx bit 1 at frame count=07h
  	jz	sendtxBit		; Go send data bit
  	cmp	A, txBit2		; tx bit 2 at frame count=0Ah
  	jz	sendtxBit		; Go send data bit
  	cmp	A, txBit3		; tx bit 3 at frame count=0Dh
  	jz	sendtxBit		; Go send data bit
  	cmp	A, txBit4		; tx bit 4 at frame count=10h
  	jz	sendtxBit		; Go send data bit
  	cmp	A, txBit5		; tx bit 5 at frame count=14h
  	jz	sendtxBit		; Go send data bit.
  	cmp	A, txBit6		; tx bit 6 at frame count=17h.
  	jz	sendtxBit		; Go send data bit.
  	cmp	A, txBit7		; tx bit 7 at frame count=1Ah.
  	jz	sendtxBit		; Go send data bit.
  	cmp	A, txStop		; tx Stop at frame count=1Dh.
  	jz	sendStop		; Go send stop bit.
  	cmp	A, txComp		; tx Stop at frame count=20h.
  	jz	txEnd			  ; Go send end transmit.
  	jmp	check_tx_bit	; Wait for the next interrupt.
  ;
  sendtxBit:
  ;  Transmit the current data bit and adjust for next pass.
  	mov	A, [txData]		  ; Get the current data.
  	or	A, txDataMask	  ; Mask out inputs.
  	iowr	Port0_Data	  ; Output the current data bit.
  	mov	A, [txData]		  ; Get the current data.
  	asr	A			          ; Align next data bit.
  	mov	[txData], A		  ; Save adjusted data.
  	mov	A, FFh		      ; Fill the accumulator.
  	mov	[txBitCount], A ; Write to bit counter.
  	jmp	check_tx_bit	  ; Wait for next bit time.
  ;
  sendStop:
  ;  The data has been sent, now send the stop bit.
  	mov	A, FFh		      ; Load stop bit.
  	iowr	Port0_Data		; Send stop bit.
  	jmp	check_tx_bit	  ; Go to end of transmit.
  ;
  txEnd:
  ;  The last data bit has been sent.
  ;  Clean up and return. 
  ;	mov	A, 00h		          ; Clear the accumulator.
  ;       mov     [interrupt_mask],A        ; Load into the mask.
  	mov	a,[interrupt_mask]
          iowr    Global_Interrupt          ; Disable all interrupts.
  	mov	A, 00h		          ; Load tx Start bit.
          mov     [txBitCount], A           ; Clear tx bit count.
  	mov	[txFrameCount], A	  ; Clear tx frame counter.
          mov     [txEnable], A             ; Clear the tx enadle flag.
  	mov	[rxFrameCount], A	  ; Clear rx frame counter.

⌨️ 快捷键说明

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