i ntroduction to pic programming by : s herif h arhash
TRANSCRIPT
![Page 1: I NTRODUCTION TO PIC PROGRAMMING By : S HERIF H ARHASH](https://reader035.vdocument.in/reader035/viewer/2022081520/5697c00f1a28abf838ccaa21/html5/thumbnails/1.jpg)
INTRODUCTION TO PIC PROGRAMMING
By :
SHERIF HARHASH
![Page 2: I NTRODUCTION TO PIC PROGRAMMING By : S HERIF H ARHASH](https://reader035.vdocument.in/reader035/viewer/2022081520/5697c00f1a28abf838ccaa21/html5/thumbnails/2.jpg)
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 .
![Page 3: I NTRODUCTION TO PIC PROGRAMMING By : S HERIF H ARHASH](https://reader035.vdocument.in/reader035/viewer/2022081520/5697c00f1a28abf838ccaa21/html5/thumbnails/3.jpg)
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) -.
![Page 4: I NTRODUCTION TO PIC PROGRAMMING By : S HERIF H ARHASH](https://reader035.vdocument.in/reader035/viewer/2022081520/5697c00f1a28abf838ccaa21/html5/thumbnails/4.jpg)
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.
![Page 5: I NTRODUCTION TO PIC PROGRAMMING By : S HERIF H ARHASH](https://reader035.vdocument.in/reader035/viewer/2022081520/5697c00f1a28abf838ccaa21/html5/thumbnails/5.jpg)
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.
![Page 6: I NTRODUCTION TO PIC PROGRAMMING By : S HERIF H ARHASH](https://reader035.vdocument.in/reader035/viewer/2022081520/5697c00f1a28abf838ccaa21/html5/thumbnails/6.jpg)
PIC MICRO-CONTROLLERS
![Page 7: I NTRODUCTION TO PIC PROGRAMMING By : S HERIF H ARHASH](https://reader035.vdocument.in/reader035/viewer/2022081520/5697c00f1a28abf838ccaa21/html5/thumbnails/7.jpg)
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.
![Page 8: I NTRODUCTION TO PIC PROGRAMMING By : S HERIF H ARHASH](https://reader035.vdocument.in/reader035/viewer/2022081520/5697c00f1a28abf838ccaa21/html5/thumbnails/8.jpg)
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
![Page 9: I NTRODUCTION TO PIC PROGRAMMING By : S HERIF H ARHASH](https://reader035.vdocument.in/reader035/viewer/2022081520/5697c00f1a28abf838ccaa21/html5/thumbnails/9.jpg)
C PROGRAMMINGLANGUAGE
![Page 10: I NTRODUCTION TO PIC PROGRAMMING By : S HERIF H ARHASH](https://reader035.vdocument.in/reader035/viewer/2022081520/5697c00f1a28abf838ccaa21/html5/thumbnails/10.jpg)
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
![Page 11: I NTRODUCTION TO PIC PROGRAMMING By : S HERIF H ARHASH](https://reader035.vdocument.in/reader035/viewer/2022081520/5697c00f1a28abf838ccaa21/html5/thumbnails/11.jpg)
OPERATORS
Relational operators: >,<,<=,>=,==,!= Logical operators: &&,||,! Bitwise operators: &, |, ^ (XOR), <<,>>,~ Precedence: 1.Casting 2.Parentheses 3.Negative 4.Multiplication and division 5.Addition and subtraction
![Page 12: I NTRODUCTION TO PIC PROGRAMMING By : S HERIF H ARHASH](https://reader035.vdocument.in/reader035/viewer/2022081520/5697c00f1a28abf838ccaa21/html5/thumbnails/12.jpg)
CONDITIONALSTATEMENTS If statement: if (expression) { statement(s); } If-else statement: if (expression1) { statement(s) } else if(expression2) { statement(s) } else { statement(s) }
![Page 13: I NTRODUCTION TO PIC PROGRAMMING By : S HERIF H ARHASH](https://reader035.vdocument.in/reader035/viewer/2022081520/5697c00f1a28abf838ccaa21/html5/thumbnails/13.jpg)
FOR LOOP
for( initialization ; conditional_test; increment )
Example : void main(void) { Int i; for(i=0; i<10; i++) printf(“%d “,i); printf(“done”); }
![Page 14: I NTRODUCTION TO PIC PROGRAMMING By : S HERIF H ARHASH](https://reader035.vdocument.in/reader035/viewer/2022081520/5697c00f1a28abf838ccaa21/html5/thumbnails/14.jpg)
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
![Page 15: I NTRODUCTION TO PIC PROGRAMMING By : S HERIF H ARHASH](https://reader035.vdocument.in/reader035/viewer/2022081520/5697c00f1a28abf838ccaa21/html5/thumbnails/15.jpg)
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() { }
![Page 16: I NTRODUCTION TO PIC PROGRAMMING By : S HERIF H ARHASH](https://reader035.vdocument.in/reader035/viewer/2022081520/5697c00f1a28abf838ccaa21/html5/thumbnails/16.jpg)
BREAK
10 MINS. ONLY BREAK
![Page 17: I NTRODUCTION TO PIC PROGRAMMING By : S HERIF H ARHASH](https://reader035.vdocument.in/reader035/viewer/2022081520/5697c00f1a28abf838ccaa21/html5/thumbnails/17.jpg)
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.
![Page 18: I NTRODUCTION TO PIC PROGRAMMING By : S HERIF H ARHASH](https://reader035.vdocument.in/reader035/viewer/2022081520/5697c00f1a28abf838ccaa21/html5/thumbnails/18.jpg)
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.
![Page 19: I NTRODUCTION TO PIC PROGRAMMING By : S HERIF H ARHASH](https://reader035.vdocument.in/reader035/viewer/2022081520/5697c00f1a28abf838ccaa21/html5/thumbnails/19.jpg)
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
![Page 20: I NTRODUCTION TO PIC PROGRAMMING By : S HERIF H ARHASH](https://reader035.vdocument.in/reader035/viewer/2022081520/5697c00f1a28abf838ccaa21/html5/thumbnails/20.jpg)
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).
![Page 21: I NTRODUCTION TO PIC PROGRAMMING By : S HERIF H ARHASH](https://reader035.vdocument.in/reader035/viewer/2022081520/5697c00f1a28abf838ccaa21/html5/thumbnails/21.jpg)
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
![Page 22: I NTRODUCTION TO PIC PROGRAMMING By : S HERIF H ARHASH](https://reader035.vdocument.in/reader035/viewer/2022081520/5697c00f1a28abf838ccaa21/html5/thumbnails/22.jpg)
EXAMPLE 1
Write a program outputs high on RC0, if RB0 is low using switches and leds.
![Page 23: I NTRODUCTION TO PIC PROGRAMMING By : S HERIF H ARHASH](https://reader035.vdocument.in/reader035/viewer/2022081520/5697c00f1a28abf838ccaa21/html5/thumbnails/23.jpg)
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.
![Page 24: I NTRODUCTION TO PIC PROGRAMMING By : S HERIF H ARHASH](https://reader035.vdocument.in/reader035/viewer/2022081520/5697c00f1a28abf838ccaa21/html5/thumbnails/24.jpg)
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.
![Page 25: I NTRODUCTION TO PIC PROGRAMMING By : S HERIF H ARHASH](https://reader035.vdocument.in/reader035/viewer/2022081520/5697c00f1a28abf838ccaa21/html5/thumbnails/25.jpg)
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.