Online random number generator for MSP430 microcontroller

This is an online random number generator for a MSP430 microcontroller, you can use it to include some randomness in your project.
When using these static random numbers in more than one MSP430 controller, they all got the same random numbers, however all oscillators differ somewhat in speed over time and thus multiple MSP430 controllers using the same random number table do really seem random.

How many random numbers do you need? Hint: you can fill the rest of the free program flash memory with random numbers. (Open the .map file, scroll to ‘MEMORY CONFIGURATION’ and lookup the ‘unused’ column on the ‘FLASH’ row.)

Save this as “randomvals.h”:

/* generated using: http://atoomnet.net/?p=462 */

#ifndef RANDOMVALS_H_
#define RANDOMVALS_H_

//32 guaranteed random numbers
const uint8_t random0[] PROGMEM = {
  0x11, 0x2F, 0x1C, 0x0B, 0xCA, 0x75, 0x36, 0x0E, 0x9F, 0x63, 0xD4, 0xD2, 0x78, 0xC4, 0xC9, 0x8B, 0x24, 0x61, 0x8F, 0xFB, 0xF8, 0x50, 0xA4, 0x17, 0xB1, 0x3A, 0x83, 0xBA, 0xFB, 0x1D, 0xA8, 0xEC, 
};

#endif /* RANDOMVALS_H_ */

Save this as “random.h”:

#ifndef RANDOM_H_
#define RANDOM_H_

#include <stdint.h>

uint8_t READrnd(void);

#endif /* RANDOM_H_ */

Save this as “random.c”

#include <stdint.h>

#include "randomvals.h"

uint16_t ptrRnd = 0;

//Read a random value from randomvals.h, wrap when reaching the end of the table.
uint8_t READrnd()
{
  uint8_t rnd = random0[ptrRnd++];

  if (ptrRnd==sizeof(random0))
    ptrRnd = 0;

  return rnd;
}

Move the saved files into your project.

This example outputs a 8-bit random value to PORT1 of the MSP430 microcontroller.
When run on the TI Launchpad, the LED1 and LED2 will flash randomly. The example was tested on the M430G2553, MSP430G2452, MSP430G2353 and the MSP430G2221 microcontroller.

#include <msp430.h>
#include <stdint.h>

#include "random.h"

int main(void)
{
  int i;
  WDTCTL = WDTPW | WDTHOLD;    // Stop watchdog timer

  P1DIR = 0xFF; //Port 1 all pins output

  for(;;)
  {
    uint8_t randomValue = READrnd(); //Get 8-bit random value from array

    P1OUT = randomValue; //Output random value to port 1

    //Do nothing for some time
    for(i = 10000; i >= 0; i--)
      asm("nop");
  }

}

Did you like this post? Please give it a thumbs up by sharing it on your social network. Thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *