• AVR Freaks

Helpful ReplyHot!What is the best method to develop multitasking code for projects ?

Page: 12345.. > >> Showing page 1 of 7
Author
eagle1
Super Member
  • Total Posts : 396
  • Reward points : 0
  • Joined: 2014/11/02 03:04:06
  • Location: Saudi Arabia
  • Status: offline
2019/11/21 11:09:01 (permalink)
0

What is the best method to develop multitasking code for projects ?

Hello,
 
I just learned that RTOS/multitasking are how the real world works. And I did a several projects with Arduino using millis() function that counts every 1ms in a 32 bits register and would overflow in 50 days. So it's very helpful in doing the timing for the functions. But I don't know what kind of RTOS I'm doing .. is it preemptive or cooperative. Even if I know something about these two types but I don't know how they should be implemented in code.
 
Also if I want to start developing my RTOS code, what's the good optimized code I can do ?
 
First of all I don't know where I copied this code for the PIC16F877A, but I know I can do the same method for my PIC18F4550 and PIC18F46k22.
 
This is the same strategy I learned from Arduino world.
 
volatile uint32_t tick_cnt_1ms;
uint32_t blink_start, blink_current, blink_period = 500;

void pins_init(void){
    TRISC = 0x00;TRISB = 0x00;TRISD = 0x00;
}

void interrupt_init(void){
    INTCONbits.GIE = 1;
    INTCONbits.PEIE = 1;
    PIE1bits.CCP1IE = 1;
    PIE1bits.TMR2IE = 1;
}

void tmr2_init_1ms(void){
    T2CON = 0x01; // for ~1ms OVF PS:4=0x01 @4MHz, PS:16=0x02 @16MHz
    PR2 = 249;
}

void start_tmr2(void){
    T2CONbits.TMR2ON = 1; // start TMR2
    TMR2 = 0;
}

void interrupt multitasking(void){
    if(PIR1bits.TMR2IF){
        tick_cnt_1ms++;
        PIR1bits.TMR2IF = 0;
    }
}

////////////////////////////////////////////////////////
// multitasking example
void blink_rd2(void){
    if(!blink_lock){
        blink_start = tick_cnt_1ms;
        PORTDbits.RD2 = 1;
        blink_lock = 1;
    }
    if(blink_lock){
        blink_current = tick_cnt_1ms;
        if((blink_current - blink_start>= 500) && blink_lock == 1){
            PORTDbits.RD2 ^= 1;
            blink_start = tick_cnt_1ms;
        }
    }
}

////////////////////////////////////////////////////////
// main
void main(void) {
    pins_init();
    tmr2_init_1ms();
    start_tmr2();
    interrupt_init();
    
    while(1){
        blink_rd2();
    }
}

 
Now how good is this strategy ?
 
I think it's ok for basic to mid projects, but I think high projects use more developed/high end coding system.
#1
NKurzman
A Guy on the Net
  • Total Posts : 18043
  • Reward points : 0
  • Joined: 2008/01/16 19:33:48
  • Location: 0
  • Status: online
Re: What is the best method to develop multitasking code for projects ? 2019/11/21 11:23:37 (permalink) ☄ Helpfulby eagle1 2019/11/21 11:25:11
+1 (1)
Your RTOS is "Bare Metal"  You do not have one.
8 Bit PIC are not very suited to using an RTOS. and it is not usually required.
preemptive on many PICs is not Possible due to the Stack.
 
