DarthMessiah15 0 Posted July 28, 2011 Share Posted July 28, 2011 Alright guys, ran into a problem that is really really holding me up and could use some quick help. first of which, I need some way to delay operations but I need to be able to pass it a variable instead of just a constant so delay_cycles won't work. Here is what some quick google searching yielded. will this work? void delay_ms(unsigned int delay) { while (delay--) { __delay_cycles(16); } } also, I need the easiest way to get a random number 1-20. I tried.. srand( (unsigned int) time(NULL) ); int i= rand()% 20; is that the best way to go about it? one last question, I need to have some way to know the total time my program has been running and stop after 20 mins. my first idea is to just keep a running tally of the total delays times and just use a while loop and exit once i reach the total, but is this the best way? Thanks in advance guys. i already receive awesome help here. Quote Link to post Share on other sites
EngIP 31 Posted July 28, 2011 Share Posted July 28, 2011 I use the following when I need a variable delay, but I'm certain there are more elegant solutions out there. I think if you need accurate timing (i.e. stop after 20ms) you will need to use timers. I'm not 100% on the syntax, but you can set a timer to increment every x ms, and use that as your base. The actual code will depend on your clock source, the clock divider, timer mode etc. void delay(unsigned int delayTime){ int x; for (x=0;x x=x*1; } } I think the random number seed code you are using needs the time.h header, which I don't think exists/works for the MSP430. Sorry I wasn't of more help, but I'm sure someone else will clear things up and put me right. Quote Link to post Share on other sites
gordon 229 Posted July 28, 2011 Share Posted July 28, 2011 rand() % 20 will give you a result ranging from 0 to 19, not 1 to 20. As for the random seed, quoting oPossum, "ADC noise - use the lsb of an ADC reading as a random bit - use 16 adc readings to make a random 16 bit integer". Quote Link to post Share on other sites
DarthMessiah15 0 Posted July 30, 2011 Author Share Posted July 30, 2011 Ok so I understand I need to use an adc reading as the seed, anyone care to explain what an adc reading is and how to go about taking one? thanks. Quote Link to post Share on other sites
DarthMessiah15 0 Posted August 1, 2011 Author Share Posted August 1, 2011 no help possible on the adc seed? or the whole twenty minute timer set up? Quote Link to post Share on other sites
gwdeveloper 275 Posted August 1, 2011 Share Posted August 1, 2011 Have you looked at TI's examples for taking an analog reading? The examples for each mcu tend to have multiple ways to use the analog to digital converter. As far as a 20 minute timer... setup your Timer ISR to increment a pointer at 1s intervals. At the start of the ISR, compare your seconds pointer against the # of seconds in 20 minutes. Quote Link to post Share on other sites
oPossum 1,083 Posted August 1, 2011 Share Posted August 1, 2011 Here is some code for random number generation using ADC noise. This can be used for seeding a PRNG. The ADC input used should be left floating - not connected to anything. The distribution is probably quite bad - I suspect it is lumped near the center. unsigned adc_rnd(void) { unsigned n; unsigned b = 16; do { ADC10CTL0 |= ADC10SC; // Start ADC conversion while(!(ADC10IFG & ADC10IFG0)); // Wait for conversion complete n <<= 1; // Make room for a bit n |= (ADC10MEM0 & 1); // Append lsb of ADC reading } while(--; return n ^ 0x5555; } // Init the ADC (in main() or other suitable location) ADC10CTL0 &= ~ADC10ENC; ADC10MCTL0 = ADC10INCH_12; ADC10CTL2 = ADC10RES; ADC10CTL1 = ADC10DIV2 | ADC10SSEL_0 | ADC10SHP; ADC10CTL0 = ADC10SHT_2 | ADC10ON | ADC10ENC; Here is a visualization of the results shown on a 256 x 192 graphic display. Quote Link to post Share on other sites
DarthMessiah15 0 Posted August 2, 2011 Author Share Posted August 2, 2011 so would I just call this function to seed srand? Thanks by the way. really bailing me out. Quote Link to post Share on other sites
gordon 229 Posted August 2, 2011 Share Posted August 2, 2011 You call this function to get a number to seed the RNG with. Quote Link to post Share on other sites
oPossum 1,083 Posted August 2, 2011 Share Posted August 2, 2011 srand(adc_rnd()); Quote Link to post Share on other sites
Fe2o3Fish 33 Posted August 2, 2011 Share Posted August 2, 2011 One question 'possum: What purpose is served by the statement "return n ^ 0x5555;" ??? -Rusty- Quote Link to post Share on other sites
ike 53 Posted August 2, 2011 Share Posted August 2, 2011 I think that, this step unbiased output, if ADC produce more 1's or 0's http://en.wikipedia.org/wiki/Hardware_r ... _whitening . New result is not more random, it is just more unbiased. In this case "random number" is used as a seed and there is no benefit of unbiasing. Quote Link to post Share on other sites
DarthMessiah15 0 Posted August 8, 2011 Author Share Posted August 8, 2011 When using this to initialize adc10, ADC10CTL0 &= ~ADC10ENC; ADC10MCTL0 = ADC10INCH_12; ADC10CTL2 = ADC10RES; ADC10CTL1 = ADC10DIV2 | ADC10SSEL_0 | ADC10SHP; ADC10CTL0 = ADC10SHT_2 | ADC10ON | ADC10ENC; I'm getting these errors? "../main.c", line 26: error: identifier "ADC10ENC" is undefined "../main.c", line 27: error: identifier "ADC10MCTL0" is undefined "../main.c", line 27: error: identifier "ADC10INCH_12" is undefined "../main.c", line 28: error: identifier "ADC10CTL2" is undefined "../main.c", line 28: error: identifier "ADC10RES" is undefined "../main.c", line 29: error: identifier "ADC10SHP" is undefined Any ideas? Quote Link to post Share on other sites
gwdeveloper 275 Posted August 8, 2011 Share Posted August 8, 2011 Have you included your msp430xxxxx.h? Normally, as a CCS user, I just use: #include But you may need to use the device specific header... msp430g2231.h or such. 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.