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

📄 uart_sw.asm

📁 ST开发的代码程序 ST开发的代码程序 ST开发的代码程序
💻 ASM
📖 第 1 页 / 共 2 页
字号:
st7/                                    

;**************** (c) 1997 STMicroelectronics **********************
;
;PROJECT:		Application Note : Software UART using 12-bit ART
;COMPILER: 		ST7 ASSEMBLY CHAIN
;MODULE: 		UART_SW.asm
;CREATION DATE: 	30th June 2003
;AUTHOR: 		Micro Application / STMicroelectronics
;
;-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
;
;   THE SOFTWARE INCLUDED IN THIS FILE IS FOR GUIDANCE ONLY. STMicroelectronics 
;   SHALL NOT BE HELD LIABLE FOR ANY DIRECT, INDIRECT OR CONSEQUENTIAL 
;   DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM USE OF THIS SOFTWARE.
;    
;-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
;
;DESCRIPTION:	Software implementation of UART, using only 12 bit ART & 2 I/O's.
;               This driver software is validated on ST7FLITE0 with internal 8Mhz clock
;               having main features,
;               - RS232 Half Duplex
;               - Async. operation
;               - Flexible data format              
;               For basic functionality please read AN first.
; 		The software consists of main 4 routines:
;		-	TX_data
;		-	RX_data
;		-	EI1_Interrupt
;		-	OPCOMP_int  
;               The file LITE0_Map.INC have hardware register map
;
;-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
;| VERSION 1.0:										|
;|	- First release									|
;| 											|
;+--------------------------------------------------------------------------------------+


	TITLE	"UART_SW.asm" 
				; This title will appear on each
				; page of the listing file.          

	MOTOROLA     		; This directive forces the Motorola 
				; format for the assembly (default).
         
	#INCLUDE "LITE0Map.inc" ; This file includes the different registers
				; mapped into Lite0 device
;******************************************************************************
;       Macro definitions
;******************************************************************************
; bit definition of PWM0CSR register ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

 #define        CMPF0   $0      ; CMPF0 PWM0 Compare Flag
 				; 1= Upcounter value matches DCR value.

; bit definition of PWMCR register ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 #define        OE0	$0      ; OE0 PWM0 Output enable
 				; 0= Output compare mode

; bit definition of ATCSR register ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 

 #define        CMPIE    $0      ; CMPIE Compare Interrupt Enable
 				 ; 1= CMPF interrupt enabled.
 #define        CK0      $3      ; Counter Clock Selection
 				 ; When CK0=0, CK1=1 then fCPU.
 #define        CK1      $4      ; Counter Clock Selection
 
; bit definition of PORTA register used for Tx & Rx~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 #define        TxD	$3       ; PortA3 for tx data
 #define	RxD	$7       ; PortA7 for receive data

 
; bit definition of the sci_status register ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

;             -----------------------------------------------------------------
; sci_status  |       |       |       |       |       |       |       |       |
;             |       |       |       |  TE   |  BS   |  RE   |  BR   |  SP   |
;  register   |       |       |       |       |       |       |       |       |
;             -----------------------------------------------------------------


 #define        SP      $0      ; Sampling Phase of reception mode.
 #define        BR      $1      ; Byte Received flag.
 #define        RE      $2      ; Reception Enable.
 #define        BS      $3      ; Byte Sent flag.
 #define        TE      $4      ; Transmission Enable.

; definition of masks

 #define    Mask_ICAP1   $01	; Mask of PA7 for the sampling on PB7 pin 
 #define    PA7_Int_conf $08	; =EICR for Falling edge only Interrupt 
;+--------------------------------------------------------------------------------------+
;| CONFIGURATION: Variables and constants				|
;|		  defined and used as local ones					|
;+--------------------------------------------------------------------------------------+

	BYTES				; Following addresses are 8 bit length

;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

	segment byte at 0-7F  'periph'	; Defines peripherals registers segment.

	segment byte at 80-BF 'ram0'	; Defines Zero Page segment

; Variable definitions ********************************************************

.tx_byte        DC.B    0       ; Byte to transmit.
.rx_byte        DC.B    0       ; Reception byte.
.bit_count      DC.B    0       ; Indicate the bit number. 
.sci_status     DC.B    0       ; 5 bits software register that indicates the 
                                ; communication mode.
                                ; See above for the bits definition.
.posn_bit       DC.B    0       ; Indicate the position of the bit to receive.
.tmp_var        DC.B    0       ; Temporary variable used in program
.data_length	DC.B	0       ; data_length + stop_bit


	WORDS			; Following addresses are 16 bit length

;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

	segment byte at C0-FF	'stack'	; Defines Stack segment

	segment byte at FA00-FFDF 'rom'	; Defines Rom Segment

;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
         
	WORDS
     
	segment 'rom'

; Constante definitions *******************************************************
; Bit delay count according to baudrate****************************************

