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

📄 1.asm

📁 this program based on MCS51 and converts a serial data to parallel on its port
💻 ASM
字号:
;Version 1.1
;============================================================================
;Definations
        PCON            equ     087h

	T0_PRESET	equ	9216		;11,059,200/12=921600 => 9216=10ms
	T1_PRESET	equ	0FDh		;9600 bps
	LED_TX		equ	0B4h		;P3.4
	LED_RX		equ	0B5h		;P3.5
;============================================================================
;Byte allocations	internal ram
	SRBUF		equ	030h		;receive buffer
	SSBUF		equ	031h		;send buffer
;============================================================================
;Bit allocations
        RI_flag         equ     00h             ;receive interrupt flag
        TI_flag         equ     01h             ;transmit interrupt flag
        TF0_flag        equ     02h             ;timer0 overflow
        TF1_flag        equ     03h             ;timer1 overflow
        TX_ready        equ     04h             ;transmit ready
;============================================================================
	org	00h
	jmp	Main
;============================================================================
	org	03h
	jmp	External0_interrupt		;external 0 interrupt vector
;============================================================================
	org	0Bh
	jmp	TimerA_interrupt		;timer 0 interrupt vector
;============================================================================
	org	013h
	jmp	External1_interrupt		;external 1 interrupt vector
;============================================================================
	org	01Bh
	jmp	TimerB_interrupt		;timer 1 interrupt vector
;============================================================================
	org	023h				;serial interrupt vector
	jmp	Serial_interrupt		;goto serial interrup
;============================================================================
	org 	030h				;start from vector 30

Main:
	call	initialize_variables		;clean and preset variables
	call	Serial_initialize		;prepare serial port
	call	Timer_initialize		;prepare timers
	setb	TR0				;start timer generator

Loop:
        call    timer0_process                  ;execute timer0 evetns
        call    serial_process                  ;execute serial events
loop_x:
	jmp	Loop
;============================================================================
initialize_variables:
	mov	P1, #0FFh
	ret
;============================================================================
Serial_process:
Serial_process_RX:
        jnb     RI_flag, Serial_process_TX
	call	Serial_receive			;get received byte
	mov	P1, A				;put value on port
	mov	ssbuf, A
	setb	tx_ready
Serial_process_TX:
        clr     TI_flag                         ;clear transmit flag
Serial_process_que:
        jnb	TX_ready, Serial_process_x	;sending is in progress wait...
	call	Serial_send			;send it
Serial_process_x:
        ret
;============================================================================
;Send a character
Serial_send:
	jb	TX_ready, Serial_send_send	;send buffer is ready
	clr	c				;carry 0 = error
	jmp	Serial_send_exit		;exit
Serial_send_send:
	clr	TX_ready			;clear transfer interrupt flag
	mov	SBUF, SSBUF			;load serial buffer from SSBUF
	setb	c				;carry 1 = done
	clr	LED_TX				;turn signal lamp on
Serial_send_exit:
	ret					;exit
;============================================================================
;Receive character receive result in carry 0=false, 1=true
Serial_receive:
        jb      RI_flag, Serial_receive_receive ;character is received
	clr	c				;carry 0 = error
	jmp	Serial_receive_exit		;exit
Serial_receive_receive:
        clr     RI_flag                         ;clear receive interrupt
	mov	A, SRBUF			;load A from serial buffer
	setb	c				;carry 1 = done
	clr	LED_RX				;turn signal lamp on
Serial_receive_exit:
	ret					;exit
;============================================================================
timer0_process:
        jnb     TF0_flag, timer0_process_x      ;if timer0 has overflown
	clr	TF0_flag			;clear timer0 overflow flag

	setb	LED_TX				;turn led off
	setb	LED_RX				;turn led off
timer0_process_x:
        ret
;============================================================================
External0_interrupt:
	reti
;============================================================================
TimerA_interrupt:
	clr	TF0				;clear timer0 overflow flag
        mov     TL0, #LOW(T0_preset)            ;reload timer from default value
        mov     TH0, #HIGH(T0_preset)           ;reload timer from default value
        setb    TF0_flag
	reti
;============================================================================
External1_interrupt:
	reti
;============================================================================
TimerB_interrupt:
	reti
;============================================================================
Serial_interrupt:
Serial_interrupt_RX:
        jnb     RI, Serial_interrupt_TX
        clr     RI
        setb    RI_flag
	mov	SRBUF, SBUF
Serial_interrupt_TX:
        jnb     TI, Serial_interrupt_x
        clr     TI
        setb    TI_flag
Serial_interrupt_x:
	reti
;============================================================================
;Timer settings for second generating and serial baud rate
Timer_initialize:
	mov	A, #00000001b
	orl	TMOD, A				;timer0 16bit
        mov     TL0, #LOW(T0_preset)            ;FC7Ch=-900
        mov     TH0, #HIGH(T0_preset)           ;1024 means 1.1ms for 11.0592MHz crystal
        clr     PT0                             ;disable interrupt priority
        setb    ET0                             ;timer0 interrupt enable
	setb	EA				;interrupt enable
;	clr	ET1				;timer1 interrupt disable
	ret
;============================================================================
;Initializing Serial port 19200 b/s, UART 8bit,
Serial_initialize:
	mov	A, #00100000b
	orl	TMOD, A				;timer1 8bit auto-reload
        mov     A, PCON                         ;prepare to set SMOD
        clr     ACC.7                           ;SMOD off
        mov     PCON, A                         ;replace SMOD
        mov     TL1, #T1_preset                 ;baud rate 9600 bps
        mov     TH1, #T1_preset                 ;baud rate 9600 bps
	mov	SCON, #01010000b		;serial control byte
        clr     PS                              ;serial interrupt priority
        setb    ES                              ;serial interrupt enable
	setb	TR1				;start baudrate generator
        setb    EA                              ;interrupt enable
        clr	RI_flag			        ;receive interrupt flag
        clr	TI_flag			        ;transmit interrupt flag
	setb	TX_ready			;transmit ready
	ret					;exit
;============================================================================
	end

⌨️ 快捷键说明

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