Lecture (04) PIC 16F84A programming I - Dr. Ahmed … Applications … ·  · 2014-10-14Lecture...

25
Lecture (04) PIC 16F84A programming I Dr. Ahmed M. ElShafee Dr. Ahmed ElShafee, ACU Spring 2014, Practical Applications CS II ١ Agenda Introduction to PIC16F84A programming using C language Preprocessors and, Compiler directives Constants Variables and data types Pointers Variables assignments Arithmetic operations Relational operations Logic operations Bitwise operations Conditions Dr. Ahmed ElShafee, ACU Spring 2014, Practical Applications CS II ٢

Transcript of Lecture (04) PIC 16F84A programming I - Dr. Ahmed … Applications … ·  · 2014-10-14Lecture...

Page 1: Lecture (04) PIC 16F84A programming I - Dr. Ahmed … Applications … ·  · 2014-10-14Lecture (04) PIC 16F84A programming I Dr. Ahmed M. ElShafee Dr. Ahmed ElShafee, ACU Spring

Lecture (04) PIC 16F84A programming I

Dr. Ahmed M. ElShafee

Dr. Ahmed ElShafee, ACU Spring 2014, Practical Applications CS II ١

Agenda

• Introduction to PIC16F84A programming using C language • Preprocessors and, Compiler directives • Constants • Variables and data types • Pointers• Variables assignments • Arithmetic operations • Relational operations• Logic operations • Bitwise operations • Conditions

Dr. Ahmed ElShafee, ACU Spring 2014, Practical Applications CS II ٢

Page 2: Lecture (04) PIC 16F84A programming I - Dr. Ahmed … Applications … ·  · 2014-10-14Lecture (04) PIC 16F84A programming I Dr. Ahmed M. ElShafee Dr. Ahmed ElShafee, ACU Spring

• Loops • Embedding assembly • Arrays • Functions • Examples

Dr. Ahmed ElShafee, ACU Spring 2014, Practical Applications CS II ٣

Introduction to 16F84

٤

• PIC16F84 belongs to a class of 8-bit microcontrollers of RISC architecture.

• Program memory (FLASH)- for storing a written program. can be programmed and cleared more than once.

• EEPROM - data memory that needs to be saved when there is no supply.

Page 3: Lecture (04) PIC 16F84A programming I - Dr. Ahmed … Applications … ·  · 2014-10-14Lecture (04) PIC 16F84A programming I Dr. Ahmed M. ElShafee Dr. Ahmed ElShafee, ACU Spring

Dr. Ahmed ElShafee, ACU Spring 2014, Practical Applications CS II ٥

• RAM - data memory used by a program during its execution. In RAM are stored all inter-results or temporary data during run-time.

• PORTA and PORTB are physical connections between the microcontroller and the outside world. Port A has five, and port B has eight pins.

• FREE-RUN TIMER is an 8-bit register inside a microcontroller that works independently of the program.

• On every fourth clock of the oscillator it increments its value until it reaches the maximum (255), and then it starts counting over again from zero.

• As we know the exact timing between each two increments of the timer contents, timer can be used for measuring time.

• CENTRAL PROCESSING UNIT has a role of connective element between other blocks in the microcontroller. It coordinates the work of other blocks and executes the user program.

Dr. Ahmed ElShafee, ACU Spring 2014, Practical Applications CS II ٦

Page 4: Lecture (04) PIC 16F84A programming I - Dr. Ahmed … Applications … ·  · 2014-10-14Lecture (04) PIC 16F84A programming I Dr. Ahmed M. ElShafee Dr. Ahmed ElShafee, ACU Spring

Clock / instruction cycle

• Clock from the oscillator enters a microcontroller via OSC1 pin where internal circuit of a microcontroller divides the clock into four even clocks Q1, Q2, Q3, and Q4 which do not overlap

Dr. Ahmed ElShafee, ACU Spring 2014, Practical Applications CS II ٧

Dr. Ahmed ElShafee, ACU Spring 2014, Practical Applications CS II ٨

• These four clocks make up one instruction cycle (also called machine cycle) during which one instruction is executed

• Q1 : fetching current instruction according to PC (program counter)

• Q2: encoding current instruction • Q3 executing the previous instruction (which saved in instruction register from previous cycle) • Q4 writing current instruction to instruction register

Page 5: Lecture (04) PIC 16F84A programming I - Dr. Ahmed … Applications … ·  · 2014-10-14Lecture (04) PIC 16F84A programming I Dr. Ahmed M. ElShafee Dr. Ahmed ElShafee, ACU Spring

• If instruction causes a change on program counter, and PC doesn't point to the following but to some other address two cycles are needed for executing the correct instruction in the new locatop

