Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

dsPIC30F4013 FOSC

Status
Not open for further replies.

AtomSoft

Well-Known Member
Hello all im stuck yet again on this dsPIC stuff heh this time i want to use a 10Mhz crystal on OSC1:2 and want to turn it to 80Mhz using the PLL on chip. Now i have 2 questions...

1. Will this give me 20MIPS ?
2. If so why doesnt this delay for 1 second ? When i run this code it takes 12 seconds for the led to toggle.

Code:
#include "p30f4013.h"

_FOSC(XT_PLL8)					
_FWDT(WDT_OFF)
_FBORPOR(MCLR_EN & PWRT_OFF)
_FGS(CODE_PROT_OFF)

void delay(void);

int main(void)
{
	ADPCFG=0xffff;
	TRISD=0;				//PORTs all outputs
	LATD=0;
	delay();
	while(1){
		LATDbits.LATD3 = 1;
		delay();
		LATDbits.LATD3 = 0;
		delay();
	}
}

void delay(void)
{
	unsigned long var1;
	for(var1=0;var1!=20000000;var1++);
}
 
The for loop does not execute just a single instruction per run through the loop. Look at the ASM output of the for loop so you can count how many instructions are actually used, then divide your delay number by that many.

In other words, if there are four instructions in the main for loop, then you would only use 5000000 as the delay count. That assumes all instructions are only 1 cycle execution times, which most are.

EDIT: You might have to write your delay loop in ASM, since I think it will be much more precise than a for loop.
 
Last edited:
According to the datasheet, table 23-16 on page 179, a 10MHz external clock will generate 20.0MIPS at PLLx8 mode.

Using their calculation of:
Code:
Instruction Execution Frequency: MIPS = (FOSC * PLLx) / 4 [since there are 4 Q clocks per instruction
cycle]

10 * 8 = 80 /4 = 20 MIPS.

There is a delay function built into C30 apparently.
Microchip Technology User Forums

Wilksey
 
thanks i did a search for delay and found:

libpic30.h:

delay_us
delay_ms

Code:
define FCY 1000000UL
#include <libpic30.h>
int main()
{
  /* at 1MHz, these are equivalent */
  __delay_us(1000);
  __delay32(1000);
}
This function relies on a user-supplied definition of FCY to represent the instruction clock frequency. FCY must be defined before header file libpic30.h is included. The specified delay is converted to the equivalent number of instruction cycles and passed to __delay32(). If FCY is not defined, then __delay_us() is declared external, causing the link to fail unless the user provides a function with that name.
 
Last edited:
20000000. It's looking for the instruction clock frequency, not the oscillator frequency.
 
Something funny here heh:
void __delay_us(unsigned int time);

Says its a UINT but in fact its a Unsigned Char....

I tried:
Code:
		__delay_ms(1000);

//but it didnt work... the below works right...

		__delay_ms(250);
		__delay_ms(250);
		__delay_ms(250);
		__delay_ms(250);
 
Last edited:
It must generate the delay based upon the internal clock, which would be 80MHz. I cant find any info about how the delay is generated, and what timing it uses.

I thought it would use the instruction clock.

Wilksey
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top