Software > Software

sending out 1.5ms

<< < (2/2)

dunk:
hmm. not really.
i'm guessing you meant to switch a pin on, wait 1500ms, switch pin off, wait ????ms, repeat?
which would be like this:

--- Code: ---main(){
  while (0) {
    i = 0;
    pin_on();
    while (i < 1500){
      i++;
      pause_ms(1)
    }
    pin_off();
    while i < 5000){
      i++;
      pause_ms(1)
    }
  }
}

--- End code ---

what i was talking about was this sort of thing:

--- Code: ---#include <avr\io.h>
#include <avr\iom8.h>
#include <avr\interrupt.h>

#define XX **insert your value here**
#define YY **insert your value here**

uint8_t tick;
/* signal handler for timer interrupt TOV0 */
ISR(TIMER0_OVF_vect) {
  /* increase tick count every time timer interupt overflows */
  tick;++;
  /* monitor tick count and set servo pin value accordingly */
  if (tick <= XX){
    /* switch servo pin on */
  }
  if ((tick > XX) && (tick <= YY))
   /*switch servo pin off */
  }
  if (tick > YY){
    /* reset timer counter */
    tick = 0;
  }
}

int main(void) {
  /* use PortB for output */
  outp(0xFF, DDRB);
  /*enable timer overflow interrupt*/
  outp((1<<TOV0), TIMSK);
  /*set timer counter initial value*/
  TCNT0=0x00;
  /*start timer without presscaler*/
  outp((1<<CS00), TCCR0);
  /* enable interrupts */
  sei();

  /*loop forever */
  while 0 {
    /* do whatever you want in here. the timer interupt routine will take care of the servo pulses */
  }
}

--- End code ---
so this code should nearly work as is but you still have the hard work to do.
you need to find out how often the timer interupt is called with your clock frequency and put in values for XX and YY accordingly.

there's a reasonable intro to timer0 on the AVRs here:
http://winavr.scienceprog.com/avr-gcc-tutorial/control-avr-8-bit-timer-counter0-using-avr-gcc.html

dunk.
(hope i haven't over complicated things...)

ed1380:
I just got this. Since this is a light seeking robot, if we unplug or cover up the photocells it'ss automatically stop and center teh servo's.
Right?

dunk:

--- Quote ---I just got this. Since this is a light seeking robot, if we unplug or cover up the photocells it'ss automatically stop and center teh servo's.
Right?
--- End quote ---
watching the video of this little bot, i don't think the servos are ever stationary. they are either full forward or full reverse.

after skimming through Admin's code,
i see where you were getting the servo_right(23) and servo_left(23) functions.
Admin has done all the hard work for you here and if i interpret his comments in the file: SoR_Utils.h correctly,
multiply the number in the servo_right() function by 0.0431 to get the number of milliseconds pulse sent to the servo.
for example:
servo_right(23)
23 x 0.0431 = 0.992ms

the servo_right(23) function switches on the pin, waits for 23 time delays (of 0.0431ms each) then switches off the pin again, then waits for a further 200 time delays (of 0.0431ms each).

ok, so, the center position....
35 x 0.0431 = 1.5081ms.
so if you replace the relevant part of the main() function in Admin's code with this to get both servos in their center position.

--- Code: ---while (0) {
  servo_right(35);
  servo_left(35);
}

--- End code ---

sorry for not taking the time to read the $50 code before taking you on a trip of AVR timing routines....
i was explaining how to do it from scratch.

hmm. now i think about it, the title of this thread requested 1500ms. that's 1.5 seconds. i think your decimal point is off there.
you want 1.5ms for the center position on a servo.

dunk.

ed1380:
Thank you so much.
Now that, that is solved I still have a problem.

Mind checking this out? ;D
http://www.societyofrobots.com/robotforum/index.php?topic=865.0

Navigation

[0] Message Index

[*] Previous page

Go to full version