LECTURE 2 - Introduction To C Programming
LECTURE 2 - Introduction To C Programming
Introduction to C Programming
PIC PROGRAMMING IN C
Microcontroller
Programming
Languages
PROGRAMMING LANGUAGES
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
Main Idea
To break a bigger problem down into several smaller pieces
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 Number Format
Integer number can be written either in decimal, hexadecimal, octal or binary
form.
FORMAT PREFIX
Decimal
Hexadecimal 0x or 0X
Binary 0b or 0B
Assignment Operations
i. PORTB = a<<3;
ii. Z = b>>4;
iii. PORTD = a & b;
iv. PORTC = b & 0xF0;
Arithmetic & Logical Operations
Arithmetic & Logical Operations
Exercises
1. Find the content of PORTB after the
following C code in each case:
a. PORTB=0x37 & 0xCA;
b. PORTB=0x37 | 0xCA;
c. PORTB=0x37 ^ 0xCA;
AND condition:
if((a > b) && ( c == d ))
OR condition:
if((a > b)||(c == d ))
Exercises
• Assuming a = 10, define result either
TRUE or FALSE
( a > 1)
( -a >= 0)
( a == 17)
( a != 3)
Constant’s Number
Integer constants can either be in decimal, hexadecimal, octal or binary form.
The compiler recognizes the constant number by a keyword const.
Number declaration is similar as assigning value to variable.
The basic set of 7- bit characters includes the upper and lower case
letters and the numerals and punctuation marks found on the
standard computer keyboard
• Variables
• Looping
• Decisions
A variable name (identifier) is a label attached to the memory location where the variable value is
stored.
The variable name and type must be declared at the start of the program block
Only alphanumeric characters (a–z, A–Z, 0–9) and underscore, instead of space, can be used.
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
Program 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(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
void main( )
{
int count;
count = 0;
while (count < 6)
{
printf("The value of count is %d\n",count);
count = count + 1;
}
}
void main( )
{
no
int i;
yes i = 0;
do {
yes
printf("the value of i is now %d\n",i);
no i = i + 1;
} while (i < 5);
}
For loop
• Useful for counter-controlled loop
• for(initialization;test
condition;update)
• Initialize at the start of a loop,
perform a logical test, execute
the statement block if the
condition is true and then update
expression.
• Process repeated until logical test
produced false result
void main( )
{
int count;
for (count=0;count < 6;count++)
{
printf(“count = %d\n",count);
}
}
42
Break,
Continue and
Goto
To 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
no
• multichoice selection - yes
which is provided by the
switch..case syntax no
yes
• switch..case : tests a
variable value and no
provides a set of
yes
alternative sequences,
one of which is selected no
depending on the test
result
44
If..else vs 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
PIC16: Array
and Pointer
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];
The first element to be at an index of 0 (height[0])
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)
memory
• Assigning initial value height[0] height[1] height[2]
48
Array
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
BASIS FOR
ARRAY POINTER
COMPARISON