Lecture (03) PIC16F84 (2)draelshafee.net/Fall2017/microcontrollers---lecture-03.pdf ·...

30
Lecture (03) PIC16F84 (2) By: Dr. Ahmed ElShafee Dr. Ahmed ElShafee, ACU : Fall 2017, MicroControllers ١ PIC16F84 has a RISC architecture, or Harvard architecture in another word Dr. Ahmed ElShafee, ACU : Fall 2017, MicroControllers ٢

Transcript of Lecture (03) PIC16F84 (2)draelshafee.net/Fall2017/microcontrollers---lecture-03.pdf ·...

Page 1: Lecture (03) PIC16F84 (2)draelshafee.net/Fall2017/microcontrollers---lecture-03.pdf · 2017-10-15 · Lecture (03) PIC16F84 (2) By: Dr. Ahmed ElShafee ١ Dr. Ahmed ElShafee, ACU :

Lecture (03)PIC16F84 (2)

By:

Dr. Ahmed ElShafee

Dr. Ahmed ElShafee, ACU : Fall 2017, MicroControllers١

• PIC16F84 has  a RISC architecture,  or Harvard architecture  in another word

Dr. Ahmed ElShafee, ACU : Fall 2017, MicroControllers٢

Page 2: Lecture (03) PIC16F84 (2)draelshafee.net/Fall2017/microcontrollers---lecture-03.pdf · 2017-10-15 · Lecture (03) PIC16F84 (2) By: Dr. Ahmed ElShafee ١ Dr. Ahmed ElShafee, ACU :

٣

• 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.

Dr. Ahmed ElShafee, ACU : Fall 2017, MicroControllers٤

• 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.

Page 3: Lecture (03) PIC16F84 (2)draelshafee.net/Fall2017/microcontrollers---lecture-03.pdf · 2017-10-15 · Lecture (03) PIC16F84 (2) By: Dr. Ahmed ElShafee ١ Dr. Ahmed ElShafee, ACU :

• 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 : Fall 2017, MicroControllers٥

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 : Fall 2017, MicroControllers٦

Page 4: Lecture (03) PIC16F84 (2)draelshafee.net/Fall2017/microcontrollers---lecture-03.pdf · 2017-10-15 · Lecture (03) PIC16F84 (2) By: Dr. Ahmed ElShafee ١ Dr. Ahmed ElShafee, ACU :

Dr. Ahmed ElShafee, ACU : Fall 2017, MicroControllers٧

• 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 

• 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 : Fall 2017, MicroControllers٨

Page 5: Lecture (03) PIC16F84 (2)draelshafee.net/Fall2017/microcontrollers---lecture-03.pdf · 2017-10-15 · Lecture (03) PIC16F84 (2) By: Dr. Ahmed ElShafee ١ Dr. Ahmed ElShafee, ACU :

Instructions set

• 16F84 precisely 35 instructions . (ex. Intel's and Motorola's microcontrollers have over hundred instructions) 

• All of these instructions are executed in one cycle 

• except for jump and branch instructions. 

• PIC16F84 usually reaches results of 2:1 in code compression and 4:1 in speed in relation to other 8‐bit microcontrollers in its class.

Dr. Ahmed ElShafee, ACU : Fall 2017, MicroControllers٩

Pin description

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

Dr. Ahmed ElShafee, ACU : Fall 2017, MicroControllers١٠

Pin no.1 RA2 Second pin on port A. Has no additional functionPin 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 pinPin 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 (03) PIC16F84 (2)draelshafee.net/Fall2017/microcontrollers---lecture-03.pdf · 2017-10-15 · Lecture (03) PIC16F84 (2) By: Dr. Ahmed ElShafee ١ Dr. Ahmed ElShafee, ACU :

١١

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 oscillatorPin no.16 OSC1 Pin assigned for connecting with an oscillatorPin no.17 RA2 Second pin on port A. No additional functionPin no.18 RA1 First pin on port A. No additional function.

Clock generator – oscillator

• Oscillator circuit is used for providing a microcontroller with a clock. Clock is needed so that microcontroller could execute a program or program instructions

• PIC16F84 can work with four different configurations of an oscillator, two of them are

– crystal oscillator 

– resistor‐capacitor (RC)

١٢

Page 7: Lecture (03) PIC16F84 (2)draelshafee.net/Fall2017/microcontrollers---lecture-03.pdf · 2017-10-15 · Lecture (03) PIC16F84 (2) By: Dr. Ahmed ElShafee ١ Dr. Ahmed ElShafee, ACU :

