• AVR Freaks

Helpful ReplyHot!srand() freezes microcontroller process?

Author
olochua
Starting Member
  • Total Posts : 53
  • Reward points : 0
  • Joined: 2017/06/06 10:38:42
  • Location: 0
  • Status: offline
2018/08/17 13:35:39 (permalink)
0

srand() freezes microcontroller process?

Hello,
 
I am using a PIC24FJ256GA606 and I am having issues with stdlib's srand(). Can calling srand() somehow cause the microcontroller to freeze?
 
I have a function that does the following:
1. Use the current timestamp (unsigned int) to call srand. 
2. Return rand().
 
I later found out that I am not supposed to call srand repeatedly because that will reduce the randomness. However, I don't think that should cause the microcontroller to malfunction. For my case, the function returns successfully for the first few tries. However, later on when it calls srand(timestamp), my control's heartbeat LED freezes and my timer interrupt stops triggering.
 
I also created a new PIC24FJ256GA606 project just to test srand and rand on their own, and they turn out to work fine. At this point, I have a feeling that something else is wrong with my software/hardware, but I can't figure it out. Do you guys know what the cause might be?
 
Thank you for your time.
 
 
post edited by olochua - 2018/08/17 13:39:23
#1
jtemples
عُضْوٌ جَدِيد
  • Total Posts : 11246
  • Reward points : 0
  • Joined: 2004/02/13 12:31:19
  • Location: Southern California
  • Status: offline
Re: srand() freezes microcontroller process? 2018/08/17 13:41:28 (permalink) ☄ Helpfulby olochua 2018/08/17 13:43:34
5 (2)
What does the debugger tell you?
#2
olochua
Starting Member
  • Total Posts : 53
  • Reward points : 0
  • Joined: 2017/06/06 10:38:42
  • Location: 0
  • Status: offline
Re: srand() freezes microcontroller process? 2018/08/17 13:44:10 (permalink)
0
That's a good call, I actually didn't think about using debug mode. It stops at while(1):
 
void __attribute__((naked, noreturn, weak)) TRAPS_halt_on_error(uint16_t code)
{
TRAPS_error_code = code;
#ifdef __DEBUG
__builtin_software_breakpoint();
/* If we are in debug mode, cause a software breakpoint in the debugger */
#endif
while(1);

}
 
Do you know where I can access the error code?
post edited by olochua - 2018/08/17 13:48:23
#3
olochua
Starting Member
  • Total Posts : 53
  • Reward points : 0
  • Joined: 2017/06/06 10:38:42
  • Location: 0
  • Status: offline
Re: srand() freezes microcontroller process? 2018/08/17 13:55:49 (permalink)
0
Quick update: by using New Watch I was able to find out that the trap error code  = 2.
 
Gort2015 in this post https://www.microchip.com/forums/m941827.aspx said the following:
 
16bit address read at odd address.
Your program or the library has bugs.
 
You may be able to look at the stack to see where the return address is.
Or set a global variable, set a no. in each function, check value at address trap.

 
Thanks again guys, I will look into this further.
post edited by olochua - 2018/08/17 13:59:07
#4
qhb
Superb Member
  • Total Posts : 9998
  • Reward points : 0
  • Joined: 2016/06/05 14:55:32
  • Location: One step ahead...
  • Status: offline
Re: srand() freezes microcontroller process? 2018/08/26 00:43:07 (permalink)
0
Did you find the problem?
 

Nearly there...
#5
olochua
Starting Member
  • Total Posts : 53
  • Reward points : 0
  • Joined: 2017/06/06 10:38:42
  • Location: 0
  • Status: offline
Re: srand() freezes microcontroller process? 2018/08/27 10:17:52 (permalink)
0
No I did not... I am still looking into this and I am using the methods proposed in this thread:
https://www.microchip.com/forums/m1065317.aspx
 
Will definitely keep you guys updated on this.
 
Thanks for your time!
#6
olochua
Starting Member
  • Total Posts : 53
  • Reward points : 0
  • Joined: 2017/06/06 10:38:42
  • Location: 0
  • Status: offline
Re: srand() freezes microcontroller process? 2019/04/25 14:09:27 (permalink)
0
This is a duplicate on one of my other posts, since they are caused by the same problem.
 
https://www.microchip.com/forums/m1065317.aspx
 
Thanks!
#7
Jump to:
© 2019 APG vNext Commercial Version 4.5