Delay_ms problem

Beta Testing discussion on mikroPascal PRO for PIC.
Post Reply
Author
Message
VCC
Posts: 463
Joined: 08 Jun 2009 18:31
Location: Romania

Delay_ms problem

#1 Post by VCC » 22 Aug 2009 13:44

Hi, for starters, if there is any post about this topic I have missed, I am sorry questioning again. Working on a project with PIC18F4550, I have found that a Delay_ms(5000) takes only 2.25s. Here is the code with which I have tested other values for Delay_ms.

Settings:
quartz = 20MHz
PLL prescaler = divide by 5
system clock = OSC1/OSC2 Src1
USB clock source selection = Clk src from 96MHz PLL/2
Oscillator = HS+PLL, USB-HS
Watchdog = disabled

Code: Select all

program TestDelay_ms;

uses
  USBdsc;

var
  userWR_buffer: array[2] of Byte;
  userRD_buffer: array[2] of Byte;

//******************************************************************************
// Main Interrupt Routine
//******************************************************************************
procedure interrupt;
begin
  HID_InterruptProc;
end;
//******************************************************************************

//******************************************************************************
// Initialization Routine
//******************************************************************************
procedure Init_Main;
begin
        //--------------------------------------
        // Disable interrupts
        //--------------------------------------
        INTCON := 0;               // Disable GIE, PEIE, TMR0IE,INT0IE,RBIE
        INTCON2 := 0xF5;
        INTCON3 := 0xC0;
        RCON.IPEN := 0;            // Disable Priority Levels on interrupts
        PIE1 := 0;
        PIE2 := 0;
        PIR1 := 0;
        PIR2 := 0;

        //ADCON1 := ADCON1 or 0x0F;  // Configure all ports with analog function as digital
        ADCON1 := 12; // AN0, AN1, AN2, others digital
        //--------------------------------------
        // Ports Configuration
        //--------------------------------------
        TRISA := 0;
        TRISB := 255;
        TRISC := 0;
        TRISD := 2;
        TRISE := 0;

        LATA := 0;
        LATB := 0;
        LATC := 0;
        LATD := 0;
        LATE := 0;
        //--------------------------------------
        // Clear user RAM
        // Banks [00 .. 07] ( 8 x 256 = 2048 Bytes )
        //--------------------------------------
end;

begin
  Delay_mS(1000);
  Init_Main;
  HID_Enable(@userRD_buffer, @userWR_buffer);
  Delay_mS(2000);

  repeat
    if PortD.1 = 0 then  //button
    begin
      LATD.0 := 1; //LED on
      Delay_ms(5000);
      LATD.0 := 0; //LED off
    end;

  until False;
end.
Here are other measurements:
Delay_ms(1000) => 0.4s
Delay_ms(5000) => 2.2s
Delay_ms(6000) => 2.6s
Delay_ms(10000) => 4.3s
Delay_ms(60000) => 25.2s

jpc
Posts: 1986
Joined: 22 Apr 2005 17:40
Location: France 87

#2 Post by jpc » 22 Aug 2009 13:55

this type of systematic error tells you that your project clocksetting corresponds NOT with the real clock at which your pic is running. Look in the datasheet at the table related to USB oscillatorconfiguration to see at what frequency your cpu is clocked , there must be a simple mistake
Au royaume des aveugles, les borgnes sont rois.

VCC
Posts: 463
Joined: 08 Jun 2009 18:31
Location: Romania

#3 Post by VCC » 22 Aug 2009 14:05

jpc wrote:this type of systematic error tells you that your project clocksetting corresponds NOT with the real clock at which your pic is running. Look in the datasheet at the table related to USB oscillatorconfiguration to see at what frequency your cpu is clocked , there must be a simple mistake
Thank you for your answer. Maybe you are right, but the USB communication works fine. These are the settings for all my USB projects and for all of them the USB work.

jpc
Posts: 1986
Joined: 22 Apr 2005 17:40
Location: France 87

#4 Post by jpc » 22 Aug 2009 14:13

this only indicates your USB runs at the required clockrate , what is the MCU Frequency in your project-settings? this should be 48 mHz if all is configured ok ( the maximum this pic runs within specs). If this setting is wrong, de delays derived from that number( this is nothing more, it does not determine the real oscillator frequency) will be wrong
Au royaume des aveugles, les borgnes sont rois.

VCC
Posts: 463
Joined: 08 Jun 2009 18:31
Location: Romania

#5 Post by VCC » 22 Aug 2009 14:22

Thank you jpc, problem solved. In the project settings, it was 20MHz. There it writes MCU Clock, maybe I don't understand it, but I know that MCU means microcontroller not the internal CPU unit, so I put there 20MHz like the quartz value. However, the CPU unit had to run at 48MHz, and I changed it to 48 MHz in project settings, and now Delay_ms(5000) takes 5s, thank you again. :D

Post Reply

Return to “mikroPascal PRO for PIC Beta Testing”