LECTURE 2 - Introduction To C Programming
LECTURE 2 - Introduction To C Programming
INTRODUCTION TO C PROGRAMMING
4
PROGRAMMING LANGUAGES
Assembly Language
(a x b = a + a + a + … + a)
Since there is no appropriate instruction for
multiplying two numbers
Major Reasons for Writing Program
in C instead of Assembly
10
Program Structure
#include <xc.h>
#pragma config FOSC = HS
#pragma config WDTE = OFF
#pragma config PWRTE = ON
#pragma config BOREN = OFF
#pragma config LVP = OFF
#define _XTAL_FREQ 20000000
Structure of every
embedded-control
program written in C.
There will always be
two main parts:
ØUnsigned int –16-bit data type that takes a value in the range of 0-65535.
Used to define 16-bit variables such as memory addresses.
Also used to set counter values more that 256.
Remember…C compiler used signed int as the default.
ØSigned int – 16 bit (MSB D15 of D15—D0 used to represent the – or + value.
Range values from -32 768 to +32767.)
signed/unsigned int/char
Integer (int)
¨ the default type is an unsigned 8-bit number, giving a range
of values of 0 – 255
¨ Range of a number is determined by the number of different
binary codes that can be represented
Signed Integers
¨ uses the most significant bit (MSB) as the sign bit, so
the range is accordingly reduced by half
¨ MSB 0 represents a positive number, MSB 1
indicates a negative number
¨ Therefore, the range for a 16-bit signed integer is –
32767 to + 32767
¨ The sign bit must be processed separately to get
the right answer from a calculation
15
I/O PROGRAMMING IN C
ØWe use the PORTA-PORTD labels as defined in C program header file
ØTRISB indicate all bit in TRISB & TRISB7 indicates B7 of the TRISB
PORT also the same
PIC16 C Data Operations
n Variable types
n Floating point numbers
n Characters
n Assignment operators
18
Assignment Operations
¨ A range of arithmetic and logic operations are
needed where single or paired operands are
processed
¨ The result is assigned to one of the operand
variables or a third variable
¨ Assignment operator is the equal sign “=” and is
used to give a variable the value of an expression.
i=0;
x=35;
sum=a+b;
19
ASSIGNMENT OPERATORS
Simple operators assign values to variables using the
common ‘=’ character. For example: a = 8
Compound assignments are specific to C language and
consist of two characters as shown in the table. An
expression can be written in a different way as well, but
this one provides more efficient machine code.
20
BITWISE OPERATORS in C
Unlike logic operations being performed to variables, the
bitwise operations are performed to single bits within
operands.
Bitwise operators are used to modify the bits of a variable.
Arithmetic & Logical Operations
22
Arithmetic & Logical Operations
Conditional Operations
¨ Where a logical condition is tested in a while, if, or
for statement, relational operators are used
¨ One variable is compared with a set value or
another variable, and the block is executed if the
condition is true
Examples :
AND condition:
if((a > b)& & ( c = d ))
OR condition:
if((a > b)||(c = d ))
Q5
Examples
Assuming a = 10
( a > 1)
( -a >= 0)
( a == 17)
( a != 3)
26
Integer Constants
ØInteger constants can be decimal, hexadecimal, octal or binary.
ØThe compiler recognizes their format on the basis of the prefix added.
If the number has no prefix, it is considered decimal by default.
31
Data Conversion Program in C
Packed BCD to ASCII Conversion
32
PIC16 C Program Basics
nVariables
nLooping
nDecisions
33
Variables
-must begin with a character or underscore
x is variable
data type
int x;
x= 99;
PORTD = x;
Note: i. the case of alphabetic characters is significant. Using “INDEX”, “index” & “InDex”
….all three refer to different variables-case sensitive
ii. Header files contain definitions of function & variables
iii. Keywords are reserved identifiers – if, else, int, char, while, …
iv. Contents of a variable can change
v. Global and local variable
35
Looping
¨ to execute continuously until the processor is turned
off or reset
¨ the program generally jumps back at the end to
repeat the main control loop
¨ implemented as a “ while ” loop while loop
int x;
while(1){
PORTD = x;
x++;
} x++; is equivalent to x=x+1;
x--; is equivalent to x=x-1;
int x;
PORTD=0x00;
while(1)
{
x=RC0;
if(x==1) RD0=1;
}
If statement for the decision making
¨ While loops
¨ Break, continue, goto
38
While Loops
¨ while(condition); : provides a logical test at the
start of a loop, and the statement block is executed
only if the condition is true
¨ do
program statement;
while(condition); : the loop block be executed at
least once, particularly if the test condition is
affected within the loop
While Loops
40
While Loops
The WHILE test occurs before the block and the DO WHILE after
main( )
{
int count;
count = 0;
while (count < 6) {
printf("The value of count is %d\n",count);
count = count + 1;
}
}
main( )
{
int i;
i = 0;
do {
printf("the value of i is now %d\n",i);
i = i + 1;
} while (i < 5);
}
Break, Continue and Goto
¨ break the execution of a loop or
block in the middle of its sequence
¨ The block must be exited in an
orderly way, and it is useful to have
the option of restarting the block
(continue) or proceeding to the next
one (break).
¨ It is achieved by assigning a label
to the jump destination and
executing a goto..label
42
If…Else
44
If..Else and Switch..Case
If..Else
if (expression) If the expression is TRUE,
statement1; statement1 is executed; statement2 is skipped
else If the expression is FALSE,
statement2; statement2 is executed; statement1 is skipped
Switch..Case
switch (integer expression) The keyword break should be included
case constant1: at the end of each case statement. In
statement1; the switch statement, it causes an exit
break; from switch shunt.
case constant2:
statement2;
break;
EXAMPLE
Writing header, configuring I/O pins, using delay function and switch operator
46
Array
Hold multiple values of the same data type
¨ List of variables that are all of the same type and can
be referenced through the same name
¨ The individual variable in the array is called an array
element
¨ Used to handle groups of related data
¨ Declaration for one-dimensional array
type var_name [size]
¤ type – valid C data type (char, int, long)
¤ var-name – the array name
¤ size – specifies how many elements are in the array
¤ Example: int height[50];
n The first element to be at an index of 0 (height[0])
n The last element to be at an index size-1(height[49])
Array
¨ Can also being declare as multidimensional array
¤ Unsigned int height[4][5] – 2 dimensional array 4x5 (4
rows, 5 columns)
¨ Assigning initial value height 23 45 67
¤ Int height[3] = { 23,45,67};
height[0] height[1] height[2]
¤ Int height[] = {23,45,67}; memory
¤ Char str[3] = {‘a’,’b’,’c’};
ii. c[1] = 123; // assign the value 123 to the 2nd element of c
iii. i[2] =12345; // assign the value 12345 to the 3rd element of i
iv. k[2] = 123* i[4]; // compute 123 x the value of the 5th element of i
Pointer
Ø A memory location (variable) that holds the address of
another memory location
Ø Declaration
type *var-name;
¤ Type – valid C data types
¤ Var-name – name of the pointer
¤ * - indicates that the variable is a pointer variable
¤ Example
void main(void)
() means value of
{
int *a;
int height[4] = {1,2,3,4};
a = &height;// assigned a to the first address of array à (a) = height[0]
a++; // set the pointer a to next array address à (a) = height[1]
printf(“%d”,*a); // print the array value as pointed by pointer a à result is 2
}
Ø Suitable for accessing look-up table
50
Memory Addressing
ØPointers are powerful features of C and (C++) programming that differentiates it
from other popular programming languages like: Java and Python.
ØPointers are used in C program to access the memory and manipulate the address.
Memory Addressing
Working Stores the value of the variable of homogeneous Store the address of the another variable
data type. of same datatype as the pointer
variable's datatype.
Storage A normal array stores values of variable and Pointers are specially designed to store
pointer array stores the address of variables. the address of variables.
Capacity An array can store the number of elements, A pointer variable can store the address
mentioned in the size of array variable. of only one variable at a time.
END OF CHAPTER
¨ “Without requirements or design, programming is
the art of adding bugs to an empty text file.” -
Louis Srygley