Transcript Interrupts

INTRODUCTION TO PIC PROGRAMMING
By :
SHERIF HARHASH
CONTENTS
1- what are micro controllers ?
 2-how to use them ?
 3- MC main criteria (I/O ports , timers ,
interrupts).
 4- C-programming ( Mikro C pro for PIC).
 5- Examples .
 6- Questions .

WHAT ARE MICRO CONTROLLERS


?
Micro-controllers are the most generic device
used in many applications .
Simply its an IC that can perform any wanted
function-( according to its features) -.
WHAT ARE MICRO CONTROLLERS

?
A microcontroller has a processor and many
peripherals integrated with it on the same chip,
like a flash memory, RAM, I/O ports, serial
communication ports, ADC …Etc.
WHAT ARE MICRO CONTROLLERS



?
A timer module to allow the MCU to perform
tasks for certain time periods.
A serial I/O port to allow data to flow between
the MCU and other devices such as a PC or
another MCU.
An ADC to allow the MCU to accept analog
inputs for processing.
PIC MICRO-CONTROLLERS
OSCILLATORS
Crystal oscillator:
 A crystal or ceramic resonator is connected to the
OSC1 and OSC2 pins to establish oscillation.
 Used for high precession timing requirements.
 The capacitors are chosen according to the
frequency and the preferred values in the
datasheet of the used device.

OSILLATOR SELECT

T0CS_bit: Clock Source Select bit
1 = Transition on T0CKI pin
 0 = Internal instruction cycle clock (CLKO)


T0SE_bit: Source Edge Select bit
1 = Increment on high-to-low transition on T0CKI pin
 0 = Increment on low-to-high transition on T0CKI pin

C PROGRAMMINGLANGUAGE
OPERATORS















+ addition
-subtraction
* multiplication
/ division
% modulus
a*=b is the same as a=a*b
a/=b a=a/b
a+=b a=a+b
a-=b a=a-b
a%=b a=a%b
a<<=b a=a<<b
a>>=b a=a>>b
a&=b a=a&b
a|=b a=a|b
a^=b a=a^b
OPERATORS












Relational operators:
>,<,<=,>=,==,!=
Logical operators:
&&,||,!
Bitwise operators:
&, |, ^ (XOR), <<,>>,~
Precedence:
1.Casting
2.Parentheses
3.Negative
4.Multiplication and division
5.Addition and subtraction
CONDITIONALSTATEMENTS


















If statement:
if (expression)
{
statement(s);
}
If-else statement:
if (expression1)
{
statement(s)
}
else if(expression2)
{
statement(s)
}
else
{
statement(s)
}
FOR LOOP
for( initialization ; conditional_test; increment )
 Example :
 void main(void)
{
 Int i;
 for(i=0; i<10; i++)
 printf(“%d “,i);
 printf(“done”);
}

ARRAYS
type array_name[size] = {value list};
 Ex.
 inti[5] = {1,2,3,4,5};

Multidimensional arrays:
 intnum[3][3]={ 1,2,3,

4,5,6,
 7,8,9};FUNCTIONS

FUNCTIONS












main() is the first function called when the program is
executed. The other functions, function1() and function2(),
can be called by any function in the program.
main()
{
Function1
}
function1(inta,intb )
{
Return()
}
function2()
{
}
BREAK
10 MINS. ONLY
BREAK

I/O PORTS
Reading a port:
 Means reading the status (voltage level) present
on the pin.

Writing to a port:
 Means writing to the port latches.
 You have to determine the direction of the I/O pin
before using it, this is done by changing the value
of the TRIS register.

I/O PORTS
The I/O pin direction is controlled by a register
called TRIS. PORT<x> is controlled by the
register TRIS<x>.
 If you write ‘1’ in a bit in TRIS<x> register, this
means that the corresponding bit in PORT<x> is
input.
 A ‘0’ means -> output.

I/O PORTS


HOW to state data direction ?
TRIS A=0b 011001011 -> set pins (0 ,3,4,6) as o/p
set pins (1,2,5,7) as i/p
“ IN PORT A “
PORT A=1; -> perform 1 as o/p for all port A pins
 PORT A.f0=1; -> perform 1 as o/p for pin(0) in
port A

SWITCHES

We use switches to give an order to the MCU to
do something by changing the voltage level
applied on an I/O pin (input).
SWITCHES
1-Using RC circuit:
 -We use it as a LPF, as the ripples
 happen very fast (high frequency).

2-By software:
 By reading the value of the pin more than one
time in small time intervals to make sure of the
real value on the pin.
 This done by testing the value, wait for some
time (1~10 ms) and test again if the value is the
same, so it’s the true value

EXAMPLE 1

Write a program outputs high on RC0, if RB0 is
low using switches and leds.
INTERRUPT
Definition:
 An interrupt is an asynchronous signalindicate
for an eventwhich needs processor’s attention
immediately regardless to the instruction it
executes at this moment.
 It’s like a Doorbell.

INTERRUPTDEFINITIONS
Interrupt Flag (IF):
 A bit that is automatically set if the interrupt
source (event) happens.
 Global Interrupt Enable (GIE):
 Enables (if set) all un-masked interrupts
(interrupts with IE=1) or disables (if cleared) all
interrupts.
 Interrupt Enable (IE):
 If the GIE was ‘1’, this bit forces the CPU to
respond to the interrupt signal when IF=1 when
the waited event happens.

INTERRUPTDEFINITIONS
When the event (interrupt source) happens, the
following steps happen:
 1-The corresponding interrupt flag (IF) will equal
‘1’.
 2-If the interrupt enable (IE)was ‘1’, and the
global interrupt enable (GIE)was ‘1’ also, the GIE
is cleared by hardware to disable any further
interrupt to avoid responding to further
interrupts.
 3-The return address is pushed into the stack
and the PC is loaded with 0004h (the interrupt
vector.
