ATMega16: Run a subroutine for a limited time

I have a problem on ATMega16. I can't seem to be able to make a subroutine run for a set amount of time. Here is the code:

void reverse(unsigned x){
OCR0=x;                 //PWM 100%
PORTB &= ~(1<<PB0);     //+ of Motor1   LOW
PORTB |= (1<<PB1);      //- of Motor1   HIGH
PORTB &= ~(1<<PB2);     //+ of Motor2   LOW
PORTB |= (1<<PB4);      //- of Motor2   HIGH
}


so it's basically a motor control subroutine which applies a Fast PWM Signal using Port PB3. The input is the duty cycle. The 2 motors are controlled using a L298.

Now, I want to transform the code so that it takes for input the duty cycle AND a certain value for the time, such as 50ms.

I'm using Atmel Studio 6.0 on Windows XP.

• I think you should fix your question slightly. First, the subroutine that you have doesn't meaningfully run for some duration. It runs once, and the change is permanent. You need to run another funtion or add more code to this, which reverses the effect after some time. Its a critical distinction, that. Secondly, you should clarify your explanation of why you want to do this. I cant understand anything of that paragraph about sensors. Its not required to answer anyway, so you should either explain clearly or replace it with less detail about your robot and more about what you need. – Chintalagiri Shashank Jan 30 '13 at 23:47
• Any particular reason you don't use the PWM hardware in the microcontroller? – Phil Frost Jan 30 '13 at 23:59
• @Chintalgiri Shashank Thank you for your answer, I think I will add a stop subroutine which stops the motors. I don't need to do anything else during the maneuver. My code is much larger and I didnt want to add it all as I assumed it was a very simple answer. Phil Frost I dont use PWM hardware because I dont know how to implement it and this Fast PWM was very simple to implement. – user18476 Jan 31 '13 at 0:00
• @user18476 I'm not saying you should add the whole code to the question. My comment was that you should clarify the question by including details specific to the question, and reduce irrelevant detail which can confuse people looking at the question. Getting something closed that way is far too easy here. – Chintalagiri Shashank Jan 31 '13 at 0:30
• Thank you for your suggestion and help, I have removed that paragraph. – user18476 Jan 31 '13 at 0:31