• AVR Freaks

Hot!External 20MHz not working on PIC18F4550

Page: 123 > Showing page 1 of 3
Author
Flipp3rix
Starting Member
  • Total Posts : 34
  • Reward points : 0
  • Joined: 2019/02/28 14:40:19
  • Location: 0
  • Status: offline
2019/03/21 05:43:27 (permalink)
0

External 20MHz not working on PIC18F4550

EDIT:
I'm having problems with my PIC18F4550 using external crystal(@20MHz) and two capacitor @22pF
My schematic is attached below...
Anyway I'm working on a breadboard
This is my code(When button on rd7 is presse turn on led1 and turn off led 2)
This is main code
#include <stdio.h>
#include <stdlib.h>
#include "XC8.h"

void delay_sec(int x) {
    int a=x*1000;
    for (int i = 0; i < a; i++) {
        __delay_ms(1);
    }
}

void delay_mil(int x) {
    for (int i = 0; i < x; i++) {
        __delay_ms(1);
    }
}

void main(void) {
    //ADCON1 |= 0b0011;

    TRISDbits.RD3=1; //rosso
    TRISDbits.RD2=1; //blue
    
    TRISD7=1;
    
    //OSCCON = 0xF2; // internal oscillator, 8MHz 
    
    
    int value=0, value2=0;
    while(1){
        value = RD7;
        if(value){
            if(value){
                RD3=l;
                RD2=h;
            }
        }
        else{
            RD3=h;
            RD2=l;
        }
        
    }
}


But when I upload this code, it just doesn't work, i made also a simple led blink cod but same thing. Nothing...
This is my configuration bits
#include <xc.h>

// PIC18F4550 Configuration Bit Settings

// 'C' source line config statements

// CONFIG1L
#pragma config PLLDIV = 5 // PLL Prescaler Selection bits (Divide by 5 (20 MHz oscillator input))
#pragma config CPUDIV = OSC1_PLL2// System Clock Postscaler Selection bits ([Primary Oscillator Src: /1][96 MHz PLL Src: /2])
#pragma config USBDIV = 2 // USB Clock Selection bit (used in Full-Speed USB mode only; UCFG:FSEN = 1) (USB clock source comes from the 96 MHz PLL divided by 2)

// CONFIG1H
#pragma config FOSC = HSPLL_HS // Oscillator Selection bits (HS oscillator, PLL enabled (HSPLL))
#pragma config FCMEN = OFF // Fail-Safe Clock Monitor Enable bit (Fail-Safe Clock Monitor disabled)
#pragma config IESO = OFF // Internal/External Oscillator Switchover bit (Oscillator Switchover mode disabled)

// CONFIG2L
#pragma config PWRT = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOR = ON // Brown-out Reset Enable bits (Brown-out Reset enabled in hardware only (SBOREN is disabled))
#pragma config BORV = 3 // Brown-out Reset Voltage bits (Minimum setting 2.05V)
#pragma config VREGEN = ON // USB Voltage Regulator Enable bit (USB voltage regulator enabled)

// CONFIG2H
#pragma config WDT = OFF // Watchdog Timer Enable bit (WDT disabled (control is placed on the SWDTEN bit))
#pragma config WDTPS = 32768 // Watchdog Timer Postscale Select bits (1:32768)

// CONFIG3H
#pragma config CCP2MX = ON // CCP2 MUX bit (CCP2 input/output is multiplexed with RC1)
#pragma config PBADEN = ON // PORTB A/D Enable bit (PORTB<4:0> pins are configured as analog input channels on Reset)
#pragma config LPT1OSC = OFF // Low-Power Timer 1 Oscillator Enable bit (Timer1 configured for higher power operation)
#pragma config MCLRE = ON // MCLR Pin Enable bit (MCLR pin enabled; RE3 input pin disabled)

// CONFIG4L
#pragma config STVREN = OFF // Stack Full/Underflow Reset Enable bit (Stack full/underflow will not cause Reset)
#pragma config LVP = OFF // Single-Supply ICSP Enable bit (Single-Supply ICSP disabled)
#pragma config ICPRT = OFF // Dedicated In-Circuit Debug/Programming Port (ICPORT) Enable bit (ICPORT disabled)
#pragma config XINST = OFF // Extended Instruction Set Enable bit (Instruction set extension and Indexed Addressing mode disabled (Legacy mode))

