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

📄 p18demo.asm

📁 详细讲解了PIC18单片机的各个功能模块的使用方法和运用技巧
💻 ASM
📖 第 1 页 / 共 2 页
字号:
;************************************************************************
;*	Microchip Technology Inc. 2002					*
;*	Assembler version: 2.0000					*
;*	Filename: 							*
;*		p18demo.asm (main routine)   				*
;*	Dependents:							*
;*		p18lcd.asm						*
;*		p18math.asm						*
;*		16f877.lkr						*
;*	March 14,2002							*
;* 	PICDEM 2 PLUS DEMO code. The following functions are included 	*
;*	with this code:							*
;*		1. Voltmeter						*
;*			The center tap of R16 is connected to RA0, the	*
;*			A/D converter converts this analog voltage and	*
;*			the result is displayed on the LCD in a range	*
;*			from 0.00V - 5.00V.				*
;*		2. Buzzer						*
;*			The Piezo buzzer is connected to RC2 and is	*
;*			driven by the CCP1 module. The period and duty	*
;*			cycle are adjustable on the fly through the LCD	*
;*			and push-buttons.				*
;*		3. Temperature						*
;*			A TC74 Serial Digital Thermal Sensor is used to	*
;*			measure ambient temperature. The PIC and TC74	*
;* 			communicate using the MSSP module. The TC74 is	*
;*			connected to the SDA & SCL I/O pins of the PIC	*
;*			and functions as a slave. Every 2 seconds, the	*
;*			temperature is logged into the external EEPROM  *
;*			in a specific memory location.			*
;*		4. Clock						*
;*			This function is a real-time clock. When the	*
;*			mode is entered, time begins at 00:00:00. The 	*
;*			user can set the time if desired.		*
;*									*
;*		The data that is sent to the LCD is also sent to the	*
;*		USART through the RS-232 port to be displayed on a PC	*
;*		HyperTerminal.						*
;************************************************************************

	list p=18f452
	#include p18f452.inc


;Program Configuration Registers
		__CONFIG    _CONFIG1H, _OSCS_OFF_1H & _EC_OSC_1H
		__CONFIG    _CONFIG2L, _BOR_OFF_2L & _PWRT_OFF_2L
		__CONFIG    _CONFIG2H, _WDT_OFF_2H
;		__CONFIG    _CONFIG3H, _CCP2MX_OFF_3H
		__CONFIG    _CONFIG4L, _STVR_OFF_4L & _LVP_OFF_4L & _DEBUG_OFF_4L
		__CONFIG    _CONFIG5L, _CP0_OFF_5L & _CP1_OFF_5L & _CP2_OFF_5L & _CP3_OFF_5L 
		__CONFIG    _CONFIG5H, _CPB_OFF_5H & _CPD_OFF_5H
		__CONFIG    _CONFIG6L, _WRT0_OFF_6L & _WRT1_OFF_6L & _WRT2_OFF_6L & _WRT3_OFF_6L 
		__CONFIG    _CONFIG6H, _WRTC_OFF_6H & _WRTB_OFF_6H & _WRTD_OFF_6H
		__CONFIG    _CONFIG7L, _EBTR0_OFF_7L & _EBTR1_OFF_7L & _EBTR2_OFF_7L & _EBTR3_OFF_7L
		__CONFIG    _CONFIG7H, _EBTRB_OFF_7H

	#define	scroll_dir	TRISA,4
	#define	scroll		PORTA,4		;Push-button RA4 on PCB
	#define	select_dir	TRISB,0		
	#define	select		PORTB,0		;Push-button RB0 on PCB

	EXTERN	LCDInit, temp_wr, d_write, i_write, LCDLine_1, LCDLine_2
	EXTERN	UMUL0808L, UDIV1608L, AARGB0, AARGB1, BARGB0


ssprw	macro				;check for idle SSP module routine
	movlw	0x00
	andwf	SSPCON2,W
	sublw	0x00
	btfss	STATUS,Z
	bra	$-8

	btfsc	SSPSTAT,R_W
	bra	$-2
	endm

