1
Microchip PIC / XC8 v1.45 มันไม่รู้จักรีจีสเตอร์ OSCCON (ตั้งความถี่ไปที่ 4MHz ไม่ได้ครับ)
« on: August 22, 2019, 10:50:46 pm »
ผมใช้ 12F1572 ทดลองฟังค์ชั้นต่างๆ ผมสามารถเขียนคำสั่ง LATAx INT IOCAPx Timer2 ได้ปกติ แต่ติดปัญหาคือเหมือนมันจะทำงานเฉพาะที่ความถี่ default 500kHz ตามดาต้าชีทเท่านั้น ผมไม่สามารถเปลี่ยนสัญญาณนาฬิกาเป็น 4MHz ได้ พอเปลี่ยนคำสั่งจาก
#define _XTAL_FREQ 500000 เป็น #define _XTAL_FREQ 4000000 ค่าดีเลย์ในโปรแกรมมันจะออกมาไม่ตรงความจริงเลย (ใช้มิเตอร์วัดความถั้จับดู)็
ผมลองใช้คำสั่ง OSCCON ก็คอมไพล์ไม่ผ่าน เนื่องจากมันไม่เข้าใจการเซท OSCCON (ผมใช้วิธัทั้งเขียนเอง ลอกเขาจากเน็ต ทุกวิธีมันก็ทำอะไรไม่ได้ เหมือนมันจะมองเป็นตัวแปรที่ไม่ได้ type ไว้) คำสั่งที่แนบผมก็แค่ใช้ทำไฟกระพริบ รบกวนช่วยดูหรือขอคำสั่ง XC8 เพื่อสั่งทำงานที่ 4MHz หน่อยครับ
#include <xc.h>
#define _XTAL_FREQ 500000
// CONFIG1
#pragma config FOSC = INTOSC // (INTOSC oscillator; I/O function on CLKIN pin)
#pragma config WDTE = OFF // Watchdog Timer Enable (WDT disabled)
#pragma config PWRTE = ON // Power-up Timer Enable (PWRT disabled)
#pragma config MCLRE = ON // MCLR Pin Function Select (MCLR/VPP pin function is MCLR)
#pragma config CP = OFF // Flash Program Memory Code Protection (Program memory code protection is disabled)
#pragma config BOREN = OFF // Brown-out Reset Enable (Brown-out Reset disabled)
#pragma config CLKOUTEN = OFF // Clock Out Enable (CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin)
// CONFIG2
#pragma config WRT = OFF // Flash Memory Self-Write Protection (Write protection off)
#pragma config PLLEN = OFF // PLL Enable (4x PLL disabled)
#pragma config STVREN = ON // Stack Overflow/Underflow Reset Enable (Stack Overflow or Underflow will cause a Reset)
#pragma config BORV = LO // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor), low trip point selected.)
#pragma config LPBOREN = OFF // Low Power Brown-out Reset enable bit (LPBOR is disabled)
#pragma config LVP = OFF // Low-Voltage Programming Enable (High-voltage on MCLR/VPP must be used for programming)
// ================== // Configure I/O pins // ==================
OSCCON = 0b01101000;
int main(void){
CM1CON1 = 0x07;
ANSELA = 0b00000000; // all pins to digital
LATA = 0b00001110; // all port bits off
TRISA = 0b00001110; // all pins to outputs except RA0,RA3 (MCLR)
// LATA5 = 0;
while(1){
LATA5 = 1;
__delay_us(2500);
LATA5 = 0;
__delay_us(2500);
}
#define _XTAL_FREQ 500000 เป็น #define _XTAL_FREQ 4000000 ค่าดีเลย์ในโปรแกรมมันจะออกมาไม่ตรงความจริงเลย (ใช้มิเตอร์วัดความถั้จับดู)็
ผมลองใช้คำสั่ง OSCCON ก็คอมไพล์ไม่ผ่าน เนื่องจากมันไม่เข้าใจการเซท OSCCON (ผมใช้วิธัทั้งเขียนเอง ลอกเขาจากเน็ต ทุกวิธีมันก็ทำอะไรไม่ได้ เหมือนมันจะมองเป็นตัวแปรที่ไม่ได้ type ไว้) คำสั่งที่แนบผมก็แค่ใช้ทำไฟกระพริบ รบกวนช่วยดูหรือขอคำสั่ง XC8 เพื่อสั่งทำงานที่ 4MHz หน่อยครับ
#include <xc.h>
#define _XTAL_FREQ 500000
// CONFIG1
#pragma config FOSC = INTOSC // (INTOSC oscillator; I/O function on CLKIN pin)
#pragma config WDTE = OFF // Watchdog Timer Enable (WDT disabled)
#pragma config PWRTE = ON // Power-up Timer Enable (PWRT disabled)
#pragma config MCLRE = ON // MCLR Pin Function Select (MCLR/VPP pin function is MCLR)
#pragma config CP = OFF // Flash Program Memory Code Protection (Program memory code protection is disabled)
#pragma config BOREN = OFF // Brown-out Reset Enable (Brown-out Reset disabled)
#pragma config CLKOUTEN = OFF // Clock Out Enable (CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin)
// CONFIG2
#pragma config WRT = OFF // Flash Memory Self-Write Protection (Write protection off)
#pragma config PLLEN = OFF // PLL Enable (4x PLL disabled)
#pragma config STVREN = ON // Stack Overflow/Underflow Reset Enable (Stack Overflow or Underflow will cause a Reset)
#pragma config BORV = LO // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor), low trip point selected.)
#pragma config LPBOREN = OFF // Low Power Brown-out Reset enable bit (LPBOR is disabled)
#pragma config LVP = OFF // Low-Voltage Programming Enable (High-voltage on MCLR/VPP must be used for programming)
// ================== // Configure I/O pins // ==================
OSCCON = 0b01101000;
int main(void){
CM1CON1 = 0x07;
ANSELA = 0b00000000; // all pins to digital
LATA = 0b00001110; // all port bits off
TRISA = 0b00001110; // all pins to outputs except RA0,RA3 (MCLR)
// LATA5 = 0;
while(1){
LATA5 = 1;
__delay_us(2500);
LATA5 = 0;
__delay_us(2500);
}