PIC16F886 - 设置配置位
PIC16F886 - set configuration bits
我在尝试设置 PIC16F886 上的配置位时遇到语法错误。
问题是有两个配置字,两个配置有多个位。
所以它的工作方式与 PIC10F206 不同。
我正在使用 pic-as v2.20 工具链。
PROCESSOR 16F886
RADIX DEC
#include <xc.inc>
;this did not work:
;config DEBUG = ON, LVP = OFF, FCMEN = OFF, IESO = OFF, BOREN = OFF
;config CPD = OFF, CP = OFF, MCLRE = OFF, PWRTE = OFF, WDTE = OFF, FOSC = OFF
;this did not work:
;config 0000000000000000000000000000
;this did not work:
;config 0x00
;this did not work:
;config config1 0x0000
;config config2 0x0000
PSECT StartCode,class=CODE,delta=2
global Start
Start:
movlw 11000000B ;set option register
;option is not in the p16f886 instruction summary
movwf 81h ;option_reg is at 81h (bank 1)
movlw 11111110B ;everything to input except for RA0
tris 05h ;05h is TRISA
bcf 05h, 0 ;clear bit zero in TRISA register
;the led on RA0 should light up now
sleep
END Start
感谢您的回答。
到目前为止,这里的代码有效。
它仍然不完美,因为我可能应该使用 BANKSEL
指令并使用变量而不是十六进制代码。
另外,我在设置选项之前没有设置 bank 1,所以这可能没有效果。
PROCESSOR 16F886
PAGEWIDTH 132
RADIX DEC
#include <xc.inc>
config DEBUG = OFF, LVP = OFF, FCMEN = OFF, IESO = OFF, BOREN = OFF
config CPD = OFF, CP = OFF, MCLRE = OFF, PWRTE = OFF, WDTE = OFF
config FOSC = INTRC_NOCLKOUT, LVP = OFF, BOR4V = BOR40V, WRT = OFF
PSECT StartCode,class=CODE,delta=2
global Start
Start:
movlw 11000000B ;set option register
;option is not in the p16f886 instruction summary
movwf 81h ;option_reg is at 81h (bank 1)
movlw 00100000B ;set the status register (select bank 1)
movwf 03h ;status register is at 03h
movlw 11111110B ;everything to input except for RA0
;there is no tris instruction on this processor
movwf 85h ;85h is TRISA register
movlw 00000000B ;set the status register (select bank 0)
movwf 03h ;status register is at 03h
;05h is PORTA
bcf 05h, 0 ;clear bit zero in PORTA register
;the led on RA0 should light up now
sleep
END Start
我怀疑您遇到的问题会让大多数 Microchip 控制器新手感到困惑。在配置为数字模式之前,具有模拟功能的 GPIO 引脚不能很好地工作。
此代码会将 PORTA 位 RA0 设置为数字输出,并每秒将其高电平和低电平切换约 2 次。
;
; File: main.S
; Target: PIC16F886
; Author: dan1138
; Date: 2020-09-08
; Compiler: pic-as(v2.20)
; IDE: MPLABX v5.40
;
; Description:
;
; Example project for the PIC16F886 controller using the pic-as(v2.20) tool chain.
;
; Add this line in the project properties box, pic-as Global Options -> Additional options:
; -Wa,-a -Wl,-pPor_Vec=0h,-pIsr_Vec=4h
;
PROCESSOR 16F886
PAGEWIDTH 132
RADIX DEC
#include <xc.inc>
; CONFIG1
config FOSC = INTRC_NOCLKOUT; Oscillator Selection bits (INTOSCIO oscillator: I/O function on RA6/OSC2/CLKOUT pin, I/O function on RA7/OSC1/CLKIN)
config WDTE = OFF ; Watchdog Timer Enable bit (WDT disabled and can be enabled by SWDTEN bit of the WDTCON register)
config PWRTE = OFF ; Power-up Timer Enable bit (PWRT disabled)
config MCLRE = ON ; RE3/MCLR pin function select bit (RE3/MCLR pin function is MCLR)
config CP = OFF ; Code Protection bit (Program memory code protection is disabled)
config CPD = OFF ; Data Code Protection bit (Data memory code protection is disabled)
config BOREN = OFF ; Brown Out Reset Selection bits (BOR disabled)
config IESO = OFF ; Internal External Switchover bit (Internal/External Switchover mode is disabled)
config FCMEN = OFF ; Fail-Safe Clock Monitor Enabled bit (Fail-Safe Clock Monitor is disabled)
config LVP = OFF ; Low Voltage Programming Enable bit (RB3 pin has digital I/O, HV on MCLR must be used for programming)
; CONFIG2
config BOR4V = BOR21V ; Brown-out Reset Selection bit (Brown-out Reset set to 2.1V)
config WRT = OFF ; Flash Program Memory Self Write Enable bits (Write protection off)
;
; Define macros to help with
; bank selection
;
#define BANK0 (0x000)
#define BANK1 (0x080)
#define BANK2 (0x100)
#define BANK3 (0x180)
;
; Skip macros
;
skipnc MACRO
btfsc STATUS,STATUS_C_POSITION
ENDM
skipc MACRO
btfss STATUS,STATUS_C_POSITION
ENDM
skipnz MACRO
btfsc STATUS,STATUS_Z_POSITION
ENDM
skipz MACRO
btfss STATUS,STATUS_Z_POSITION
ENDM
;
; Power-On-Reset entry point
;
PSECT Por_Vec,global,class=CODE,delta=2
global resetVec
resetVec:
PAGESEL Start
goto Start
;
; Data space use by interrupt handler to save context
PSECT Isr_Data,global,class=COMMON,space=1,delta=1,noexec
;
GLOBAL WREG_save,STATUS_save,PCLATH_save
;
WREG_save: DS 1
STATUS_save: DS 1
PCLATH_save: DS 1
;
; Interrupt vector and handler
PSECT Isr_Vec,global,class=CODE,delta=2
GLOBAL IsrVec
;
IsrVec:
movwf WREG_save
swapf STATUS,W
movwf STATUS_save
movf PCLATH,W
movwf PCLATH_save
;
IsrHandler:
;
IsrExit:
movf PCLATH_save,W
movwf PCLATH
swapf STATUS_save,W
movwf STATUS
swapf WREG_save,F
swapf WREG_save,W
retfie ; Return from interrupt
;
; Initialize the PIC hardware
;
Start:
clrf INTCON ; Disable all interrupt sources
banksel BANK1
clrf PIE1
clrf PIE2
movlw 0b01100000
movwf OSCCON ; Set internal oscillator at 4MHz
movlw 0b10000001 ; Pull-ups off, INT edge high to low, WDT prescale 1:1
movwf OPTION_REG ; TMR0 clock edge low to high, TMR0 clock = FCY, TMR0 prescale 1:4
; TIMER0 will assert the overflow flag every 256*4 (1024)
; instruction cycles, with a 4MHz oscilator this ia 1.024 milliseconds.
movlw 0b11111111 ;
movwf TRISA
movlw 0b11111111 ;
movwf TRISB
movlw 0b11111111 ;
movwf TRISC
; Set all ADC inputs for digital I/O
banksel BANK3
movlw 0b00000000
movwf ANSEL
movlw 0b00000000
movwf ANSELH
banksel BANK2
clrf CM1CON0 ; turn off comparator
clrf CM2CON0 ; turn off comparator
banksel BANK1
movlw 0b00000000
movwf ADCON1
clrf VRCON ; turn off voltage reference
banksel BANK0
movlw 0b10000000
movwf ADCON0
pagesel main
goto main
;
; Main application data
;
PSECT MainData,global,class=RAM,space=1,delta=1,noexec
global count
count: DS 1 ;reserve 1 byte for TOMER0 rollover count
;
; Main application code
;
PSECT MainCode,global,class=CODE,delta=2
;
; Function to Wait for TIMER0 to rollover 250 times
;
WaitOnTimer0:
movlw 250
BANKSEL count
movwf BANKMASK(count)
WaitT0a:
bcf INTCON,INTCON_TMR0IF_POSITION
WaitT0b:
btfss INTCON,INTCON_TMR0IF_POSITION
goto WaitT0b
decfsz BANKMASK(count),F
goto WaitT0a
return
;
; Set PORTA bit 0 as an output then set low wait
; for 250 rollovers of TIMER0 and set hihg and
; wait another 250 rollovers of TIMER0 then loop
;
main:
BANKSEL TRISA
bcf BANKMASK(TRISA),TRISA_TRISA0_POSITION ; Make PORTA bit RA0 an output
loop:
BANKSEL PORTA
bcf BANKMASK(PORTA),PORTA_RA0_POSITION ; Make PORTA bit RA0 LOW
call WaitOnTimer0
bsf BANKMASK(PORTA),PORTA_RA0_POSITION ; Make PORTA bit RA0 HIGH
call WaitOnTimer0
goto loop
;
; Declare Power-On-Reset entry point
;
END resetVec
试一试,告诉我进展如何。
我在尝试设置 PIC16F886 上的配置位时遇到语法错误。
问题是有两个配置字,两个配置有多个位。
所以它的工作方式与 PIC10F206 不同。
我正在使用 pic-as v2.20 工具链。
PROCESSOR 16F886
RADIX DEC
#include <xc.inc>
;this did not work:
;config DEBUG = ON, LVP = OFF, FCMEN = OFF, IESO = OFF, BOREN = OFF
;config CPD = OFF, CP = OFF, MCLRE = OFF, PWRTE = OFF, WDTE = OFF, FOSC = OFF
;this did not work:
;config 0000000000000000000000000000
;this did not work:
;config 0x00
;this did not work:
;config config1 0x0000
;config config2 0x0000
PSECT StartCode,class=CODE,delta=2
global Start
Start:
movlw 11000000B ;set option register
;option is not in the p16f886 instruction summary
movwf 81h ;option_reg is at 81h (bank 1)
movlw 11111110B ;everything to input except for RA0
tris 05h ;05h is TRISA
bcf 05h, 0 ;clear bit zero in TRISA register
;the led on RA0 should light up now
sleep
END Start
感谢您的回答。
到目前为止,这里的代码有效。
它仍然不完美,因为我可能应该使用 BANKSEL
指令并使用变量而不是十六进制代码。
另外,我在设置选项之前没有设置 bank 1,所以这可能没有效果。
PROCESSOR 16F886
PAGEWIDTH 132
RADIX DEC
#include <xc.inc>
config DEBUG = OFF, LVP = OFF, FCMEN = OFF, IESO = OFF, BOREN = OFF
config CPD = OFF, CP = OFF, MCLRE = OFF, PWRTE = OFF, WDTE = OFF
config FOSC = INTRC_NOCLKOUT, LVP = OFF, BOR4V = BOR40V, WRT = OFF
PSECT StartCode,class=CODE,delta=2
global Start
Start:
movlw 11000000B ;set option register
;option is not in the p16f886 instruction summary
movwf 81h ;option_reg is at 81h (bank 1)
movlw 00100000B ;set the status register (select bank 1)
movwf 03h ;status register is at 03h
movlw 11111110B ;everything to input except for RA0
;there is no tris instruction on this processor
movwf 85h ;85h is TRISA register
movlw 00000000B ;set the status register (select bank 0)
movwf 03h ;status register is at 03h
;05h is PORTA
bcf 05h, 0 ;clear bit zero in PORTA register
;the led on RA0 should light up now
sleep
END Start
我怀疑您遇到的问题会让大多数 Microchip 控制器新手感到困惑。在配置为数字模式之前,具有模拟功能的 GPIO 引脚不能很好地工作。
此代码会将 PORTA 位 RA0 设置为数字输出,并每秒将其高电平和低电平切换约 2 次。
;
; File: main.S
; Target: PIC16F886
; Author: dan1138
; Date: 2020-09-08
; Compiler: pic-as(v2.20)
; IDE: MPLABX v5.40
;
; Description:
;
; Example project for the PIC16F886 controller using the pic-as(v2.20) tool chain.
;
; Add this line in the project properties box, pic-as Global Options -> Additional options:
; -Wa,-a -Wl,-pPor_Vec=0h,-pIsr_Vec=4h
;
PROCESSOR 16F886
PAGEWIDTH 132
RADIX DEC
#include <xc.inc>
; CONFIG1
config FOSC = INTRC_NOCLKOUT; Oscillator Selection bits (INTOSCIO oscillator: I/O function on RA6/OSC2/CLKOUT pin, I/O function on RA7/OSC1/CLKIN)
config WDTE = OFF ; Watchdog Timer Enable bit (WDT disabled and can be enabled by SWDTEN bit of the WDTCON register)
config PWRTE = OFF ; Power-up Timer Enable bit (PWRT disabled)
config MCLRE = ON ; RE3/MCLR pin function select bit (RE3/MCLR pin function is MCLR)
config CP = OFF ; Code Protection bit (Program memory code protection is disabled)
config CPD = OFF ; Data Code Protection bit (Data memory code protection is disabled)
config BOREN = OFF ; Brown Out Reset Selection bits (BOR disabled)
config IESO = OFF ; Internal External Switchover bit (Internal/External Switchover mode is disabled)
config FCMEN = OFF ; Fail-Safe Clock Monitor Enabled bit (Fail-Safe Clock Monitor is disabled)
config LVP = OFF ; Low Voltage Programming Enable bit (RB3 pin has digital I/O, HV on MCLR must be used for programming)
; CONFIG2
config BOR4V = BOR21V ; Brown-out Reset Selection bit (Brown-out Reset set to 2.1V)
config WRT = OFF ; Flash Program Memory Self Write Enable bits (Write protection off)
;
; Define macros to help with
; bank selection
;
#define BANK0 (0x000)
#define BANK1 (0x080)
#define BANK2 (0x100)
#define BANK3 (0x180)
;
; Skip macros
;
skipnc MACRO
btfsc STATUS,STATUS_C_POSITION
ENDM
skipc MACRO
btfss STATUS,STATUS_C_POSITION
ENDM
skipnz MACRO
btfsc STATUS,STATUS_Z_POSITION
ENDM
skipz MACRO
btfss STATUS,STATUS_Z_POSITION
ENDM
;
; Power-On-Reset entry point
;
PSECT Por_Vec,global,class=CODE,delta=2
global resetVec
resetVec:
PAGESEL Start
goto Start
;
; Data space use by interrupt handler to save context
PSECT Isr_Data,global,class=COMMON,space=1,delta=1,noexec
;
GLOBAL WREG_save,STATUS_save,PCLATH_save
;
WREG_save: DS 1
STATUS_save: DS 1
PCLATH_save: DS 1
;
; Interrupt vector and handler
PSECT Isr_Vec,global,class=CODE,delta=2
GLOBAL IsrVec
;
IsrVec:
movwf WREG_save
swapf STATUS,W
movwf STATUS_save
movf PCLATH,W
movwf PCLATH_save
;
IsrHandler:
;
IsrExit:
movf PCLATH_save,W
movwf PCLATH
swapf STATUS_save,W
movwf STATUS
swapf WREG_save,F
swapf WREG_save,W
retfie ; Return from interrupt
;
; Initialize the PIC hardware
;
Start:
clrf INTCON ; Disable all interrupt sources
banksel BANK1
clrf PIE1
clrf PIE2
movlw 0b01100000
movwf OSCCON ; Set internal oscillator at 4MHz
movlw 0b10000001 ; Pull-ups off, INT edge high to low, WDT prescale 1:1
movwf OPTION_REG ; TMR0 clock edge low to high, TMR0 clock = FCY, TMR0 prescale 1:4
; TIMER0 will assert the overflow flag every 256*4 (1024)
; instruction cycles, with a 4MHz oscilator this ia 1.024 milliseconds.
movlw 0b11111111 ;
movwf TRISA
movlw 0b11111111 ;
movwf TRISB
movlw 0b11111111 ;
movwf TRISC
; Set all ADC inputs for digital I/O
banksel BANK3
movlw 0b00000000
movwf ANSEL
movlw 0b00000000
movwf ANSELH
banksel BANK2
clrf CM1CON0 ; turn off comparator
clrf CM2CON0 ; turn off comparator
banksel BANK1
movlw 0b00000000
movwf ADCON1
clrf VRCON ; turn off voltage reference
banksel BANK0
movlw 0b10000000
movwf ADCON0
pagesel main
goto main
;
; Main application data
;
PSECT MainData,global,class=RAM,space=1,delta=1,noexec
global count
count: DS 1 ;reserve 1 byte for TOMER0 rollover count
;
; Main application code
;
PSECT MainCode,global,class=CODE,delta=2
;
; Function to Wait for TIMER0 to rollover 250 times
;
WaitOnTimer0:
movlw 250
BANKSEL count
movwf BANKMASK(count)
WaitT0a:
bcf INTCON,INTCON_TMR0IF_POSITION
WaitT0b:
btfss INTCON,INTCON_TMR0IF_POSITION
goto WaitT0b
decfsz BANKMASK(count),F
goto WaitT0a
return
;
; Set PORTA bit 0 as an output then set low wait
; for 250 rollovers of TIMER0 and set hihg and
; wait another 250 rollovers of TIMER0 then loop
;
main:
BANKSEL TRISA
bcf BANKMASK(TRISA),TRISA_TRISA0_POSITION ; Make PORTA bit RA0 an output
loop:
BANKSEL PORTA
bcf BANKMASK(PORTA),PORTA_RA0_POSITION ; Make PORTA bit RA0 LOW
call WaitOnTimer0
bsf BANKMASK(PORTA),PORTA_RA0_POSITION ; Make PORTA bit RA0 HIGH
call WaitOnTimer0
goto loop
;
; Declare Power-On-Reset entry point
;
END resetVec
试一试,告诉我进展如何。