Файл: Introduction to PIC Microcontrollers (Complete Guide to PIC).pdf

ВУЗ: Не указан

Категория: Не указан

Дисциплина: Не указана

Добавлен: 14.06.2025

Просмотров: 1209

Скачиваний: 0

ВНИМАНИЕ! Если данный файл нарушает Ваши авторские права, то обязательно сообщите нам.

Chapter 2 - Microcontroller PIC16F84

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

Types of oscillators

PIC16F84 can work with four different configurations of an oscillator. Since configurations with crystal oscillator and resistor-condenser (RC) are the ones that are used most frequently, these are the only ones we will mention here. Microcontroller type with a crystal oscillator has in its designation XT, and a microcontroller with resistor-condenser pair has a designation RC. This is important because you need to mention the type of oscillator when buying a microcontroller.

XT Oscillator

Crystal oscillator is kept in metal housing with two pins where you have written down the frequency at which crystal oscillates. One ceramic condenser of 30pF whose other end is connected to the mass needs to be connected with each pin.

Oscillator and condensers can be packed in joint housing with three pins. Such element is called ceramic resonator and is represented in charts like the one below. Center pins of the element is the mass, while end pins are connected with OSC1 and OSC2 pins on the microcontroller. When designing a device, the rule is to place an oscillator nearer a microcontroller, so as to avoid any interference on lines on which microcontroller is receiving a clock.

Connecting the quartz oscillator to give clock to a microcontroller

Connecting a resonator onto a microcontroller

RC Oscillator

http://www.mikroelektronika.co.yu/english/books/2_02Poglavlje.htm (1 of 3) [30/12/2001 16:53:30]

Chapter 2 - Microcontroller PIC16F84

In applications where great time precision is not necessary, RC oscillator offers additional savings during purchase. Resonant frequency of RC oscillator depends on supply voltage rate, resistance R, capacity C and working temperature. It should be mentioned here that resonant frequency is also influenced by normal variations in process parameters, by tolerance of external R and C components, etc.

Above diagram shows how RC oscillator is connected with PIC16F84. With value of resistor R being below 2.2k, oscillator can become unstable, or it can even stop the oscillation. With very high value of R (ex.1M) oscillator becomes very sensitive to noise and humidity. It is recommended that value of resistor R should be between 3 and 100k. Even though oscillator will work without an external condenser (C=0pF), condenser above 20pF should still be used for noise and stability. No matter which oscillator is being used, in order to get a clock that microcontroller works upon, a clock of the oscillator must be divided by 4. Oscillator clock divided by 4 can also be obtained on OSC2/CLKOUT pin, and can be used for testing or synchronizing other logical circuits.

Relationship between a clock and a number of instruction cycles

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

http://www.mikroelektronika.co.yu/english/books/2_02Poglavlje.htm (2 of 3) [30/12/2001 16:53:30]

Chapter 2 - Microcontroller PIC16F84

Signal of an oscillator clock after receiving the supply on the microcontroller

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. Above diagram shows a typical shape of a signal which microcontroller gets from the quartz oscillator following a supply.

© Copyright 1999. mikroElektronika. All Rights Reserved. For any comments contact webmaster.

http://www.mikroelektronika.co.yu/english/books/2_02Poglavlje.htm (3 of 3) [30/12/2001 16:53:30]


Chapter 2 - Microcontroller PIC16F84

Chapter 2 - Microcontroller PIC16F84

