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)
CodePudding user response:
The ATmega32 only has one core, unfortunately. There is such a thing called a "real time operating system" that lets you run multiple tasks like this at once and I'm sure you could select one that works on the ATmega32 and port your code to it, but that is probably way more trouble than it is worth. Concurrent execution of code can be achieved more easily by writing your code to behave in a non-blocking way (i.e. remove all blocking delays) and/or using interrupts if appropriate.
CodePudding user response:
Yes this is totally possible. It's called cooperative multitasking. Start by studying Blink Without Delay, and my answer here: How to do high-resolution, timestamp-based, non-blocking, single-threaded cooperative multi-tasking
Example:
int main(void)
{
doSetupStuff();
configureHardwareTimer();
while (1)
{
doTask1();
doTask2();
doTask3();
}
}
See my answer above for examples of how to write the 3 tasks in non-blocking ways.
When done correctly, this technique works on literally any platform, from AVR to Arduino to STM32 to Windows, Linux, Raspberry Pi, etc., and can be done in any programming language.
And, it is designed to be single threaded, so there is no need for an OS (Operating System), nor multiple cores, nor a full scheduler. In effect, this is a type of light-weight, cooperative (as opposed to preemptive) scheduler.
It is so light-weight that I believe it could be done on even the tiniest of AVR microcontrollers which have just 64 bytes of RAM, all the way up to the biggest of PCs with hundreds of gigabytes of RAM.