• This is so because instruction must be processed again, but this time from the right address.

Dr. Ahmed ElShafee, ACU Spring 2014, Practical Applications CS II ٩

Pin description

• PIC16F84 has a total of 18 pins (DIP18 ) (Dual In Package) or (SMD 18) (Surface Mount Devices).

Dr. Ahmed ElShafee, ACU Spring 2014, Practical Applications CS II ١٠

Pin no.1 RA2 Second pin on port A. Has no additional function Pin no.2 RA3 Third pin on port A. Has no additional function. Pin no.3 RA4 Fourth pin on port A. TOCK1 which functions as a timer is also found on this pin Pin no.4 MCLR Reset input and Vpp programming voltage of a microcontrollerPin no.5 Vss Ground of power supply. Pin no.6 RB0 Zero pin on port B. Interrupt input is an additional function.

Page 6: Lecture (04) PIC 16F84A programming I - Dr. Ahmed … Applications … ·  · 2014-10-14Lecture (04) PIC 16F84A programming I Dr. Ahmed M. ElShafee Dr. Ahmed ElShafee, ACU Spring

١١

Pin no.7 RB1 First pin on port B. No additional function. Pin no.8 RB2 Second pin on port B. No additional function. Pin no.9 RB3 Third pin on port B. No additional function. Pin no.10 RB4 Fourth pin on port B. No additional function. Pin no.11 RB5 Fifth pin on port B. No additional function. Pin no.12 RB6 Sixth pin on port B. 'Clock' line in program mode. Pin no.13 RB7 Seventh pin on port B. 'Data' line in program mode. Pin no.14 Vdd Positive power supply pole. Pin no.15 OSC2 Pin assigned for connecting with an oscillator Pin no.16 OSC1 Pin assigned for connecting with an oscillator Pin no.17 RA2 Second pin on port A. No additional function Pin no.18 RA1 First pin on port A. No additional function.

١٢

XT Oscillator • Crystal oscillator is kept in metal

housing with two pins , the operating frequency is written on housing.

• A 33pf ceramic capacitors are connected to gnd.

• Oscillator and capacitors can be packed in joint case with three pins, called ceramic resonator

Page 7: Lecture (04) PIC 16F84A programming I - Dr. Ahmed … Applications … ·  · 2014-10-14Lecture (04) PIC 16F84A programming I Dr. Ahmed M. ElShafee Dr. Ahmed ElShafee, ACU Spring

Reset

١٣

• Reset is used when – microcontroller doesn't behave the way

we want it to, – interrupt in program execution, – to get a microcontroller ready when

loading a program. • MCLR has to be connected via resistor to

the positive supply pole, to prevent from bringing a logical zero to MCLR pin

Ports

• refers to a group of pins on a microcontroller which can be accessed simultaneously, or on which we can set the desired combination of zeros and ones, or read from them an existing status.

• Physically, port is a register inside a microcontroller which is connected by wires to the pins of a microcontroller.

• some pins have twofold roles like PA4/TOCKI , fourth bit of port A and an external input for free-run counter.

١٤

Page 8: Lecture (04) PIC 16F84A programming I - Dr. Ahmed … Applications … ·  · 2014-10-14Lecture (04) PIC 16F84A programming I Dr. Ahmed M. ElShafee Dr. Ahmed ElShafee, ACU Spring

• Selection of one of these two pin functions is done in one of the configuration registers

• In order to define a pin as input or output pin, the right combination of zeros and ones must be written in TRIS register. – logical "1", then that pin is an input pin – logical “0", then that pin is an output pin

• port A has TRISA, and port B has TRISB • Pin direction can be changed during your program • PORTA and PORTB state registers are located in bank 0, while

TRISA and TRISB pin direction registers are located in bank 1

١٥

PORTB and TRISB • Setting a bit in

TRISB register defines the corresponding port pin as input, and resetting a bit in TRISB register defines the corresponding port pin as output.

١٦

Page 9: Lecture (04) PIC 16F84A programming I - Dr. Ahmed … Applications … ·  · 2014-10-14Lecture (04) PIC 16F84A programming I Dr. Ahmed M. ElShafee Dr. Ahmed ElShafee, ACU Spring

• Each PORTB pin has a weak internal pull-up resistor (resistor which defines a line to logic one)

• These 'pull-up' resistors are automatically being turned off when port pin is configured as an output.

• When a microcontroller is started, pull-ups are disabled.

١٧

PORTA and TRISA • PORTA has 5 adjoining

pins. The corresponding register for data direction is TRISA

• Note: RA4 can be input only

١٨