// CONFIG5L
#pragma config CP0 = OFF // Code Protection bit (Block 0 (000800-001FFFh) is not code-protected)
#pragma config CP1 = OFF // Code Protection bit (Block 1 (002000-003FFFh) is not code-protected)
#pragma config CP2 = OFF // Code Protection bit (Block 2 (004000-005FFFh) is not code-protected)
#pragma config CP3 = OFF // Code Protection bit (Block 3 (006000-007FFFh) is not code-protected)

// CONFIG5H
#pragma config CPB = OFF // Boot Block Code Protection bit (Boot block (000000-0007FFh) is not code-protected)
#pragma config CPD = OFF // Data EEPROM Code Protection bit (Data EEPROM is not code-protected)

// CONFIG6L
#pragma config WRT0 = OFF // Write Protection bit (Block 0 (000800-001FFFh) is not write-protected)
#pragma config WRT1 = OFF // Write Protection bit (Block 1 (002000-003FFFh) is not write-protected)
#pragma config WRT2 = OFF // Write Protection bit (Block 2 (004000-005FFFh) is not write-protected)
#pragma config WRT3 = OFF // Write Protection bit (Block 3 (006000-007FFFh) is not write-protected)

// CONFIG6H
#pragma config WRTC = OFF // Configuration Register Write Protection bit (Configuration registers (300000-3000FFh) are not write-protected)
#pragma config WRTB = OFF // Boot Block Write Protection bit (Boot block (000000-0007FFh) is not write-protected)
#pragma config WRTD = OFF // Data EEPROM Write Protection bit (Data EEPROM is not write-protected)

// CONFIG7L
#pragma config EBTR0 = OFF // Table Read Protection bit (Block 0 (000800-001FFFh) is not protected from table reads executed in other blocks)
#pragma config EBTR1 = OFF // Table Read Protection bit (Block 1 (002000-003FFFh) is not protected from table reads executed in other blocks)
#pragma config EBTR2 = OFF // Table Read Protection bit (Block 2 (004000-005FFFh) is not protected from table reads executed in other blocks)
#pragma config EBTR3 = OFF // Table Read Protection bit (Block 3 (006000-007FFFh) is not protected from table reads executed in other blocks)

// CONFIG7H
#pragma config EBTRB = OFF // Boot Block Table Read Protection bit (Boot block (000000-0007FFh) is not protected from table reads executed in other blocks)

// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.

#define _XTAL_FREQ 48000000

post edited by Flipp3rix - 2019/03/21 09:57:16

Attached Image(s)

#1