١٣

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 

١٤

RC Oscillator 

• Resonant frequency of RC oscillator depends on supply voltage rate, resistance R, capacity C and working temperature.

• R value should be greater than 2.2K and less than 1M to avoid un‐stability

• Recommended R is between 3K and 100k

• capacitor should be above 20pF 

Page 8: Lecture (03) PIC16F84 (2)draelshafee.net/Fall2017/microcontrollers---lecture-03.pdf · 2017-10-15 · Lecture (03) PIC16F84 (2) By: Dr. Ahmed ElShafee ١ Dr. Ahmed ElShafee, ACU :

١٥

• Oscillator clock divided by 4 can also be obtained on OSC2/CLKOUT pin, and can be used for testing or synchronizing other logical circuits

• Following a supply, oscillator starts oscillating. Oscillation at first has an unstable period and amplitude, but after some period of time it becomes stabilized

• To prevent such inaccurate clock from influencing microcontroller's performance, we need to keep the microcontroller in reset state during stabilization of oscillator's clock.

١٦

Page 9: Lecture (03) PIC16F84 (2)draelshafee.net/Fall2017/microcontrollers---lecture-03.pdf · 2017-10-15 · Lecture (03) PIC16F84 (2) By: Dr. Ahmed ElShafee ١ Dr. Ahmed ElShafee, ACU :

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

sources of resets:

– Reset during power on, POR (Power‐On Reset)

– Reset during regular work by bringing logical zero to MCLR microcontroller's pin.

– Reset during SLEEP regime

– Reset at watchdog timer (WDT) overflow

– Reset during at WDT overflow during SLEEP work regime.

• During a reset, RAM memory locations are not being reset. They are unknown during a power up and are not changed at any reset. 

• SFR registers are reset to a starting position initial state.settinga program counter (PC) to zero (0000h)

١٨

Page 10: Lecture (03) PIC16F84 (2)draelshafee.net/Fall2017/microcontrollers---lecture-03.pdf · 2017-10-15 · Lecture (03) PIC16F84 (2) By: Dr. Ahmed ElShafee ١ Dr. Ahmed ElShafee, ACU :

Brown‐out Reset

• Impulse for resetting is generated by microcontroller itself when supply doesn't drop to zero but falls below the limit that guarantees microcontroller's proper functioning (in a range from 1.2V to 1.8V)

١٩

• That impulse lasts 72ms which is enough time for an oscillator to get stabilized. 

• These 72ms are provided by an internal PWRT timer which has its own RC oscillator.

٢٠

Page 11: Lecture (03) PIC16F84 (2)draelshafee.net/Fall2017/microcontrollers---lecture-03.pdf · 2017-10-15 · Lecture (03) PIC16F84 (2) By: Dr. Ahmed ElShafee ١ Dr. Ahmed ElShafee, ACU :

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.

٢١

• 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

٢٢

Page 12: Lecture (03) PIC16F84 (2)draelshafee.net/Fall2017/microcontrollers---lecture-03.pdf · 2017-10-15 · Lecture (03) PIC16F84 (2) By: Dr. Ahmed ElShafee ١ Dr. Ahmed ElShafee, ACU :

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.

٢٣

• 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.

٢٤

Page 13: Lecture (03) PIC16F84 (2)draelshafee.net/Fall2017/microcontrollers---lecture-03.pdf · 2017-10-15 · Lecture (03) PIC16F84 (2) By: Dr. Ahmed ElShafee ١ Dr. Ahmed ElShafee, ACU :

PORTA and TRISA

• PORTA has 5 adjoining pins. The corresponding register for data direction is TRISA

• Note: RA4 can be input only

٢٥

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 : Fall 2017, MicroControllers٢٦

Page 14: Lecture (03) PIC16F84 (2)draelshafee.net/Fall2017/microcontrollers---lecture-03.pdf · 2017-10-15 · Lecture (03) PIC16F84 (2) By: Dr. Ahmed ElShafee ١ Dr. Ahmed ElShafee, ACU :

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

Dr. Ahmed ElShafee, ACU : Fall 2017, MicroControllers٢٧

• 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 : Fall 2017, MicroControllers٢٨