Page 10: Lecture (04) PIC 16F84A programming I - Dr. Ahmed … Applications … ·  · 2014-10-14Lecture (04) PIC 16F84A programming I Dr. Ahmed M. ElShafee Dr. Ahmed ElShafee, ACU Spring

Writing

a specialized program in a PC called compiler is in charge of compiling program into machine language. Unlike assembly compilers, these create an executable code which is not always the shortest possible. Dr. Ahmed ElShafee, ACU Spring 2014, Practical Applications CS II ١٩

• Here is an example of a simple program written in C language:

Dr. Ahmed ElShafee, ACU Spring 2014, Practical Applications CS II ٢٠

Page 11: Lecture (04) PIC 16F84A programming I - Dr. Ahmed … Applications … ·  · 2014-10-14Lecture (04) PIC 16F84A programming I Dr. Ahmed M. ElShafee Dr. Ahmed ElShafee, ACU Spring

• the higher programming languages, such as C, are concerned because somebody has already solved this and many other similar problems for you.

• It will do to write a*b. Dr. Ahmed ElShafee, ACU Spring 2014, Practical Applications CS II ٢١

• The main idea of writing program in C language is to break a bigger problem down into several smaller pieces.

• To write a program for the microcontroller that is going to measure temperature and show results on an LCD display.

1. Activate and set built-in A/D converter;

2. Measure analogue value; 3. Calculate temperature; and 4. Send data in the proper form to LCD

display Dr. Ahmed ElShafee, ACU Spring 2014, Practical Applications CS II ٢٢

Page 12: Lecture (04) PIC 16F84A programming I - Dr. Ahmed … Applications … ·  · 2014-10-14Lecture (04) PIC 16F84A programming I Dr. Ahmed M. ElShafee Dr. Ahmed ElShafee, ACU Spring

Creating project

Dr. Ahmed ElShafee, ACU Spring 2014, Practical Applications CS II ٢٣

Dr. Ahmed ElShafee, ACU Spring 2014, Practical Applications CS II ٢٤

Page 13: Lecture (04) PIC 16F84A programming I - Dr. Ahmed … Applications … ·  · 2014-10-14Lecture (04) PIC 16F84A programming I Dr. Ahmed M. ElShafee Dr. Ahmed ElShafee, ACU Spring

Dr. Ahmed ElShafee, ACU Spring 2014, Practical Applications CS II ٢٥

Dr. Ahmed ElShafee, ACU Spring 2014, Practical Applications CS II ٢٦

Page 14: Lecture (04) PIC 16F84A programming I - Dr. Ahmed … Applications … ·  · 2014-10-14Lecture (04) PIC 16F84A programming I Dr. Ahmed M. ElShafee Dr. Ahmed ElShafee, ACU Spring

Dr. Ahmed ElShafee, ACU Spring 2014, Practical Applications CS II ٢٧

Dr. Ahmed ElShafee, ACU Spring 2014, Practical Applications CS II ٢٨

Page 15: Lecture (04) PIC 16F84A programming I - Dr. Ahmed … Applications … ·  · 2014-10-14Lecture (04) PIC 16F84A programming I Dr. Ahmed M. ElShafee Dr. Ahmed ElShafee, ACU Spring

Dr. Ahmed ElShafee, ACU Spring 2014, Practical Applications CS II ٢٩

Dr. Ahmed ElShafee, ACU Spring 2014, Practical Applications CS II ٣٠

Page 16: Lecture (04) PIC 16F84A programming I - Dr. Ahmed … Applications … ·  · 2014-10-14Lecture (04) PIC 16F84A programming I Dr. Ahmed M. ElShafee Dr. Ahmed ElShafee, ACU Spring

Dr. Ahmed ElShafee, ACU Spring 2014, Practical Applications CS II ٣١

Dr. Ahmed ElShafee, ACU Spring 2014, Practical Applications CS II ٣٢

Page 17: Lecture (04) PIC 16F84A programming I - Dr. Ahmed … Applications … ·  · 2014-10-14Lecture (04) PIC 16F84A programming I Dr. Ahmed M. ElShafee Dr. Ahmed ElShafee, ACU Spring

LedFlasher

Dr. Ahmed ElShafee, ACU Spring 2014, Practical Applications CS II ٣٣

#include <xc.h> #include <pic16f84a.h> int main(void) { TRISB=0x00; while(1)

{ PORTB=0xff; _delay(10); PORTB=0x00; _delay(10); } return (0); }

Working with simulator

Dr. Ahmed ElShafee, ACU Spring 2014, Practical Applications CS II ٣٤

