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

📄 p18demo.asm

📁 详细讲解了PIC18单片机的各个功能模块的使用方法和运用技巧
💻 ASM
📖 第 1 页 / 共 2 页
字号:
	btfss	STATUS,Z
	bra	get_temp
	movlw	0x00			;temp is ready for reading
	movwf	cmd_byte		;send temp register command
	bra	temp_now

convert_temp
	movwf	temperature
	call	bin_bcd			;get temp ready for LCD
	call	LCDLine_1
	
	movlw	A'T'			;send "Temp=" to LCD
	movwf	temp_wr	
	call	d_write
	movlw	A'e'
	movwf	temp_wr	
	call	d_write
	movlw	A'm'
	movwf	temp_wr	
	call	d_write
	movlw	A'p'
	movwf	temp_wr	
	call	d_write
	movlw	0x20			;space
	movwf	temp_wr	
	call	d_write
	movlw	A'='
	movwf	temp_wr	
	call	d_write

	movlw	0x20			;space
	movwf	temp_wr	
	call	d_write

	movf	MSD,W			;send high digit
	movwf	temp_wr
	call	d_write
	movf	MsD,W			;send middle digit
	movwf	temp_wr
	call	d_write
	movf	LSD,W			;send low digit
	movwf	temp_wr
	call	d_write
	movlw	A'C'			;send "C" for celcius
	movwf	temp_wr	
	call	d_write

	movlw	0x20			;space
	movwf	temp_wr	
	call	d_write
	movlw	0x20			;space
	movwf	temp_wr	
	call	d_write
	movlw	0x20			;space
	movwf	temp_wr	
	call	d_write
	
	call	LCDLine_2		;send "RB0 = Exit" to LCD
	movlw	.144
	movwf	ptr_pos
	call	stan_char_2
	
	btfss	select			;wait for RB0 release
	bra	$-2
	call	delay_100ms
	btfss	select			;exit ?
	bra	menu_clock		;YES, goto main menu
	btfsc	PIR1,TMR1IF		;2 second overflow occur ??
	call	write_eeprom		;YES
	bra	get_temp		;NO, get temperature again

	
;----------------- CLOCK ------------------------------------------

clock
	btfss	select			;wait for RB0 button release
	bra	$-2
	movlw	0x0F			;intitialize TIMER1
	movwf	T1CON
	clrf	seconds
	clrf	minutes
	clrf	hours
overflow	
	bcf	PIR1,TMR1IF
	movlw	0x80		
	movwf	TMR1H			;load regs for 1 sec overflow
	clrf	TMR1L

	incf	seconds,F		;increment seconds
	movf	seconds,W
	sublw	.60
	btfss	STATUS,Z		;increment minutes ?
	bra	clk_done
	incf	minutes,F		
	clrf	seconds

	movf	minutes,W
	sublw	.60
	btfss	STATUS,Z		;increment hours ?
	bra	clk_done	
	incf	hours,F			
	clrf	minutes

	movf	hours,W
	sublw	.13
	btfss	STATUS,Z
	bra	clk_done
	movlw	.1			;start a new 12 hour period
	movwf	hours
clk_done
	movf	hours,W			;send hours to LCD
	call	bin_bcd

	call	LCDLine_1		;place time on line 1

	movf	MsD,W			;send middle digit
	movwf	temp_wr
	call	d_write
	movf	LSD,W			;send low digit
	movwf	temp_wr
	call	d_write	
	movlw	0x3A			;send  :   colon
	movwf	temp_wr
	call	d_write

	movf	minutes,W		;send minutes to LCD
	call	bin_bcd

	movf	MsD,W			;send middle digit
	movwf	temp_wr
	call	d_write
	movf	LSD,W			;send low digit
	movwf	temp_wr
	call	d_write	
	movlw	0x3A			; send :   colon
	movwf	temp_wr
	call	d_write

	movf	seconds,W		;send seconds to LCD
	call	bin_bcd

	movf	MsD,W			;send middle digit
	movwf	temp_wr
	call	d_write
	movf	LSD,W			;send low digit
	movwf	temp_wr
	call	d_write

	movlw	0x20			;send 3 spaces after 00:00:00
	movwf	temp_wr
	call	d_write
	movlw	0x20
	movwf	temp_wr
	call	d_write
	movlw	0x20
	movwf	temp_wr
	call	d_write
	
	movlw	.112			;send "RA4=Dn RB0=Menu" to LCD
	movwf	ptr_pos
	call	stan_char_2

	btfss	scroll			;set time ??
	bra	set_time

	btfss	select			;return to main menu ??
	bra	menu
	
	btfss	PIR1,TMR1IF		;has timer1 overflowed ?	
	bra	$-2			;NO, wait til overflow
	bra	overflow		;YES

	return
;*******************************************************************


;************************** ROUTINES ******************************
;******************************************************************
;******************************************************************
		
