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.

Link to post
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.

Link to post
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.

Link to post
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.

 

Link to post
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?

Link to post
Share on other sites

Join the conversation

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

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

×
×
  • Create New...