variables	UDATA
ptr_pos		RES 1
ptr_count	RES 1
temp_1		RES 1
temp_2		RES 1
temp_3		RES 1
cmd_byte	RES 1
temperature	RES 1
LSD		RES 1
MsD		RES 1
MSD		RES 1
seconds		RES 1
minutes		RES 1
hours		RES 1

NumH		RES 1
NumL		RES 1
TenK		RES 1
Thou		RES 1
Hund		RES 1
Tens		RES 1
Ones		RES 1

STARTUP CODE
	NOP
	goto	start
	NOP
	NOP
	NOP
PROG1 	CODE

stan_table				;table for standard code
	;	"XXXXXXXXXXXXXXXX"
	;				ptr:
	data	"   Voltmeter    "	;0
	data	"     Buzzer     "	;16
	data	"  Temperature   "	;32
	data	"     Clock      "	;48
	data	"RA4=Next RB0=Now"	;64
	data	"   Microchip    "      ;80
	data	" PICDEM 2 PLUS  "	;96
	data	"RA4=Set RB0=Menu"	;112
	data	"RA4= --> RBO= ++"	;128
	data	"   RB0 = Exit 	 "	;144
	data	"Volts =	 "	;160
	data	"Prd.=128 DC=128 "	;176

start	
	call LCDInit
	
	movlw	B'10100100'		;initialize USART
	movwf	TXSTA			;8-bit, Async, High Speed
	movlw	.25
	movwf	SPBRG			;9.6kbaud @ 4MHz
	movlw	B'10010000'
	movwf	RCSTA

	bcf	TRISC,2			;configure CCP1 module for buzzer
;	bcf	TRISC,6
	movlw	0x80
	movwf	PR2			;initialize PWM period 
	movlw	0x80			;initialize PWM duty cycle
	movwf	CCPR1L
	bcf	CCP1CON,CCP1X
	bcf	CCP1CON,CCP1Y
	
	movlw	0x05			;postscale 1:1, prescaler 4, Timer2 ON
	movwf	T2CON
		
	bsf	TRISA,4			;make switch RA4 an Input
	bsf	TRISB,0			;make switch RB0 an Input


;**************** STANDARD CODE MENU SELECTION *******************
			;Introduction
	movlw	.80			;send "Microchip" to LCD
	movwf	ptr_pos
	call	stan_char_1

	movlw	.96			;send "PICDEM 2 PLUS" to LCD
	movwf	ptr_pos
	call	stan_char_2
	call	delay_1s		;delay for display
	call	delay_1s		;delay for display
menu
;------------------ VOLT MEASUREMENT  ----------------------------
	btfss	scroll			;wait for RA4 release
	goto	$-2		
	btfss	select			;wait for RB0 release
	goto	$-2

	movlw	0x00			;voltmeter
	movwf	ptr_pos
	call	stan_char_1

	movlw	.64			;RA4=Next  RB0=Now
	movwf	ptr_pos
	call	stan_char_2
v_wait
	btfss	select			;voltmeter measurement ??
	bra	voltmeter
	btfsc	scroll			;next mode ??
	bra	v_wait			;NO
	btfss	scroll			;YES
	bra	$-2			;wait for RA4 release
;------------------ BUZZER --------------------------------------
menu_buz
	btfss	select			;wait for RB0 release
	bra	$-2			

	movlw	.16			;buzzer
	movwf	ptr_pos
	call	stan_char_1

	movlw	.64			;RA4=Next  RB0=Now
	movwf	ptr_pos
	call	stan_char_2
b_wait
	btfss	select			;Buzzer sound ??
	goto	buzzer			;YES
	btfsc	scroll			;NO, next mode ??
	goto	b_wait			;NO
	btfss	scroll			;YES
	bra	$-2			;wait for RA4 release
;----------------- TEMPERATURE MEASUREMENT ----------------------
menu_temp
	btfss	scroll			;wait for RA4 release
	bra	$-2		

	movlw	.32			;temperature
	movwf	ptr_pos
	call	stan_char_1

	movlw	.64			;RA4=Next  RB0=Now
	movwf	ptr_pos
	call	stan_char_2
t_wait
	btfss	select			;temperature measurement ??
	bra	temp			;YES
	btfsc	scroll			;NO, next mode ??
	bra	t_wait			;NO
	btfss	scroll			;YES
	bra	$-2			;wait for release
