Файл: Introduction to PIC Microcontrollers (Complete Guide to PIC).pdf
ВУЗ: Не указан
Категория: Не указан
Дисциплина: Не указана
Добавлен: 14.06.2025
Просмотров: 1207
Скачиваний: 0
Chapter 2 - Microcontroller PIC16F84
2.7 Free timer TMR0
Timers are ordinarily most complicated parts of a microcontroller, so it is necessary to set aside more time for their mastering. With their application it is possible to create relations between a real dimension such as "time" and a variable which represents status of a timer within a microcontroller. Physically, timer is a register whose value is continually increasing to 255, and then it starts all over again: 0, 1, 2, 3, 4...255.....1, 2, 3......etc.
Relation between the timer TMR0 and prescaler
http://www.mikroelektronika.co.yu/english/books/2_08Poglavlje.htm (1 of 6) [30/12/2001 16:53:43]
Chapter 2 - Microcontroller PIC16F84
This incrementing is done in the background of everything a microcontroller does. It is up to programmer to "think up a way" how he will take advantage of this characteristic for his needs. One of the ways is increasing some variable on each timer overflow. If we know how much time a timer needs to make one complete round, then multiplying the value of a variable by that time will yield the total amount of elapsed time.
PIC16F84 has an 8-bit timer. Number of bits determines what value timer counts to before starting to count from zero again. In the case of an 8-bit timer, that number is 256. A simplified scheme of relation between a timer and a prescaler is represented on the previous diagram. Prescaler is a name for the part of a microcontroller which divides oscillator clock before it will reach logic that increases timer status. Number which divides a clock is defined through first three bits in OPTION register. The highest divisor is 256. This actually means that only at every 256th clock, timer status would increase by one. This provides us with the ability to measure longer timer periods.
Time diagram of interrupt occurence with TMR0 timer
After each count up to 255, timer resets its value to zero and starts with a new cycle of counting to 255. During each transition from 255 to zero, T0IF bit in INTCOM register is set. If interrupts are allowed to occur, this can be taken advantage of in generating interrupts and in processing interrupt routine. It is up to programmer to reset T0IF bit in interrupt routine, so that new interrupt, or new overflow could be detected. Beside the internal oscillator clock, timer status can also be increased by the external clock on RA4/TOCKI pin. Choosing one of these two options is done in OPTION register through T0CS bit. If this option of external clock is selected, it is possible to define the edge of a signal (rising or falling), on which timer will increase its value.
http://www.mikroelektronika.co.yu/english/books/2_08Poglavlje.htm (2 of 6) [30/12/2001 16:53:43]
Chapter 2 - Microcontroller PIC16F84
Application of TMR0 timer to determining a number of full axle turns of the working machine of a motor
In practice, one of the typical examples that is solved via external clock and a timer is counting full turns of an axle of some production machine, like transformer winder for instance. Let's wind four metal screws on the axle of a winder. These four screws will represent metal convexity. Let's place now the inductive sensor at a distance of 5mm from the head of a screw. Inductive sensor will generate the falling signal every time the head of the screw is parallel with sensor head. Each signal will represent one fourth of a full turn, and the sum of all full turns will be found in TMR0 timer. Program can easily read this data from the timer through a data bus.
The following example illustrates how to initialize timer to signal falling edges from external clock source with a prescaler 1:4. Timer works in "polig" mode.
http://www.mikroelektronika.co.yu/english/books/2_08Poglavlje.htm (3 of 6) [30/12/2001 16:53:43]
Chapter 2 - Microcontroller PIC16F84
The same example can be realized through an interrupt in the following way:
Prescaler can join either timer TMR0 or a watchdog. Watchdog is a mechanism which microcontroller uses to defend itself against programs getting stuck. As with any other electrical circuit, so with a microcontroller too can occur failure, or some work impairment. Unfortunately,
http://www.mikroelektronika.co.yu/english/books/2_08Poglavlje.htm (4 of 6) [30/12/2001 16:53:43]
Chapter 2 - Microcontroller PIC16F84
microcontroller also has a component called program where problems can occur as well. When this happens, microcontroller will stop working and will remain in that state until someone resets it. Because of this, watchdog mechanism has been introduced. After a certain period of time, watchdog resets the microcontroller (microcontroller in fact resets itself). Watchdog works on a simple principle: if timer overflow occurs, microcontroller is reset, and it starts executing a program all over again. In this way, reset will occur in case of both correct and incorrect functioning. Next step is preventing reset in case of correct functioning, which is done by writing zero in WDT register (instruction CLRWDT) every time it nears its overflow. Thus program will prevent a reset as long as it's executing correctly. Once it gets stuck, zero will be written, overflow of WDT timer and a reset will occur which will bring the microcontroller back to correct functioning again.
Prescaler is accorded to timer TMR0, or to watchdog timer with the help of PSA bit in OPTION register. By clearing PSA bit, prescaler will be accorded to timer TMR0. When prescaler is accorded to timer TMR0, all instructions of writing to TMR0 register (CLRF TMR0, MOVWF TMR0, BSF TMR0,...) will clear prescaler. When prescaler is assigned to a watchdog timer, only CLRWDT instruction will clear a prescaler at the same time watchdog clears it. Prescaler change is completely under programmer's control, and can be changed while program is running.
There is only one prescaler and one timer. Depending on the needs, they are accorded either to timer TMR0 or to a watchdog.
OPTION Control Register
Bit 0:2 PS0, PS1, PS2 (Prescaler Rate Select bit)
The subject of a prescaler, and how these bits affect the work of a microcontroller will be covered in section on TMR0.
http://www.mikroelektronika.co.yu/english/books/2_08Poglavlje.htm (5 of 6) [30/12/2001 16:53:43]
Chapter 2 - Microcontroller PIC16F84
bit 3 PSA (Prescaler Assignment bit)
Bit which assigns prescaler between TMR0 and watchdog timer. 1=prescaler is assigned to watchdog timer.
0=prescaler is assigned to free timer TMR0
bit 4 T0SE (TMR0 Source Edge Select bit)
If we are able to trigger TMR0 with impulses from a RA4/T0CKI pin, this bit will determine whether it will be on the rising or falling edge of a signal.
1=falling edge 0=rising edge
bit 5 T0CS (TMR0 Clock Source Select bit)
This pin enables a free timer to increment its status either from an internal oscillator, which is on every 1 of oscillator clock, or via external impulses on RA4/T0CKI pin.
1=external impulses 0=1/4 internal clock
bit 6 INTEDG (Interrupt Edge Select bit)
If occurrence of interrupts is enabled, this bit will determine at what edge interrupt on RB0/INT pin will occur.
1= "pull-up" resistors turned off 0= "pull-up" resistors turned on
bit 7 RBPU (PORTB Pull-up Enable bit)
This bit turns internal pull-up resistors on port B on or off. 1='pull-up' resistors turned on
0='pull-up' resistors turned off
© Copyright 1999. mikroElektronika. All Rights Reserved. For any comments contact webmaster.
http://www.mikroelektronika.co.yu/english/books/2_08Poglavlje.htm (6 of 6) [30/12/2001 16:53:43]
Chapter 2 - Microcontroller PIC16F84
2.8 EEPROM Data memory
PIC16F84 has 64 bytes of EEPROM memory locations on addresses from 00h to 63h that can be written to or read from. The most important characteristic of this memory is that it does not loose its contents during supply. That practically means that what is written to it remains even if microcontroller is turned off. Data can be retained in EEPROM without supply for up to 40 years (as maker of PIC16F84 microcontroller says), and up to 10000 cycles of writing can be executed.
In practice, EEPROM memory is used for storing important data or some process parameters. One such parameter is a given temperature, assigned when setting up a temperature regulator to some process. In case that this data isn't retained, it will be necessary to adjust a given
temperature after each loss of supply. Since this is very impractical (and even dangerous), makers of microcontrollers have began installing one smaller type of EEPROM memory.
EEPROM memory is contained in a special memory space and can be accessed through special registers. These registers are:
•EEDATA at address 08h, which holds data that is read or that needs to be written.
•EEADR at address 09h, which contains an address of EEPROM location being accessed.
•EECON1 at address 88h, which contains control bits.
•EECON2 at address 89h. This register does not exist physically and serves to protect EEPROM from accidental writing.
EECON1 register at address 88h is a control register with five applied bits.
Bits 5, 6 and 7 are not used, and when read always are zero. Interpretation of EECON1 register bits follows.
EECON1 Register
bit 0 RD (Read Control bit)
Setting this bit initializes transfer of data from address defined in EEADR to EEDATA register. Since time is not as essential in reading data as in writing, data from EEDATA can already be used further in the next instruction.
http://www.mikroelektronika.co.yu/english/books/2_09Poglavlje.htm (1 of 3) [30/12/2001 16:53:47]
Chapter 2 - Microcontroller PIC16F84
1=initializes reading 0=does not initialize reading
bit 1 WR (Write Control bit)
Setting of this bit initializes writing data from EEDATA register to the address on EEADR register. 1=initializes writing
0=does not initialize writing
bit 2 WREN (EEPROM Write Enable bit) Enables writing to EEPROM If this bit is not set, microcontroller will not allow writing to EEPROM. 1=writing allowed
0=writing disallowed
bit 3 WRERR (EEPROM Error Flag bit) Error during writing to EEPROM
This bit is set only in cases when writing to EEPROM was interrupted by a reset signal or by running out of time in watchdog timer (if it's activated).
1=error occured 0=error did not occur
bit 4 EEIF (EEPROM Write Operation Interrupt Flag bit) Bit used to inform that writing data to EEPROM has ended.
When writing has terminated, this bit will be set automatically. Programmer must reset EEIF bit in his program in order to detect new termination of writing.
1=writing terminated
0=writing not terminated yet, or has not started
Reading from EEPROM Memory
Setting the RD bit initializes transfer of data from address found in EEADR register to EEDATA register. As in reading data we don't need so much time as in writing, data taken over from EEDATA register can already be used further in the next instruction.
Sample of the part of a program which reads data in EEPROM, could look something like the following:
After the last program instruction, contents from an EEPROM address zero can be found in working register w.
Writing to EEPROM Memory
In order to write data to EEPROM location, programmer must first write address to EEADR register and data to EEDATA register. Only then is it useful to set WR bit which sets the whole action in
http://www.mikroelektronika.co.yu/english/books/2_09Poglavlje.htm (2 of 3) [30/12/2001 16:53:47]
Chapter 2 - Microcontroller PIC16F84
motion. WR bit will be reset, and EEIF bit set following a writing which may be used in processing interrupts. Values 55h and AAh are the first and the second key which make it impossible for accidental writing to EEPROM to occur. These two values are written to EECON2 which serves only that purpose, to receive these two values and thus prevent any accidental writing to EEPROM memory. Program lines marked as 1, 2, 3, and 4 must be executed in that order in even time intervals. Therefore, it is very important to turn off interrupts which could change the timing needed for executing instructions. After writing, interrupts can be enabled again in the end.
Example of the part of a program which writes data 0xEE to first location in EEPROM memory could look something like the following:
It is recommended that WREN be turned off the whole time except when writing data to EEPROM, so that possibility of accidental writing would be minimal.
All writing to EEPROM will automatically clear a location prior to writing anew!
© Copyright 1999. mikroElektronika. All Rights Reserved. For any comments contact webmaster.
http://www.mikroelektronika.co.yu/english/books/2_09Poglavlje.htm (3 of 3) [30/12/2001 16:53:47]
Chapter 3 - Instruction Set
CHAPTER 3
Instruction Set
Introduction
Instruction set in PIC16Cxx microcontroller family
Data Transfer
Arithmetic and logic
Bit operations
Directing the program flow
Instruction execution period
Word list
Introduction
We have already mentioned that microcontroller is not like any other integrated circuit. When they come out of production most integrated circuits are ready to be built into devices which is not the case with microcontrollers. In order to "make" microcontroller perform a task, we have to tell it exactly what to do, or in other words we must write the program microcontroller will execute. We will describe in this chapter instructions which make up the assembler, or program language for PIC microcontrollers of lower standard.
Instruction Set in PIC16Cxx Microcontroller Family
Complete set which encompasses 35 instructions is given in the following table. A reason for such a small number of instructions lies primarily in the fact that we are talking about a RISC microcontroller whose instructions are well optimized considering the speed of work, architectural simplicity and code compactness. The only drawback is that programmer is expected to master "uncomfortable" technique of using a modest set of 35 instructions.
http://www.mikroelektronika.co.yu/english/books/3_Poglavlje.htm (1 of 5) [30/12/2001 16:53:49]
Chapter 3 - Instruction Set
Data transfer
Transfer of data in a microcontroller is done between work (W) register and an 'f' register that represents any location in internal RAM (regardless whether those are special or general purpose registers).
First three instructions (look at the following table) provide for a constant being written in W register (MOVLW is short for MOVe Literal to W), and for data to be copied from W register onto RAM and data from RAM to be copied onto W register (or on the same RAM location, at which point only the status of Z flag changes). Instruction CLRF writes constant 0 in 'f ' register, and CLRW writes constant 0 in register W. SWAPF instruction exchanges places of the 4-bit nibbles crosswise inside a register.
Arithmetic and logic
Of all arithmetic operations, PIC like most microcontrollers supports only subtraction and addition. Flags C, DC and Z are set depending on a result of addition or subtraction, but with one exception: since subtraction is performed like addition of a negative value, C flag is inverse following a subtraction. In other words, it is set if operation is possible, and reset if larger number was subtracted from a smaller one.
Logic one of PIC has capability of performing operations AND, OR, EX-OR, negations (COMF) and rotation (RLF and RRF).
Instructions which rotate the register contents move bits inside a register through flag C by one space to the left (toward bit 7), or to the right (toward bit 0). Bit which "comes out" of a register is written in flag C, and status of that flag is written in a bit on the "opposite side" of the register.
Bit operations
Instructions BCF and BSF do setting or resetting of one bit anywhere in the memory. Even though this seems like a simple operation, it is executed so that CPU first reads the whole byte, changes one bit in it and then writes in the entire byte at the same place.
Directing a program flow
Instructions GOTO, CALL and RETURN are executed the same way as on all other microcontrollers, only stack is independent of internal RAM and limited to eight levels.
'RETLW k' instruction is identical with RETURN instruction, except that before coming back from a subprogram a constant defined by instruction operand is written in W register. This instruction enables us to design easily the Lookup tables (lists). Mostly we use them by determining data position on our table adding it to the address at which the table begins, and then we read data from that location (which is usually found in program memory).
Table can be formed as a subprogram which consists of a series of 'RETLW k' instructions, where 'k' constants are members of the table.
http://www.mikroelektronika.co.yu/english/books/3_Poglavlje.htm (2 of 5) [30/12/2001 16:53:49]