;----Standard code, Place characters on line-1--------------------------
stan_char_1
	call	LCDLine_1		;mvoe cursor to line 1 
	movlw	.16			;1-full line of LCD
	movwf	ptr_count
	movlw	UPPER stan_table
	movwf	TBLPTRU
	movlw	HIGH stan_table
	movwf	TBLPTRH
	movlw	LOW stan_table
	movwf	TBLPTRL
	movf	ptr_pos,W
	addwf	TBLPTRL,F
	clrf	WREG
	addwfc	TBLPTRH,F
	addwfc	TBLPTRU,F

stan_next_char_1
	tblrd	*+
	movff	TABLAT,temp_wr			
	call	d_write			;send character to LCD

	decfsz	ptr_count,F		;move pointer to next char
	bra	stan_next_char_1

	movlw	"\n"			;move data into TXREG
	movwf	TXREG			;next line
	btfss	TXSTA,TRMT		;wait for data TX
	goto	$-2
	movlw	"\r"			;move data into TXREG
	movwf	TXREG			;carriage return
	btfss	TXSTA,TRMT		;wait for data TX
	goto	$-2

	return

;----Standard code, Place characters on line-2--------------------------
stan_char_2	
	call	LCDLine_2		;move cursor to line 2 
	movlw	.16			;1-full line of LCD
	movwf	ptr_count
	movlw	UPPER stan_table
	movwf	TBLPTRU
	movlw	HIGH stan_table
	movwf	TBLPTRH
	movlw	LOW stan_table
	movwf	TBLPTRL
	movf	ptr_pos,W
	addwf	TBLPTRL,F
	clrf	WREG
	addwfc	TBLPTRH,F
	addwfc	TBLPTRU,F

stan_next_char_2
	tblrd	*+
	movff	TABLAT,temp_wr
	call	d_write			;send character to LCD

	decfsz	ptr_count,F		;move pointer to next char
	bra	stan_next_char_2

	movlw	"\n"			;move data into TXREG
	movwf	TXREG			;next line
	btfss	TXSTA,TRMT		;wait for data TX
	goto	$-2
	movlw	"\r"			;move data into TXREG
	movwf	TXREG			;carriage return
	btfss	TXSTA,TRMT		;wait for data TX
	goto	$-2

	return
;----------------------------------------------------------------------


;------------------ 100ms Delay --------------------------------
delay_100ms
	movlw	0xFF
	movwf	temp_1
	movlw	0x83
	movwf	temp_2

d100l1
	decfsz	temp_1,F
	bra	d100l1
	decfsz	temp_2,F
	bra	d100l1
	return

;---------------- 1s Delay -----------------------------------
delay_1s
	movlw	0xFF
	movwf	temp_1
	movwf	temp_2
	movlw	0x05
	movwf	temp_3
d1l1
	decfsz	temp_1,F
	bra	d1l1
	decfsz	temp_2,F
	bra	d1l1
	decfsz	temp_3,F
	bra	d1l1
	return	

;---------------- Set Current Time ----------------------------
set_time
	movlw	.128			;send "RA4= --> RBO= ++" to LCD
	movwf	ptr_pos
	call	stan_char_2
set_time_again
	btfss	scroll			;wait for button release
	bra	$-2

	call	LCDLine_1		;start at 0x00 on LCD

	btfss	select			;wait for RB0 button release
	bra	$-2
	call	delay_100ms			
	btfss  	select			;increment hours (tens) ?
	bra	inc_hours
	bra	next_digit
inc_hours	
	incf	hours
	movf	hours,W			;check if hours has passed 12 ?
	sublw	.13
	btfss	STATUS,Z
	bra	next_digit
	clrf	hours			;YES, reset hours to 00
next_digit
	btfss	scroll			;move to next digit
	bra	inc_mins
	movf	hours,W		

	call	bin_bcd			;get hours ready for display
	
	movf	MsD,W			;send tens digit
	movwf	temp_wr
	call	d_write
	movf	LSD,W			;send ones digit
	movwf	temp_wr
	call	d_write	
	movlw	0x3A			;send   :   colon
	movwf	temp_wr
	call	d_write

	bra	set_time_again
	
inc_mins
	btfss	scroll			;wait for RA4 button release
	bra	$-2
	call	LCDLine_1
	movlw	0x14			;shift cursor to right 3 places
	movwf	temp_wr
	call	i_write
	movlw	0x14
	movwf	temp_wr
	call	i_write
	movlw	0x14
	movwf	temp_wr
	call	i_write
	
	btfss	select			;wait for RB0 button release
	bra	$-2
	call	delay_100ms
	btfss  	select			;increment minutes (tens) ?
	bra	inc_minutes
	bra	next_digit?
inc_minutes	
	incf	minutes
	movf	minutes,W		;check if hours has passed 12 ?
	sublw	.60
	btfss	STATUS,Z
	bra	next_digit?
	clrf	minutes