40 Replies Related Threads

    NiKHiL
    New Member
    • Total Posts : 23
    • Reward points : 0
    • Joined: 2017/11/24 05:02:22
    • Location: India
    • Status: offline
    Re: External 20MHz not working 2019/03/21 06:14:11 (permalink)
    0
    Here D2 and D3 are used to blink. Then these pins must be configured as outputs.
    So use 
    TRISDbits.RD2 = 0;
    TRISDbits.RD3 = 0;
    instead of
    TRISDbits.RD2 = 1;
    TRISDbits.RD3 = 1;
    #2
    Flipp3rix
    Starting Member
    • Total Posts : 34
    • Reward points : 0
    • Joined: 2019/02/28 14:40:19
    • Location: 0
    • Status: offline
    Re: External 20MHz not working 2019/03/21 06:21:36 (permalink)
    0
    nikhilms
    Here D2 and D3 are used to blink. Then these pins must be configured as outputs.
    So use 
    TRISDbits.RD2 = 0;
    TRISDbits.RD3 = 0;
    instead of
    TRISDbits.RD2 = 1;
    TRISDbits.RD3 = 1;


    yes of course, but it was only for debug...they don't blink in any case
    #3
    Flipp3rix
    Starting Member
    • Total Posts : 34
    • Reward points : 0
    • Joined: 2019/02/28 14:40:19
    • Location: 0
    • Status: offline
    Re: External 20MHz not working 2019/03/21 06:29:25 (permalink)
    0
    of course my leds and my button works fine with internal oscillator
    #4
    NiKHiL
    New Member
    • Total Posts : 23
    • Reward points : 0
    • Joined: 2017/11/24 05:02:22
    • Location: India
    • Status: offline
    Re: External 20MHz not working 2019/03/21 06:31:37 (permalink)
    0
    Actually your code is not for blinking, according to user input on d7 change d2 and d3.
    If D7(input) is high D2 is high and D3 is low and
    if D7 is low, D2 is low and D3 is high
    For this, no need for the delay routines
     
    #5
    Flipp3rix
    Starting Member
    • Total Posts : 34
    • Reward points : 0
    • Joined: 2019/02/28 14:40:19
    • Location: 0
    • Status: offline
    Re: External 20MHz not working 2019/03/21 06:35:18 (permalink)
    0
    nikhilms
    Actually your code is not for blinking, according to user input on d7 change d2 and d3.
    If D7(input) is high D2 is high and D3 is low and
    if D7 is low, D2 is low and D3 is high
    For this, no need for the delay routines
     


    Ok, but it doesn't work even with this code and a simple led blinking code
    #6
    mbrowning
    Just a Member
    • Total Posts : 1424
    • Reward points : 0
    • Joined: 2005/03/16 14:32:56
    • Location: Melbourne, FL
    • Status: offline
    Re: External 20MHz not working 2019/03/21 06:35:43 (permalink)
    +1 (1)
    Your topic is "External 20MHz not working" but you didn't address that at all in your post, with no schematic, hardware description, or what you've done to determine that it isn't working.
     
    Then you present code and say LED's don't blink. NiKHiL shows you why and you say "yes of course".
     
    You need to seriously improve the quality of information you are providing if you expect to get useful aid.

    Oh well - there's always next year
    #7
    NiKHiL
    New Member
    • Total Posts : 23
    • Reward points : 0
    • Joined: 2017/11/24 05:02:22
    • Location: India
    • Status: offline
    Re: External 20MHz not working 2019/03/21 06:38:36 (permalink)
    +1 (1)
    use LAT instead of PORT for output pins
     
    #8
    Flipp3rix
    Starting Member
    • Total Posts : 34
    • Reward points : 0
    • Joined: 2019/02/28 14:40:19
    • Location: 0
    • Status: offline
    Re: External 20MHz not working 2019/03/21 06:38:50 (permalink)
    0
    I want to know if my configuration bits is just fine for an external 20MHz crystal, because I suspect there is a problem because not even a simple blinking code works. I'm using PIC18F4550
     
    #9
    Flipp3rix
    Starting Member
    • Total Posts : 34
    • Reward points : 0
    • Joined: 2019/02/28 14:40:19
    • Location: 0
    • Status: offline
    Re: External 20MHz not working 2019/03/21 06:40:30 (permalink)
    0
    mbrowning
    Your topic is "External 20MHz not working" but you didn't address that at all in your post, with no schematic, hardware description, or what you've done to determine that it isn't working.
     
    Then you present code and say LED's don't blink. NiKHiL shows you why and you say "yes of course".
     
    You need to seriously improve the quality of information you are providing if you expect to get useful aid.


    Yeah, I'm sorry for that, i'm late for work :(

    Later I send you my current schematics
    #10
    NiKHiL
    New Member
    • Total Posts : 23
    • Reward points : 0
    • Joined: 2017/11/24 05:02:22
    • Location: India
    • Status: offline
    Re: External 20MHz not working 2019/03/21 06:47:55 (permalink)
    0
    Try this, its working for me along with your configuration bits in Proteus
     
    #define LED1 LATDbits.LATD2
    #define LED2 LATDbits.LATD3
    #define INPUT PORTDbits.RD7
    void main() {
         TRISDbits.RD2 = 0;
         TRISDbits.RD3 = 0;
         TRISDbits.RD7 = 1;

         while (1) {
               if(INPUT) {
                    LED1 = 1;
                    LED2 = 0;
              } else {
                    LED1 = 0;
                    LED2 = 1;
             }
        }
    }
    #11
    jack@kksound
    code tags!
    • Total Posts : 3198
    • Reward points : 0
    • Joined: 2014/05/14 10:03:19
    • Location: 0
    • Status: offline
    Re: External 20MHz not working 2019/03/21 07:49:07 (permalink)
    +1 (1)
    If everything "works" with the internal oscillator and "does not work" with the external crystal then I would suspect two things:
    1) your config settings - these appear to be OK
    2) your crystal/capacitors - wrong values, incorrect circuit, bad connections? But then you did not show show ANY of that detail so all is guesswork.
    #12
    hexreader
    Super Member
    • Total Posts : 1027
    • Reward points : 0
    • Joined: 2008/05/04 03:06:55
    • Location: England
    • Status: offline
    Re: External 20MHz not working 2019/03/21 09:02:49 (permalink)
    +1 (1)
    20MHz is really hard to work with, especially on breadboard (no idea if this is the case)
     
    I would recommend 10MHz or lower frequency of crystal, with appropriate configuration register changes. Much easier for an amateur to make work reliably. 

    Experienced Hobbyist
    #13
    KTrenholm
    Super Member
    • Total Posts : 709
    • Reward points : 0
    • Joined: 2012/08/08 14:04:23
    • Location: Connecticut, USA
    • Status: offline
    Re: External 20MHz not working 2019/03/21 09:20:31 (permalink)
    +1 (1)
    hexreader
    20MHz is really hard to work with, especially on breadboard (no idea if this is the case)
     
    I would recommend 10MHz or lower frequency of crystal, with appropriate configuration register changes. Much easier for an amateur to make work reliably. 




    jack@kksound
    If everything "works" with the internal oscillator and "does not work" with the external crystal then I would suspect two things:
    1) your config settings - these appear to be OK
    2) your crystal/capacitors - wrong values, incorrect circuit, bad connections? But then you did not show show ANY of that detail so all is guesswork.



    All of this is solid advice.
    Make sure to check the obvious things too, like if this is a PCB and not a breadboard, make sure the crystal is mounted in the right direction, because yes I have spent an afternoon debugging a non-starting PIC only to realize the crystal was placed backward.
    #14
    Flipp3rix
    Starting Member
    • Total Posts : 34
    • Reward points : 0
    • Joined: 2019/02/28 14:40:19
    • Location: 0
    • Status: offline
    Re: External 20MHz not working 2019/03/21 09:57:49 (permalink)
    0
    Main post edited complete with schematics and some info
    #15
    jack@kksound
    code tags!
    • Total Posts : 3198
    • Reward points : 0
    • Joined: 2014/05/14 10:03:19
    • Location: 0
    • Status: offline
    Re: External 20MHz not working 2019/03/21 10:10:59 (permalink)
    0
    hexreader
    20MHz is really hard to work with, especially on breadboard (no idea if this is the case)
     
    I would recommend 10MHz or lower frequency of crystal, with appropriate configuration register changes. Much easier for an amateur to make work reliably. 


    Good advice however if the OP intends to use the USB peripheral in this pic then there are restrictions on the oscillator frequency. There is a table in the datasheet that shows the usable frequencies and the proper settings for each:
    TABLE 2-3: OSCILLATOR CONFIGURATION OPTIONS FOR USB OPERATION
     
    #16
    Flipp3rix
    Starting Member
    • Total Posts : 34
    • Reward points : 0
    • Joined: 2019/02/28 14:40:19
    • Location: 0
    • Status: offline
    Re: External 20MHz not working 2019/03/21 10:19:44 (permalink)
    0
    So any advice?

    ps. i update my first post with info and schematics
    #17
    Gort2015
    Klaatu Barada Nikto
    • Total Posts : 3122
    • Reward points : 0
    • Joined: 2015/04/30 10:49:57
    • Location: 0
    • Status: offline
    Re: External 20MHz not working 2019/03/21 10:24:28 (permalink)
    -1 (1)
    I am using a 20MHz xtal on a breadboard with no problems.
     
    I was getting uart garbage but that was solved by using two power cables at the left and right side of the breadboard to join the top and bottom tracks.
     
    (Top and bottom of bread board - blue/red tracks)
     
     
     

    MPLab X playing up, bug in your code? Nevermind, Star Trek:Discovery will be with us soon.
    https://www.youtube.com/watch?v=Iu1qa8N2ID0
    + ST:Continues, "What Ships are Made for", Q's back.
    #18
    Flipp3rix
    Starting Member
    • Total Posts : 34
    • Reward points : 0
    • Joined: 2019/02/28 14:40:19
    • Location: 0
    • Status: offline
    Re: External 20MHz not working 2019/03/21 10:27:27 (permalink)
    0
    Yeah but there is something wrong in my case?
     
    ps. i update my first post with info and schematics
    #19
    Gort2015
    Klaatu Barada Nikto
    • Total Posts : 3122
    • Reward points : 0
    • Joined: 2015/04/30 10:49:57
    • Location: 0
    • Status: offline
    Re: External 20MHz not working 2019/03/21 10:28:21 (permalink)
    -1 (1)
    Joining the 3.3v (top and bottom) and (0v top and bottom) tracks.
     

    MPLab X playing up, bug in your code? Nevermind, Star Trek:Discovery will be with us soon.
    https://www.youtube.com/watch?v=Iu1qa8N2ID0
    + ST:Continues, "What Ships are Made for", Q's back.
    #20
    Page: 123 > Showing page 1 of 3
    Jump to:
    © 2019 APG vNext Commercial Version 4.5