+ Reply to Thread
Page 1 of 2
1 2 Last
Results 1 to 15 of 17

Thread: PIC18F4550 running on 20MHz?

  1. #1
    MrNobody Good MrNobody Good
    Join Date
    Aug 2006
    Posts
    249

    Default PIC18F4550 running on 20MHz?

    Hi,
    I'm planning to do PWM and Timer2 with USB feature using PIC18F4550 with crystal of 20MHz.

    What I intend to do is to have both PWM and Timer2 interrupt running on 20MHz as in FOSC = 20 MHz.

    Attached is the print screen of my settings in configuration bits.

    To test whether the code is running at 20MHz or not, i program a simple code to blink the LED ON for 1 second and then OFF for another second and i measure its time by using stopwatch. What i found is the code is running slower compared to the stopwatch.

    Below is how i did the blinking code. I put it in a while loop so that the PIC will do nothing else except blinking the LED.

    Code:
    // I'm using 20MHz crystal oscillator.
    void main(void)
    {
        InitializeSystem();
        while(1)
        {
            USBTasks();         // USB Tasks
            ProcessIO();        // See user\user.c & .h
            while (1)
            {
                    PORTD = 0xFF;	
    		Delay10KTCYx(200);
    		Delay10KTCYx(200);
    		Delay10KTCYx(100);
    		PORTD = 0x00;
    		Delay10KTCYx(200);
    		Delay10KTCYx(200);
    		Delay10KTCYx(100); 
    		}
        }//end while
    }//end main
    
    The reason why i did the above testing is to ensure that the PIC is indeed running at 20MHz so that when I do PWM and Timer2, I can use FOSC = 20MHz for the calculation. However, it seems that the PIC is running abit slower than 20MHz and because of that, I'm afraid when I do the PWM and Timer2, the result will be off.. Can somebody pls advice..
    Thanks.
    Attached Images


  2. #2
    mvs sarma Excellent mvs sarma Excellent mvs sarma Excellent mvs sarma Excellent mvs sarma Excellent mvs sarma Excellent mvs sarma Excellent mvs sarma Excellent mvs sarma Excellent
    Join Date
    Oct 2006
    Location
    Hyderabad, India.
    Posts
    2,468
    Blog Entries
    1

    Default

    did you use the stopwatch of MPLAB ?
    Regards,
    Sarma.

  3. #3
    TiagoSilva Newbie
    Join Date
    Feb 2004
    Location
    Portugal, Aveiro, Ovar
    Posts
    186

    Default

    Hi, i would disable the brown out detection,

    and since you are using the crazy USB framework from microchip you should do as they say and place all you code inside the User.c/.h because you are using a framework not just some functions.
    Also, i find strange that your pic is running that while you placed because the ProcessIO has a loop at some point, to keep processing USB requests and your user code...

    I'm new to USB with PICs, i have posted a thread about the USB function on this pic, search for it, it has some links to some projects that exclude the microchip framework and use a simpler set of functions.

    In my case the PIC initiates and when i enable the USB module my windows detects a USB device but it can't get the vendor ID from the PIC, don't know why yet...


    Well, i hope this helps, somehow...
    Last edited by TiagoSilva; 2nd February 2009 at 08:30 PM.
    Best Regards,
    Tiago Silva.

    Visit my personal webpage: http://www.tiagosilva.eu/

  4. #4
    MrNobody Good MrNobody Good
    Join Date
    Aug 2006
    Posts
    249

    Default

    mvs sarma:
    Yes, I did try the stopwatch and it indicates that instead of 1s, it takes about 1.2s which is about the same as the actual stopwatch.
    Would it be safe to say that the extra 0.2s is the time it requires to call those functions and not because of the PIC not running on 20MHz..? If it is safe then if i do Timer2, will the result be accurate..?

    TiagoSilva:
    I place the blinking code in "void main(void)" function because i don't want any 'interruption' while i test the blinking of LED. At this point i don't need the USB feature yet. My whole idea is just to see how fast/slow the LED blink and how close/far it is from the 1s intended. However, when I actually do the code later, i will do it in ProcessIO.
    BTW, there is an ebook in welcome - gigapedia.com on USB if you want. The title is "Advanced PIC Microcontroller Projects in C: From USB to RTOS with the PIC18F Series" ebook. Its free but you have to register as member. The book uses MikroC compiler tho.

    Thanks all.

  5. #5
    Help us help you blueroomelectronics Excellent blueroomelectronics Excellent blueroomelectronics Excellent blueroomelectronics Excellent blueroomelectronics Excellent blueroomelectronics Excellent blueroomelectronics Excellent blueroomelectronics Excellent blueroomelectronics Excellent blueroomelectronics Excellent
    Join Date
    Jan 2007
    Location
    Toronto, Canada
    Posts
    10,670
    Blog Entries
    5

    Default

    The 18f4550 has a different pll than pics without USB.
    Bill
    Smart Kits build Smart People

    http://www.blueroomelectronics.com/

  6. #6
    Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent
    Join Date
    Mar 2005
    Location
    Brisbane Australia
    Posts
    6,773

    Default

    The timing suggest that your chip is running at 16MHz. The only way this can happen is if you have the PLL output divided by 6 as the clock source, but your config has HS selected. Have you tried setting it in source, I never use MPLAB to set the config as I find it confusing.

    Mike.

  7. #7
    MrNobody Good MrNobody Good
    Join Date
    Aug 2006
    Posts
    249

    Default

    Quote Originally Posted by Pommie View Post
    The timing suggest that your chip is running at 16MHz. The only way this can happen is if you have the PLL output divided by 6 as the clock source, but your config has HS selected. Have you tried setting it in source, I never use MPLAB to set the config as I find it confusing.

    Mike.
    Hmm, do i set it at CONFIG1L and CONFIG1H register?
    Is the code below correct..? By the way, I would like the FOSC to be 20 MHz and the USB at 96MHz if possible..


    Code:
    // USB clock source comes from the 96 MHz PLL divided by 2
    // Primary oscillator used directly for system clock (no postscaler)
    // Divide by 5 (20 MHz oscillator input)
    CONFIG1L = 0b00100100;
    
    // Oscillator Switchover mode disabled
    // Fail-Safe Clock Monitor disabled
    // HS oscillator (HS)
    CONFIG1H = 0b00001100;
    
    Thanks.

  8. #8
    Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent
    Join Date
    Mar 2005
    Location
    Brisbane Australia
    Posts
    6,773

    Default

    I always use the config directive. You can find a list of parameters in the MPLAB help file - it's so confusing they gave it its own help section.

    I think for your application you want,
    Code:
    #pragma config WDT = OFF, LVP=OFF 
    #pragma config FOSC = HS, PLLDIV = 5, CPUDIV = OSC1_PLL2, USBDIV = 2
    
    Mike.

  9. #9
    MrNobody Good MrNobody Good
    Join Date
    Aug 2006
    Posts
    249

    Default

    Oh.. thanks..

    BTW, when i tried to enter the code below, it gives me compilation error. I also cannot seem to find CONFIG1L and CONFIG1H in PIC18F4550's header file. Just for understanding sake, is there any way to set the PIC registers by writing to it directly..?

    Code:
    // USB clock source comes from the 96 MHz PLL divided by 2
    // Primary oscillator used directly for system clock (no postscaler)
    // Divide by 5 (20 MHz oscillator input)
    CONFIG1L = 0b00100100;
    
    // Oscillator Switchover mode disabled
    // Fail-Safe Clock Monitor disabled
    // HS oscillator (HS)
    CONFIG1H = 0b00001100;
    

  10. #10
    Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent
    Join Date
    Mar 2005
    Location
    Brisbane Australia
    Posts
    6,773

    Default

    Quote Originally Posted by MrNobody View Post
    Just for understanding sake, is there any way to set the PIC registers by writing to it directly..?
    Not that I am aware of. You can probably do it by using a section but I find it easier to just use the directive.

    Mike.

  11. #11
    TiagoSilva Newbie
    Join Date
    Feb 2004
    Location
    Portugal, Aveiro, Ovar
    Posts
    186

    Default

    Well, did you set the i/o on TRISA ?
    You know, they recomend using LATA for output rather than PORTA, yet both work...

    If you are not using the USB module why don't you have it commented ?
    This pic PLL is suited to generate the USB working frequency...
    Best Regards,
    Tiago Silva.

    Visit my personal webpage: http://www.tiagosilva.eu/

  12. #12
    TiagoSilva Newbie
    Join Date
    Feb 2004
    Location
    Portugal, Aveiro, Ovar
    Posts
    186

    Default

    Quote Originally Posted by Pommie View Post
    Not that I am aware of. You can probably do it by using a section but I find it easier to just use the directive.

    Mike.
    Why not using the MPLAB interactive configuration ? it will generate the needed ASM code without having the user writting directly to the config registers... is this what you asked ?
    Best Regards,
    Tiago Silva.

    Visit my personal webpage: http://www.tiagosilva.eu/

  13. #13
    MrNobody Good MrNobody Good
    Join Date
    Aug 2006
    Posts
    249

    Default

    TiagoSilva:
    MPLAB interactive configuration..?
    How do i use that..?
    Where can i find it..?
    Thanks..

    Pommie:
    Tried the directive. The result is the same as before.. So i guess the time difference compared to the stopwatch is because of the latency or extra time needed to call the delay function.. Will try timer2 as soon as i can and hope timer2 will give me a more accurate result.
    Thanks anyway.

  14. #14
    TiagoSilva Newbie
    Join Date
    Feb 2004
    Location
    Portugal, Aveiro, Ovar
    Posts
    186

    Default

    Its easy, the page looks like this one (attached), only the bits change to suit the desired pic.
    You should find it in this order -> Configure>Configuration Bits

    Then you need to enable the MPLAB configuration bits and disable the code ones (if you have any).

    Have fun!
    Attached Images
    Best Regards,
    Tiago Silva.

    Visit my personal webpage: http://www.tiagosilva.eu/

  15. #15
    Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent
    Join Date
    Mar 2005
    Location
    Brisbane Australia
    Posts
    6,773

    Default

    TiagoSilva,

    I guess you missed the attachment to the first post.

    Mike.

+ Reply to Thread
Page 1 of 2
1 2 Last

Similar Threads

  1. Hitachi Oscilloscope (V-223 20MHz) - Help Needed
    By gatoruss in forum General Electronics Chat
    Replies: 6
    Latest: 22nd March 2009, 04:08 PM
  2. 20Mhz PIC & HD44780-clone
    By ArtemisGoldfish in forum Micro Controllers
    Replies: 2
    Latest: 7th June 2008, 09:37 AM
  3. How to generate a 20Mhz Clock Sognal For ADC
    By jitun2 in forum Electronic Projects Design/Ideas/Reviews
    Replies: 11
    Latest: 29th February 2008, 06:24 PM
  4. 20MHz Oscillator not working correctly
    By imhereithink in forum General Electronics Chat
    Replies: 23
    Latest: 3rd August 2007, 08:38 AM
  5. is this possible to see 20mhz oscilloscope 1000mhz
    By imgemini in forum Electronic Projects Design/Ideas/Reviews
    Replies: 3
    Latest: 3rd September 2004, 09:18 AM

Tags for this Thread