Introduction to pic programming

25
INTRODUCTION TO PIC PROGRAMMING By : SHERIF HARHASH

description

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 ?. - PowerPoint PPT Presentation

Transcript of Introduction to pic programming

Page 1: Introduction to pic programming

INTRODUCTION TO PIC PROGRAMMING

By : SHERIF HARHASH

Page 2: Introduction to pic programming

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: Introduction to pic programming

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: Introduction to pic programming

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: Introduction to pic programming

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: Introduction to pic programming

PIC MICRO-CONTROLLERS

Page 7: Introduction to pic programming

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: Introduction to pic programming

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: Introduction to pic programming

C PROGRAMMINGLANGUAGE

Page 10: Introduction to pic programming

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: Introduction to pic programming

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: Introduction to pic programming

CONDITIONALSTATEMENTS If statement: if (expression) { statement(s); } If-else statement: if (expression1) { statement(s) } else if(expression2) { statement(s) } else { statement(s) }

Page 13: Introduction to pic programming

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: Introduction to pic programming

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: Introduction to pic programming

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: Introduction to pic programming

BREAK

10 MINS. ONLY BREAK

Page 17: Introduction to pic programming

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: Introduction to pic programming

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: Introduction to pic programming

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: Introduction to pic programming

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: Introduction to pic programming

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: Introduction to pic programming

EXAMPLE 1 Write a program outputs high on RC0, if RB0

is low using switches and leds.

Page 23: Introduction to pic programming

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: Introduction to pic programming

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: Introduction to pic programming

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.