Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 11:16 01 Aug 2025 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : Suggestions for Embedded MMbasic.

Author Message
MustardMan

Senior Member

Joined: 30/08/2019
Location: Australia
Posts: 175
Posted: 10:10pm 09 Sep 2020
Copy link to clipboard 
Print this post

Hi,

I had a look through the user manuals for MMbasic and could not find the following features which I think would be very useful for some embedded systems. The first needs to be implemented (executed) right at boot time, and the second depends how it is implemented.

1] Identification of the reset source.
2] True-random number seed.

1 - MMbasic has a dedicated variable to indicate if the reset source was the watchdog, but (at least on the PIC12 devices I've used) the PIC processors also have the ability to give more detail of the reset source.
For the application I wrote it was important to know if the reset was caused by a cold-boot (power on event), or MCLR reset, or watchdog. I think it could identify one other too.
For my use I wanted to know if the device had just been turned on for the first time, or if a power glitch had caused it to restart.
It was easy to do in assembler, but the XC8 compiler needed a little more effort to get it to co-operate. There were a couple of bits (documented) in one of the status registers that XC8 would trash unless specifically told to keep.

2 - Getting a true random number is a problem for computers. Nearly all random number generators are pseudorandom and always give the same sequence when started. There are scant few ways for a micro (without add-on hardware) to get a true random number as a micro always starts up the same way and in a known state.
There are a couple of ways - use the LSbit of repeated reads of a high-res ADC connected to something external, or read and XOR the RAM (obviously before it is used for anything).
My application (a different one to the reset source issue) required a better-than-pseudo random number, and I tried the RAM method (my chip had no ADC). It worked very well with it executing a non-predictable timeout.

Although I really like the RAM method I can implement [2] in basic by reading the ADC a few times, but I don't see any way to implement [1].


Any thoughts?

Cheers,
 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6283
Posted: 10:45pm 09 Sep 2020
Copy link to clipboard 
Print this post

For the random number seed, a spare floating analog input is handy.
spare = 23 ' any spare analog pin left floating.
setpin spare, ain
RANDOMIZE pin(spare)*1000

print rnd()


> RUN
0.300794
> RUN
0.0807553
> RUN
0.830729
> RUN
0.62072
> RUN
0.270705
> RUN
0.170715
> RUN
0.370797
>


  Quote  For my use I wanted to know if the device had just been turned on for the first time, or if a power glitch had caused it to restart.


For a first time run, saving a flag using VAR SAVE should do the trick.
If the VAR RESTORE doesn't give what you expect, it must be a first time run...

Jim
VK7JH
MMedit
 
MustardMan

Senior Member

Joined: 30/08/2019
Location: Australia
Posts: 175
Posted: 03:49am 10 Sep 2020
Copy link to clipboard 
Print this post

Hi Jim,

I was looking for a little more detail... like what caused the reset. Was it bad programming (watchdog), bad power (brownout), loss of power (POR), hard reset (MCLR) or soft reset (RESET instruction)...

Mind you, I don't know if these are supported by the PIC32, but being a significantly more capable chip, they probably are. I can understand it, but not having a single datasheet for the PIC32MX chips makes it very difficult (time consuming) to find out even simple information.

Cheers,




UPDATE: I found the relevant PIC32 data sheet - couldn't find it on the Microchip site, had to google it! Pretty poor reflection on the manufacturer. Different register names and bit interpretations, but everything can be ascertained.



Edited 2020-09-10 15:09 by MustardMan
 
Geoffg

Guru

Joined: 06/06/2011
Location: Australia
Posts: 3292
Posted: 07:56am 10 Sep 2020
Copy link to clipboard 
Print this post

You can always use PEEK to examine the reset register (RCON).
As follows: status% = PEEK(WORD &HBF80F600)

The bits are described in the PIC32 MX170 or MX470 data sheet.  Look for the section headed "RCON: RESET CONTROL REGISTER".  This is a summary:

bit 7 EXTR: External Reset (MCLR) Pin Flag
1 = Master Clear (pin) Reset has occurred
0 = Master Clear (pin) Reset has not occurred

bit 6 SWR: Software Reset Flag bit
1 = Software Reset was executed
0 = Software Reset as not executed

bit 4 WDTO: Watchdog Timer Time-out Flag bit
1 = WDT Time-out has occurred
0 = WDT Time-out has not occurred

bit 1 BOR: Brown-out Reset Flag bit
1 = Brown-out Reset has occurred
0 = Brown-out Reset has not occurred

bit 0 POR: Power-on Reset Flag bit
1 = Power-on Reset has occurred
0 = Power-on Reset has not occurred

Note that MMBasic maintains its own watchdog timer and uses a Software Reset if it has expired,

Geoff
Geoff Graham - http://geoffg.net
 
Print this page


To reply to this topic, you need to log in.

The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025