variables in microC
variables in microC
Types of Microcontrollers
الشركات المصنعة لي
الحاكمات الدقيقة
RENESAS
INTEL
ATMEL Microchip
ﺗﻧﺗﺞ 8051
1
13/02/2024
2
13/02/2024
3
13/02/2024
Computers Vs Microcontrollers
• Computer Microcontroller
• 1)CPU 1) Micro processor
• 2) Hard disc 2) flash memory
• 3) RAM 3) RAM
• 4) ROM 4) ROM
• 5) Sound card 5) ADC interface
• 6) video card 6) serial interface
Microprocessor
4
13/02/2024
لغــــات البرمجة
Programming
Languages
C, C++,
لغة اﻻسمبلي
PASCAL,
Assembly
BASIC
5
13/02/2024
Variables in MikroC
OUTLINE
1.Variables
2.Variable Types
3.Variables in MikroC
4.Constants Declaration
6
13/02/2024
1. Variables
• A variable is any number that changes its
value during program operation.
2. Variable Types
TYPE SIZE IN BYTES RANGE
Bit 1 –bit 0 or 1
Sbit 1 –bit 0 or 1
(unsigned) char 1 byte 0-255
Signed char 1 byte -128 127
(signed) short (int) 1 byte -128-127
Unsigned short (int) 1 byte 0-255
(Signed) int 2 bytes -32768 – 32767
Unsigned (int) 2 bytes 0 – 65535
Unsigned long int 4 bytes 0-4294967295
Float 4 bytes -1.5 * 𝟏𝟎𝟒𝟓 – 3.4 *𝟏𝟎𝟑𝟖
7
13/02/2024
MikroC Keywords
8
13/02/2024
• Examples:
– Temp_degree
– Totalvolt
– Sum_2
– _counter
– Check the following variable names
– Total Volt
– 2_sum
– 4degree
– Static
– Switch
– Up.counter
Note:
Case Sensitivity:
MikroC variables are not case sensitive
Total = Total =TOTAL= totaL
9
13/02/2024
Wrong declarations
• Unsigned int x = -8; // sign
• Unsigned int 5degree= 30; // name
• Unsigned char temp_degree =C; // ‘C’
• Unsigned char volt= 300; // not in range
• Float volt=10; // 10 must be 10.0
• Unsigned int switch =10; // mikroC keyword
• Bit Off =0; // value in the same line
• Unsigned bit x; x=1; // unsigned
• bit x; x=2; // not in range
• Sbit leds at portC; // only C0 Can be used.
10
13/02/2024
11
13/02/2024
12
13/02/2024
Operators
العمليــــــــــــــة Operator
الجمع +
الطرح -
الضرب *
القسمة /
(باقي عملية القسمة )قسمة العدد الصحيح %
1 زيادة ذاﺗية لقيمة المﺗغير ب ++
1 ﻧقصان ذاﺗي لقيمة المﺗغير ب --
Arithmetic Operators
7/4; /* equals 1*/
7*3/4; // equals 5
7. * 3./4.; // equals 5.25, because we're working with
floats
9%3; // equals 0
7%3; // equals 1 (remainder )الباقي
/*Post-Increment operator*/
J=4;
k=j++; // k=4, j=5
/*pre-increment operator */
j=4;
k=++j; // k=5, j=5
13
13/02/2024
Bitwise Operators
العملية Operator
Bitwise AND &
Bitwise OR |
Bitwise EXOR ^
Bitwise complement ~
shift left ازاحة لليسار <<
shift right إزاحة لليمين >>
14
13/02/2024
15
13/02/2024
Bitwise OR operator
• The result of the bitwise OR operation is 1 if
at least one of the expression has the value
as 1;
16
13/02/2024
Bitwise XOR
• The result of the bitwise Exclusive-or (XOR)
operation is 1 if only one of the expression
has the value as 1; otherwise the result is 0
17
13/02/2024
18
13/02/2024
• Example:
x=255;
y=~x;
Answer: x=11111111,,,,, Y=0
Example:
x=4;
y=x<<2 (number of shifts=2)
x= 00000100
y=00010000 (y=16)
one shift left as you multiply by 2
19
13/02/2024
Relational Operators
العمليــــــــــــــة Operator
هل ﺗساوي ==
ﻻ يساوي !=
اكبر من >
أصغر من <
اكبر من او يساوي >=
اقل او يساوي <=
Logic Operators
العملية Operator
AND &&
OR ||
Example:
unsigned char x=7, y=27, q;
q=x&&y;
q will equal to ……
20
13/02/2024
Finite Loop
for (Initialization; condition; increment){
………………………………………………….}
Example:
Unsigned char I;
For (i=0; i<10; i++)
{………………………..}
21
13/02/2024
Void main() {
trisd.b7=0;
portd.b7=1;
while (1){
portd.b7= ~portd.b7;
delay_ms(1000); } }
22
13/02/2024
23