next_digit?
	btfss	scroll			;move to next digit
	bra	set_time_done
	movf	minutes,W
		
	call	bin_bcd			;get minutes ready for display
	
	movf	MsD,W			;send tens digit
	movwf	temp_wr
	call	d_write
	movf	LSD,W			;send ones digit
	movwf	temp_wr
	call	d_write	
	movlw	0x3A			;send  :   colon
	movwf	temp_wr
	call	d_write	
	bra	inc_mins

set_time_done
	btfss	scroll			;wait for RA4 button release
	bra	$-2
	bra	overflow
	
;---------------- Binary (8-bit) to BCD -----------------------
;		255 = highest possible result
bin_bcd
	clrf	MSD
	clrf	MsD
	movwf	LSD		;move value to LSD
ghundreth	
	movlw	.100		;subtract 100 from LSD
	subwf	LSD,W
	btfss	STATUS,C	;is value greater then 100
	bra	gtenth		;NO goto tenths
	movwf	LSD		;YES, move subtraction result into LSD
	incf	MSD,F		;increment hundreths
	bra	ghundreth	
gtenth
	movlw	.10		;take care of tenths
	subwf	LSD,W
	btfss	STATUS,C
	bra	over		;finished conversion
	movwf	LSD
	incf	MsD,F		;increment tenths position
	bra	gtenth
over				;0 - 9, high nibble = 3 for LCD
	movf	MSD,W		;get BCD values ready for LCD display
	xorlw	0x30		;convert to LCD digit
	movwf	MSD
	movf	MsD,W
	xorlw	0x30		;convert to LCD digit
	movwf	MsD
	movf	LSD,W
	xorlw	0x30		;convert to LCD digit
	movwf	LSD
	retlw	0

;---------------- Binary (16-bit) to BCD -----------------------
;		xxx = highest possible result
bin16_bcd
	                       ; Takes number in NumH:NumL 
                                ; Returns decimal in 
                                ; TenK:Thou:Hund:Tens:Ones 
        swapf   NumH,W 
        andlw   0x0F
        addlw   0xF0
        movwf   Thou 
        addwf   Thou,F 
        addlw   0xE2 
        movwf   Hund 
        addlw   0x32 
        movwf   Ones 

        movf    NumH,W 
        andlw   0x0F 
        addwf   Hund,F 
        addwf   Hund,F 
        addwf   Ones,F 
        addlw   0xE9 
        movwf   Tens 
        addwf   Tens,F 
        addwf   Tens,F 

        swapf   NumL,W 
        andlw   0x0F 
        addwf   Tens,F 
        addwf   Ones,F 

        rlcf     Tens,F 
        rlcf     Ones,F 
        comf    Ones,F 
        rlcf     Ones,F 

        movf    NumL,W 
        andlw   0x0F 
        addwf   Ones,F 
        rlcf     Thou,F 

        movlw   0x07 
        movwf   TenK 

        movlw   0x0A                             ; Ten 
Lb1: 
        decf    Tens,F 
        addwf   Ones,F 
        btfss   STATUS,C 
         bra   Lb1 
Lb2: 
        decf    Hund,F 
        addwf   Tens,F 
        btfss   STATUS,C 
         bra   Lb2 
Lb3: 
        decf    Thou,F 
        addwf   Hund,F 
        btfss   STATUS,C
         bra   Lb3 
Lb4: 
        decf    TenK,F 
        addwf   Thou,F 
        btfss   STATUS,C 
         bra   Lb4 

        retlw   0


;---------------------------- EEPROM WRITE -------------------------------
write_eeprom	
	bsf	SSPCON2,SEN		;start bit
	btfsc	SSPCON2,SEN
	goto	$-2	
	movlw	B'10100000'		;send control byte (write)
	movwf	SSPBUF
	ssprw
	btfsc	SSPCON2,ACKSTAT		;ack?
	goto	$-2

	movlw	0x00			;send slave address HIGH byte
	movwf	SSPBUF
	ssprw
	btfsc	SSPCON2,ACKSTAT		;ack?
	goto	$-2

	movlw	0x05			;send slave address LOW byte(0x0005)
	movwf	SSPBUF
	ssprw
	btfsc	SSPCON2,ACKSTAT		;ack?
	goto	$-2

	movf	temperature,w		;send slave DATA = temperature
	movwf	SSPBUF
	ssprw
	btfsc	SSPCON2,ACKSTAT		;ack?
	goto	$-2

	bsf	SSPCON2,PEN		;stop bit
	btfsc	SSPCON2,PEN
	goto	$-2
		
	bcf	PIR1,TMR1IF		;clear TIMER1 overflow flag
	clrf	TMR1L			;clear registers for next overflow
	clrf	TMR1H

	return

;*********************************************************************
	end	

⌨️ 快捷键说明

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