这是我用curiosity开发板写的一个例子,你看看吧。
#include "p16f1619.inc"
; CONFIG1
; __config 0xFFFC
__CONFIG _CONFIG1, _FOSC_INTOSC & _PWRTE_OFF & _MCLRE_ON & _CP_OFF & _BOREN_ON & _CLKOUTEN_OFF & _IESO_ON & _FCMEN_ON
; CONFIG2
; __config 0xFFFF
__CONFIG _CONFIG2, _WRT_OFF & _PPS1WAY_ON & _ZCD_OFF & _PLLEN_ON & _STVREN_ON & _BORV_LO & _LPBOR_OFF & _LVP_ON
; CONFIG3
; __config 0xFF9F
__CONFIG _CONFIG3, _WDTCPS_WDTCPS1F & _WDTE_OFF & _WDTCWS_WDTCWSSW & _WDTCCS_SWC
;*******************************************************************************
; Reset Vector
;*******************************************************************************
RES_VECT CODE 0x0000 ; processor reset vector
GOTO START ; go to beginning of program
;*******************************************************************************
; MAIN PROGRAM
;*******************************************************************************
MAIN_PROG CODE ; let linker place main program
START
;***************************************************************************
;初始化系统时钟
BANKSEL OSCCON
MOVLW 0x38
MOVWF OSCCON
;***************************************************************************
;初始化I/0
BANKSEL PORTA
CLRF PORTA
CLRF PORTB
CLRF PORTC
BANKSEL TRISA
MOVLW 0x11
MOVWF TRISA
CLRF TRISA
MOVLW 0xF0
MOVWF TRISB
MOVLW 0xDF
MOVWF TRISC
BANKSEL ANSELA
CLRF ANSELA
CLRF ANSELB
MOVLW 0x01
MOVWF ANSELC
BANKSEL WPUA
CLRF WPUA
CLRF WPUB
;CLRF WPUC
MOVLW 0x01 ;POT1弱上拉会增加采样电压,否则最低亮度太暗
MOVWF WPUC
BANKSEL OPTION_REG
BCF OPTION_REG,NOT_WPUEN
;***************************************************************************
;初始化ADC
BANKSEL ADCON0
MOVLW 0x11
MOVWF ADCON0
CLRF ADCON1
CLRF ADCON2
CLRF ADRESL
CLRF ADRESH
BSF ADCON0, ADGO
;***************************************************************************
;TMR2初始化
BANKSEL T2CLKCON
CLRF T2CLKCON ;FOSC/4
CLRF T2HLT ;自由运行周期,软件门控
MOVLW 0x01
MOVWF T2RST
MOVLW 0xFF
MOVWF PR2
CLRF TMR2
BCF PIR1, TMR2IF
BSF T2CON, TMR2ON
;***************************************************************************
;初始化PWM1
BANKSEL CCP1CON
MOVLW 0x9F
MOVWF CCP1CON ;MODE PWM,EN enabled,FMT left_aligned;
CLRF CCPTMRS ;Selecting Timer 2
;MOVLW 0x03
MOVLW 0xCC
MOVWF CCPR1H
;MOVLW 0x32
MOVLW 0x80
MOVWF CCPR1L
BANKSEL RC5PPS
MOVLW 0x0C
MOVWF RC5PPS ;Set RC5(D7) as output of CCP1 using PPS
;*******************************************************************************
;主程序
MAIN
BANKSEL ADCON0
BTFSC ADCON0, ADGO
GOTO $-1
CALL LOAD_TO_CCP1
GOTO MAIN
LOAD_TO_CCP1
MOVF ADRESH, W
BANKSEL CCPR1H
MOVWF CCPR1H
BANKSEL ADRESL
MOVF ADRESL, W
BANKSEL CCPR1L
MOVWF CCPR1L
;***************************************************************************
;再次开启ADC
BANKSEL ADCON0
BSF ADCON0, ADGO
RETURN
END
|