Search code examples
cembeddedavratmegaatmega32

I want to run two functions simultaneously on AVR microcontroller. Is there any method to do so?


int main(void)
{
   
    DDRC = DDRC | (1<<2);
    DDRC = DDRC | (1<<3);
 
    while (1)
    {  
     //openSolenoidValves(100,60);
     //startStepperMotor();


    } 
void openSolenoidValves(double air, double oxygen) {
                
                PORTC = PORTC | (1<<2);  //open oxygen(normally closed valve)
                PORTC = PORTC & (~(1<<3)); //open air (normally open valve)
                _delay_ms(oxygen);
                PORTC = PORTC & (~(1<<2));//close oxygen
                _delay_ms(air-oxygen);
                PORTC = PORTC | (1<<3);//close air
                _delay_ms(air);
}

void startStepperMotor(){
//this function also has delays
}

I want to start both openSolenoidValve function and startStepperMotor function same time.But both functions have delays. Is there any method to do that? (MicroController-Atmega32)


Solution

  • This millis() function help me to solve my problem.

    Just like the millis() function in Arduino, this function returns the time in milliseconds since the program started.

    As Developers' details, This function has only been tested on the atmega328p but may work on many other AVRs as well.

    Implementing this function is easy - take a look at the example:

    1. First you must initiate the clock with init_millis()
    2. After calling init_millis(), call sei() to enable global interrupts
    3. Use millis() whenever you like to get the time in milliseconds since the program started

    Developers' repo => https://github.com/monoclecat/avr-millis-function#:~:text=avr%2Dmillis%2Dfunction-,Just%20like%20the%20millis()%20function%20in%20Arduino%2C%20this%20function,many%20other%20AVRs%20as%20well.