Page 15: Lecture (03) PIC16F84 (2)draelshafee.net/Fall2017/microcontrollers---lecture-03.pdf · 2017-10-15 · Lecture (03) PIC16F84 (2) By: Dr. Ahmed ElShafee ١ Dr. Ahmed ElShafee, ACU :

• 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 : Fall 2017, MicroControllers٢٩

Creating project

Dr. Ahmed ElShafee, ACU : Fall 2017, MicroControllers٣٠

Page 16: Lecture (03) PIC16F84 (2)draelshafee.net/Fall2017/microcontrollers---lecture-03.pdf · 2017-10-15 · Lecture (03) PIC16F84 (2) By: Dr. Ahmed ElShafee ١ Dr. Ahmed ElShafee, ACU :

Dr. Ahmed ElShafee, ACU : Fall 2017, MicroControllers٣١

Dr. Ahmed ElShafee, ACU : Fall 2017, MicroControllers٣٢

Page 17: Lecture (03) PIC16F84 (2)draelshafee.net/Fall2017/microcontrollers---lecture-03.pdf · 2017-10-15 · Lecture (03) PIC16F84 (2) By: Dr. Ahmed ElShafee ١ Dr. Ahmed ElShafee, ACU :

Dr. Ahmed ElShafee, ACU : Fall 2017, MicroControllers٣٣

Dr. Ahmed ElShafee, ACU : Fall 2017, MicroControllers٣٤

Page 18: Lecture (03) PIC16F84 (2)draelshafee.net/Fall2017/microcontrollers---lecture-03.pdf · 2017-10-15 · Lecture (03) PIC16F84 (2) By: Dr. Ahmed ElShafee ١ Dr. Ahmed ElShafee, ACU :

Dr. Ahmed ElShafee, ACU : Fall 2017, MicroControllers٣٥

Dr. Ahmed ElShafee, ACU : Fall 2017, MicroControllers٣٦

Page 19: Lecture (03) PIC16F84 (2)draelshafee.net/Fall2017/microcontrollers---lecture-03.pdf · 2017-10-15 · Lecture (03) PIC16F84 (2) By: Dr. Ahmed ElShafee ١ Dr. Ahmed ElShafee, ACU :

Dr. Ahmed ElShafee, ACU : Fall 2017, MicroControllers٣٧

Dr. Ahmed ElShafee, ACU : Fall 2017, MicroControllers٣٨

Page 20: Lecture (03) PIC16F84 (2)draelshafee.net/Fall2017/microcontrollers---lecture-03.pdf · 2017-10-15 · Lecture (03) PIC16F84 (2) By: Dr. Ahmed ElShafee ١ Dr. Ahmed ElShafee, ACU :

• When programming MC , you need to set configuration word manually

Dr. Ahmed ElShafee, ACU : Fall 2017, MicroControllers٣٩

LedFlasher

Dr. Ahmed ElShafee, ACU : Fall 2017, MicroControllers٤٠

#include <xc.h>#include <pic16f84a.h>

int main(void) {TRISB=0x00;while(1){

PORTB=0xff;_delay(100000);PORTB=0x00;_delay(100000);

}return (0);

}

Page 21: Lecture (03) PIC16F84 (2)draelshafee.net/Fall2017/microcontrollers---lecture-03.pdf · 2017-10-15 · Lecture (03) PIC16F84 (2) By: Dr. Ahmed ElShafee ١ Dr. Ahmed ElShafee, ACU :

Working with simulator

Dr. Ahmed ElShafee, ACU : Fall 2017, MicroControllers٤١

Dr. Ahmed ElShafee, ACU : Fall 2017, MicroControllers٤٢

Page 22: Lecture (03) PIC16F84 (2)draelshafee.net/Fall2017/microcontrollers---lecture-03.pdf · 2017-10-15 · Lecture (03) PIC16F84 (2) By: Dr. Ahmed ElShafee ١ Dr. Ahmed ElShafee, ACU :

Dr. Ahmed ElShafee, ACU : Fall 2017, MicroControllers٤٣

Dr. Ahmed ElShafee, ACU : Fall 2017, MicroControllers٤٤

Page 23: Lecture (03) PIC16F84 (2)draelshafee.net/Fall2017/microcontrollers---lecture-03.pdf · 2017-10-15 · Lecture (03) PIC16F84 (2) By: Dr. Ahmed ElShafee ١ Dr. Ahmed ElShafee, ACU :

Dr. Ahmed ElShafee, ACU : Fall 2017, MicroControllers٤٥

