Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Mulltiple sensors with pic microcontroller

Status
Not open for further replies.

meche

Junior Member level 2
Joined
Apr 12, 2017
Messages
24
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
252
Please I need help!

I am trying to build a project that will interface multiple sensors(about 5 sensors in total:temperature, humidity, pressure etc) with pic micro controller using a simple time triggered real time operating system (based on probably, timer2 overflow interrupt) to read each sensors in turn at specified time interval.

How do I call multiple tasks from this operating system?
 

You didn't mention which PIC or which language but the basic principle is:

Timer tick interrupt
ISR increments a task counter
if counter is at maximum, reset it
do the numbered task

Depending on timing, you can either read the sensors inside the ISR or better still, set a flag in the ISR to alert your main program that the task counter has changed, then reset it and do the task in the main part of the program. This is the preferred method as it gets you out of the ISR as quickly as possible. Think of each task as being a function (or subroutine) that does it's job then returns with the sensor reading.

Brian.
 

Sorry about that. The pic' s I have in mind is pic16f877a or pic18f26k20 and the language is C, with MPLAB XC8 compiler. Please can you give me a sample code to start with especially with regards to " do the numbered task" in your listed principle for the operating system, and setting of flag in the ISR. Or better still just a generalized C code for multiple task calling in a time triggered embedded operating system. I appreciate your response. Thanks

- - - Updated - - -

Sorry about that. The pic' s I have in mind is pic16f877a or pic18f26k20 and the language is C, with MPLAB XC8 compiler. Please can you give me a sample code to start with especially with regards to " do the numbered task" in your listed principle for the operating system, and setting of flag in the ISR. Or better still a generalized C code for multiple task calling in a time triggered embedded operating system.

From you listed principles for the operating system:

1. Timer tick interrupt - I understand this
2. ISR increments a task counter - If I have 5 tasks, i can declare an integer variable and give it the value 5 and increment this variable at every tick
3. If task counter is at maximum, reset it - If the counter is equal to 5 after each tick, the variable starts counting again from 0
4. Do the numbered task? - I don't have an idea about this

This is my own little understanding of your explanation.
 

1. Timer tick interrupt - I understand this
2. ISR increments a task counter - If I have 5 tasks, i can declare an integer variable and give it the value 5 and increment this variable at every tick
3. If task counter is at maximum, reset it - If the counter is equal to 5 after each tick, the variable starts counting again from 0
4. Do the numbered task? - I don't have an idea about this

Sorry, I'm not at a computer with XC8 installed at the moment so I can't write code but in essence it is like this:

ISR code:
Code:
clear the timer interrupt bit
increment the task counter
if the task number is 5 reset it to 0
reload the timer value if necessary
task_flag = 1;
end of ISR
then your main program loop includes:
Code:
if task_flag != 1 skip the rest of this code
task_flag = 0;  // ready for next time
switch (task number)
{
case 0: {task0(); break;}
case 1: {task1(); break;}
case 2; {task2(); break;}
case 3; {task3(); break;}
case 4; task4();
}

The ISR maintains a count looping from 0 to 4 and sets the 'task_flag' (char or bit variable) when the task number changes.
The main program loop checks to see if the task_flag has been set, meaning it's time to perform a task. If it was set, it gets reset again in readiness for the next use and the appropriately numbered task is called.

Brian.
 

Thanks Brian. your code has given me the right insight. I think I should be able to go forward from here.
 

caution: in the example I gave you should declare the variable used for the task number as 'volatile' as it may be changed in the ISR and maybe in the main code as well. Declaring it volatile "volatile char task_num = 0;" for example will make sure the compiler double checks the value before using it otherwise there is a risk of the update in the ISR being ignored.

Brian.
 

thanks for that. I will take note
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top