Your Code is Round Robin (more similar to cooperative).
You can read all about in on the Internet. 
An RTOS is a code Framework, not a codding style.
 
    if(blink_lock)
    {
        blink_current = tick_cnt_1ms;
        if((blink_current - blink_start>= 500) && blink_lock == 1)
 
you are checking blink_lock twice
 
 
#2
eagle1
Super Member
  • Total Posts : 396
  • Reward points : 0
  • Joined: 2014/11/02 03:04:06
  • Location: Saudi Arabia
  • Status: offline
Re: What is the best method to develop multitasking code for projects ? 2019/11/21 11:39:50 (permalink)
0
NKurzman
Your RTOS is "Bare Metal"  You do not have one.
8 Bit PIC are not very suited to using an RTOS. and it is not usually required.

 
ok, but why it's not suited for RTOS, then what is the main goal of 8-bit PICs ? what is the main target devices used for 8-bit PICs I know microchip is manufacturing these chips for commercial distribution for other companies to be installed in simple home appliances, am I right ? so these simple appliances don't need RTOS system, but rather a more secure simple code to work just ok and not freeze or stuck on consumers.
 
 
preemptive on many PICs is not Possible due to the Stack.
so this type is the more complex one I guess
 
Your Code is Round Robin (more similar to cooperative).
You can read all about in on the Internet. 
An RTOS is a code Framework, not a codding style.

 
 Yep it's a framework of course.
 

void blink_rd2(void){
if(!blink_lock){
blink_start = tick_cnt_1ms;
PORTDbits.RD2 = 1;
blink_lock = 1;
}
if(blink_lock){
blink_current = tick_cnt_1ms;
if((blink_current - blink_start>= 500)){
PORTDbits.RD2 ^= 1;
blink_start = tick_cnt_1ms;
blink_lock = 0;
}
}
}

 I deleted the second check and cleared blink_lock.



#3
NKurzman
A Guy on the Net
  • Total Posts : 18043
  • Reward points : 0
  • Joined: 2008/01/16 19:33:48
  • Location: 0
  • Status: online
Re: What is the best method to develop multitasking code for projects ? 2019/11/21 12:19:29 (permalink) ☄ Helpfulby eagle1 2019/11/21 12:39:36
+2 (2)
A Preemptive RTOS Usually will point to a different Stack when the Task Changes.
This is not possible with most PIC10-18 CPUs  They only have a Address Stack.  Not the common Address/parameter Stack "Real" CPUs have.  the Stack can not even be read in many PICs. this means that it can not be copied (slow, But Possible).  There are other issues with paging of the PICs memory.
If your Goal is learning RTOS then that in not the correct CPU. 
Most ( All?) 8bit PIC Projects do not require and RTOS.
 
Since an RTOS is Not a requirement for many Projects, the PIC is susceptible for  for those small projects.
 
#4
ric
Super Member
  • Total Posts : 24608
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: online
Re: What is the best method to develop multitasking code for projects ? 2019/11/21 12:24:45 (permalink) ☄ Helpfulby eagle1 2019/11/21 12:46:21
+3 (3)
eagle1
I just learned that RTOS/multitasking are how the real world works.

This is a vague statement that doesn't really mean anything.
If you mean every real microcontroller project includes an RTOS, you're wrong.
 

But I don't know what kind of RTOS I'm doing .. is it preemptive or cooperative.

You're not.
If you're succesfully doing a few operations in parallel the way you mention, that could be regarded as "round robin multitasking". It's not an RTOS (Real Time Operating System).
 

Also if I want to start developing my RTOS code, what's the good optimized code I can do ?

For starters, stop throwing around terms like RTOS which you don't really understand, and concentrate on what you really require.
 

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#5
eagle1
Super Member
  • Total Posts : 396
  • Reward points : 0
  • Joined: 2014/11/02 03:04:06
  • Location: Saudi Arabia
  • Status: offline
Re: What is the best method to develop multitasking code for projects ? 2019/11/21 12:45:04 (permalink)
0
NKurzman
A Preemptive RTOS Usually will point to a different Stack when the Task Changes.
This is not possible with most PIC10-18 CPUs  They only have a Address Stack.  Not the common Address/parameter Stack "Real" CPUs have.  the Stack can not even be read in many PICs. this means that it can not be copied (slow, But Possible).  There are other issues with paging of the PICs memory.
If your Goal is learning RTOS then that in not the correct CPU. Most ( All?) 8bit PIC Projects do not require and RTOS.
OK, I understand that. 8-bit chips aren't for bigger/more complex code.
 
 
Since an RTOS is Not a requirement for many Projects, the PIC is susceptible for  for those small projects.
here where I want to understand more, is that the main target for 8-bit chips and what kind of code is suitable for it ? also if the project doesn't require an RTOS code, then what is the other alternative or the default coding system that is required or proper ?
 




#6
eagle1
Super Member
  • Total Posts : 396
  • Reward points : 0
  • Joined: 2014/11/02 03:04:06
  • Location: Saudi Arabia
  • Status: offline
Re: What is the best method to develop multitasking code for projects ? 2019/11/21 13:08:34 (permalink)
0
ric
This is a vague statement that doesn't really mean anything.

it was just an assumption I thought about that companies that ship devices or appliances with microcontrollers inside is doing some kind of multitasking/RTOS. Also I don't know if multitasking is the same as RTOS or they are different ? But I think RTOS is much more for bigger systems; like, smartphone kernels .. etc.
 
If you mean every real microcontroller project includes an RTOS, you're wrong.
Yeah I thought so, not every device including a microcontroller chip should/must have an RTOS code.
 
For example, but this is my guess I'm not sure if I'm right or not; new modern TVs with advanced menu, smartphones, car's computer unit ... etc. These applications require a RTOS.
 
But other simpler devices; like, TV remote, AC split remote and split receiver unit, gamepad controller, keyboard, mouse, clothe/dish washing machine, refrigerator ... these don't require heavy task scheduler.
 
 
 
You're not. If you're succesfully doing a few operations in parallel the way you mention, that could be regarded as "round robin multitasking". It's not an RTOS (Real Time Operating System).

 
Yep it's actually doing few operations in parallel, there's no task scheduler system.
 
OK, now "round robin multitasking" is good for many projects, right ? but how to know what code is suitable for certain project ? when to know that I need a "round robin multitasking" or something else ?
 
also how round robin multitasking is rated at ? is it a good strategy ? should learn something else ?
 
For starters, stop throwing around terms like RTOS which you don't really understand, and concentrate on what you really require.

 
Yep, you're right, I should learn every thing as I move more and more in microcontrollers, I shouldn't jump to something that is rather very difficult and I don't need right now.
 
I just wanted to know what is the best way or task scheduler that I can count on for my projects?
#7
Sandra232
New Member
  • Total Posts : 5
  • Reward points : 0
  • Joined: 2019/11/12 17:27:16
  • Location: 0
  • Status: offline
Re: What is the best method to develop multitasking code for projects ? 2019/11/21 13:23:57 (permalink)
0
 8-bit chips aren't for bigger/more complex code. 
#8
jtemples
عُضْوٌ جَدِيد
  • Total Posts : 11428
  • Reward points : 0
  • Joined: 2004/02/13 12:31:19
  • Location: Southern California
  • Status: offline
Re: What is the best method to develop multitasking code for projects ? 2019/11/21 13:46:47 (permalink) ☄ Helpfulby eagle1 2019/11/21 14:34:03
+3 (3)
8-bit chips aren't for bigger/more complex code.

 
What does "bigger/more complex" mean?  Is 25,000 lines of C "complex"?
#9
NKurzman
A Guy on the Net
  • Total Posts : 18043
  • Reward points : 0
  • Joined: 2008/01/16 19:33:48
  • Location: 0
  • Status: online
Re: What is the best method to develop multitasking code for projects ? 2019/11/21 14:24:43 (permalink) ☄ Helpfulby eagle1 2019/11/21 14:33:32
+2 (2)
It is Not about Bigger or more Complex.  But 8bit PICs have limited Resources compared to other CPUs.  The Issue in in the name RTOS.  Real Time.  what does Real time mean to your Project.  cam it meet its schedule?  without an RTOS your program must be made in to steps small enough that it does.  This is harder to write and time to insure the program does not fall behind or miss its targets.  An RTOS s
can simplify this.
A good Example is Harmony.  You have the Option of Round Robin, or RTOS.  However its drivers are written in such a way that they do not always finish within the allotted time.  They could be coded to conform, or an RTOS can be used to insure that critical tasks meet their schedules. 
#10
ric
Super Member
  • Total Posts : 24608
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: online
Re: What is the best method to develop multitasking code for projects ? 2019/11/21 14:27:10 (permalink) ☄ Helpfulby eagle1 2019/11/21 14:33:47
+4 (4)
Understanding how to implement a "state machine" in your code is essential to writing good cooperative multitasking code.
 

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#11
Mysil
Super Member
  • Total Posts : 3482
  • Reward points : 0
  • Joined: 2012/07/01 04:19:50
  • Location: Norway
  • Status: online
Re: What is the best method to develop multitasking code for projects ? 2019/11/21 14:32:24 (permalink) ☄ Helpfulby eagle1 2019/11/21 14:35:49
+2 (2)
Hi eagle1,
With small 8 bit microcontrollers,
the most common way, might well be: One Microcontroller for each Task.
Typical cost of a small PIC microcontroller may be around 1$.
 
When you have some task that need to be done fast, reliably and precisely,
then there is a lot of benefits in having a microcontroller dedicated to just that task, 
with nothing else that may interfere.
 
What about controlling a switching power supply, or a washing machine motor?
Also, many things not depending on high speed, you may want it to work 24/7,
You may want your alarm clock to keep time, even if there is a power failure. 
With careful design and coding, a small microcontroller may be made to keep alive for a long time,
even with a small battery.
Lightening strikes may still be a problem, although then there may be other problems also.
 
A RTOS cannot make tasks happen at the same time, the CPU can only perform one stream of instructions at any time. So it may only look like different tasks are serviced simultaneously.
In fact a RTOS will use time on it's own to switch between tasks.
And it may also cause variable delays before tasks get done.
 
There is a lot that may be done on a microcontroller without introducing a RTOS framework.
One of the common ways to perform tasks on a microcontroller, is to use interrupts.
On your PIC18F46K22 there are 2 levels of interrupt priority available, in addition to the main program loop.
This may give very precise and predictable timing for the high priority interrupt task.
But it may also be misused.
 
Microchip like to promote benefits of Core Independent Peripherals (CIP),
You may look up their descriptions on the website.
Your PIC18F46K22  have a lot of fancy peripherals,
including a lot of PWM stuff, that might be used for lighting control, or to control several motors or power inverters.
Although more than one  3-phase AC motor may be a stretch.
 
    Mysil
#12
jtemples
عُضْوٌ جَدِيد
  • Total Posts : 11428
  • Reward points : 0
  • Joined: 2004/02/13 12:31:19
  • Location: Southern California
  • Status: offline
Re: What is the best method to develop multitasking code for projects ? 2019/11/21 14:56:08 (permalink)
+3 (3)
the most common way, might well be: One Microcontroller for each Task

 
That is definitely not common in my experience.  There are almost no circumstances in which I would consider that a good idea.
#13
eagle1
Super Member
  • Total Posts : 396
  • Reward points : 0
  • Joined: 2014/11/02 03:04:06
  • Location: Saudi Arabia
  • Status: offline
Re: What is the best method to develop multitasking code for projects ? 2019/11/21 14:56:55 (permalink)
+1 (1)
Guys thank you so much for the answers I really enjoyed reading them because I'm really interested to learn about the details about this topic.
#14
eagle1
Super Member
  • Total Posts : 396
  • Reward points : 0
  • Joined: 2014/11/02 03:04:06
  • Location: Saudi Arabia
  • Status: offline
Re: What is the best method to develop multitasking code for projects ? 2019/11/21 15:59:29 (permalink)
0
ric
Understanding how to implement a "state machine" in your code is essential to writing good cooperative multitasking code.
 


 
How about this code I was working on, is it near the concept of "state machine"?

void lcd_msg_mgt(void){
    switch(lcd_state){
      case START_MSG:
        lcd.clear();
        lcd.print("RFID SYSTEM");
        lcd.setCursor(0, 1);
        lcd.print("Hi ..");
        lcd_state = REGISTER_MSG;
      break;

      case REGISTER_MSG:
        lcd.clear();
        lcd.print("REGISTER your");
        lcd.setCursor(0, 1);
        lcd.print("RFID CARD no: ");
        lcd.print(card_no+1);
        register_lock = 0;
      break;
      
      case CHECK_MSG:
        lcd.clear();
        lcd.print("ENTER your");
        lcd.setCursor(0, 1);
        lcd.print("RFID CARD:");
        check_lock = 1;
        sys_state = 0;
      break;
             
      case ACCEPTED_MSG:
        lcd.clear();
        lcd.print("CARD ACCEPTED");
        lcd_state = CHECK_MSG;
      break;
      
      case DENIED_MSG:
        lcd.clear();
        lcd.print("CARD DENIED");
        lcd_state = CHECK_MSG;
      break;

      case ALREADY_MSG:
        lcd.clear();
        lcd.print("CARD ALREADY");
        lcd.setCursor(0, 1);
        lcd.print("REGISTERED");
        lcd_state = REGISTER_MSG;
        register_lock = 0;
      break;
       
      case FINISH_MSG:
        lcd.clear();
        lcd.print("FINISHED");
        lcd.setCursor(0, 1);
        lcd.print("REGISTERATION");
        lcd_state = CHECK_MSG;
      break;
    }
}

#15
eagle1
Super Member
  • Total Posts : 396
  • Reward points : 0
  • Joined: 2014/11/02 03:04:06
  • Location: Saudi Arabia
  • Status: offline
Re: What is the best method to develop multitasking code for projects ? 2019/11/21 16:06:21 (permalink)
0
Mysil
There is a lot that may be done on a microcontroller without introducing a RTOS framework.
 

This arises an important question: When to use RTOS/multitask and when it's not required ?
 
Also as ric mentioned I should stop mentioning RTOS because it's much more wide developed coding framework than what I'm talking about in this post. So I would mean here multitask in its simple form, then Round Robin is the one I would mean.
#16
NKurzman
A Guy on the Net
  • Total Posts : 18043
  • Reward points : 0
  • Joined: 2008/01/16 19:33:48
  • Location: 0
  • Status: online
Re: What is the best method to develop multitasking code for projects ? 2019/11/21 16:39:22 (permalink) ☄ Helpfulby eagle1 2019/11/21 17:26:15
+2 (2)
When do you Use an RTOS?
1. When an analysis of the Project says it requires it.
2. When you determine that it will save time in coding the Project.
3. When you want to play with an RTOS to see how they are (and your CPU will support it)
4. When it is all you know and would not consider not having one.
 
Note Some Projects are a wash. They could be done with or without one.
Use one when you do not need it may require a Bigger CPU.
Don't use one when you should, harder to write code and time spent analysis worst case code timing.
 
 
#17
eagle1
Super Member
  • Total Posts : 396
  • Reward points : 0
  • Joined: 2014/11/02 03:04:06
  • Location: Saudi Arabia
  • Status: offline
Re: What is the best method to develop multitasking code for projects ? 2019/11/21 17:47:34 (permalink)
0
How about AN777 ? It is what I'm talking about I guess.
#18
NKurzman
A Guy on the Net
  • Total Posts : 18043
  • Reward points : 0
  • Joined: 2008/01/16 19:33:48
  • Location: 0
  • Status: online
Re: What is the best method to develop multitasking code for projects ? 2019/11/21 18:34:48 (permalink)
0
I do not see the PIC16 in their site:
http://www.pumpkininc.com/
 
And it does not appear to be free
 
Just becuse you can use an RTOS, does not mean you should.
#19
NKurzman
A Guy on the Net
  • Total Posts : 18043
  • Reward points : 0
  • Joined: 2008/01/16 19:33:48
  • Location: 0
  • Status: online
Re: What is the best method to develop multitasking code for projects ? 2019/11/21 18:37:29 (permalink)
0
Lite Mode is free.
#20
Page: 12345.. > >> Showing page 1 of 7
Jump to:
© 2019 APG vNext Commercial Version 4.5