dino7 0 Posted June 16, 2013 Share Posted June 16, 2013 In an attempt to port one of my projects from AVR to the MSP430G2553 and failing horribly because the printf() function in CCS does not fit into the 512 bytes RAM, I did manage to port a single function. The function, READrnd(), reads some pre generated random numbers from flash memory. I use them to give my projects some (predefined) randomness. For example, the project I want to port uses the predefined random numbers to "randomly" turn on and off 8 relays. I am going to use it for some night/day simulation in houses on a model railroad track. Anyway, with the MSP430 version of the online number generator you can generate random numbers. Here are the sources. randomvals.h: /* generated using: http://atoomnet.net/?p=462 */ #ifndef RANDOMVALS_H_ #define RANDOMVALS_H_ //32 guaranteed random numbers const uint8_t random0[] PROGMEM = { 0x87, 0x22, 0xFB, 0x0D, 0x23, 0x03, 0x60, 0x3F, 0x7A, 0x65, 0xC5, 0xF2, 0x10, 0xF2, 0x2A, 0xB3, 0x7D, 0xD4, 0x5D, 0x8B, 0xD5, 0xF5, 0x1B, 0x4C, 0xC7, 0x45, 0xAF, 0x5E, 0x81, 0xAB, 0x71, 0xF8, }; #endif /* RANDOMVALS_H_ */ random.h: #ifndef RANDOM_H_ #define RANDOM_H_ #include <stdint.h> uint8_t READrnd(void); #endif /* RANDOM_H_ */ 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; } With these files you have (in this example) 32 bytes of random numbers to work with. Here is the example program running on the TI launchpad I used to test the above sources. Led1 and led2 will flash seemingly random. main.c: #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"); } } All settings are default except the optimization: -O4 --opt_for_speed=0The asm("nop") forces the compiler to not optimize away the (for it) pointless for-loop. I hope you like it and, as a beginner in the MSP430 architecture all feedback is welcome. Quote Link to post Share on other sites
pabigot 355 Posted June 16, 2013 Share Posted June 16, 2013 A fixed set of numbers can work in some cases, but I think srand() and rand() are more commonly used for this sort of thing. Are they missing from CCS's libraries?embtextf might help with your printf size issues. If you use CCS as an IDE you might have to run the configure step manually to create the header, but from that point you should be able to just add the sources to your project. Quote Link to post Share on other sites
dino7 0 Posted June 16, 2013 Author Share Posted June 16, 2013 Thanks, did not know srand() existed (even for avr so it seems). So, in essence I reinvented the wheel. Thanks for the embtextf link, will have a look into that. Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.