Файл: The PIC Microcontroller Book for beginning (Nebojsa Matic).pdf
ВУЗ: Не указан
Категория: Не указан
Дисциплина: Не указана
Добавлен: 15.06.2025
Просмотров: 1180
Скачиваний: 0
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. If it receives a character, it will write it on the display and move the cursor one space to the right. The Cursor marks the next location where a character will be written. When we want to write a string of characters, first we need to set up the starting address, and then send one character at a time. Characters that can be shown on the display are stored in data display (DD) RAM. The size of DDRAM is 80 bytes.
The LCD display also possesses 64 bytes of CharacterGenerator (CG) RAM. This memory is used for characters defined by the user. Data in CG RAM is represented as an 8- bit character bit-map.
Each character takes up 8 bytes of CG RAM, so the total number of characters, which the user can define is eight. In order to read in the character bit-map to the LCD display, we must first set the CG RAM address to starting point (usually 0), and then write data to the display. The definition of a 'special' character is given in the picture .
Before we access DD RAM after defining a special character, the program must set the DD RAM address. Writing and reading data from any LCD memory is done from the last address which was set up using set-address instruction. Once the address of DD RAM is set, a new written character will be displayed at the appropriate place on the screen.
Until now we discussed the operation of writing and reading to an LCD as if it were an ordinary memory. But this is not so. The LCD controller needs 40 to 120 microseconds (uS) for writing and reading. Other operations can take up to 5 mS. During that time, the microcontroller can not access the LCD, so a program needs to know when the LCD is busy. We can solve this in two ways.
One way is to check the BUSY bit found on data line D7. This is not the best method because
http://www.mikroelektronika.co.yu/english/product/books/PICbook/6_10Poglavlje.htm (2 of 11) [4/2/2003 16:19:08]
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. Thus we can reduce the total number of communication lines to seven. The wiring for connection via a 4-bit data bus is shown in the diagram below. In this example we use an LCD display with 2x16 characters, labelled LM16X212 by Japanese maker SHARP. The message 'character' is written in the first row: and two special characters '~' and '}' are displayed. In the second row we have produced the word 'mikroElektronika'.
Connecting an LCD display to a microcontroller
File LCD.inc contains a group of macros for use when working with LCD displays.
http://www.mikroelektronika.co.yu/english/product/books/PICbook/6_10Poglavlje.htm (3 of 11) [4/2/2003 16:19:08]
Chapter 6 - Samples
http://www.mikroelektronika.co.yu/english/product/books/PICbook/6_10Poglavlje.htm (4 of 11) [4/2/2003 16:19:08]
Chapter 6 - Samples
http://www.mikroelektronika.co.yu/english/product/books/PICbook/6_10Poglavlje.htm (5 of 11) [4/2/2003 16:19:08]
Chapter 6 - Samples
http://www.mikroelektronika.co.yu/english/product/books/PICbook/6_10Poglavlje.htm (6 of 11) [4/2/2003 16:19:08]
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 .3
LCDline line_num Set cursor to the beginning of 1st or 2nd row
Example: LCDline 2
When working with a microcontroller the numbers are presented in a binary form.
As such, they cannot be displayed on a display. That's why it is necessary to change the numbers from a binary system into a decimal system so they can be easily understood. Listings of two macros LCDval_08 and LCDval_16 are given below.
Macro LCDval_08 converts an eight-bit binary number into a decimal number from 0 to 255 and displays it on the LCD display. It is necessary to declare the following variables in the main program: TEMP1, TEMP2, LO, LO_TEMP, Bcheck. An eight-bit binary number is found in variable LO. When a macro was executed, the decimal equivalent of its number would be displayed on the LCD display. The leading zeros before the number will not be displayed.
http://www.mikroelektronika.co.yu/english/product/books/PICbook/6_10Poglavlje.htm (7 of 11) [4/2/2003 16:19:08]
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.yu/english/product/books/PICbook/6_10Poglavlje.htm (8 of 11) [4/2/2003 16:19:08]
Chapter 6 - Samples
http://www.mikroelektronika.co.yu/english/product/books/PICbook/6_10Poglavlje.htm (9 of 11) [4/2/2003 16:19:08]
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.yu/english/product/books/PICbook/6_10Poglavlje.htm (10 of 11) [4/2/2003 16:19:08]
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.htm (11 of 11) [4/2/2003 16:19:08]
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.
The answer is to take a number of digital lines and combine them so they can "read" an analogue value. An analogue value is any value between 0 and 1. You can also call it a "fractional value." All the above quantities must now be converted to a value between 0 and 1 so they can be fed into a computer.
This is the broad concept. It becomes a little more complex in application.
If we take 8 lines and arrange than so they accept binary values, the total count will be 256 (this is obtained by a count to 255 plus the value 0).
If we connect these 8 lines into a "black box," they will be called output lines and so we must provide a single input line. With this arrangement we can detect up to 255 increments between "0" and "1." This black box is called a CONVERTER and since we are converting from Analogue to Digital, the converter is called an A-to-D converter or ADC.
AD converters can be classified according to different parameters. The most important parameters are precision and mode of data transfer. As to precision, the range is: 8-bit, 10bit, 12-bit, 14-bit, 16-bit. Since 12-bit conversion is an industrial standard, the example we have provided below was done with a 12-bit ADC. The other important parameter is the way data is transferred to a microcontroller. It can be parallel or serial. Parallel transmission is faster. However, these converters are usually more expensive. Serial transmission is slower, but in terms of cost and fewer input lines to a microcontroller, it is the favourite for many applications. Analogue signals can sometimes go above the allowed input limit of an ADC. This may damage the converter. To protect the input, two diodes are connected as shown in the diagram. This will protect from voltages above 5V and below 0V.
In our example we used a LTC1286 12-bit ADC (Linear Technology). The converter is connected to the microcontroller via three lines: data, clock and CS (Chip Select). The CS line is used to select an input device as it is possible to connect other input devices (eg: input shift register, output shift register, serial ADC) to the same lines of the microcontroller.
The circuit below shows how to connect an ADC, reference and LCD display to a micro. The LCD display has been added to show the result of the AD conversion.
http://www.mikroelektronika.co.yu/english/product/books/PICbook/6_11Poglavlje.htm (1 of 6) [4/2/2003 16:19:11]
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.htm (2 of 6) [4/2/2003 16:19:11]
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.
The following example shows how macros are used in the program. The program reads the value from an ADC and displays it on the LCD display. The result is given in quantums. Eg: for 0V the
http://www.mikroelektronika.co.yu/english/product/books/PICbook/6_11Poglavlje.htm (3 of 6) [4/2/2003 16:19:11]
Chapter 6 - Samples
result is 0, and for 5V it is 4095.
http://www.mikroelektronika.co.yu/english/product/books/PICbook/6_11Poglavlje.htm (4 of 6) [4/2/2003 16:19:11]
Chapter 6 - Samples
http://www.mikroelektronika.co.yu/english/product/books/PICbook/6_11Poglavlje.htm (5 of 6) [4/2/2003 16:19:11]
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.htm (6 of 6) [4/2/2003 16:19:11]