Файл: The PIC Microcontroller Book for beginning (Nebojsa Matic).pdf
ВУЗ: Не указан
Категория: Не указан
Дисциплина: Не указана
Добавлен: 15.06.2025
Просмотров: 1181
Скачиваний: 0
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. By changing the prescaler we can change the length of the overflow interval of the counter TMR0.
http://www.mikroelektronika.co.yu/english/product/books/PICbook/6_02Poglavlje.htm (1 of 4) [4/2/2003 16:18:43]
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.
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 show a string 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 string.
http://www.mikroelektronika.co.yu/english/product/books/PICbook/6_02Poglavlje.htm (2 of 4) [4/2/2003 16:18:43]
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. Next we will have in the program counter an address of retlw instruction with the desired member of the string. When this instruction is executed, member of the string 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 string ends.
Macro PRINT has five arguments:
PRINT macro Addr, Start, End, Var, Out
Addr is an address where one or more strings (which follow one by one) begin. Start is an address of the first member of the string
End is an address where the string ends
Var is the variable which has a role of showing (pointing ) the members of the string
Out is an argument we use to send the address of existing subprograms assigned to output devices such as : LCD, RS-232, etc.
http://www.mikroelektronika.co.yu/english/product/books/PICbook/6_02Poglavlje.htm (3 of 4) [4/2/2003 16:18:43]
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.htm (4 of 4) [4/2/2003 16:18:43]
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'. 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 10mA (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 are 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 ground of the project (circuit). In order to identify each lead, the cathode is the shorter lead and the LED "bulb" 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 to 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. Maximum current flow trough LED was defined by manufacturer. 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.2v to 1.6v 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 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 anodes connected together to the positive supply. When a LOW output from the microcontroller is delivered to the cathode and resistor, the LED will illuminate.
http://www.mikroelektronika.co.yu/english/product/books/PICbook/6_03Poglavlje.htm (1 of 3) [4/2/2003 16:18:45]
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.htm (2 of 3) [4/2/2003 16:18:45]
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.htm (3 of 3) [4/2/2003 16:18:45]
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.
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 length of the delay depends on the key, and the purpose of the key. The problem can be partially solved by adding a capacitor across the key, but a well-designed program is a much-better answer. The program can be adjusted until false detection is completely eliminated.
In some case a simple delay will be adequate but if you want the program to be attending to a number of things at the same time, a simple delay will mean the processor is "doing-nothing" for a long period of time and may miss other inputs or be taken away from outputting to a display.
The solution is to have a program that looks for the press of a key and also the release of a key. The macro below can be used for keypress debounce.
http://www.mikroelektronika.co.yu/english/product/books/PICbook/6_04Poglavlje.htm (1 of 3) [4/2/2003 16:18:47]
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.
Delay is a number from 0 to 255, used to assign the time needed for key debounce detection - contact oscillation - to stop. It is calculated as TIME = Delay x 1ms.
Address is the address where the micro goes after a key is detected. The sub-routine at the address carries out the required instruction for the keypress.
Example 1: TESTER 0, PORTA, 3, .100, Tester1_above
Key-1 is connected to RA0 (the first output of port A) with a delay of 100 microseconds and a reaction to logic zero. Subprogram that processes key is found at address of label Tester1_above.
Example2: TESTER 0, PORTA, 2, .200, Tester2_below
Key-2 is connected to RA1 (the second output of port A) with 200 mS delay and a reaction to logic one. Subprogram that processes key is found at address of label Tester2_below.
The next example shows the use of macros in a program. TESTER.ASM turns LED on and off. The LED is connected to the seventh output of port B. Key-1 is used to turn LED on. Key-2 turns LED off.
http://www.mikroelektronika.co.yu/english/product/books/PICbook/6_04Poglavlje.htm (2 of 3) [4/2/2003 16:18:47]
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.htm (3 of 3) [4/2/2003 16:18:47]