;------------------ CLOCK TIME ----------------------------------
menu_clock
	btfss	select			;wait for RB0 release
	bra	$-2		

	movlw	.48			;clock
	movwf	ptr_pos
	call	stan_char_1

	movlw	.64			;RA4=Next  RB0=Now
	movwf	ptr_pos
	call	stan_char_2
c_wait
	btfss	select			;goto time ??
	bra	clock			;YES
	btfsc	scroll			;NO, next mode ??
	bra	c_wait			;NO
	btfss	scroll			;YES
	bra	$-2			;wait for release
;-------------------------------------------------------------------
	bra	menu			;begining of menu
	return

;*******************************************************************




;************* STANDARD USER CODE **********************************

;------------- Voltmeter--------------------------------------------
voltmeter
	btfss	select			;wait for RB0 release
	bra	$-2

	movlw	B'01000001'		;configure A/D converter	
	movwf	ADCON0			;turn A/D on
	movlw	b'00001110'		;RA0 = analog input
	movwf	ADCON1

	movlw	.160			;send "Volts = " to the LCD
	movwf	ptr_pos
	call	stan_char_1
volts_again
	bsf	ADCON0,GO		;start conversion
	btfsc	ADCON0,GO
	bra	$-2
	movf	ADRESH,W

	movwf	AARGB0			;move adresh into AARGB1
	movlw	0xC3			;19.5mV/step   0xC3 = 195
	movwf	BARGB0
	call	UMUL0808L
	
	movlw	0x64			;divide result by 100 (0x64)
	movwf	BARGB0
	call	UDIV1608L
	
	movf	AARGB0,W		;prepare for 16-bit binary to BCD
	movwf	NumH
	movf	AARGB1,W
	movwf	NumL
	call	bin16_bcd		;get volts ready for LCD
	
	call	LCDLine_2		;display A/D result on 2nd line
	movf	Hund,W			;get hunds
	call	bin_bcd
	movf	LSD,W			;send high digit from the LSD #.xx	
	movwf	temp_wr
	call	d_write
	movlw	A'.'			;send decimal point "."
	movwf	temp_wr
	call	d_write
	
	movf	Tens,W			;get tens
	call	bin_bcd
	movf	LSD,W			;send low digit   x.#x
	movwf	temp_wr
	call	d_write

	movf	Ones,W			;get ones
	call	bin_bcd
	movf	LSD,W			;send low digit   x.x#
	movwf	temp_wr
	call	d_write
	movlw	A'V'			;send "V" unit
	movwf	temp_wr
	call	d_write

	movlw	0x20			;3 spaces	
	movwf	temp_wr
	call	d_write
	movlw	0x20			
	movwf	temp_wr
	call	d_write
	movlw	0x20			
	movwf	temp_wr
	call	d_write
	movlw	A'R'			;send "RB0=Exit" to LCD
	movwf	temp_wr
	call	d_write
	movlw	A'B'			
	movwf	temp_wr
	call	d_write
	movlw	A'0'			
	movwf	temp_wr
	call	d_write
	movlw	A'='			
	movwf	temp_wr
	call	d_write
	movlw	A'E'			
	movwf	temp_wr
	call	d_write
	movlw	A'x'		
	movwf	temp_wr
	call	d_write
	movlw	A'i'			
	movwf	temp_wr
	call	d_write
	movlw	A't'			
	movwf	temp_wr
	call	d_write
	movlw	0x20			;2 spaces	
	movwf	temp_wr
	call	d_write
	movlw	0x20			
	movwf	temp_wr
	call	d_write

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

	btfss	select			;exit volt measurement ??
	bra	menu_buz			;YES
	bra	volts_again		;NO, do conversion again

;--------------------- BUZZER --------------------------------------
buzzer
	btfss	select			;wait for RB0 release
	bra	$-2

	movlw	0x80			;start at these PWM values
	movwf	PR2			;initialize PWM period 
	movlw	0x80			
	movwf	CCPR1L			;initialize PWM duty cycle
	
	call	LCDLine_1
	movlw	.176			;send "Prd.=128 DC=128" to LCD
	movwf	ptr_pos
	call	stan_char_1
	call	LCDLine_2
	movlw	.128			;send "RA4= -> RB0 = ++" to LCD
	movwf	ptr_pos
	call	stan_char_2
		
	movlw	0x0F			;turn buzzer on	
	movwf	CCP1CON	
		