Impulse for resetting during supply (power-up) is generated by microcontroller itself when it detects an increase in supply Vdd (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. Microcontroller is in a reset mode as long as PWRT is active. However, as device is working, problem arises when supply doesn't drop to zero but falls below the limit that guarantees microcontroller's proper functioning. This is a likely case in practice, especially in industrial environment where disturbances and instability of supply are an everyday occurrence. To solve this problem we need to make sure that microcontroller is in a reset state each time supply falls below the approved limit.

Examples of supply drop below the limit

If, according to electrical specification, internal reset circuit of a microcontroller can not satisfy the needs, special electronic components can be used which are capable of generating the desired reset signal. Beside this function, they can also function in watching over supply voltage. If voltage drops below specified level, a logical zero appears on MCLR pin which holds the microcontroller in reset state until voltage is not within limits that guarantee correct performance.

© Copyright 1999. mikroElektronika. All Rights Reserved. For any comments contact webmaster.

http://www.mikroelektronika.co.yu/english/books/2_03Poglavlje.htm (2 of 2) [30/12/2001 16:53:31]


Chapter 2 - Microcontroller PIC16F84

2.3 Central Processing Unit

Central processing unit (CPU) is the brain of a microcontroller. That part is responsible for finding and obtaining the right instruction which needs to be executed, for decoding that instruction, and finally for its execution.

Outline of the central processing unit – CPU

Central processing unit connects all parts of the microcontroller into one whole. Surely, its most important function is to decode program instructions. When programmer writes a program, instructions have a clear form like MOVLW 0x20. However, in order for a microcontroller to understand that, this 'letter' form of an instruction must be translated into a series of zeros and ones which is called an 'opcode'. This transition from a letter to binary form is done by translators such as assembler translator (also known as an assembler). Instruction thus derived from program memory must be decoded by a central processing unit. We can then select from the table of all the instructions a set of actions which execute a needed assignment defined in that instruction. As instructions may within themselves contain assignments which require different transfers of data from one memory into another, from memory onto ports, or some other calculations, CPU must be connected with all parts of the microcontroller. This is made possible through a data bus and an address bus.

http://www.mikroelektronika.co.yu/english/books/2_04Poglavlje.htm (1 of 6) [30/12/2001 16:53:35]

Chapter 2 - Microcontroller PIC16F84

Arithmetic Logic Unit (ALU)

Arithmetic logic unit is responsible for performing operations of adding, subtracting, moving (left or right within a register) and logic operations. Moving data inside a register is also known as 'shifting'. PIC16F84 contains an 8-bit arithmetic logic unit and 8-bit work registers.

Arithmetic-logic unit and how it works

In instructions with two operands, ordinarily one operand is in work register (W register), and the other is one of the registers or a constant. By operand we mean the contents on which some operation is being done, and a register is any one of the GPR or SFR registers. GPR is short for 'General Purposes Registers', and SFR for 'Special Function Registers'. In instructions with one operand, an operand is either W register or one of the registers. As an addition in doing operations in arithmetic and logic, ALU controls status bits (bits found in STATUS register). Execution of some instructions affects status bits, which depends on the result itself. Depending on which instruction is being executed, ALU can affect values of Carry (C), Digit Carry (DC), and Zero (Z) bits in STATUS register.

http://www.mikroelektronika.co.yu/english/books/2_04Poglavlje.htm (2 of 6) [30/12/2001 16:53:35]

Chapter 2 - Microcontroller PIC16F84

More detailed block outline of PIC16F84 microcontroller

STATUS Register

http://www.mikroelektronika.co.yu/english/books/2_04Poglavlje.htm (3 of 6) [30/12/2001 16:53:35]

Chapter 2 - Microcontroller PIC16F84

bit 0 C (Carry) Transfer

Bit that is affected by operations of addition, subtraction and shifting. This bit is set when a smaller value is being subtracted from a larger one, and is reset when a larger one is subtracted from a smaller one.

1= transfer occured from the highest resulting bit 0=transfer did not occur

C bit is affected by ADDWF, ADDLW, SUBLW, SUBWF instructions.

bit 1 DC (Digit Carry) DC Transfer

Bit affected by operations of addition and subtraction. Unlike C bit, this bit represents transfer from the fourth resulting place. It is set when smaller value is subtracted from a larger one, and is reset when a larger one is subtracted from a smaller one.

1=transfer occured on the fourth bit according to the order of the result 0=transfer did not occur

DC bit is affected by ADDWF, ADDLW, SUBLW, SUBWF instructions.

bit 2 Z (Zero bit) Indication of a zero result

This bit is set when the result of an executed arithmetic logic operation is zero. 1=result equals zero

0=result does not equal zero

bit 3 PD (Power-down bit)

Bit which is set whenever supply is brought to a microcontroller as it starts running, after each regular reset and after execution of instruction CLRWDT. Instruction SLEEP resets it when microcontroller falls into low spending/usage regime. Its repeated setting is possible via reset or by turning the supply on, or off . Setting can be triggered also by a signal on RB0/INT pin, change on RB port, completion of writing in internal DATA EEPROM, and by a watchdog, too.

1=after supply has been turned on 0= executing SLEEP instruction

bit 4 TO Time-out ; Watchdog overflow.

Bit is set after turning on the supply and execution of CLRWDT and SLEEP instructions. Bit is reset when watchdog gets to the end signaling that something is not right.

1=overflow did not occur 0=overflow did occur

bit6:5 RP1:RP0 (Register Bank Select bits)

These two bits are upper part of the address for direct addressing. Since instructions which address the memory directly have only seven bits, they need one more bit in order to address all 256 bytes which is how many bytes PIC16F84 has. RP1 bit is not used, but is left for some future expansions of this microcontroller.

01=first bank 00=zero bank

bit 7 IRP (Register Bank Select bit)

Bit whose role is to be an eighth bit for indirect addressing of internal RAM. 1=bank 2 and 3

0=bank 0 and 1 (from 00h to FFh)

STATUS register contains arithmetic status ALU (C, DC, Z), RESET status (TO, PD) and bits for selecting of memory bank (IRP, RP1, RP0). Considering that selection of memory bank is controlled through this register, it has to be present in each bank. Memory bank will be discussed

http://www.mikroelektronika.co.yu/english/books/2_04Poglavlje.htm (4 of 6) [30/12/2001 16:53:35]


Chapter 2 - Microcontroller PIC16F84

in more detail in Memory organization chapter. STATUS register can be a destination for any instruction, with any other register. If STATUS register is a destination for instructions which affect Z, DC or C bits, then writing to these three bits is not possible.

OPTION register

bit 0:2 PS0, PS1, PS2 (Prescaler Rate Select bit)

These three bits define prescaler rate select bit. What a prescaler is and how these bits can affect the work of a microcontroller will be dealt with in section on TMR0.

bit 3 PSA (Prescaler Assignment bit)

Bit which assigns prescaler between TMR0 and watchdog. 1=prescaler is assigned to watchdog

0=prescaler is assigned to a free timer TMR0

bit 4 T0SE (TMR0 Source Edge Select bit)

If it is allowed to trigger TMR0 by impulses from the pin RA4/T0CKI, this bit determines whether this will be to the falling or rising edge of a signal.

1=falling edge 0=rising edge

bit 5 TOCS (TMR0 Clock Source Select bit)

This pin enables free timer to increase its state either from internal oscillator on every ¼ of oscillator clock, or through external impulses on RA4/T0CKI pin.

1=external impulses 0=1/4 internal clock

bit 6 INTEDG (Interrupt Edge Select bit)

If interrupt is made possible this bit will determine the edge at which an interrupt will be activated on pin RB0/INT.

1=rising edge

http://www.mikroelektronika.co.yu/english/books/2_04Poglavlje.htm (5 of 6) [30/12/2001 16:53:35]

Chapter 2 - Microcontroller PIC16F84

0=falling edge

bit 7 RBPU (PORTB Pull-up Enable bit)

This bit turns on and off internal 'pull-up' resistors on port B. 1= "pull-up" resistors turned off

0= "pull-up" resistors turned on

© Copyright 1999. mikroElektronika. All Rights Reserved. For any comments contact webmaster.

http://www.mikroelektronika.co.yu/english/books/2_04Poglavlje.htm (6 of 6) [30/12/2001 16:53:35]


Chapter 2 - Microcontroller PIC16F84

2.4 Ports

Port 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. Ports represent physical connection of Central Processing Unit with an outside world. Microcontroller uses them in order to watch over or direct other components or devices. Due to functionality, some pins have twofold roles like PA4/TOCKI for instance, which is simultaneously the fourth bit of port A and an external input for free counter. Selection of one of these two pin functions is done in one of the configurational registers. An illustration of this is the fifth bit T0CS in OPTION register. By selecting one of the functions the other one is disabled.

Relationship between TRISA and PORTA register

All port pins can be defined as input or output, according to the needs of a device that's being developed. In order to define a pin as input or output pin, the right combination of zeros and ones must be written in TRIS register. If at the appropriate place in TRIS register a logical "1" is written, then that pin is an input pin, and if the opposite is true, it's an output pin. Every port has its proper TRIS register. Thus, port A has TRISA at address 85h, and port B has TRISB at address 86h.

http://www.mikroelektronika.co.yu/english/books/2_05Poglavlje.htm (1 of 3) [30/12/2001 16:53:36]

Chapter 2 - Microcontroller PIC16F84

PORTB

PORTB has 8 pins joined to it. The appropriate register for direction of data is TRISB at address 86h. Setting a bit in TRISB register defines the corresponding port pin as an input pin, and resetting a bit in TRISB register defines the corresponding port pin as the output pin. Each pin on PORTB has a weak internal pull-up resistor (resistor which defines a line to logic one) which can be activated by resetting the seventh bit RBPU in OPTION register. These 'pull-up' resistors are automatically being turned off when port pin is configured as an output. When a microcontroller is started, pull-up's are disabled.

Four pins PORTB, RB7:RB4 can cause an interrupt which occurs when their status changes from logical one into logical zero and the other way around. Only pins configured as input can cause this interrupt to occur (if any RB7:RB4 pin is configured as an output, an interrupt won't be generated at the change of status.) This interrupt option along with internal pull-up resistors makes it easier to solve common problems we find in practice like for instance that of matrix keyboard. If rows on the keyboard are connected to these pins, each push on a key will then cause an interrupt. A microcontroller will determine which key is at hand while processing an interrupt It is not recommended to refer to port B at the same time that interrupt is being processed.

The above example shows how pins 0, 1, 2, and 3 are declared for input, and pins 4, 5, 6, and 7 for output.

PORTA

PORTA has 5 pins joined to it. The corresponding register for data direction is TRISA at address 85h. Like with port B, setting a bit in TRISA register defines also the corresponding port pin as an input pin, and resetting a bit in TRISA register defines the corresponding port pin as an output pin. The fifth pin of port A has dual function. On that pin is also situated an external input for timer TMR0. One of these two options is chosen by setting or resetting the T0CS bit (TMR0 Clock Source Select bit). This pin enables the timer TMR0 to increase its status either from internal oscillator or via external impulses on RA4/T0CKI pin.

http://www.mikroelektronika.co.yu/english/books/2_05Poglavlje.htm (2 of 3) [30/12/2001 16:53:36]

Chapter 2 - Microcontroller PIC16F84

Example shows how pins 0, 1, 2, 3, and 4 are declared to be input, and pins 5, 6, and 7 to be output pins.

© Copyright 1999. mikroElektronika. All Rights Reserved. For any comments contact webmaster.

http://www.mikroelektronika.co.yu/english/books/2_05Poglavlje.htm (3 of 3) [30/12/2001 16:53:36]