Jump to content
43oh

# Desperate Help Needed.

## Recommended Posts

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.

##### Share on other sites

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.

##### Share on other sites

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".

##### Share on other sites

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.

##### Share on other sites

no help possible on the adc seed? or the whole twenty minute timer set up?

##### Share on other sites

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.

##### Share on other sites

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.

##### Share on other sites

so would I just call this function to seed srand? Thanks by the way. really bailing me out.

##### Share on other sites

You call this function to get a number to seed the RNG with.

##### Share on other sites
```srand(adc_rnd());
```

##### Share on other sites

One question 'possum:

What purpose is served by the statement "return n ^ 0x5555;" ???

-Rusty-

##### Share on other sites

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.

##### Share on other sites

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?

##### Share on other sites

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.

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
• Blog

• #### Activity

×
• Create New...