• AVR Freaks

Hot!Changing clock

Author
john_ive
Super Member
  • Total Posts : 212
  • Reward points : 0
  • Joined: 2017/05/22 22:22:08
  • Location: 0
  • Status: offline
2019/05/09 00:09:52 (permalink)
0

Changing clock

I like to learn about the internal oscillator in PIC16F886.
What is different between external oscillator(Crystal with capacitor).
How to use internal oscillator with 8MHZ clock..?
#1

8 Replies Related Threads

    qhb
    Superb Member
    • Total Posts : 9999
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: offline
    Re: Changing clock 2019/05/09 00:15:33 (permalink)
    0
    john_ive
    What is different between external oscillator(Crystal with capacitor).

    This is not a complete question.
    The difference between "external oscillator(Crystal with capacitor)", and what?
     

    How to use internal oscillator with 8MHZ clock..?

    First you need to select the internal oscillator in the CONFIG settings.
    You have two choices. You can output the clock /4 on the "RA6/OSC2/CLKOUT" pin, in which case you should set "FOSC=INTOSC"
    or you can leave RA6 as a general purpose IO pin, in which case you should set "FOSC=INTOSCIO"
     
    That will make the PIC start up using the internal oscillator at the default speed of 4MHz.
    You then need to write to the "IRCF" bits in the OSCCON register to set them to 0b111 to set the clock to 8MHz.
    i.e.
    OSCCONbits.IRCF = 0b111;    // switch clock to 8MHz

     
     

    Nearly there...
    #2
    john_ive
    Super Member
    • Total Posts : 212
    • Reward points : 0
    • Joined: 2017/05/22 22:22:08
    • Location: 0
    • Status: offline
    Re: Changing clock 2019/05/09 01:00:02 (permalink)
    0
    While using the internal oscillator,is anything disadvantage while compared with external crystal oscillator..?
    Or both are same.
    If internal oscillator is same like external oscillator means,why we using external oscillator instead of internal oscillator..?
    #3
    pcbbc
    Super Member
    • Total Posts : 1373
    • Reward points : 0
    • Joined: 2014/03/27 07:04:41
    • Location: 0
    • Status: offline
    Re: Changing clock 2019/05/09 01:09:11 (permalink)
    0
    External crystal is more accurate than internal RC oscilator. Also you have complete control of frequency, whereas with internal oscilator you must chose from a set range.
    #4
    john_ive
    Super Member
    • Total Posts : 212
    • Reward points : 0
    • Joined: 2017/05/22 22:22:08
    • Location: 0
    • Status: offline
    Re: Changing clock 2019/05/09 01:15:57 (permalink)
    0
    Please refer the image.
    Right now i'm using 8MHZ crystal along with 22pF capacitor as external crystal without any resistors..
    But in datasheet there are two resistors connected (Rp and Rs). Is these two resistor have to include in oscillator circuit..?

    Attached Image(s)

    #5
    pcbbc
    Super Member
    • Total Posts : 1373
    • Reward points : 0
    • Joined: 2014/03/27 07:04:41
    • Location: 0
    • Status: offline
    Re: Changing clock 2019/05/09 01:24:00 (permalink)
    0
    Not necessarily required, no. Did you read the notes, refer to the spec of your crystal and the rest of the PIC datasheet?
    #6
    qhb
    Superb Member
    • Total Posts : 9999
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: offline
    Re: Changing clock 2019/05/09 01:26:08 (permalink)
    0
    That depends upon the characteristics of your crystal, but in general for high speed crystals (i.e. much bigger than 32kHz) you don't need either resistor.
    Have a look at specification "OS08" in the datasheet, which is the accuracy if the internal oscillator.
    It's only guaranteed +/- 2% from zero to 85 degrees, or 5% from -40 to 85 (or 125 for "extended temp range" devices)
    An external crystal will be more more accurate over the full temperature range.
     

    Nearly there...
    #7
    john_ive
    Super Member
    • Total Posts : 212
    • Reward points : 0
    • Joined: 2017/05/22 22:22:08
    • Location: 0
    • Status: offline
    Re: Changing clock 2019/06/26 05:00:49 (permalink)
    0

    #define _XTAL_FREQ 8000000
    #include <xc.h>
    // CONFIG1
    #pragma config FOSC = EXTRC_CLKOUT// Oscillator Selection bits (RC oscillator: CLKOUT function on RA6/OSC2/CLKOUT pin, RC on RA7/OSC1/CLKIN)
    #pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled and can be enabled by SWDTEN bit of the WDTCON register)
    #pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
    #pragma config MCLRE = ON // RE3/MCLR pin function select bit (RE3/MCLR pin function is MCLR)
    #pragma config CP = OFF // Code Protection bit (Program memory code protection is disabled)
    #pragma config CPD = OFF // Data Code Protection bit (Data memory code protection is disabled)
    #pragma config BOREN = OFF // Brown Out Reset Selection bits (BOR disabled)
    #pragma config IESO = OFF // Internal External Switchover bit (Internal/External Switchover mode is disabled)
    #pragma config FCMEN = OFF // Fail-Safe Clock Monitor Enabled bit (Fail-Safe Clock Monitor is disabled)
    #pragma config LVP = OFF // Low Voltage Programming Enable bit (RB3 pin has digital I/O, HV on MCLR must be used for programming)

    // CONFIG2
    #pragma config BOR4V = BOR40V // Brown-out Reset Selection bit (Brown-out Reset set to 4.0V)
    #pragma config WRT = OFF // Flash Program Memory Self Write Enable bits (Write protection off)

    void System_init(void) {

        OSCCONbits.IRCF = 0b111;

        TRISA = 0b00000000;
        PORTA = 0b00000000;

        TRISB = 0b00000000;
        PORTB = 0b00000000;

        TRISC = 0b00000000;
        PORTC = 0b00000000;

        ANSEL = 0X00;
        ANSELH = 0X00;
    }

    void main(void) {

        System_init();

        while (1) {
            RB0 = 1;
            __delay_us(500);
            RB0 = 0;
            __delay_us(500);
        }
    }

     
    Above code is used for Internal oscillator.
    While executing the code,their is no response from the controller...
     

    #define _XTAL_FREQ 8000000
     

     
    If this line is removed means  __delay_us(500); is not working...
    Is it ok to use #define _XTAL_FREQ 8000000while using internal oscillator..?
    #8
    ric
    Super Member
    • Total Posts : 24189
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: Changing clock 2019/06/26 05:15:13 (permalink)
    +1 (1)
    If you use __delay_us(), then you MUST define _XTAL_FREQ.
    That line does NOT control the PIC frequency. It just tells the compiler how fast the PIC is running according to whatever else you have done, so it can calculate how many instruction loops are required for the delays.
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #9
    Jump to:
    © 2019 APG vNext Commercial Version 4.5