pr2_again			
	btfsc	select			;increment PR2 ???
	bra	pr2_out			;NO
	call	delay_100ms		;YES
	call	delay_100ms		
	incf	PR2,F			;increment PR2
pr2_out
	movlw	0x85			;move cursor into position
	movwf	temp_wr
	call	i_write

pol1
	btfss	scroll			;goto increment CCPR1L
	bra	inc_dc
	btfsc	select			;wait for RB0 press
	bra	pol1

	movf	PR2,W			;send PR2 register to conversion
	call	bin_bcd

	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

	bra	pr2_again		

;------------------------
		;adjust Duty Cycle
inc_dc
	btfss	scroll			;wait for button release
	bra	$-2

inc_ccpr1l	
	btfsc	select			;increment CCPR1L ???
	goto	ccpr1l_out		;NO
	call	delay_100ms		;YES
	call	delay_100ms
	incf	CCPR1L,F		;increment CCPR1L
ccpr1l_out
	movlw	0x8C			;move cursor into position
	movwf	temp_wr
	call	i_write

col1
	btfss	scroll			;exit?
	bra	pwm_out
	btfsc	select			;wait for RB0 press
	bra	col1
	
	movf	CCPR1L,W		;send PR2 register to conversion
	call	bin_bcd
	
	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			
	bra	inc_ccpr1l

pwm_out
	movlw	0
	movwf	CCP1CON			;turn buzzer off
	bra	menu_temp

;---------------------- Temperature -------------------------------- 
temp
;	This code if for the TC74A5-5.0VAT temperature sensor
;		1st. Check if temperature is ready to be read in config reg.
;		2nd. If ready, retireve temperatute in hex.
;		     If not ready, check config register again.

	bsf	TRISC,3			;initialize MSSP module
	bsf	TRISC,4
	movlw	B'00101000'
	movwf	SSPCON1
	bsf	SSPSTAT,SMP
	movlw	.5
	movwf	SSPADD

	bcf	PIR1,TMR1IF
	clrf	TMR1H			;load Timer1 for 2 sec overflow
	clrf	TMR1L

get_temp
	movlw	0x01			;config register command byte
	movwf	cmd_byte
temp_now
	bsf	SSPSTAT,6		;SMBUS spec for TC74

	bsf	SSPCON2,SEN		;write to TC74
	btfsc	SSPCON2,SEN
	bra	$-2	
	movlw	B'10011010'		;send TC74 ADDRESS (write)
	movwf	SSPBUF
	ssprw
	btfsc	SSPCON2,ACKSTAT		;ack?
	bra	$-2

	movf	cmd_byte,W		;send COMMAND byte (config)
	movwf	SSPBUF
	ssprw
	btfsc	SSPCON2,ACKSTAT		;ack?
	bra	$-2

	bsf	SSPCON2,RSEN		;send repeated start
	btfsc	SSPCON2,RSEN
	bra	$-2
	movlw	B'10011011'		;send TC74 ADDRESS (read)
	movwf	SSPBUF
	ssprw				;module idle?
	btfsc	SSPCON2,ACKSTAT		;ack?
	bra	$-2

	bsf	SSPCON2,RCEN		;enable receive mode
	btfsc	SSPCON2,RCEN
	bra	$-2

	movf	SSPBUF,W		;retrieve config reg or temp reg
	
	bsf	SSPCON2,ACKDT		;send NOT-ACK
	bsf	SSPCON2,ACKEN
	btfsc	SSPCON2,ACKEN
	bra	$-2
			
	bsf	SSPCON2,PEN		;stop
	btfsc	SSPCON2,PEN
	bra	$-2
	
	btfss	cmd_byte,0		;config command OR temp command
	bra	convert_temp		;get temperature ready for display

	andlw	0x40			;is temp ready ??
	sublw	0x40

⌨️ 快捷键说明

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