PIC book Previous page Table of contents Chapter overview PIC microcontrollers Next page for beginners,too! Author: Nebojsa Matic Paperback - 252 pages (May 15, 2000) Dimensions (in inches): 0.62 x 9.13 x 7.28 PIC microcontrollers; low-cost computers-in-a-chip; allows electronics designers and hobbyists add intelligence and functions that mimic big computers for almost any electronic product or project.
PIC book 1.8 Analog to digital converter 1.9 Program CHAPTER II MICROCONTROLLER PIC16F84 Introduction CISC, RISC Applications Clock/instruction cycle Pipelining Pin description 2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.
PIC book ● ● 4.7 org 4.8 end Conditional instructions ● ● ● ● ● ● ● 4.9 if 4.10 else 4.11 endif 4.12 while 4.13 endw 4.14 ifdef 4.15 ifndef Data directives ● ● ● ● ● 4.16 4.17 4.18 4.19 4.20 cblock endc db de dt Configurating a directive ● ● 4.21 _CONFIG 4.22 Processor Assembler arithmetic operators Files created as a result of program translation Macros CHAPTER V MPLAB Introduction 5.1 5.2 5.3 5.4 5.5 5.6 5.7 5.
PIC book ● ● Macros WAIT, WAITX Macro PRINT 6.3 Samples ● ● ● ● ● ● ● ● ● ● Light Emitting Diodes Keyboard Optocoupler ❍ Optocouplering the input lines ❍ Optocouplering the output lines Relays Generating a sound Shift registers ❍ Input shift register ❍ Output shift register 7-segment Displays (multiplexing) LCD display 12-bit AD converter Serial communication APPENDIX A INSTRUCTION SET APPENDIX B NUMERIC SYSTEMS Introduction B.1 Decimal numeric system B.2 Binary numeric system B.
PIC book Send us a comment about a book Subject : Cooment about book PIC microcontrollers Name : State : USA E-mail : Your message: Submit Reset © C o p y r i g h t 2 0 0 1. m i k r o E l e k t r o n i k a. All Rights Reserved. For any comments contact webmaster. http://www.mikroelektronika.co.yu/english/product/books/PICbook/0_Uvod.
Chapter 1 - Introduction to Microprocessors Previous page Table of contents Chapter overview Next page CHAPTER 1 Introduction to Microcontrollers Introduction History Microcontrollers versus microprocessors 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.
Chapter 1 - Introduction to Microprocessors idea what treasure they had. During that year, there appeared on the market a microprocessor called 4004. That was the first 4-bit microprocessor with the speed of 6 000 operations per second. Not long after that, American company CTC requested from INTEL and Texas Instruments to make an 8-bit microprocessor for use in terminals.
Chapter 1 - Introduction to Microprocessors components for receiving and sending data must be added to it. In short that means that microprocessor is the very heart of the computer. On the other hand, microcontroller is designed to be all of that in one. No other external components are needed for its application because all necessary peripherals are already built into it. Thus, we save the time and space needed to construct devices. 1.
Chapter 1 - Introduction to Microprocessors Registers are therefore memory locations whose role is to help with performing various mathematical operations or any other operations with data wherever data can be found. Look at the current situation. We have two independent entities (memory and CPU) which are interconnected, and thus any exchange of data is hindered, as well as its functionality.
Chapter 1 - Introduction to Microprocessors 1.4 Input-output unit Those locations we've just added are called "ports". There are several types of ports : input, output or bidiectional ports. When working with ports, first of all it is necessary to choose which port we need to work with, and then to send data to, or take it from the port. When working with it the port acts like a memory location.
Chapter 1 - Introduction to Microprocessors As we have separate lines for receiving and sending, it is possible to receive and send data (info.) at the same time. So called full-duplex mode block which enables this way of communication is called a serial communication block. Unlike the parallel transmission, data moves here bit by bit, or in a series of bits what defines the term serial communication comes from.
Chapter 1 - Introduction to Microprocessors Of course, when this happens with a computer, we simply reset it and it will keep working. However, there is no reset button we can push on the microcontroller and thus solve our problem. To overcome this obstacle, we need to introduce one more block called watchdog. This block is in fact another free-run counter where our program needs to write a zero in every time it executes correctly.
Chapter 1 - Introduction to Microprocessors Microcontroller outline with its basic elements and internal connections For a real application, a microcontroller alone is not enough. Beside a microcontroller, we need a program that would be executed, and a few more elements which make up a interface logic towards the elements of regulation (which will be discussed in later chapters). 1.9 Program Program writing is a special field of work with microcontrollers and is called "programming".
Chapter 1 - Introduction to Microprocessors The program adds the contents of two memory locations, and views their sum on port A. The first line of the program stands for moving the contents of memory location "A" into one of the registers of central processing unit. As we need the other data as well, we will also move it into the other register of the central processing unit.
Chapter 2 - Microcontroller PIC16F84 Previous page Table of contents Chapter overview Next page CHAPTER 2 Microcontroller PIC16F84 Introduction CISC, RISC Applications Clock/instruction cycle Pipelining Pin description 2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8 Clock generator - oscillator Reset Central processing unit Ports Memory organization Interrupts Free timer TMR0 EEPROM Data memory Introduction PIC16F84 belongs to a class of 8-bit microcontrollers of RISC architecture.
Chapter 2 - Microcontroller PIC16F84 microcontroller. It coordinates the work of other blocks and executes the user program. CISC, RISC It has already been said that PIC16F84 has a RISC architecture. This term is often found in computer literature, and it needs to be explained here in more detail. Harvard architecture is a newer concept than von-Neumann's. It rose out of the need to speed up the work of a microcontroller. In Harvard architecture, data bus and address bus are separate.
Chapter 2 - Microcontroller PIC16F84 more 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. According to what its maker says, 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.
Chapter 2 - Microcontroller PIC16F84 Instruction cycle consists of cycles Q1, Q2, Q3 and Q4. Cycles of calling and executing instructions are connected in such a way that in order to make a call, one instruction cycle is needed, and one more is needed for decoding and execution. However, due to pipelining, each instruction is effectively executed in one cycle.
Chapter 2 - Microcontroller PIC16F84 Pins on PIC16F84 microcontroller have the following meaning: Pin Pin Pin Pin Pin Pin Pin Pin Pin Pin Pin Pin Pin Pin Pin Pin Pin Pin no.1 RA2 Second pin on port A. Has no additional function no.2 RA3 Third pin on port A. Has no additional function. no.3 RA4 Fourth pin on port A. TOCK1 which functions as a timer is also found on this pin no.4 MCLR Reset input and Vpp programming voltage of a microcontroller no.5 Vss Ground of power supply. no.6 RB0 Zero pin on port B.
Chapter 2 - Microcontroller PIC16F84 Previous page Table of contents Chapter overview Next page 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.
Chapter 2 - Microcontroller PIC16F84 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.
Chapter 2 - Microcontroller PIC16F84 Previous page Table of contents Chapter overview Next page © Copyright 1999. mikroElektronika. All Rights Reserved. For any comments contact webmaster. http://www.mikroelektronika.co.yu/english/product/books/PICbook/2_02Poglavlje.
Chapter 2 - Microcontroller PIC16F84 Previous page Table of contents Chapter overview Next page 2.2 Reset Reset is used for putting the microcontroller into a 'known' condition. That practically means that microcontroller can behave rather inaccurately under certain undesirable conditions. In order to continue its proper functioning it has to be reset, meaning all registers would be placed in a starting position.
Chapter 2 - Microcontroller PIC16F84 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. 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.
Chapter 2 - Microcontroller PIC16F84 Previous page Table of contents Chapter overview Next page 2.3 Central Processing Unit Central processing unit (CPU) is the brain of a microcontroller. That part is responsible for finding and fetching the right instruction which needs to be executed, for decoding that instruction, and finally for its execution. Central processing unit connects all parts of the microcontroller into one whole. Surely, its most important function is to decode program instructions.
Chapter 2 - Microcontroller PIC16F84 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 an abreviation 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.
Chapter 2 - Microcontroller PIC16F84 STATUS Register bit 0 C (Carry) Transfer Bit that is affected by operations of addition, subtraction and shifting. 1= transfer occured from the highest resulting bit 0=transfer did not occur http://www.mikroelektronika.co.yu/english/product/books/PICbook/2_04Poglavlje.
Chapter 2 - Microcontroller PIC16F84 C bit is affected by ADDWF, ADDLW, SUBLW, SUBWF instructions. bit 1 DC (Digit Carry) DC Transfer Bit affected by operations of addition, subtraction and shifting. Unlike C bit, this bit represents transfer from the fourth resulting place. It is set by addition when occurs carry from bit3 to bit4, or by subtraction when occurs borrow from bit4 to bit3, or by shifting in both direction.
Chapter 2 - Microcontroller PIC16F84 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 explained in section on TMR0. bit 3 PSA (Prescaler Assignment bit) Bit which assigns prescaler between TMR0 and watchdog.
Chapter 2 - Microcontroller PIC16F84 © Copyright 1999. mikroElektronika. All Rights Reserved. For any comments contact webmaster. http://www.mikroelektronika.co.yu/english/product/books/PICbook/2_04Poglavlje.
Chapter 2 - Microcontroller PIC16F84 Previous page Table of contents Chapter overview Next page 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.
Chapter 2 - Microcontroller PIC16F84 logical one into logical zero and opposite. 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.
Chapter 2 - Microcontroller PIC16F84 Previous page Table of contents Chapter overview Next page 2.5 Memory organization PIC16F84 has two separate memory blocks, one for data and the other for program. EEPROM memory and GPR registers in RAM memory make up a data block, and FLASH memory makes up a program block.
Chapter 2 - Microcontroller PIC16F84 Memory Banks Beside this 'length' division to SFR and GPR registers, memory map is also divided in 'width' (see preceding map) to two areas called 'banks'. Selecting one of the banks is done via RP0 and RP1 bits in STATUS register. Example: bcf STATUS, RP0 Instruction BCF clears bit RP0 (RP0=0) in STATUS register and thus sets up bank 0. http://www.mikroelektronika.co.yu/english/product/books/PICbook/2_06Poglavlje.
Chapter 2 - Microcontroller PIC16F84 bsf STATUS, RP0 Instruction BSF sets the bit RP0 (RP0=1) in STATUS register and thus sets up bank1. Usually, groups of instructions that are often in use, are connected into one unit which can easily be recalled in a program, and whose name has a clear meaning, so called Macros. With their use, selection between two banks becomes more clear and the program itself more legible.
Chapter 2 - Microcontroller PIC16F84 Bsf STATUS, RP0 ;Bankl movlw 0xFF ;w=0xFF movwf TRISA ;address of TRISA register is taken from ;instruction movwf Direct addressing Indirect Addressing Indirect unlike direct addressing does not take an address from an instruction but makes it with the help of IRP bit of STATUS and FSR registers. Addressed location is accessed via INDF register which in fact holds the address indicated by a FSR.
Chapter 2 - Microcontroller PIC16F84 An of such example can be sending a set of data via serial communication, working with buffers and indicators (which will be discussed further in a chapter with examples), or erasing a part of RAM memory (16 locations) as in the following instance. Reading data from INDF register when the contents of FSR register is equal to zero returns the value of zero, and writing to it results in NOP operation (no operation).
Chapter 2 - Microcontroller PIC16F84 Previous page Table of contents Chapter overview Next page 2.6 Interrupts Interrupts are a mechanism of a microcontroller which enables it to respond to some events at the moment when they occur, regardless of what microcontroller is doing at the time. This is a very important part, because it provides connection between a microcontroller and environment which surrounds it.
Chapter 2 - Microcontroller PIC16F84 bit 0 RBIF (RB Port Change Interrupt Flag bit) Bit which informs about changes on pins 4, 5, 6 and 7 of port B. 1=at least one pin has changed its status 0=no change occured on any of the pins bit 1 INTF (INT External Interrupt Flag bit) External interrupt occured. 1=interrupt occured 0=interrupt did not occur If a rising or falling edge was detected on pin RB0/INT, (which is defined with bit INTEDG in OPTION register), bit INTF is set.
Chapter 2 - Microcontroller PIC16F84 Generally speaking, each interrupt source has two bits joined to it. One enables interrupts, and the other detects when interrupts occur. There is one common bit called GIE which can be used to disallow or enable all interrupts simultaneously. This bit is very useful when writing a program because it allows for all interrupts to be disabled for a period of time, so that execution of some important part of a program would not be interrupted.
Chapter 2 - Microcontroller PIC16F84 One of the possible cases of errors if saving was not done when going to a subprogram of an interrupt Due to simplicity and frequent usage, these parts of the program can be made as macros. The concept of a Macro is explained in "Program assembly language". In the following example, contents of W and STATUS registers are stored in W_TEMP and STATUS_TEMP variables prior to interrupt routine.
Chapter 2 - Microcontroller PIC16F84 The same example can be realized by using macros, thus getting a more legible program. Macros that are already defined can be used for writing new macros. Macros BANK1 and BANK0 which are explained in "Memory organization" chapter are used with macros 'push' and 'pop'. External interrupt on RB0/INT pin of microcontroller http://www.mikroelektronika.co.yu/english/product/books/PICbook/2_07Poglavlje.
Chapter 2 - Microcontroller PIC16F84 External interrupt on RB0/INT pin is triggered by rising signal edge (if bit INTEDG=1 in OPTION<6> register), or falling edge (if INTEDG=0). When correct signal appears on INT pin, INTF bit is set in INTCON register. INTF bit (INTCON<1>) must be reset in interrupt routine, so that interrupt wouldn't occur again while going back to the main program.
Chapter 2 - Microcontroller PIC16F84 Return from interrupt routine can be accomplished with instructions RETURN, RETLW and RETFIE. It is recommended that instruction RETFIE be used because that instruction is the only one which automatically sets the GIE bit which allows new interrupts to occur. Previous page Table of contents Chapter overview Next page © Copyright 1999. mikroElektronika. All Rights Reserved. For any comments contact webmaster. http://www.mikroelektronika.co.
Chapter 2 - Microcontroller PIC16F84 Previous page Table of contents Chapter overview Next page 2.7 Free-run timer TMR0 Timers are usually most complicated parts of a microcontroller, so it is necessary to set aside more time for their explaining. 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.
Chapter 2 - Microcontroller PIC16F84 by one. This provides us with the ability to measure longer timer periods. 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.
Chapter 2 - Microcontroller PIC16F84 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.
Chapter 2 - Microcontroller PIC16F84 Prescaler is accorded to timer TMR0, or to watchdog timer trough 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 and watchdog timer at the same time .
Chapter 2 - Microcontroller PIC16F84 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 Previous page Table of contents Chapter overview Next page © Copyright 1999. mikroElektronika. All Rights Reserved. For any comments contact webmaster. http://www.mikroelektronika.co.yu/english/product/books/PICbook/2_08Poglavlje.
Chapter 2 - Microcontroller PIC16F84 Previous page Table of contents Chapter overview Next page 2.8 EEPROM Data memory PIC16F84 has 64 bytes of EEPROM memory locations on addresses from 00h to 63h those can be written to or read from. The most important characteristic of this memory is that it does not loose its contents during power supply turned off. That practically means that what was written to it will be remaining even if microcontroller is turned off.
Chapter 2 - Microcontroller PIC16F84 0=writing disallowed bit 3 WRERR (Write EEPROM Error Flag ) Error during writing to EEPROM This bit was set only in cases when writing to EEPROM had been 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 would be set automatically.
Chapter 2 - Microcontroller PIC16F84 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 a new! Previous page Table of contents Chapter overview Next page © Copyright 1999. mikroElektronika. All Rights Reserved. For any comments contact webmaster. http://www.mikroelektronika.co.yu/english/product/books/PICbook/2_09Poglavlje.
Chapter 3 - Instruction Set Previous page Table of contents Chapter overview Next page 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.
Chapter 3 - Instruction Set 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.
Chapter 3 - Instruction Set instruction. Instruction Execution Period All instructions are executed in one cycle except for conditional branch instructions if condition was true, or if the contents of program counter was changed by some instruction. In that case, execution requires two instruction cycles, and the second cycle is executed as NOP (No Operation). Four oscillator clocks make up one instruction cycle.
Chapter 3 - Instruction Set *1 If I/O port is source operand, status on microcontroller pins is read *2 If this instruction is executed on TMR register and if d=1, prescaler assigned to that timer will automatically be cleared *3 If PC was modified, or test result =1, instruction was executed in two cycles. Previous page Table of contents Chapter overview Next page © Copyright 1999. mikroElektronika. All Rights Reserved. For any comments contact webmaster. http://www.mikroelektronika.co.
http://www.mikroelektronika.co.yu/english/product/books/PICbook/4_Poglavlje.htm Previous page Table of contents Chapter overview Next page CHAPTER 4 Assembly Language Programming Introduction An example writting program Control directives ● ● ● ● ● ● ● ● 4.1 4.2 4.3 4.4 4.5 4.6 4.7 4.8 define include constant variable set equ org end Conditional instructions ● ● ● ● ● ● ● 4.9 if 4.10 else 4.11 endif 4.12 while 4.13 endw 4.14 ifdef 4.15 ifndef Data directives ● ● ● ● ● 4.16 4.17 4.18 4.19 4.
http://www.mikroelektronika.co.yu/english/product/books/PICbook/4_Poglavlje.htm Files created as a result of program translation Macros Introduction The ability to communicate is of great importance in any field. However, it is only possible if both communication partners know the same language, i.e follow the same rules during communication. Using these principles as a starting point, we can also define communication that occurs between microcontrollers and man .
http://www.mikroelektronika.co.yu/english/product/books/PICbook/4_Poglavlje.htm capable of producing an ASCII file on the computer disc or in specialized surroundings such as MPLAB - to be explained in the next chapter.
http://www.mikroelektronika.co.yu/english/product/books/PICbook/4_Poglavlje.htm Operands Operands are the instruction elements for the instruction is being executed. They are usually registers or variables or constants. Comments Comment is a series of words that a programmer writes to make the program more clear and legible. It is placed after an instruction, and must start with a semicolon ";".
http://www.mikroelektronika.co.yu/english/product/books/PICbook/4_Poglavlje.htm Since this data isn't important for the assembly translator, it is written as comments. It should be noted that a comment always begins with a semicolon and it can be placed in a new row or it can follow an instruction. After the opening comment has been written, the directive must be included. This is shown in the example above.
http://www.mikroelektronika.co.yu/english/product/books/PICbook/4_Poglavlje.htm occurs. For that purpose, one infinite loop is made where the micro is retained while power is connected. The necessary "end" at the end of each program informs the assembly translator that no more instructions are in the program. Control directives 4.
http://www.mikroelektronika.co.yu/english/product/books/PICbook/4_Poglavlje.htm Description: By using this directive, textual designation changes with particular value. It differs from CONSTANT directive in that after applying the directive, the value of textual designation can be changed. Example: variable level=20 variable time=13 Similar directives: SET, CONSTANT 4.
http://www.mikroelektronika.co.yu/english/product/books/PICbook/4_Poglavlje.htm 4.8 END End of program Syntax: end Description: At the end of each program it is necessary to place 'end' directive so that assembly translator would know that there are no more instructions in the program. Example: . . movlw 0xFF movwf PORTB end Conditional instructions 4.
http://www.mikroelektronika.co.yu/english/product/books/PICbook/4_Poglavlje.htm Syntax: endif Description: Directive is written at the end of a conditional block to inform the assembly translator that it is the end of the conditional block Example: If level=100 goto LOADS else goto UNLOADS endif Similar directives: ELSE, IF 4.12 WHILE Execution of program section as long as condition is met Syntax: while .
http://www.mikroelektronika.co.yu/english/product/books/PICbook/4_Poglavlje.htm reached. Example: #define test . ifdef test ;how the test was defined ......; instructions from these lines would execute endif Similar directives: #DEFINE, ELSE, ENDIF, IFNDEF, #UNDEFINE 4.
http://www.mikroelektronika.co.yu/english/product/books/PICbook/4_Poglavlje.htm 4.17 ENDC End of constant block definition Syntax: endc Description: Directive was used at the end of a definition of a block of constants so assembly translator could know that there are no more constants. Similar directives: CBLOCK 4.18 DB Defining one byte data Syntax: [
http://www.mikroelektronika.co.yu/english/product/books/PICbook/4_Poglavlje.htm _ _config or_ _config, Description: Oscillator, watchdog timer application and internal reset circuit are defined. Before using this directive, the processor must be defined using PROCESSOR directive. Example: _CONFIG _CP_OFF&_WDT_OFF&_PWRTE_ON&_XT_OSC Similar directives: _IDLOCS, PROCESSOR 4.
http://www.mikroelektronika.co.yu/english/product/books/PICbook/4_Poglavlje.htm Files created as a result of program translation As a result of the process of translating a program written in assembler language we get files like: ● ● ● Executing file (Program_Name.HEX) Program errors file (Program_Name.ERR) List file (Program_Name.LST) The first file contains translated program which was read in microcontroller by programming.
http://www.mikroelektronika.co.yu/english/product/books/PICbook/4_Poglavlje.htm http://www.mikroelektronika.co.yu/english/product/books/PICbook/4_Poglavlje.
http://www.mikroelektronika.co.yu/english/product/books/PICbook/4_Poglavlje.htm At the end of the "list" file there is a table of symbols used in a program. Useful element of 'list' file is a graph of memory utilization. At the very end, there is an error statistic as well as the amount of remaining program memory. Macros Macros are a very useful element in assembly language. They could briefly be described as "user defined group of instructions which will enter assembler program where macro was called".
Chapter 5 - MPLAB Previous page Table of contents Chapter overview Next page CHAPTER 5 MPLAB Introduction 5.1 5.2 5.3 5.4 5.5 5.6 5.7 5.8 Installing the MPLAB program package Introduction to MPLAB Choosing the development mode Designing a project Designing new assembler file Writing a program MPSIM simulator Toolbar Introduction MPLAB is a Windows program package that makes writing and developing a program easier.
Chapter 5 - MPLAB - Generating and processing a program (Text Editor) - Simulator of the written program used for simulating program function on the microcontroller. Besides these, there are support systems for Microchip products such as PICStart Plus and ICD (In Circuit Debugger). As this book does not cover these , they will be mentioned only as options. Minimal computer requirements for staring the MPLAB are: · · · · · · PC compatible computer 486 or higher Microsoft Windows 3.
Chapter 5 - MPLAB Welcome screen at the beginning of MPLAB installment At the very beginning, it is necessary to select those MPLAB components we will be working with. Since we don't have any original Microchip hardware components such as programmers or emulators, we will only install MPLAB environment, Assembler, Simulator and the instructions. http://www.mikroelektronika.co.yu/english/product/books/PICbook/5_01Poglavlje.
Chapter 5 - MPLAB Selecting components of MPLAB developing environment As it is assumed you will work in Windows 95 ( or a newer operating system), everything in connection with DOS operating system has been taken out during selection of assembler language. However, if you still wish to work in DOS, you need to deselect all options connected with Windows, and choose the components appropriate for DOS. Selecting the assembler and the operating system http://www.mikroelektronika.co.
Chapter 5 - MPLAB Like any other program, MPLAB should be installed into some directory. This option could be moved into any directory on any hard disc of your computer. If you didn't have a more pressing need, it should be left at selected place. Choosing the directory where MPLAB will be installed Users who have already had MPLAB (older version than this one) need the following option.
Chapter 5 - MPLAB Option for users who are installing a new version over an already installed MPLAB Start menu is a group of program pointers, and is selected by clicking on START option in the lower left corner of the screen. Since MPLAB will be started from here, we need to leave this option as it is. Adding the MPLAB to the start menu http://www.mikroelektronika.co.yu/english/product/books/PICbook/5_01Poglavlje.
Chapter 5 - MPLAB Location that will be mentioned from here on, has to do with a part of MPLAB whose explanation we don't need to get into. By selecting a special directory , MPLAB will keep all files in connection with the linker in a separate directory. Determining a directory for linker files Every Windows program has system files usually stored in a directory containing Windows program. After a number of different installments, the Windows directory becomes overcrowded and too big.
Chapter 5 - MPLAB Selecting a directory for system files After all of the above steps, installment begins by clicking on 'Next'. Screen prior to installment Installment doesn't take long, and the process of copying the files can be viewed on a small http://www.mikroelektronika.co.yu/english/product/books/PICbook/5_01Poglavlje.
Chapter 5 - MPLAB window in the right corner of the screen. Installment flow After installment have been completed, there are two dialog screens, one for the last minute information regarding program versions and corrections, and the other is a welcome screen. If text files (Readme.txt) have opened, they would need to be closed. http://www.mikroelektronika.co.yu/english/product/books/PICbook/5_01Poglavlje.
Chapter 5 - MPLAB Last minute information regarding program versions and corrections. By clicking on Finish, installment of MPLAB is finished. Previous page Table of contents Chapter overview Next page © Copyright 1999. mikroElektronika. All Rights Reserved. For any comments contact webmaster. http://www.mikroelektronika.co.yu/english/product/books/PICbook/5_01Poglavlje.
Chapter 5 - MPLAB Previous page Table of contents Chapter overview Next page 5.2 MPLAB Following the installment procedure, you will get a screen of the program itself. As you can see, MPLAB looks like most of the Windows programs. Near working area there is a "menu" (upper blue colored area with options File, Edit..etc.), "toolbar" (an area with illustrations the size of small squares), and status line on the bottom of the window.
Chapter 5 - MPLAB Opening a new window for a simulator Opening a new window for variables whose values we watch (Watch Window) Saving a window with variables whose values we are watching Setting the break points in a simulator (Break point) Preparing a program to be read in a microcontroller can boil down to several basic steps: Previous page Table of contents Chapter overview Next page © Copyright 1999. mikroElektronika. All Rights Reserved. For any comments contact webmaster. http://www.
Chapter 5 - MPLAB Previous page Table of contents Chapter overview Next page 5.3 Choosing the development mode Setting a developing mode is necessary so that MPLAB can know what tools will be used to execute the written program. In our case, we need to set up the simulator as a tool that's being used.
http://www.mikroelektronika.co.yu/english/product/books/PICbook/5_04Poglavlje.htm Previous page Table of contents Chapter overview Next page 5.4 Designing a project In order to start writing a program you need to create a project first. By clicking on PROJECT --> NEW PROJECT you are able to name your project and store it in a directory of your choice. In the picture below, a project named 'test.pjt' is being created and stored in c:\PIC\PROJEKTS\ directory.
http://www.mikroelektronika.co.yu/english/product/books/PICbook/5_04Poglavlje.htm Adjusting project elements Using a mouse click on "test [.hex]" which activates 'Node properties' option in the bottom right corner of a window. By clicking on it you get the following window. http://www.mikroelektronika.co.yu/english/product/books/PICbook/5_04Poglavlje.
http://www.mikroelektronika.co.yu/english/product/books/PICbook/5_04Poglavlje.htm Defining parameters of MPASM assembler From the picture we see that there are many different parameters. Each kind corresponds to one parameter in "Command line" . As memorizing these parameters is very uncomfortable, even forbidding for beginners, graphic adjustment has been introduced. From the picture we see which options need to be turned on.
http://www.mikroelektronika.co.yu/english/product/books/PICbook/5_04Poglavlje.htm Opening a new project By clicking on OK we go back to the starting window where we see added an assembler file. Assembler file added By clicking on OK we return to MPLAB environment. Previous page Table of contents Chapter overview Next page © Copyright 1999. mikroElektronika. All Rights Reserved. For any comments contact webmaster. http://www.mikroelektronika.co.yu/english/product/books/PICbook/5_04Poglavlje.
Chapter 5 - MPLAB Previous page Table of contents Chapter overview Next page 5.5 Designing a new assembler file (writing a new program) When "project" part of the work is finished, we need to start writing a program. In other words, new file must be opened, and will be named "test.asm". In our case, file has to be named "test.asm" because in projects which have only one file (such as ours), name of the project and name of the source file have to be the same. New file is opened by clicking on FILE>NEW.
Chapter 5 - MPLAB Naming and saving a new assembler file When we get this window, we need to write 'test.asm' below 'File name:', and click on OK. After that, we will see 'test.asm' file name at the top of our window. Previous page Table of contents Chapter overview Next page © Copyright 1999. mikroElektronika. All Rights Reserved. For any comments contact webmaster. http://www.mikroelektronika.co.yu/english/product/books/PICbook/5_05Poglavlje.
Chapter 5 - MPLAB Previous page Table of contents Chapter overview Next page 5.6 Writing a program Only after all of the preceding operations have been completed we are able to start writing a program. Since a simple program has already been written in "Assembly Language Programming" section of the book, so we will use that same program here, too.
Chapter 5 - MPLAB Window with messages following a translation of assembler program We can see from the picture that we get "test.hex" file as a result of translation process, that MPASMWIN program is used for translation, and that there is one message. In all that information, the last sentence in the window is the most important one because it shows whether translation was successful or not. 'Build completed successfully' is a message stating that translation was successful and that there were no errors.
Chapter 5 - MPLAB Previous page Table of contents Chapter overview Next page 5.7 MPSIM Simulator Simulator is part of MPLAB environment which provides a better insight into the workings of a microcontroller. Trough a simulator, we can monitor current variable values, register values and status of port pins. Truthfully, simulator does not have the same value in all programs.
Chapter 5 - MPLAB If there are variables in the program, it is good to watch them, too. To each variable is assigned one window (Watch Windows) by clicking on WINDOW->WATCH WINDOWS. Simulator with open windows for SFR registers, file registers and variables. The next command in a simulator is DEBUG>RUN>STEP which starts our steping through the program. The same command could have been assigned from a keyboard with key (generally speaking, all significant commands have keys assigned on the keyboard).
Chapter 5 - MPLAB Previous page Table of contents Chapter overview Next page 5.8 Toolbar Since MPLAB has more than one component, each of the components has its own toolbar. However, there is a toolbar which is some compilation of all toolbars, and can serve as a commonly used toolbar. This toolbar is enough for our needs, and it will be explained in more detail. In the picture below, we can see a toolbar we need with a brief explanation of each icon.
Chapter 5 - MPLAB Icon for saving a project. Saved project will keep all window adjustments and all parameter adjustments. When we read in a program again, everything will return to the screen as when the project was closed. Searching for a part of the program, or words is operation we need when searching through bigger assembler or other programs. By using it, we can find quickly a part of the program, label, macro, etc. Cutting a part of the text out.
Chapter 5 - MPLAB © Copyright 1999. mikroElektronika. All Rights Reserved. For any comments contact webmaster. http://www.mikroelektronika.co.yu/english/product/books/PICbook/5_08Poglavlje.
Chapter 6 - Samples Previous page Table of contents Chapter overview Next page CHAPTER 6 The Samples Introduction 6.1 Supplying the microcontroller 6.2 Macros used in programs ● ● Macros WAIT, WAITX Macro PRINT 6.
Chapter 6 - Samples likely that a man who is breathing in fresh air will live longer than a man who's living in a polluted environment. For a proper function of any microcontroller, it is necessary to provide a stable source of supply, a sure reset when you turn it on and an oscillator. According to technical specifications by the manufacturer of PIC microcontroller, supply voltage should move between 2.0V to 6.0V in all versions.
Chapter 6 - Samples Previous page Table of contents Chapter overview Next page Macros used in programs Examples given in the following sections of this chapter often use macros WAIT, WAITX and PRINT, so they will be explained in more detail. Macros WAIT, WAITX File Wait.inc contains two macros WAIT and WAITX. Through these macros it is possible to assign time delays in different intervals. Both macros use the overflow of counter TMR0 as a basic interval.
Chapter 6 - Samples If we use the oscillator (resonator) of 4MHz, for prescaler values 0, 1, and 7 that divide the basic clock of the oscillator, the interval followed by an overflow of timer TMR0 will be 0.512, 1.02 and 65.3 mS. Practically, that means that the biggest delay would be 256x65.3mS which is equal to 16.72 seconds. In order to use macros in the main program it is necessary do declare variables wcycle and prescWAIT as will be done in examples which follow in this chapter.
Chapter 6 - Samples How one such sequence is formed by using dt instruction is shown in the following example: org 0x00 goto Main String movwf PCL String1 dt "this is 'ASCII' string" String2 dt "Second string" End Main movlw .5 call String : First instruction after label Main writes the position of a member of the string in w register. We jump with instruction call onto label string where position of a member of the string is added to the value of the program counter: PCL=PCL+W.
Chapter 6 - Samples Macro PRINT writes out a string of ASCII caracters for 'MikroElektronika' on LCD display. The string takes up one part of program memory beginning at address 0x03. Previous page Table of contents Chapter overview Next page © Copyright 1999. mikroElektronika. All Rights Reserved. For any comments contact webmaster. http://www.mikroelektronika.co.yu/english/product/books/PICbook/6_02Poglavlje.
Chapter 6 - Samples Previous page Table of contents Chapter overview Next page Samples Light-Emitting Diodes - LEDs LEDs are surely one of the most commonly used elements in electronics. LED is an abbreviation for 'Light Emitting Diode'.
Chapter 6 - Samples Connecting LED diodes to PORTB microcontroller The following example initializes port B as output and sets logic one to each pin of port B to turn on all LEDs. http://www.mikroelektronika.co.yu/english/product/books/PICbook/6_03Poglavlje.
Chapter 6 - Samples Previous page Table of contents Chapter overview Next page © Copyright 1999. mikroElektronika. All Rights Reserved. For any comments contact webmaster. http://www.mikroelektronika.co.yu/english/product/books/PICbook/6_03Poglavlje.
Chapter 6 - Samples Previous page Table of contents Chapter overview Next page Keyboard Keyboards are mechanical devices used to execute a break or make connection between two points. They come in different sizes and with different purposes. Keys that are used here are also called "dip-keys". They are soldered directly onto a printed board and are often found in electronics. They have four pins (two for each contact) which give them mechanical stability.
Chapter 6 - Samples The above macro has several arguments that need to be explained: TESTER macro HiLo, Port, Bit, Delay, Address HiLo can be '0' or '1' which represents rising or falling edge where service subprogram will be executed when you press a key. Port is a microcontroller's port to which a key is connected. In the case of a PIC16F84 microcontroller, it can be PORTA or PORTB. Bit is port's pin to which the key is connected.
Chapter 6 - Samples Previous page Table of contents Chapter overview Next page © Copyright 1999. mikroElektronika. All Rights Reserved. For any comments contact webmaster. http://www.mikroelektronika.co.yu/english/product/books/PICbook/6_04Poglavlje.
Chapter 6 - Samples Previous page Table of contents Chapter overview Next page Optocoupler Optocoupler combine a LED and photo-transistor in the same case. The purpose of an optocoupler is to separate two parts of a circuit. This is done for a number of reasons: ● ● ● Interference. One part of a circuit may be in a location where it picks up a lot of interference (such as from electric motors, welding equipment, petrol motors etc.
Chapter 6 - Samples Input line optocoupler example http://www.mikroelektronika.co.yu/english/product/books/PICbook/6_05Poglavlje.
Chapter 6 - Samples Optocoupler on an output line An Optocoupler can be used to separate the output signal of a microcontroller from an output device. This may be needed for high voltage separation or current amplification. The output of some microcontrollers is limited to 25mA. The optocoupler will take the low-current signal from the microcontroller and it's output transistor will drive a LED or relay, as shown below: Output line optocoupler example The program for this example is simple.
Chapter 6 - Samples Previous page Table of contents Chapter overview Next page The Relay The relay is an electromechanical device, which transforms an electrical signal into mechanical movement. It consists of a coil of insulated wire on a metal core, and a metal armature with one or more contacts. When a supply voltage was delivered to the coil, current would flow and a magnetic field would be produced that moves the armature to close one set of contacts and/or open another set.
Chapter 6 - Samples Connecting the optocoupler and relay to a microcontroller A relay can also be activated via an optocoupler which at the same time amplifies the current related to the output of the microcontroller and provides a high degree of isolation. High current optocouplers usually contain a 'darlington' output transistor to provide high output current.
Chapter 6 - Samples http://www.mikroelektronika.co.yu/english/product/books/PICbook/6_06Poglavlje.
Chapter 6 - Samples Previous page Table of contents Chapter overview Next page © Copyright 1999. mikroElektronika. All Rights Reserved. For any comments contact webmaster. http://www.mikroelektronika.co.yu/english/product/books/PICbook/6_06Poglavlje.
Chapter 6 - Samples Previous page Table of contents Chapter overview Next page Generating a sound A Piezo diaphragm can be added to an output line of a microcontroller to deliver a "speaker" tones, beeps and signals. It is important to know there are two main types of piezo sound-emitting devices. One has active components inside the case and only requires a DC supply for the "speaker" to emit a tone or beep.
Chapter 6 - Samples duration: sound duration. The higher the number, the longer the sound. Example 1: BEEP 0xFF, 0x02 The output of the piezo diaphragm has the highest frequency and duration at 2 cycles per 65.3mS which gives 130.6 mS Example2: BEEP 0x90, 0x05 The output of the piezo diaphragm has a frequency of 0x90 and duration of 5 cycles per 65.3mS. It is best to determine these macro arguments through experimentation and select the sound that best suits the application.
Chapter 6 - Samples The following example shows the use of a macro in a program. The program produces two melodies which are obtained by pressing T1 or T2. Some of the previously discussed macros are included in the program. http://www.mikroelektronika.co.yu/english/product/books/PICbook/6_07Poglavlje.
Chapter 6 - Samples Previous page Table of contents Chapter overview Next page © Copyright 1999. mikroElektronika. All Rights Reserved. For any comments contact webmaster. http://www.mikroelektronika.co.yu/english/product/books/PICbook/6_07Poglavlje.
Chapter 6 - Samples Previous page Table of contents Chapter overview Next page Shift registers There are two types of shift registers: input and output. Input shift registers receive data in parallel, through 8 lines and then send it serially through two lines to a microcontroller. Output shift registers work in the opposite direction; they receive serial data and on a "latch" line signal, they turn it into parallel data.
Chapter 6 - Samples How to connect an input shift register to a microcontroller In order to simplify the main program, a macro can be used for the input shift register. Macro HC597 has two arguments: HC597 macro Var, Var1 Var variable where data from shift register input pins is transferred Var1 loop counter Example: HC597 data, counter Data from the input pins of the shift register is stored in data variable. Timer/counter variable is used as a loop counter. Macro listing: http://www.mikroelektronika.co.
Chapter 6 - Samples Example of how to use the HC597 macro is given in the following program. Program receives data from a parallel input of the shift register and moves it serially into the RX variable of the microcontroller. LEDs connected to port B will indicate the result of the data input. http://www.mikroelektronika.co.yu/english/product/books/PICbook/6_08Poglavlje.
Chapter 6 - Samples Output shift register Output shift registers transform serial data into parallel data. On every rising edge of the clock, the shift register reads the value from data line, stores it in temporary register, and then repeats this cycle 8 times. On a signal from 'latch' line, data is copied from the shift register to input http://www.mikroelektronika.co.yu/english/product/books/PICbook/6_08Poglavlje.
Chapter 6 - Samples register, thus data is transformed from serial into parallel data. An outline of the 74HC595 shift register connections is shown on the diagram below: Connecting an output shift register to a microcontroller Macro used in this example is found in hc595.inc file, and is called HC595. Macro HC595 has two arguments: HC595 macro Var, Var1 Var variable whose contents is transferred to outputs of shift register.
Chapter 6 - Samples An example of how to use the HC595 macro is given in the following program. Data from variable TX is serially transferred to shift register. LEDs connected to the parallel output of the shift register will indicate the state of the lines. In this example value 0xCB (1100 1011) is sent so that the eighth, seventh, fourth, second and first LEDs are illuminated. http://www.mikroelektronika.co.yu/english/product/books/PICbook/6_08Poglavlje.
Chapter 6 - Samples Previous page Table of contents Chapter overview Next page © Copyright 1999. mikroElektronika. All Rights Reserved. For any comments contact webmaster. http://www.mikroelektronika.co.yu/english/product/books/PICbook/6_08Poglavlje.
Chapter 6 - Samples Previous page Table of contents Chapter overview Next page Seven-Segment Display (multiplexing) The segments in a 7-segment display are arranged to form a single digit from 0 to F as shown in the animation: We can display a multi-digit number by connecting additional displays. Even though LCD displays are more comfortable to work with, 7-segment displays are still standard in the industry. This is due to their temperature robustness, visibility and wide viewing angle.
Chapter 6 - Samples Connecting a microcontroller to 7-segment displays in multiplex mode File Led.inc contains two macros: LED_Init and LED_Disp2. The first macro is used for display initialization. That is where display refreshment period is defined as well as microcontroller pins used for connecting the displays. The second macro is used for displaying numbers from 0 to 99 on two displays.
Chapter 6 - Samples http://www.mikroelektronika.co.yu/english/product/books/PICbook/6_09Poglavlje.
Chapter 6 - Samples The following example shows the use of macros in a program. Program displays number '21' in two 7-segment digits. http://www.mikroelektronika.co.yu/english/product/books/PICbook/6_09Poglavlje.
Chapter 6 - Samples Previous page Table of contents Chapter overview Next page © Copyright 1999. mikroElektronika. All Rights Reserved. For any comments contact webmaster. http://www.mikroelektronika.co.yu/english/product/books/PICbook/6_09Poglavlje.
Chapter 6 - Samples Previous page Table of contents Chapter overview Next page LCD Display More microcontroller devices are using 'smart LCD' displays to output visual information. The following discussion covers the connection of a Hitachi LCD display to a PIC microcontroller. LCD displays designed around Hitachi's LCD HD44780 module, are inexpensive, easy to use, and it is even possible to produce a readout using the 8 x 80 pixels of the display.
Chapter 6 - Samples Reading data from the LCD is done in the same way, but control line R/W has to be high. When we send a high to the LCD, it will reset and wait for instructions. Typical instructions sent to LCD display after a reset are: turning on a display, turning on a cursor and writing characters from left to right. When the LCD is initialized, it is ready to continue receiving data or instructions.
Chapter 6 - Samples LCD's can get stuck, and program will then stay forever in a loop checking the BUSY bit. The other way is to introduce a delay in the program. The delay has to be long enough for the LCD to finish the operation in process. Instructions for writing to and reading from an LCD memory are shown in the previous table. At the beginning we mentioned that we needed 11 I/O lines to communicate with an LCD. However, we can communicate with an LCD through a 4-bit data bus.
Chapter 6 - Samples http://www.mikroelektronika.co.yu/english/product/books/PICbook/6_10Poglavlje.
Chapter 6 - Samples http://www.mikroelektronika.co.yu/english/product/books/PICbook/6_10Poglavlje.
Chapter 6 - Samples http://www.mikroelektronika.co.yu/english/product/books/PICbook/6_10Poglavlje.
Chapter 6 - Samples Macro for LCD support LCDinit macro used to initialize port connected to LCD. LCD is configured to work in four-bit mode. Example: LCDinit LCDchar LCDarg Write ASCII character. Argument is ASCII caracter. Example: LCDChar 'd' LCDw Write character found in W register. Example: movlw 'p' LCDw LCDcmd LCDcommand Sending command instructions Example: LCDcmd LCDCH LCD_DDAdr DDRamAddress Set DD RAM address. Example: LCD_DDAdr .
Chapter 6 - Samples Macro LCDval_16 converts 16-bit binary number into decimal number from 0 to 65535 and displays it on LCD display. The following variables need to be declared in the main program: TEMP1, TEMP2, TEMP3, LO, HI, LO_TEMP, HI_TEMP, Bcheck. A 16-bit binary number is found in variables LO and HI. When a macro was executed, a decimal equivalent of this number would be displayed on LCD display. The leading zeros before the number would not be displayed. http://www.mikroelektronika.co.
Chapter 6 - Samples http://www.mikroelektronika.co.yu/english/product/books/PICbook/6_10Poglavlje.
Chapter 6 - Samples The main program is a demonstration of using the LCD display and generate new characters. At the beginning of a program, we need to declare variables LCDbuf and LCDtemp used by subprograms for the LCD as well as the microcontroller port connected to the LCD. The program writes the message 'characters:' on the first row and shows two special characters '~' and '}'. In the second row, 'mikroElektronika' is displayed. http://www.mikroelektronika.co.
Chapter 6 - Samples Previous page Table of contents Chapter overview Next page © Copyright 1999. mikroElektronika. All Rights Reserved. For any comments contact webmaster. http://www.mikroelektronika.co.yu/english/product/books/PICbook/6_10Poglavlje.
Chapter 6 - Samples Previous page Table of contents Chapter overview Next page 12-bit Analog to Digital converter Since everything in the microcontroller world is represented with "0's" and "1's", how do we cater for a signal that is 0.5 or 0.77? Most of the world outside a computer consists of analogue signals. Apart from speech and music, there are many quantities that need to be fed into a computer. Humidity, temperature, air pressure, colour, turbidity, and methane levels, are just a few.
Chapter 6 - Samples Connecting an AD converter with voltage reference to a microcontroller The Macro used in this example is LTC86 and is found in LTC1286.inc file. http://www.mikroelektronika.co.yu/english/product/books/PICbook/6_11Poglavlje.
Chapter 6 - Samples The LTC86 Macro has three arguments: LTC86 macro Var_LO, Var_HI, Var Var_LO variable is where the result of lower byte conversion is stored Var_HI variable is where the result of higher byte conversion is stored Var loop counter Example: LTC86 LO, HI, Count The four bits of the highest value are in variable HI, and first eight bits of conversion result are in variable LO. Count is an assistant variable to count the passes through loops.
Chapter 6 - Samples result is 0, and for 5V it is 4095. http://www.mikroelektronika.co.yu/english/product/books/PICbook/6_11Poglavlje.
Chapter 6 - Samples http://www.mikroelektronika.co.yu/english/product/books/PICbook/6_11Poglavlje.
Chapter 6 - Samples Previous page Table of contents Chapter overview Next page © Copyright 1999. mikroElektronika. All Rights Reserved. For any comments contact webmaster. http://www.mikroelektronika.co.yu/english/product/books/PICbook/6_11Poglavlje.
Chapter 6 - Samples Previous page Table of contents Chapter overview Next page Serial Communication SCI is an abbreviation for Serial Communication Interface and, as a special subsystem, it exists on most microcontrollers. When it is not available, as is the case with PIC16F84, it can be created in software. As with hardware communication, we use standard NRZ (Non Return to Zero) format also known as 8 (9)-N-1, or 8 or 9 data bits, without parity bit and with one stop bit.
Chapter 6 - Samples Connecting a microcontroller to a PC via a MAX232 line interface chip. File RS232.inc contains a group of macros used for serial communication. http://www.mikroelektronika.co.yu/english/product/books/PICbook/6_12Poglavlje.
Chapter 6 - Samples http://www.mikroelektronika.co.yu/english/product/books/PICbook/6_12Poglavlje.
Chapter 6 - Samples Using the macro: RS232init Macro for initializing RB0 pin and line for transmitting data (TX-pin). Example: RS232init SEND S_string Sending ASCII character. Argument is ASCII sign. Example: SEND 'g' SENDw Sending data found in W register. Example: movlw 't' SENDw RECEIVE macro in interrupt routine receives data for RS232 and stores it in RXD register Example: At the beginning of the main program, we need to declare variables RS_TEMP1, RE_TEMP2, TXD, RXD and TX pin on microcontroller.
Chapter 6 - Samples Main program: http://www.mikroelektronika.co.yu/english/product/books/PICbook/6_12Poglavlje.
Chapter 6 - Samples Previous page Table of contents Chapter overview Next page © Copyright 1999. mikroElektronika. All Rights Reserved. For any comments contact webmaster. http://www.mikroelektronika.co.yu/english/product/books/PICbook/6_12Poglavlje.
Appendix A - Instruction Set Previous page Table of contents Chapter overview Next page Appendix A Instruction Set Introduction Appendix contains all instructions presented separately with examples for their use. Syntax, description and its effects on status bits are given for each instruction. ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● A.1 MOVLW A.2 MOVWF A.3 MOVF A.4 CLRW A.5 CLRF A.6 SWAPF A.7 ADDLW A.8 ADDWF A.9 SUBLW A.10 SUBWF A.11 ANDLW A.12 ANDWF A.13 IORLW A.
Appendix A - Instruction Set ● A.35 SLEEP A.1 MOVLW A.2 MOVWF A.3 MOVF Write constant in W register Copy W to f Copy f to d http://www.mikroelektronika.co.yu/english/product/books/PICbook/A_Dodatak.
Appendix A - Instruction Set A.4 CLRW Write 0 in W A.5 Write 0 in f http://www.mikroelektronika.co.yu/english/product/books/PICbook/A_Dodatak.
Appendix A - Instruction Set A.6 SWAPF Copy the nibbles from f to d crosswise A.7 ADDLW Add W to a constant http://www.mikroelektronika.co.yu/english/product/books/PICbook/A_Dodatak.
Appendix A - Instruction Set A.8 ADDWF Add W to f A.9 SUBLW Subtract W from a constant http://www.mikroelektronika.co.yu/english/product/books/PICbook/A_Dodatak.
Appendix A - Instruction Set A.10 SUBWF Subtract W from f A.11 ANDLW Logic AND W with constant http://www.mikroelektronika.co.yu/english/product/books/PICbook/A_Dodatak.
Appendix A - Instruction Set A.12 ANDWF A.13 IORLW Logic AND W with f Logic OR W with constant http://www.mikroelektronika.co.yu/english/product/books/PICbook/A_Dodatak.
Appendix A - Instruction Set A.14 IORWF Logic OR W with f A.15 XORLW Logic exclusive OR W with constant http://www.mikroelektronika.co.yu/english/product/books/PICbook/A_Dodatak.
Appendix A - Instruction Set A.16 XORWF A.17 INCF Logic exclusive OR W with f Increment f http://www.mikroelektronika.co.yu/english/product/books/PICbook/A_Dodatak.
Appendix A - Instruction Set A.18 DECF Decrement f http://www.mikroelektronika.co.yu/english/product/books/PICbook/A_Dodatak.
Appendix A - Instruction Set A.19 RLF Rotate f to the left through CARRY http://www.mikroelektronika.co.yu/english/product/books/PICbook/A_Dodatak.
Appendix A - Instruction Set A.20 RRF Rotate f to the right through CARRY http://www.mikroelektronika.co.yu/english/product/books/PICbook/A_Dodatak.
Appendix A - Instruction Set A.21 COMF Complement f http://www.mikroelektronika.co.yu/english/product/books/PICbook/A_Dodatak.
Appendix A - Instruction Set A.22 BCF Reset bit b in f A.23 BSF Set bit b in f http://www.mikroelektronika.co.yu/english/product/books/PICbook/A_Dodatak.
Appendix A - Instruction Set A.24 BTFSC Test bit b in f, skip if it = 0 A.25 BTFSS Test bit b in f, skip if =1 http://www.mikroelektronika.co.yu/english/product/books/PICbook/A_Dodatak.
Appendix A - Instruction Set A.26 INCFSZ Increment f, skip if=0 A.27 DECFSZ Decrement f, skip if = 0 http://www.mikroelektronika.co.yu/english/product/books/PICbook/A_Dodatak.
Appendix A - Instruction Set A.28 GOTO A.29 CALL Jump to address Call a program http://www.mikroelektronika.co.yu/english/product/books/PICbook/A_Dodatak.
Appendix A - Instruction Set A.30 RETURN Return from a subprogram A.31 RETLW Return from a subprogram with constant in W http://www.mikroelektronika.co.yu/english/product/books/PICbook/A_Dodatak.
Appendix A - Instruction Set A.32 RETFIE A.33 NOP Return from interrupt routine No operation A.34 CLRWDT Initialize watchdog timer http://www.mikroelektronika.co.yu/english/product/books/PICbook/A_Dodatak.
Appendix A - Instruction Set A.35 SLEEP Previous page Stand by mode Table of contents Chapter overview Next page http://www.mikroelektronika.co.yu/english/product/books/PICbook/A_Dodatak.
Appendix A - Instruction Set © Copyright 1999. mikroElektronika. All Rights Reserved. For any comments contact webmaster. http://www.mikroelektronika.co.yu/english/product/books/PICbook/A_Dodatak.
Appendix B - Numeric Systems Previous page Table of contents Chapter overview Next page Appendix B Numeric Systems Introduction B.1 Decimal numeric system B.2 Binary numeric system B.3 Hexadecimal numeric system Conclusion Introduction It was always difficult for people to accept the fact that some things differ from them or their way of thinking. That is probably one of the reasons why numeric systems which differ from a decimal are still hard to understand.
Appendix B - Numeric Systems Operations of addition, subtraction, division, and multiplication in a decimal numeric system are used in a way that is already known to us, so we won't discuss it further. B.2 Binary numeric system Binary numeric system differs in many aspects from the decimal system we are used to in our everyday lives. Its numeric basis is 2, and each number can have only two values, '1' or '0'.
Appendix B - Numeric Systems As you can see, converting a binary number into a decimal number is done by calculating the expression on the left side. Depending on the position in a binary number, digits carry different values which are multiplied by themselves, and by adding them we get a decimal number we can understand. Let's further suppose that there are few marbles in each of the drawers: 2 in the first one, 4 in the second drawer, 7 in the third and 3 in the fourth drawer.
Appendix B - Numeric Systems We can check whether result is correct by transferring these number to decimal numeric system and by performing addition in it. With a transfer we get a value 10 as the first number, value 9 as the second, and value 19 as the sum. Thus we have proven that operation was done correctly. Trouble comes when sum is greater than what can be represented by a binary number with a given number of binary digits.
Appendix B - Numeric Systems which proves the accuracy of our action. In order to get a decimal equivalent of a hexadecimal number, we need to multiply each digit of a number with number 16 which is gradated by the position of that digit in hexadecimal number. Example: Addition is, like in two preceding examples, performed in a similar manner. Example: We need to add corresponding number digits. If their sum is equal 16, write 0 and transfer one to the next higher place.
Appendix B - Numeric Systems Previous page Table of contents Chapter overview © Copyright 1999. mikroElektronika. All Rights Reserved. For any comments contact webmaster. http://www.mikroelektronika.co.yu/english/product/books/PICbook/B_Dodatak.
Appendix C - Glossary Previous page Table of contents Chapter overview Next page Appendix C Glossary Introduction ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● Microcontroller I/O pin Software Hardware Simulator ICE EPROM Emulator Assembler HEX file List file Source File Debugging ROM, EPROM, EEPROM, FLASH, RAM Addressing ASCII Carry Code Byte, Kilobyte, Megabyte Flag Interrupt vector or interrupts Programmer Product Introduction Since all the fields of man's activity are regularly based on adequate an
Appendix C - Glossary I/O pin External microcontroller's connector pin which can be configured as input or output. In most cases I/O pin enables a microcontroller to communicate, control or read information. Software Information that microcontroller needs in order to be able to function. Software can not have any errors if we want the program and a device to function properly. Software can be written in different languages such as: Basic, C, pascal or assembler.
Appendix C - Glossary File written in the language understood by man and assembler translator. By translating the source file, we get HEX and LIST files. Debugging Error made in writing a program, which error we are not aware of. Errors can be quite simple such as typing errors, and quite complex such as incorrect use of program language. Assembler will find most of these errors and report them to '.LST' file. Other errors will need to be searched for by trying it out and watching how device functions.
Appendix C - Glossary Product Product development is a combination of luck and experience. Short terms, or time-limits for production should be avoided because even with most simple assignments, much time is needed to develop and improve. When creating a project, we need time, quiet, logical mind and most importantly, a thorough understanding of consumer's needs. Typical course in creating a product would have the following algorithm. Previous page Table of contents Chapter overview © Copyright 1999.