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

📄 main.asm

📁 example for PWM with PIC16F684
💻 ASM
字号:
;**********************************************************************
;   This file is a basic code template for assembly code generation   *
;                                                                     *
;**********************************************************************
;                                                                     *
;    Filename:	    main.asm                                          *
;    Date:          7/12/2006                                         *
;    Author:        Benjamin Stephens                                 *
;    Company:       Northwestern University                           *
;                   Laboratory for Intelligent Mechanical Systems     * 
;                                                                     *
;**********************************************************************
;                                                                     *
;    Files required:                                                  *
;    	macros.inc - some code macros                                 *
;       PIC16F684.INC - variable names for PIC16F684                  *
;                                                                     *
;**********************************************************************
;                                                                     *
;    Notes:                                                           *
;                                                                     *
;                                                                     *
;                                                                     *
;                                                                     *
;**********************************************************************


	LIST		p=16f684			; list directive to define processor
	#include	"P16F684.inc"		; processor specific variable definitions
	#include	"macros.inc"
	
	__CONFIG    _CP_OFF & _CPD_OFF & _BOD_OFF & _PWRTE_ON & _WDT_OFF & _INTRC_OSC_NOCLKOUT & _MCLRE_OFF & _FCMEN_OFF & _IESO_OFF

; '__CONFIG' directive is used to embed configuration data within .asm file.
; The labels following the directive are located in the respective .inc file.
; See respective data sheet for additional information on configuration word.

;***** VARIABLE DEFINITIONS
w_temp		EQU	0x71			; variable used for context saving
status_temp	EQU	0x72			; variable used for context saving		


;**********************************************************************
	ORG		0x000				; processor reset vector
  	GOTO		MAIN			; go to beginning of program


	ORG		0x004				; interrupt vector location
	MOVWF		w_temp			; save off current W register contents
	MOVF		STATUS,W		; move status register into W register
	MOVWF		status_temp		; save off contents of STATUS register

;	ORG		0x2100				; data EEPROM location
;	DE		1,2,3,4				; define first four EEPROM locations as 1, 2, 3, and 4

;****************************
;  PINOUT DIAGRAM - for Analog to PWM
;                   _______
;          1 +5v   |  |_|  | 14 GND
;          2 RA<5> |       | 13 RA<0> - Analog IN
;          3 RA<4> |       | 12 RA<1>
;          4 RA<3> |       | 11 RA<2>
;  PWM A - 5 RC<5> |       | 10 RC<0>
;  PWM B - 6 RC<4> |       |  9 RC<1>
;          7 RC<3> |_______|  8 RC<2>
;           
;  PWM Channels on RC<5:2>
;****************************


INIT_IO	
	BANK0
	CLRF 		PORTA 			;Init PORTA - initially set to LOW
	MOVLW 	B'000101'			;Set RA<0> as input
	MOVWF 		TRISA 			;	and set RA<5:1>
								;	as outputs
	BANK1
	CLRF 		PORTC 			;Init PORTC - initially set to LOW
	MOVLW 	B'000000'			;Set RC<5:0> as outputs
	MOVWF 		TRISC

	CLRF		ANSEL			;Set all pins initially to digital I/O
	MOVLW	B'000001'
	MOVWF		ANSEL			;Set AN<0> (RA<0>) to Analog input
								;	RA<5:1> remain digital I/O
	RETURN

INIT_PWM
	BANK0
	MOVLW	B'10001100'			;Dual-Output, Half-Bridge Mode
	MOVWF		CCP1CON			;P1A,P1C active-high, P1B,P1D active-high
	MOVLW	B'00000100'			;Timer2 enabled with prescale 1
	MOVWF		T2CON
	BANK1
	MOVLW	0xFF
	MOVWF		PR2				;Set frequency to 19.53 kHz
	BANK0
	MOVLW	B'01111111'			;50% duty cycle
	MOVWF		CCPR1L	
	RETURN

INIT_ADC
	BANK0
    MOVLW   B'00000001'			; Set output to left justified
								; Select AN<0> (RA<0>) as input
								; Internal Vref
								; Start ADC ON
    MOVWF   	ADCON0
	BANK1
	CLRF		ADCON1
    MOVLW   B'00100000' 		;Set Clock TAD to 1.6us (Fosc/32)
    MOVWF   	ADCON1
	RETURN

ADC_TO_PWM
	BANK0
	BTFSC		ADCON0,1		; check if DONE goes LOW
	GOTO 		$+3				; if not, skip to the RETURN
	CALL 		UPDATE_PWM		; if so, update the PWM duty cycle
	START_ADC					; and start the ADC again
	RETURN

UPDATE_PWM
	BANK0
	MOVFW	ADRESH			; copy the 8 MSbs to the CCPR1L register
	MOVWF	CCPR1L
	BTFSC	ADRESL,7		; is ADC bit 1 set?
	BCF		CCP1CON,5		; if not, clear the PWM bit 1
	BSF		CCP1CON,5		; if so, set the PWM bit 1
	BTFSC	ADRESL,6		; same thing, for bit 0
	BCF		CCP1CON,4
	BSF		CCP1CON,4	

	RETURN

MAIN
	BANK0
	CALL	INIT_IO
	CALL	INIT_ADC
	CALL	INIT_PWM
	START_ADC

MAINLOOP
    CALL 	ADC_TO_PWM
	GOTO 	MAINLOOP

	END                       ; directive 'end of program'

⌨️ 快捷键说明

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