PIC24HJ32G202 Timing off by a factor of 100

General discussion on mikroBasic PRO for dsPIC30/33 and PIC24.
Post Reply
Author
Message
Chaz
Posts: 13
Joined: 03 Dec 2007 07:25

PIC24HJ32G202 Timing off by a factor of 100

#1 Post by Chaz » 29 Jan 2010 08:10

I upgraded to the Pro compiler. Very nice! My first program is for the PIC24HJ32G202. The program is very simple. It blinks an LED connected to AN0. I use MPLAB v8.43 to program the PIC using ICSP via ICD2. My board uses a 20MHz crystal. I use the project defaults except for the Oscillator source which I changed to HS from the XT default.

The program is below.

Code: Select all

program P24HJ32GP202_LED_BLINK

main:

  TRISA = 0x0000

  while TRUE
        LATA = 0xFFFF
        delay_ms(5)
        LATA = 0x0000
        delay_ms(5)
  wend

end.
The LED flashes but the on time and off times are about 500 millseconds not the 5 ms I programmed. Can someone please HELP!


Image


Image

javor.pas
Posts: 98
Joined: 16 Mar 2007 11:25
Contact:

#2 Post by javor.pas » 29 Jan 2010 10:32

IMHO it's a configuration mistake. You've configured the Oscillator for 20MHz and Primary one as HS. So far, so good. In the Primary Oscillator Mode I see HS Oscillator Mode. It means the frequency is multiplied. So if you change either the Oscillator Frequency or the Primary Oscillator Mode everything will be fine :)

Regards,

Javor
I sit and think, sit and think... and some time I notice I only sit :)

peterverkaik
Posts: 174
Joined: 31 Aug 2009 22:44

#3 Post by peterverkaik » 29 Jan 2010 14:16

Here is a PLL_Init routine

Code: Select all

'//external resonator 20MHz
'//Fosc = (20MHz/10)*(79)/2 = 79MHz
'//           PLLPRE^    ^PLLPOST
'//           2MHz        158MHz
sub procedure PLL_Init
  PLLFBD = 77                      '// M = 79
  CLKDIV = (CLKDIV AND $FFE0) OR 8 'PLLPRE = 8            '// N1 = 10
  CLKDIV = (CLKDIV AND $FF3F) OR 0 'PLLPOST = 0           '// N2 = 2
  OSCTUN = 0
  RCON.SWDTEN = 0
  '// Clock switch to incorporate PLL
  OSCCONH = 0x01    '// Initiate Clock Switch to FRC with PLL (NOSC=0b001)
  OSCCONL = 0x01    '// Start clock switching
  while ((OSCCON AND $7000) <> $4000) '// Wait for Clock switch to occur
  wend
  while (OSCCON.LOCK_ <> 1)
  wend
end sub

This is used on a dspic33FJ128GP804

regards peter

Chaz
Posts: 13
Joined: 03 Dec 2007 07:25

#4 Post by Chaz » 29 Jan 2010 20:39

I displayed the configuration bits using the imported hex file in MPLAB. It shows a different configuration than I selected in the Pro Basic for dsPIC compiler. It appears the low speed RC oscillator has been enabled. I can run the PIC program after removing the crystal from my circuit.

Below is the MPLAB configuration values. I am now totally confused.

Image

extrapilot
Posts: 130
Joined: 28 Nov 2006 17:59

#5 Post by extrapilot » 01 Feb 2010 03:17

Per the spec, you cannot boot the dsPIC with an external clock > 8Mhz without clock switching (from FRC to HS). Seems like some of the ME boards ship with 10Mhz externals, and they boot without clock switching- but regardless...

For your Config file (Edit Project), the Oscillator should be set as Internal FRC. The rest looks ok (i.e. Primary is HS Oscillator mode, Clock Switching is Enabled, Mon disabled).

With that, the dsPIC can at least boot and get to clock switching code (like Peter referenced).

It might be that you are able to boot without the external xtal because the dsPIC is falling back to the internal osc.

There is a pretty detailed section on oscillator config in the data sheet- which will describe the divider and PLL multiplier values. Just make sure that you mind the limitations set for each stage, as there are many combinations that will yield a desired Fcy but will violate the spec for a given stage in the PLL.

Post Reply

Return to “mikroBasic PRO for dsPIC30/33 and PIC24 General”