 |
:: استاذ و مشرف قسم الالكترونيات ::
تاريخ التسجيل: May 2007
المشاركات: 6,832
|
|
نشاط [ F.Abdelaziz ]
قوة السمعة:
|
|
09-06-2015, 01:20 AM
المشاركة 15
|
|
البرنامج :
البرنامج :
كود:
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
// SIMPLE RTOS EXAMPLE - RANDOM NUMBER GENERATOR
// This is a simple RTOS example. 8 LEDs are connected to PORTB
// of a PIC18F452 microcontroller. Also, a push-button switch is
// connected to port RC0 of PORTC, and an LED is connected to port
// RC7 of the microcontroller. The push-button switch is normally at logic 1.
// The program consists of 3 tasks called "Generator", "Display", and "Live".
// Task "Generator" runs in a loop and increments a counter from 0 to 255.
// This task also checks the state of the push-button switch. When the
// push-button switch is pressed, the task sends the value of the count to the
// "Display" task using messaging passing mechanism. The “Display” task
// receives the value of count and displays it on the PORTB LEDs.
// Task "Live" flashes the LED connected to port RC7 at a rate of 250ms.
// This task is used to indicate that the system is working and is ready for
// the user to press the push-button.
// The microcontroller is operated from a 4MHz crystal
//////////////////////////////////////////////////////////////////////////////////
#include <18F452.h>
#use delay (clock=4000000)
int count;
///////////////////////////////////////////////
// Define which timer to use and minor_cycle for RTOS
#use rtos(timer=0, minor_cycle=1ms)
/////////////////////////////////////////////////////
// Declare TASK "Live" - called every 200ms
// This task flashes the LED on port RC7
#task(rate=200ms, max=1ms)
void Live()
{
output_toggle(PIN_D7);
}
////////////////////////////////////////////////////////
// Declare TASK "Display" - called every 10ms
#task(rate=10ms, max=1ms, queue=1)
void Display()
{
if(rtos_msg_poll() > 0) // Is there a message ?
{
output_b(rtos_msg_read()); // Send to PORTB
}
}
///////////////////////////////////////////////////////////////////
// Declare TASK "Generator" - called every millisecond
#task(rate=1ms, max=1ms)
void Generator()
{
count++; // Increment count
if(input(PIN_D0) == 0) // Switch pressed ?
{
rtos_msg_send(Display,count); // send a message
}
}
//////////////////////////////////////////////////
// Start of MAIN program
//
void main()
{
set_tris_b(0); // Configure PORTB as outputs
set_tris_d(1); // RD0=input, RD7=output
rtos_run(); // Start RTOS
}
////////////////////////////////////////////////////////////
البرنامج الرئيسى :
يأتى فى نهاية البرنامج ، وفيه يتم أعاداد أطراف المنفذ PORTB كمخارج . أيضا إعداد الطرف RD0 كمدخل وباقى أطراف المنفذ PORTD مخارج .
كود:
//////////////////////////////////////////////////
// Start of MAIN program
//
void main()
{
set_tris_b(0); // Configure PORTB as outputs
set_tris_d(1); // RD0=input, RD7=output
rtos_run(); // Start RTOS
}
///////////////////////////////////////////////////////////
رأس (مقدمة) البرنامج :
يتم استخدام المؤقت Timer 0 كمؤقت لنظام RTOS ، ويتم تحديد الزمن minor_cycle بواحد ثانية :
كود:
///////////////////////////////////////////////
// Define which timer to use and minor_cycle for RTOS
#use rtos(timer=0, minor_cycle=1ms)
/////////////////////////////////////////////////////
يتكون البرنامج من 3 مهام :
• المهمة الأولى Live ويتم تشغيلها كل 200 ملى ثانية ، وتقوم بوميض الليد المتصل بالطرف RD0 . هذه الليد تشير إلى أن النظام يعمل :
كود:
/////////////////////////////////////////////////////
// Declare TASK "Live" - called every 200ms
// This task flashes the LED on port RC7
#task(rate=200ms, max=1ms)
void Live()
{
output_toggle(PIN_D7);
}
////////////////////////////////////////////////////////
• المهمة الثانية Generator ويتم تشغيلها كل ملى ثانية وتقوم بتزايد متغير بايت يسمى count بصفة مستمرة . عندما يتم الضغط على الزر الضاغط فإن الطرف RD0 يذهب للحالة المنخفضة . عند حدوث ذلك فإن قيمة العداد الحالية يتم إرسالها إلى المهمة الثالثة Display باستخدام دالة نظام RTOS المسماه rtos_msg_send(display, count) ، حيث display هو اسم المهمة التى يتم إرسال الرسالة إليها ، و count هو البايت المرسل .
كود:
///////////////////////////////////////////////////////////////////
// Declare TASK "Generator" - called every millisecond
#task(rate=1ms, max=1ms)
void Generator()
{
count++; // Increment count
if(input(PIN_D0) == 0) // Switch pressed ?
{
rtos_msg_send(Display,count); // send a message
}
}
//////////////////////////////////////////////////
• المهمة الثالثة Display : ويتم تشغيلها كل 10 ملى ثانية . هذه المهمة تقوم باختبار ما إذا كان هناك رسالة فى قائمة انتظار الرسائل . إذا كان هناك رسالة ، يتم استخراج الرسالة باستخدام دالة نظام RTOS المسماة call rtos_msg_read() ، ويتم أرسال البايت الذى تم قراءته إلى الليدات المتصلة بالمنفذ PORTB :
كود:
////////////////////////////////////////////////////////
// Declare TASK "Display" - called every 10ms
#task(rate=10ms, max=1ms, queue=1)
void Display()
{
if(rtos_msg_poll() > 0) // Is there a message ?
{
output_b(rtos_msg_read()); // Send to PORTB
}
}
///////////////////////////////////////////////////////////////////
وهكذا ، فإن الليدات تعرض القيمة الثنائية للعداد كلما تم الضغط على الزر الضاغط . قائمة انتظار الرسائل يجب التحقق منها بواسطة استخدام الدالة rtos_msg_poll() ، لأن محاولة قراءة قائمة الانتظار بدون وجود أى بايتات فى القائمة قد يجمد البرنامج .
النتيجة :

|