; Baud rate selection for a 8MHz internal clock ~~~~~~~~~~~~~~~~~~~~~~~~~~~
; Low and high bytes of the number of timer cycles to create a delay of 
; one byte for a transmission speed of 19200bd.
;.del_1bl        DC.B    $a0     
;.del_1bh        DC.B    $01
; Baud rate selection for a 8MHz internal clock ~~~~~~~~~~~~~~~~~~~~~~~~~~~
; Low and high bytes of the number of timer cycles to create a delay of 
; one byte for a transmission speed of 9600bd.
;.del_1bl        DC.B    $41     
;.del_1bh        DC.B    $03
; Baud rate selection for a 8MHz internal clock ~~~~~~~~~~~~~~~~~~~~~~~~~~~
; Low and high bytes of the number of timer cycles to create a delay of 
; one byte for a transmission speed of 4800bd.
;.del_1bl        DC.B    $82     
;.del_1bh        DC.B    $06
; Baud rate selection for a 8MHz internal clock ~~~~~~~~~~~~~~~~~~~~~~~~~~~
; Low and high bytes of the number of timer cycles to create a delay of 
; one byte for a transmission speed of 2400bd.
.del_1bl        DC.B    $05	    
.del_1bh        DC.B    $0d                                                    

; Half Bit delay count according to baudrate**********************************

; Delay to sample in the middle of the first byte ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; Low and high bytes to sample in the middle of the first byte for 
; a transmission speed of 19200bd.
;.del_sampl      DC.B    $10		
;.del_samph      DC.B    $00		
; Delay to sample in the middle of the first byte ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; Low and high bytes to sample in the middle of the first byte for 
; a transmission speed of 9600bd.
;.del_sampl      DC.B    $a0
;.del_samph      DC.B    $01
; Delay to sample in the middle of the first byte ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; Low and high bytes to sample in the middle of the first byte for 
; a transmission speed of 4800bd.
;.del_sampl      DC.B    $41
;.del_samph      DC.B    $03
; Delay to sample in the middle of the first byte ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; Low and high bytes to sample in the middle of the first byte for 
; a transmission speed of 2400bd.
.del_sampl      DC.B     $82	
.del_samph      DC.B     $06	

; Configuration of Data length + no. of STOP bits~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

;.stop_bit       DC.B    $01 ; STOP bits to transmit/receive = One STOP bit    
.stop_bit        DC.B    $02 ; STOP bits to transmit/receive = Two STOP bits
;.txrx_data_lnth DC.B	 $08 ; data bits to transmit/receive = 7 data bits + START bit 
.txrx_data_lnth DC.B	 $09 ; data bits to transmit/receive = 8 data bits + START bit



; Program code ****************************************************************

;       ********************************************
;       *                                          *
;       *          MAIN-ROUTINES SECTION           *
;       *                                          *
;       ********************************************


.main	
 	CALL	UART_init	; Init I/O's & variables

	LD      A,#'$'          ; '$' is
        LD      tx_byte,A       ; first byte to send.
        CALL	TX_data		; transmit data in tx_byte 

; Main loop, receive & then transmit received data~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
rx_tx    
	CALL	RX_data         ; Receive data in rx_byte
        LD      A,rx_byte       ; 
        LD      tx_byte,A       ; Transmit byte=Received byte.
	CALL	TX_data         ; Transmit data
        JP      rx_tx           ; loop to Rx_Tx

	
;+--------------------------------------------------------------------------------------+
; Program code ****************************************************************

;       ********************************************
;       *                                          *
;       *       SUB-ROUTINES LIBRARY SECTION       *
;       *                                          *
;       ********************************************

;------------------------------------------------------------------------------
;ROUTINE NAME : Init_UART
;INPUT/OUTPUT : None.
;DESCRIPTION  : Init I/O's & variables used for UART
;COMMENTS     : 
;------------------------------------------------------------------------------

UART_init                 
	
;I/O init 
       
        BSET	PADDR,#TxD      ;PA3=TxD defined as output push-pull
        BSET	PAOR,#TxD
        
	BRES	PAOR,#RxD       ;PA7=RxD defined as input floating
        BRES	PADDR,#RxD                     
 
        BSET	PADR,#TxD       ;TxD High

	LD	A,txrx_data_lnth;
        ADD	A,stop_bit
	LD	data_length,A   ;START+data+STOP bits  
        
     	RET     
     	
;------------------------------------------------------------------------------
;ROUTINE NAME : RX_data 
;INPUT/OUTPUT : rx_byte
;DESCRIPTION  : Receive mode routine
;COMMENTS     : Receive data & store it in rx_byte
;------------------------------------------------------------------------------   	
RX_data 

	LD      A,#$04          ; 
        LD      sci_status,A    ; Receive mode.
        LD      A,#0
        LD      bit_count,A     ; Clear bit number (LSB first)
        LD      rx_byte,A       ; Clear receive byte var
        LD      A,#1
        LD      posn_bit,A      ; Initialize posn_bit 

	LD	A,#PA7_Int_conf 
	LD	EICR,A          ; Configure EICR
	        
        BSET	PAOR,#RxD       ;PA7 defined as pull up interrupt input
        BRES	PADDR,#RxD        
	
        RIM                     ; Enable interrupt

; Loop for receiving byte ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
recv_wait
        BTJF    sci_status,#BR,recv_wait    ; Byte received? 

        SIM                     ; Set interrupt mask.

	RET 

;------------------------------------------------------------------------------
;ROUTINE NAME : TX_data  
;INPUT/OUTPUT : tx_byte.
;DESCRIPTION  : Transmit routine
;COMMENTS     : Transmit data in var. tx_byte
;------------------------------------------------------------------------------   		
TX_data 
        LD      A,#$10
        LD      sci_status,A    ; Enter transmission mode.

⌨️ 快捷键说明

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