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

📄 ex1301.asm

📁 关于PIC16F877A单片机的一些源代码,比较丰富.初学者认真看看应该有收获的.
💻 ASM
字号:
;
;	8 BITS A/D(VR) + LCD + PWM + CAPTURE
;
	LIST P=16F877, R=DEC
	#INCLUDE P16F877.INC
	CBLOCK 0X20
	ENDC
;
;	DEFINE RAM
;
	CBLOCK
	 BCDBFH, BCDBFL
	 R0
	 W_BAK, STATUS_BAK		; back up RAM for w register
	 CAPH, CAPL
	ENDC
;
	ORG 0
	NOP
	GOTO MAIN
;
	ORG 4
;
	MOVWF W_BAK		; save w register
	SWAPF STATUS,W
	CLRF STATUS		; select bank 0		
	MOVWF STATUS_BAK	; save status register
;
	BTFSS	PIR2,CCP2IF
	GOTO 	TIMER1		; timer1 interrupt means time out
;
	CLRF	TMR1L
	CLRF	TMR1H
	MOVF	CCPR2L,W
	MOVWF	CAPL
	MOVF	CCPR2H,W
	MOVWF	CAPH
	BCF	PIR2,CCP2IF
	BCF	PIR1,TMR1IF
	GOTO 	POP
TIMER1:
	CLRF	CAPL		; 0000h indicates time out( >0xffffh uS)
	CLRF	CAPH
	BCF	PIR1,TMR1IF
POP:
	SWAPF 	STATUS_BAK,W	; Restore STATUS
	MOVWF	STATUS
	SWAPF 	W_BAK,F		; Restore WREG w/o
	SWAPF	W_BAK,W		; affecting STATUS
	RETFIE

;
;
;---------------------------------------------------------------;
								;
	#INCLUDE <putLINE.INC>					;
								;
;	#include <lcd.inc>	; optional include		;
								;
;	ASCII table must be 16 characters in one line		;
; !!!	tables must be located at the front end of every page	;
								;
putLINE_TBL:							;
	ADDWF PCL						;
	DT "A/D(VR)+PWM+CAP "	; (16 chracters in one line)
;								;
;===============================================================;
;
;---------------------------------------------------------------;
								;
	#INCLUDE <LCD.INC>					;
								;
; Defines for I/O ports that provide LCD data & control		;
								;
LCD_DATA	equ	PORTB					;
LCD_CNTL	equ	PORTB					;
								;
RS	equ	5	; RS=PORTB,5				;
E	equ	4						;
								;
;===============================================================;
;
	INCLUDE <HX2BCD.INC>
	INCLUDE <DLY2_5mS.INC>
	INCLUDE <DLY0_2S.INC>

;
;=======================
MAIN:
	call	InitLCD
	call	clrLCD
	call	L1homeLCD
	MOVLW 	0
	CALL 	putLINE		; put "AN0   PWM      " in line 1

	BANKSEL TRISE
	BCF	TRISE,0		; back light
	BANKSEL PORTE
	BCF	PORTE,0		; turn on back light

;
;************************************************
;*   - Initialize the A/D converter             *
;*        - Left justified                      *
;*        - 1 analog channels, 0 references     *
;*        - Fosc/8 clock source                 *
;*        - Channel 0                           *
;*        - Enable A/D                          *
;************************************************
;
	BANKSEL ADCON1		; ADCON1 is in bank1
	MOVLW B'00001110'	; select left(0) justified (d7)
				; 1 ch(AN0)/0 refs
	MOVWF ADCON1
;
	BANKSEL ADCON0
	MOVLW B'01000001'	; select Fosc/8(01), AN0(000)
	MOVWF ADCON0		; a/d done(0), x(0), a/d ON(1)
;
;************************************************
;*   - Initialize the CCP1 Module for PWM       *
;*        - Set the period to FFh               *
;*        - Make RC2 an output                  *
;*        - Configure CCP1 for PWM (xxxx11xx)   *
;*   - Initialize Timer2                        *
;*        - 1:1 Postscale (x0000xxx)            *
;*        - 1:4 Prescale  (xxxxxx01)            *
;*        - Enable the timer (xxxxx1xx)         *
;************************************************
;
	BANKSEL PR2
	MOVLW H'FF'
	MOVWF PR2
	BCF TRISC,2
	BANKSEL CCP1CON
	MOVLW	B'00001111'
	MOVWF	CCP1CON
	MOVLW	B'00000101'
	MOVWF	T2CON
;***********************************************;
;*   - Initialize Timer1                        *
;*        - 1:1 Prescale                        *
;*        - Oscillator Off                      *
;*        - Internal Clock source               *
;*        - Enable the timer                    *
;*   - Initialize the CCP2 Module for CAPTURE   *
;*        - Configure CCP2 for Capture          *
;*        - Capture every rising edge           *
;*   - Enable Timer1 overflow interrupt         *
;*   - Enable Capture2 interrupt                *
;*   - Enable Peripheral interrupts             *
;*   - Enable global interrupts                 *
;************************************************

	MOVLW	B'00100001'
	MOVWF 	T1CON
	MOVLW 	B'00000101'
	MOVWF	CCP2CON
	BANKSEL PIE1
	BSF	PIE1,TMR1IE
	BSF	PIE2,CCP2IE
	BANKSEL INTCON
	BSF	INTCON,PEIE
	BSF	INTCON,GIE

	CALL DLY2_5mS		; wait for input charge
MAIN1:
	BSF ADCON0,GO		; start a/d converting
MAIN2:
	BTFSC ADCON0,GO		; a/d convertion finished?
	GOTO MAIN2		; no	
;
	call	L2homeLCD
	MOVF ADRESH,W		; read a/d result ( high byte )
	CALL HX2BCD		; convert HEX to BCD
				; results in BCDBFH/BCDBFL
;
	MOVF BCDBFH,W		; output a/d results to LCD
	CALL putHexLCD
;
	MOVF BCDBFL,W
	CALL putHexLCD
;
	MOVF ADRESH,W
	MOVWF CCPR1L		; Also in Duty cycle
;
	MOVLW 7			; put 7 spaces
	CALL movcurLCD
;
	MOVF CAPH,W
	CALL putHexLCD
;
	MOVF CAPL,W
	CALL putHexLCD
;
	MOVLW 'h'
	CALL putcLCD
;
	CALL DLY0_2S		; delay 0.6 sec
	CALL DLY0_2S		
	CALL DLY0_2S
	GOTO MAIN1
;
;
;
	END

⌨️ 快捷键说明

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