Page 18: Lecture (04) PIC 16F84A programming I - Dr. Ahmed … Applications … ·  · 2014-10-14Lecture (04) PIC 16F84A programming I Dr. Ahmed M. ElShafee Dr. Ahmed ElShafee, ACU Spring

Dr. Ahmed ElShafee, ACU Spring 2014, Practical Applications CS II ٣٥

Dr. Ahmed ElShafee, ACU Spring 2014, Practical Applications CS II ٣٦

Page 19: Lecture (04) PIC 16F84A programming I - Dr. Ahmed … Applications … ·  · 2014-10-14Lecture (04) PIC 16F84A programming I Dr. Ahmed M. ElShafee Dr. Ahmed ElShafee, ACU Spring

Dr. Ahmed ElShafee, ACU Spring 2014, Practical Applications CS II ٣٧

Dr. Ahmed ElShafee, ACU Spring 2014, Practical Applications CS II ٣٨

Page 20: Lecture (04) PIC 16F84A programming I - Dr. Ahmed … Applications … ·  · 2014-10-14Lecture (04) PIC 16F84A programming I Dr. Ahmed M. ElShafee Dr. Ahmed ElShafee, ACU Spring

Dr. Ahmed ElShafee, ACU Spring 2014, Practical Applications CS II ٣٩

Dr. Ahmed ElShafee, ACU Spring 2014, Practical Applications CS II ٤٠

Page 21: Lecture (04) PIC 16F84A programming I - Dr. Ahmed … Applications … ·  · 2014-10-14Lecture (04) PIC 16F84A programming I Dr. Ahmed M. ElShafee Dr. Ahmed ElShafee, ACU Spring

Dr. Ahmed ElShafee, ACU Spring 2014, Practical Applications CS II ٤١

Dr. Ahmed ElShafee, ACU Spring 2014, Practical Applications CS II ٤٢

Page 22: Lecture (04) PIC 16F84A programming I - Dr. Ahmed … Applications … ·  · 2014-10-14Lecture (04) PIC 16F84A programming I Dr. Ahmed M. ElShafee Dr. Ahmed ElShafee, ACU Spring

Dr. Ahmed ElShafee, ACU Spring 2014, Practical Applications CS II ٤٣

PressControllerdFlasher

Dr. Ahmed ElShafee, ACU Spring 2014, Practical Applications CS II ٤٤

#include <xc.h> #include <pic16f84a.h> int main(void) { TRISB=0x00; TRISA=0xff;

while(1) { if(RA0==1) { PORTB=0xff; _delay(10); PORTB=0x00; _delay(10); }

Page 23: Lecture (04) PIC 16F84A programming I - Dr. Ahmed … Applications … ·  · 2014-10-14Lecture (04) PIC 16F84A programming I Dr. Ahmed M. ElShafee Dr. Ahmed ElShafee, ACU Spring

Dr. Ahmed ElShafee, ACU Spring 2014, Practical Applications CS II ٤٥

else { PORTB=0x00; } } return (0); }

PressControlledLedStatusToogle

Dr. Ahmed ElShafee, ACU Spring 2014, Practical Applications CS II ٤٦

#include <xc.h> #include <pic16f84a.h> int main(void) { int led_status=0;

TRISB=0x00; TRISA=0xff; PORTB=0x00; while(1) { if(RA0==1) { _delay(10); if(led_status==0){led_status=1;} else if(led_status==1){led_status=0;} }

Assignment

Page 24: Lecture (04) PIC 16F84A programming I - Dr. Ahmed … Applications … ·  · 2014-10-14Lecture (04) PIC 16F84A programming I Dr. Ahmed M. ElShafee Dr. Ahmed ElShafee, ACU Spring

Dr. Ahmed ElShafee, ACU Spring 2014, Practical Applications CS II ٤٧

if(led_status==0) PORTB=0x00;

else if(led_status==1) PORTB=0xff; _delay(1000);

} return (0); }

Assignment

Fara7BentEl3omdah #include <xc.h> #include <pic16f84a.h> int main(void) { TRISB=0x00; while(1) { _delay(10); PORTB=0x00; RB0=1; _delay(10); RB0=0; RB1=1; _delay(10);

Dr. Ahmed ElShafee, ACU Spring 2014, Practical Applications CS II ٤٨

RB1=0; RB2=1; _delay(10); RB2=0; RB3=1; } return (0); }

Page 25: Lecture (04) PIC 16F84A programming I - Dr. Ahmed … Applications … ·  · 2014-10-14Lecture (04) PIC 16F84A programming I Dr. Ahmed M. ElShafee Dr. Ahmed ElShafee, ACU Spring

Thanks,.. See you next week (ISA),…

Dr. Ahmed ElShafee, ACU Spring 2014, Practical Applications CS II ٤٩