Dr. Ahmed ElShafee, ACU : Fall 2017, MicroControllers٤٦

Page 24: Lecture (03) PIC16F84 (2)draelshafee.net/Fall2017/microcontrollers---lecture-03.pdf · 2017-10-15 · Lecture (03) PIC16F84 (2) By: Dr. Ahmed ElShafee ١ Dr. Ahmed ElShafee, ACU :

Dr. Ahmed ElShafee, ACU : Fall 2017, MicroControllers٤٧

Dr. Ahmed ElShafee, ACU : Fall 2017, MicroControllers٤٨

Page 25: Lecture (03) PIC16F84 (2)draelshafee.net/Fall2017/microcontrollers---lecture-03.pdf · 2017-10-15 · Lecture (03) PIC16F84 (2) By: Dr. Ahmed ElShafee ١ Dr. Ahmed ElShafee, ACU :

Dr. Ahmed ElShafee, ACU : Fall 2017, MicroControllers٤٩

Dr. Ahmed ElShafee, ACU : Fall 2017, MicroControllers٥٠

Page 26: Lecture (03) PIC16F84 (2)draelshafee.net/Fall2017/microcontrollers---lecture-03.pdf · 2017-10-15 · Lecture (03) PIC16F84 (2) By: Dr. Ahmed ElShafee ١ Dr. Ahmed ElShafee, ACU :

Dr. Ahmed ElShafee, ACU : Fall 2017, MicroControllers٥١

PressControllerdFlasher

Dr. Ahmed ElShafee, ACU : Fall 2017, MicroControllers٥٢

#include <xc.h>#include <pic16f84a.h>

int main(void) {TRISB=0x00;TRISA=0xff;while(1){

if(RA0==0){

PORTB=0xff;_delay(100000);PORTB=0x00;_delay(100000);

}

Page 27: Lecture (03) PIC16F84 (2)draelshafee.net/Fall2017/microcontrollers---lecture-03.pdf · 2017-10-15 · Lecture (03) PIC16F84 (2) By: Dr. Ahmed ElShafee ١ Dr. Ahmed ElShafee, ACU :

Dr. Ahmed ElShafee, ACU : Fall 2017, MicroControllers٥٣

else{

PORTB=0x00;}

}return (0);

}

Dr. Ahmed ElShafee, ACU : Fall 2017, MicroControllers٥٤

Page 28: Lecture (03) PIC16F84 (2)draelshafee.net/Fall2017/microcontrollers---lecture-03.pdf · 2017-10-15 · Lecture (03) PIC16F84 (2) By: Dr. Ahmed ElShafee ١ Dr. Ahmed ElShafee, ACU :

PressControlledLedStatusToggleC

Dr. Ahmed ElShafee, ACU : Fall 2017, MicroControllers٥٥

#include <xc.h>#include <pic16f84a.h>

int main(void) {int led_status=0;TRISB=0x00;TRISA=0xff;PORTB=0x00;while(1){

if(RA0==0){

_delay(10000); if(led_status==0){led_status=1;}else if(led_status==1){led_status=0;}

}

Dr. Ahmed ElShafee, ACU : Fall 2017, MicroControllers٥٦

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

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

_delay(100000);

}return (0);

}

Page 29: Lecture (03) PIC16F84 (2)draelshafee.net/Fall2017/microcontrollers---lecture-03.pdf · 2017-10-15 · Lecture (03) PIC16F84 (2) By: Dr. Ahmed ElShafee ١ Dr. Ahmed ElShafee, ACU :

LedChaserC

#include <xc.h>

#include <pic16f84a.h>

int main(void) {

unsigned char ch=0x01;

TRISB=0x00;

while(1)

{

_delay(100000);

if(ch==0x00)ch=0x01;

else ch=ch*2;

PORTB=ch;

}

return (0);

}Dr. Ahmed ElShafee, ACU : Fall 2017, MicroControllers٥٧

Dr. Ahmed ElShafee, ACU : Fall 2017, MicroControllers٥٨

Page 30: Lecture (03) PIC16F84 (2)draelshafee.net/Fall2017/microcontrollers---lecture-03.pdf · 2017-10-15 · Lecture (03) PIC16F84 (2) By: Dr. Ahmed ElShafee ١ Dr. Ahmed ElShafee, ACU :

Thanks,..

See you next week (ISA),…

Dr. Ahmed ElShafee, ACU : Fall 2017, MicroControllers٥٩