i ntroduction to pic programming by : s herif h arhash

25
INTRODUCTION TO PIC PROGRAMMING By : SHERIF HARHASH

Upload: poppy-bryan

Post on 21-Jan-2016

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: I NTRODUCTION TO PIC PROGRAMMING By : S HERIF H ARHASH

INTRODUCTION TO PIC PROGRAMMING

By :

SHERIF HARHASH

Page 2: I NTRODUCTION TO PIC PROGRAMMING By : S HERIF H ARHASH

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

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

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

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

PIC MICRO-CONTROLLERS

Page 7: I NTRODUCTION TO PIC PROGRAMMING By : S HERIF H ARHASH

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

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

C PROGRAMMINGLANGUAGE

Page 10: I NTRODUCTION TO PIC PROGRAMMING By : S HERIF H ARHASH

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

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

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

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

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

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

BREAK

10 MINS. ONLY BREAK

Page 17: I NTRODUCTION TO PIC PROGRAMMING By : S HERIF H ARHASH

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

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

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

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

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

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

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

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

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.