• AVR Freaks

AnsweredHot!caculating how many instructions pic32 dose each time round a loop.

Author
Karina
New Member
  • Total Posts : 2
  • Reward points : 0
  • Joined: 2019/11/26 03:48:47
  • Location: Manchester
  • Status: offline
2019/11/29 04:03:54 (permalink)
0

caculating how many instructions pic32 dose each time round a loop.

Hi guys, i'm really new to programming and i'm writing a delay for an LED loop, i'm using the PIC32MZ2048EFH it has a clock speed of 200mHz. I have this code for a pic16 I did a while back: 
 
int i, j;
for(j = 13; j == 0; j‐‐)
{
for(i = 65535; i == 0; i‐‐);
}
 
If the inner loop takes 0.79 seconds to complete, running that loop 13 times should create a delay of about 10 sections because the pic16 dose 12 instructions per each time around the loop and each instruction takes 1us. 
 
now I want to workout how many instructions the pic32 dose each time around a loop to figure out what value I need in my loop so that it is 1 second each loop. can anyone help? thankyou in advance :) 
#1
ric
Super Member
  • Total Posts : 25225
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: online
Re: caculating how many instructions pic32 dose each time round a loop. 2019/12/02 16:23:30 (permalink) ☄ Helpfulby Karina 2019/12/09 01:28:27
5 (2)
You can't do delays like that in a PIC32 processor
Compiler optimisation will discard your code, and even if you make your variables "volatile" to avoid that, the caching nature of the execution engine complicate the maths.
Search this forum for ways to generate delays using the core timer.
 

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#2
NKurzman
A Guy on the Net
  • Total Posts : 18186
  • Reward points : 0
  • Joined: 2008/01/16 19:33:48
  • Location: 0
  • Status: online
Re: caculating how many instructions pic32 dose each time round a loop. 2019/12/02 17:33:59 (permalink) ☼ Best Answerby Karina 2019/12/09 01:29:25
5 (2)
something like:
void delayUs(uint16_t microseconds)
{
   uint32_t time;

   time =  _CP0_GET_COUNT()); // Read Core Timer

  time += (SYS_CLK_FREQ / 2 / 1000000) * microseconds; // calc the Stop Time

  while ((int32_t)(time -  _CP0_GET_COUNT()) > 0){};

}
 
SYS_CLK_FREQ is generated by Harmony.
You could also use one of the Hardware timers.
Note this will only work for 2^15 core timer counts.
 
But in Answer to you question and assuming you head ric's advice.  You can read the core timer before and after your delay.  BUT if interrupts are firing, those will be in the count too.
The Simulator may also get the number you want.
 
And for the PIC16 the XC8 compiler has __delay_us() and __delay_ms() 
 
 
 
#3
ric
Super Member
  • Total Posts : 25225
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: online
Re: caculating how many instructions pic32 dose each time round a loop. 2019/12/02 17:47:35 (permalink)
5 (1)
oh and FYI, "200 mHz" is 200 milliHertz, which is one clock cycle every 5 seconds.
I doubt you'd need any delays at that speed! ;)
 
"mHz" != "MHz"

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#4
Karina
New Member
  • Total Posts : 2
  • Reward points : 0
  • Joined: 2019/11/26 03:48:47
  • Location: Manchester
  • Status: offline
Re: caculating how many instructions pic32 dose each time round a loop. 2019/12/09 01:30:30 (permalink)
4 (1)
Thankyou for your help guys i'm so bad at this haha i'm trying to learn it all. 
#5
Jump to:
© 2020 APG vNext Commercial Version 4.5