我是 PIC 编程的新手,我正在努力在程序中设置振荡器,如果有人能帮助我解释我做错了什么,我将不胜感激。
// PIC16F1575 Configuration Bit Settings
// 'C' source line config statements
// CONFIG1
#pragma config FOSC = INTOSC // Oscillator Selection Bits (INTOSC oscillator; I/O functio
#pragma config WDTE = OFF // Watchdog Timer Enable (WDT disabled)
#pragma config PWRTE = ON // Power-up Timer Enable (PWRT enabled)
#pragma config MCLRE = ON // MCLR Pin Function Select (MCLR/VPP pin function is MCLR)
#pragma config CP = ON // Flash Program Memory Code Protection (Program memory code
#pragma config BOREN = OFF // Brown-out Reset Enable (Brown-out Reset disabled)
#pragma config CLKOUTEN = ON // Clock Out Enable (CLKOUT function is enabled on the CLKOU
// CONFIG2
#pragma config WRT = OFF // Flash Memory Self-Write Protection (Write protection off)
#pragma config PPS1WAY = ON // PPSLOCK bit One-Way Set Enable bit (PPSLOCKED Bit Can Be
#pragma config PLLEN = ON // PLL Enable (4x PLL disabled)
#pragma config STVREN = ON // Stack Overflow/Underflow Reset Enable (Stack Overflow or
#pragma config BORV = LO // Brown-out Reset Voltage Selection (Brown-out Reset Voltag
#pragma config LPBOREN = OFF // Low Power Brown-out Reset enable bit (LPBOR is disabled)
#pragma config DEBUG = OFF // In-Circuit Debugger Mode (In-Circuit Debugger disabled, I
#pragma config LVP = ON // Low-Voltage Programming Enable (Low-voltage programming e
// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
#include <xc.h>
-----------------------------------------------
#include "config.h"
#include <xc.h>
//configuring which oscillator to use (target 64MHz)
#define _XTAL_FREQ 64000000
void main(void) {
OSCCONbits.IRCF = 0b1111; //sets internal oscillator speed to 16MHz
OSCCONbits.SPLLEN = 0b1; //sets prescaler to x4 making signal 64MHz
OSCCONbits.SCS = 0b00; //sets system clock to fosc setting
//below is a blinking LED test to make sure oscillator configured correctly
//sets RA2 to digital output
ANSELAbits.ANSA2 = 0; //removes analogue out to led
TRISAbits.TRISA2 = 0; //sets RA0 to output
//blinks LED on output
while(1){
LATAbits.LATA2 = 1;
__delay_ms(1000);
LATAbits.LATA2 = 0;
__delay_ms(1000);
}
return;
}
我以为我已经正确设置了所有内容,之前 LED 灯可以正常工作,但现在它似乎不再工作了。
看起来您正在尝试将时钟速度设置为 64Mhz,这对于该设备来说根本不可能,因为最大时钟速度为 32Mhz。看看时钟图:
您可以看到,将
PLLEN
设置为 ON
并将 FOSC
设置为 INTOSC
,您正在将 INTOSC
信号运行到 PLL 中。与:
OSCCONbits.IRCF = 0b1111; //sets internal oscillator speed to 16MHz
将 16 Mhz 运行到 4x PLL 中以获得 64Mhz,这太快了。该设备的最大时钟速度为 32Mhz。为了获得 32Mhz,请将
IRCF
设置为 0b1110
以在 INTOSC
上获得 8Mhz(如数据表所述):
bit 6-3 IRCF<3:0>: Internal Oscillator Frequency Select bits
1111 = 16 MHz HF
1110 = 8 MHz or 32 MHz HF (see Section 5.2.2.1 “HFINTOSC”)
1101 = 4 MHz HF
1100 = 2 MHz HF
1011 = 1 MHz HF
1010 = 500 kHz HF(1)
1001 = 250 kHz HF(1)
1000 = 125 kHz HF(1)
0111 = 500 kHz MF (default upon Reset)
0110 = 250 kHz MF
0101 = 125 kHz MF
0100 = 62.5 kHz MF
0011 = 31.25 kHz HF(1)
0010 = 31.25 kHz MF
000x = 31 kHz LF
另一个选项是设置:
OSCCONbits.SCS = 0b10;
直接使用
INTOSC
信号。通过将 SCS
设置为 0b1111
,使用此方法可以获得的最大速度将为 16Mhz。
确保根据您的决定更新
_XTAL_FREQ
。