This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Random number using timer on MSP430

Other Parts Discussed in Thread: MSP430F2370, MSP430F2618

Hi,

I am trying to get a random number in my MSP430F2370 application with help of the SLAA338 document, but I cannot understand the following sentence below point 2, "Setup". "ACLK is set to the VLO, which is the trigger for the capture."

How do I get ACLK as source for capture, the capture input select bits (CCISx) has only these settings; 00 CCIxA, 01 CCIxB, 10 GND and 11 VCC.

Must I use the ACLK output and route it to a CCIxA or CCIxB input, is it not any way to fix this internally?

Instead I tried to use both timer A and B with DCOCLK and VLOCLK, counting number of DCO clockcycles on one VLO clockcycle, but it always return an even number of DCO clockcycles, although very different numbers every time..

Any ideas?

http://www.ti.com/lit/slaa338

/Regards

  • Hi trewq,

    what about using the library function rand located in stdlib.h?

     int    rand(void);

     void   srand(unsigned _seed); // for initializing the random number generator

    Rgds
    aBUGSworstnightmare 

  • ACLK is internally routed to CCI2B of both TA and TB. See Data Sheet description of Timer_A3 and Timer_B3 (probably on page 15 and 16).

  • Might be a bad idea. rand() and srand() are most likely pseudo-random, not random. Not good for cryptography...

  • Thanks , I found it, P1.3 with CCI2B is ACLK (internal)....very strange that there is nothing about this in the Timer section of MSP430x2xx Family User's Guide

    I will try it on monday!....

    I have though about using rand() function, but when I saw the timer solution I wanted to test it...

  • Tried it now and it works quite well, generating 1.000.000 bits, 50.16% were even, i.e zero..

  • please, can anyone explain me the method for generating random number in MSPF2132. I dont want to use the method of using clocks. 

  • There is no such thing as "the method for generating random number in MSPF2132".

    TI application note slaa338.pdf suggested a method, which can be used in any F2xx. It uses the CPU, the DCO, the VLO, and the Timer-CC. But you do not want to use clocks. What do you want to use instead?

    If you have a random voltage source, may be you can use the ADC10. If you have a random noise generator, may be you can use that. A Geiger Counter or roulette wheel could work too.

  • The trick on the 2132 is that both signals, clock output and capture input are on the 2132 (and only there) internally connected to the same physical pin. So you can activate both signals (clocking the capture input by the clock output) using no external connection. On all other MSPs I know of, both signals are on separate pins and require a different port pin handling as well as an external connection.

    The backgound of the algorithm is that two clocks are used which have different speed to each others and across different processors and also change speed differently depending on the operating temperature and supply voltage changes. So if you count the number of ticks of one clock during the other clocks tick period, the result is a little bit random. At least much more than using the stdlib function, which results in the very non-random and always identical chain of  values.

    Anyway, a good random number generator requires at least a seed that is really random (even if it may be very complex and time-consuming to create it).
    So you can combine the system function and the timer-based random generator to generate a more random seed than the fixed value normally used.
    Sound consfusing? It is. Randomness IS confusing. How random is random? A true random generator may produce a million times the same number. This is as random as a million different numbers where no two are ever the same. And should happen with the same probability. Probably useless for the intended purpose, but random nevertheless. As long as it does not happen after every program start.

  • Hi everyone,

    I've just installed on my MSP430f2618 the random number generator code published by Lane Westlund with his paper SLAA338, and I think it works (fine?) in debugging mode. I would like to send the generated randum numbers to the computer via the UART interface (USCI_A1). Because the SMCLK and the ACLK are used and changed in every cycle, has anyone any idea of how can i solve the data transfering? (which clock source and how..)

    Thanks in advance, Ákos

  • I cannot chew gum and walk at the same time, but I can chew gum while sitting down, spit it out, stand up, and start to walk.

  • Can we implement Your answer in practice? (practically what is Your opinion?) :-) 

  • After you got a random number, keep it in a variable, set up one of the clocks to drive the UART, use the UART to send the saved random number.

    When you cannot do two or more things at the same time, try to do them one at a time consecutively. They may just work out.

  • Does TI provide any documentation for the stdlib.h functions?    I want to use the rand() function and had some questions that would be answered in a API doc.  I looked in the install folder for CCS for *.pdf and also checked the wiki page:

    http://processors.wiki.ti.com/index.php/Category:Code_Composer_Studio_v5

    Thanks,

    Doug

**Attention** This is a public forum