الخميس، 8 نوفمبر 2012

Introduction To Microcontroller (2)

Introduction To Microcontroller (2)



سوف نتناول اليوم كيفية التعامل مع برنامج MikroC V4.15 و كيفية كتابة كود بلغة الـ C.

أولاً برنامج Mikro C:
يُمكنك تحميل البرنامج من هنا (( اضغط هنا ))
مرحلة ما بعد تسطيب البرنامج على جهازك، قم بفتح البرنامج ثم اتبع الخطوات الآتية:

شكل (1)


شكل (2)


شكل (3)


شكل (4)


شكل (5)


------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------





و الآن سوف نتعلم كيفية كتابة الكود بلغة الـ C.


ترتيب الكود في أي برنامج يتم كتابته




1- Preprocessor data:
و هي عبارة عن text replacement لبعض الكلمات بالقيم المرتبطة بها، و هيه تتم قبل عمل compilation للبرنامج، مثال
#define on 1
#define off  0
#define led trisb.b0

هنا يقوم الـ compiler باستبدال كل كلمة "on" بـ قيمة "1" ، و هذا يجعل الكود أكثر قراءة و يجعلني أكثر قدرة على قراءة و تعديل الكود ex: Led = on بدلاً من أن أقول Led = 1 فالأولى تجعل الكود أكثر قراءة و أسرع في التعديل.

2- Variable declaration:
ملحوظة:
سوف أقوم بكتابة بعض التعليقات بجوار الـ كود و ذلك لجعل الكود أكثر وضوحاً، و سوف يسبق التعليق // .
و هنا نقوم بتعريف المتغيرات التي سوف أقوم باستخدامها في البرنامج :

ex: int x = 5 ;       // declare a variable ((x)) As Integer  and assign 5 to its value.

ex: float z = 2.35;    // declare a variable ((z)) As float and assign 2.35 to its value.

ex: char y = ‘A’    // declare a variable ((y)) As Character and assign A to its value.

ex: char name [ ] = “SC.CSED”    // declare a Array ((name)) As Array of characters (like string) and assign SC.CSED to its value.

note :
a-  every line in the C code must be closed by “ ;
b- in the C language there is no type to store string directly >> so we use array of character to store strings.

3- User defined functions :
الـ function هيه عبارة عن برنامج فرعي يتم كتابته ليقوم بأداء وظيفة معينة، لانشاء function نقوم بثلاث مراحل :
أولا: مرحلة الـ prototype :
و تتم هذه المرحلة قبل كتابة الـ main.
<return_type> <function_name> <(variables that the function receive)> ;

ex :
int Add ( int x, int y ) ;
ثانياً مرحلة الـ implementation:
و هنا يتم كتابة الكود الذي تقوم به هذه الـ function.
<return_type> <function_name> <(variables that the function receive)>
ex:

int Add (int x, int y)
{
int z ;
z = x + y ;
return (z) ;
}

ملحوظة:
الفرق بين الـ prototype و الـ implementation في أول سطر هوه الـ ;

ثالثاً مرحلة الـ calling :
و هي مرحلة استدعاء الـ function لتقوم بالوظيفة المحددة.
<Variable_name> = <function_name><( the variable that i want to send to the implementation)>
ex:

int Add (int x, int y) ; // prototype, function_name is “Add”, receive two variable, and return integer variable.

void main ()
{
int num1, num2, result ;
num1 = 5 ;
num2 = 3 ;
result = Add (num1, num2) ; // calling the function, and send the two variables that i want to add.
}

int Add ( int x, int y)  // implementation of the function
{
int z ;  // define a variable z As integer.
z = x + y ;  
return (z) ;  // return the value of z and put it in result variable.
}

4- Void main :
و هنا يتم كتابة الـ كود الرئيسي في البرنامج، و استدعاء كافة الـ functions ، التي تم كتابتها ، و يتم استخدام الـ preprocessor data، و يتم كتابة الـ configuration  اللازمة لظبط الهاردوير و السوفت وير ليعملا سوياً.
و الآن سوف نستعرض سوياً:
a- Conditional statement :

if ( condition 1 )
{
   // write the code that you desire to execute if the condition 1 is true.
}
else if ( condition 2 )
{
   // write the code that you desire to execute if the condition 2 is true.
}
else
{
  // write the code that you desire to execute if the condition is false.
}

// to And between the condition 1 and 2,, write:

if ( condition 1 && condition 2 )
{
   // write the code that you desire to execute if the condition 1 and condition 2 is true.
}
if ( condition 1 | | condition 2 )
{
   // write the code that you desire to execute if the condition 1 or condition 2 is true.
}


b- loops:
I need to loop when i want to repeat some of code for n times.

1- For Loop:

For ( initial value ; condition ; step )
{
// write the code that you want to repeat.
}

ex: to print numbers form 0 to 50

void main ()
int i ;  // define variable i As integer.

For ( i = 0 ; i <= 50 ; i ++ )  // loop from i = 0 to i less than 50 ; every time increment the value of i.
{
// print the value of i >> it will be 1 2 3 4  . … … 48 49 50
}




c- Switch case :
switch ( variable )
{

case 1 :
// write that code you want to execute if the variable matching with the first case
break ;

case 2 :
// write that code you want to execute if the variable matching with the first case
break ;


default  :
// write that code you want to execute if the variable doesn’t  match with any of the cases.
break ;

}

ليست هناك تعليقات:

إرسال تعليق