0% found this document useful (0 votes)
4 views

variables in microC

Uploaded by

jacobashor
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

variables in microC

Uploaded by

jacobashor
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

‫‪13/02/2024‬‬

‫‪Types of Microcontrollers‬‬
‫الشركات المصنعة لي‬
‫الحاكمات الدقيقة‬

‫‪RENESAS‬‬
‫‪INTEL‬‬

‫‪ATMEL‬‬ ‫‪Microchip‬‬

‫ﺗﻧﺗﺞ ‪8051‬‬

‫‪AVR,‬‬ ‫ﺗﻧﺗﺞ ال ‪ PIC‬وهاذه الﺗي سوف يﺗم‬


‫‪Atmega‬‬ ‫دراسﺗها‬

‫‪1‬‬
13/02/2024

STK500 AVR Atmel

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

Low level High level


language language

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.

• Declaration formula for variables:


– Type name = value;
– Example:
– Int y = 9;
– Float degree = 37.5;

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

Variable names conditions


1. Variables can be combined from characters
A-Z, capital or small, digits 0-9 and the
underscore symbol _
2. Variables must not start with digit, only with
character or “_”
3. No spaces between the combined variable
name
4. mikroC keywards cannot be used (if, while…)

MikroC Keywords

Note: keywords are case sensitive and they


must be written in lower case

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

Declaration of Single variable


1. type name = value;
2. type name; // initial value =0
Name = value;
Example for single variable:
unsigned int x = 5; // range 2 byte
unsigned int x;
x =10;
bit type variable: bit on;
on =1; // must be declared in the second line
Sbit led at portd.b0; //we must deal with 1 bit

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

• Declaration of more than one variable:


• Type name1=value1; int x =10;
• Type name 2=Value2; float y = 10.0;
• Type name1, name2, name3,…….
• Name1=value1; name2=value2
– Int x, y, z;
– Int x = 100, y= 50, z=9;

• Examples for more than one variable:


• Unsigned char temp=20;
• Float voltage= 200.0;
• Unsigned char temp, voltage;
• Temp= ‘C’
• Signed short c =20, v=-120;
• Bit On, Off; On=1; Off=0;

11
13/02/2024

• Constant, unlike variables, is a number or a


character having a fixed value that cannot be
changed during program execution.
• Stored in the flash program memory
• Declaration formula for constants:
• Const x =10;
• Cosnt y = -100;
• Const x[3] =(2, 1, 3.4}

Constants represent fixed values (numeric or


character) in programs that cannot be
changed. Constants are stored in the flash
memory of the pic microcontroller, thus not
wasting valuable and limited RAM memory
Const Max =100;
Const Min=0b11110000;
Const Total =0xff;
Const CNT=017; // this is an octal number
Const Temp=37.50; // float variable
Const first_Alpha ='A'; // first_Alpha is a
character.

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 ‫إزاحة لليمين‬ >>

Bitwise AND operator


• The result of the bitwise AND operation is 1 if
both bits have the value as 1;

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

Bitwise Complement Operator


• The bitwise complement is also called as
one’s complement operator since it always
takes only one value or an operand.

18
13/02/2024

Bitwise Shift Operators


• The bitwise shift operators are used to move
or shift the bit patterns either to the left or
right side. Left and right are two shift
operators provided by ‘C’ which are
represented as follows.

• 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++)
{………………………..}

Body of the program


#define PI 3.14 ‫ﺗعريف الثوابت‬
Void main() ‫الدالة الرئيسية‬
{
Variable declaration ‫اﻻعﻼن عن المﺗغيرات واﻧواعها‬
Data direction ‫اﺗجاه الداﺗا هل دخل ام خرج‬
Initial values ‫قيمة ابﺗدائية للمﺗغيرات‬
While(1) ‫وضع حلقة ﻻﻧهائية‬
{ ………………………….;
}}

21
13/02/2024

Void main() {
trisd.b7=0;
portd.b7=1;
while (1){
portd.b7= ~portd.b7;
delay_ms(1000); } }

Sbit LED at portd.b7;


Const Delay =1000;
Void main(){
LED=0;
Trisd.b7 =0;
While (1){
LED=~LED;
Delay_ms(Delay);

22
13/02/2024

#define LED portb.f1


#define d1 delay_ms(1000)
#define d2 delay_ms(2000)
#define on 1
#define off 0
Void main ()
{
for (;;) { LED =on; d1; LED=off; d2;}
}

23

You might also like