Файл: Introduction to PIC Microcontrollers (Complete Guide to PIC).pdf
ВУЗ: Не указан
Категория: Не указан
Дисциплина: Не указана
Добавлен: 14.06.2025
Просмотров: 1211
Скачиваний: 0
Chapter 5 - MPLAB
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 sort of a combination of all toolbars, and can serve as a common 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. Because of the limited format of this book, this toolbar is shown as a hanging toolbar. Generally, it is placed horizontally below the menu, over the entire length of the screen.
Universal toolbar with brief explanations of the icons
http://www.mikroelektronika.co.yu/english/books/5_08Poglavlje.htm (1 of 3) [30/12/2001 16:54:16]
Chapter 5 - MPLAB
Meaning of icons in a toolbar
If the current toolbar for some reason does not respond to a click on this icon, the next one appears. Changeover is repeated so that on the fourth click we will get the same toolbar again.
Icon for opening a project. Project opened in this way contains all screen adjustments and adjustment of all elements which are crucial to the current project.
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. This one and the following three icons are standard in all programs that deal with processing textual files. Since each program is actually a common text file, those operations are useful.
Copying a part of the text. There is a difference between this one and the previous icon. With cut operation, when you cut a part of the text out, it disappears from the screen (and from a program) and is copied afterwards. But with copy operation, text is copied but not cut out, and it remains on the screen.
When a part of the text is copied, it is moved into a part of the memory which serves for transferring data in Windows operational system. Later, by clicking on this icon it can be 'pasted' in the text where the cursor is.
Saving a program (assembler file).
Start program execution in full speed. It is recognized by appearance of a yellow status line. With this kind of program execution, simulator executes a program in full speed until it is interrupted by clicking on the red traffic light icon.
Stop program execution in full speed. After clicking on this icon, status line becomes gray again, and program execution can continue step by step.
Step by step program execution. By clicking on this icon, we begin executing an instruction from the next program line in relation to the current one.
Skip requirements. Since simulator is still a software simulation of real work, it is possible to simply skip over some program requirements. This is especially handy with instructions which are waiting for some requirement following which program can proceed further. That part of the program which follows a requirement is the part that's interesting to a programmer.
Resetting a microcontroller. By clicking on this icon, program counter is positioned at the beginning of a program and simulation can start.
By clicking on this icon we get a window with a program, but this time as program memory where we can see which instruction is found at which address.
http://www.mikroelektronika.co.yu/english/books/5_08Poglavlje.htm (2 of 3) [30/12/2001 16:54:16]
Chapter 5 - MPLAB
With the help of this icon we get a window with the contents of RAM memory of a microcontroller.
By clicking on this icon, window with SFR register appears. Since SFR registers are used in every program, it is recommended that in simulator this window is always active.
If a program contains variables whose values we need to keep track of (ex. counter), a window needs to be added for each of them, which is done by using this icon.
When certain errors in a program are noticed during simulation process, program has to be corrected. Since simulator uses HEX file as its input, so we need to translate a program again so that all changes would be transferred to a simulator. By clicking on this icon, entire project is translated again, and we get the newest version of HEX file for the simulator.
© Copyright 1999. mikroElektronika. All Rights Reserved. For any comments contact webmaster.
http://www.mikroelektronika.co.yu/english/books/5_08Poglavlje.htm (3 of 3) [30/12/2001 16:54:16]
Chapter 6 - Samples
CHAPTER 6
Samples
Introduction
6.1 Supplying the microcontroller
6.2 Macros used in programs
●Macros WAIT, WAITX
●Macro PRINT
6.3 Samples
●LED 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
Introduction
http://www.mikroelektronika.co.yu/english/books/6_01Poglavlje.htm (1 of 3) [30/12/2001 16:54:17]
Chapter 6 - Samples
Examples given in this chapter will show you how to connect the PIC microcontroller with other peripheral components or devices when developing your own microcontroller system. Each example contains detailed description of the hardware part with electrical outline and comments about the program. All programs can be taken directly from the from copied from 'MikroElektronika' internet presentation.
6.1 Supplying the microcontroller
Generally speaking, the correct voltage supply is of utmost importance for the proper functioning of the microcontroller system. It can easily be compared to a man breathing in the air. It is more likely that a man who is breathing in fresh air will live longer than a man who lives 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 maker of PIC microcontroller, supply voltage should move between 2.0V to 6.0V in all versions. The simplest solution to the source of supply is using the voltage stabilizer LM7805 which gives stable +5V on its output. One such source is shown in the picture below.
In order to function properly, or in order to have stable 5V at the output (pin 3), input voltage on pin 1 of LM7805 should be between 7V through 24V. Depending on current consumption of device we will use the appropriate type of voltage stabilizer LM7805. There are several versions of LM7805. For electricity consumption of up to 1A we should use the version in TO-220 housing with the capability of additional cooling. If the total consumption is 50mA, we can use 78L05 (stabilizer version in small TO - 92 packaging for electricity of up to 100mA).
http://www.mikroelektronika.co.yu/english/books/6_01Poglavlje.htm (2 of 3) [30/12/2001 16:54:17]
Chapter 6 - Samples
© Copyright 1999. mikroElektronika. All Rights Reserved. For any comments contact webmaster.
http://www.mikroelektronika.co.yu/english/books/6_01Poglavlje.htm (3 of 3) [30/12/2001 16:54:17]
Chapter 6 - Samples
6.2 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. By changing the prescaler we can change the length of the overflow interval of the counter TMR0.
http://www.mikroelektronika.co.yu/english/books/6_02Poglavlje.htm (1 of 5) [30/12/2001 16:54:19]
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 that can result would be 256x65.3mS which is equal to 16.72 seconds.
http://www.mikroelektronika.co.yu/english/books/6_02Poglavlje.htm (2 of 5) [30/12/2001 16:54:19]
Chapter 6 - Samples
In order to use macros in the main program it is necessary do declare variables wcycle and prescWAIT as has been done in examples which follow in this chapter.
Macro WAIT has one argument. The standard value assigned to prescaler of this macro is 1 (1.02mS), and it can not be changed.
WAIT timeconst_1
timeconst_1 is number from 0 to 255. By multiplying that number with the overflow time period we get the total amount of the delay: TIME=timeconst_1 x 1.02mS.
Example: WAIT .100
Example shows how to make a delay of 100x1.02mS, or total of 102mS.
Unlike macro WAIT, macro WAITX has one more argument that can assign prescaler value. Macro WAITX has two arguments:
Timeconst_2 is number from 0 to 255. By multiplying that number with the overflow time period we get the total amount of the delay:
TIME=timeconst_1 x 1.02mS x PRESCext
PRESCext is number from 0 to 7 which sets up the relationship between a clock and timer TMR0.
Example: WAITX .100,7
Example shows how to make a delay of 100x65.3 mS, or total of 653mS.
Macro PRINT
Macro PRINT is found in Print.inc file. It makes it easy to send a series of data on one of the output devices such as : LCD, RS232, matrix printer...etc. The easiest way to form a series is by using a dt (define table) directive. This instruction stores a series of data into program memory as a group of retlw instructions whose operand is data from the series.
http://www.mikroelektronika.co.yu/english/books/6_02Poglavlje.htm (3 of 5) [30/12/2001 16:54:19]
Chapter 6 - Samples
How one such sequence is formed by using dt instruction is shown in the following example:
org 0x00 goto Main
Series movwf PCL
Series1 dt "this is 'ASCII' series"
Series2 dt "Second series"
End
Main
movlw .5 call Series
:
First instruction after label Main writes the position of a member of the sequence in w register. We jump with instruction call onto label series where position of a member of the sequence is added to the value of the program counter: PCL=PCL+W. Next we will have in the program counter an address of retlw instruction with the desired member of the sequence. When this instruction is executed, member of the sequence will be in w register, and address of the instruction that executed after the call instruction will be in the program counter. End label is an elegant way to mark the address at which the series ends.
Macro PRINT has five arguments:
http://www.mikroelektronika.co.yu/english/books/6_02Poglavlje.htm (4 of 5) [30/12/2001 16:54:19]
Chapter 6 - Samples
PRINT macro Addr, Start, End, Var, Out
Addr is an address where one or more sequences (which follow one after another) begin. Start is an address of the first member of the sequence
End is an address where the sequence ends
Var is the variable which has a role of showing (pointing ) the members of the sequence
Out is an argument we use to send the address of existing subprograms in working with output devices such as : LCD, RS-232 etc.
Macro PRINT writes out a series of ASCII signs for 'MikroElektronika' on LCD display. The sequence takes up one part of program memory beginning at address 0x03.
© Copyright 1999. mikroElektronika. All Rights Reserved. For any comments contact webmaster.
http://www.mikroelektronika.co.yu/english/books/6_02Poglavlje.htm (5 of 5) [30/12/2001 16:54:19]
Chapter 6 - Samples
6.3 Samples
LED Diodes
LEDs are surely one of the most commonly used elements in electronics. LED is short for 'Light Emitting Diode'. When choosing a LED, several parameters should be looked at: diameter, which is usually 3 or 5 mm (millimeters), working current which is usually about 20mA (It can be as low as 2mA for LEDs with high efficiency - high light output), and color of course, which can be red or green though there is also orange, blue, yellow....
LEDs must be connected around the correct way, in order to emit light and the current-limiting resistor must be the correct value so that the LED is not damaged or burn out (overheated). The positive of the supply is taken to the anode, and the cathode goes to the negative or earth of the project (circuit). In order to identify each lead, the cathode is the shorter lead and the LED housing usually has a cut or "flat" on the cathode side. Diodes will emit light only if current is flowing from anode to cathode. Otherwise, its PN junction is reverse biased and current won't flow. In order to connect a LED correctly, a resistor must be added in series that will limit the amount of current through the diode, so that it does not burn out. The value of the resistor is determined by the amount of current you want to flow through the LED. This can range from 2mA to 25mA. High-efficiency LEDs can produce a very good output with a current as low as 2mA.
To determine the value of the dropper-resistor, we need to know the value of the supply voltage. From this we subtract the characteristic voltage drop of a LED. This value will range from 1.7v to 2.3v depending on the color of the LED. The answer is the value of Ur. Using this value and the current we want to flow through the LED (0.002A to 0.01A) we can work out the value of the resistor from the formula R=Ur/I.
LEDs are connected to a microcontroller in two ways. One is to turn them on with logic zero, and other to turn them on with logic one. The first is called NEGATIVE logic and the other is called POSITIVE logic. The normal method is POSITIVE logic. The above diagram shows how they are connected for POSITIVE logic. Since POSITIVE logic provides a voltage of +5V to the diode and dropper resistor, it will emit light each time a pin of port B is provided with a logic 1 (1 = HIGH output). NEGATIVE logic requires the LED to be turned around the other way and the cathode
http://www.mikroelektronika.co.yu/english/books/6_03Poglavlje.htm (1 of 3) [30/12/2001 16:54:21]
Chapter 6 - Samples
connected to the positive supply. When a LOW output from the microcontroller is delivered to the LED and resistor, the LED will illuminate.
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/books/6_03Poglavlje.htm (2 of 3) [30/12/2001 16:54:21]
Chapter 6 - Samples
© Copyright 1999. mikroElektronika. All Rights Reserved. For any comments contact webmaster.
http://www.mikroelektronika.co.yu/english/books/6_03Poglavlje.htm (3 of 3) [30/12/2001 16:54:21]
Chapter 6 - Samples
Keyboard
Keyboards are mechanical devices used to execute an interrupt or make connections 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 plate and are often found in electronics. They have four pins (two for each contact) which give them mechanical stability.
Example of connecting keys to microcontroller pins.
Key function is simple. When we press a key, two contacts are joined together and connection is made. Still, it isn't all that simple. The problem lies in the nature of voltage as an electrical dimension, and in the imperfection of mechanical contacts. That is to say, before contact is made or cut off, there is a short time period when vibration (oscillation) can occur as a result of unevenness of mechanical contacts, or as a result of the different speed in pressing a key (this depends on person who presses the key). The term given to this phenomena is called SWITCH (CONTACT) DEBOUNCE. If this is overlooked when program is written, an error can occur, or the program can produce more than one output pulse for a single key press. In order to avoid this, we can introduce a small delay when we detect the closing of a contact. This will ensure that the press of a key is interpreted as a single pulse. The debounce delay is produced in software and the
http://www.mikroelektronika.co.yu/english/books/6_04Poglavlje.htm (1 of 4) [30/12/2001 16:54:22]