ВУЗ: Не указан
Категория: Не указан
Дисциплина: Не указана
Добавлен: 14.06.2025
Просмотров: 984
Скачиваний: 0
Chapter 6 - Samples
1 Character
Writing data to the LCD is done in several steps:
Set R/W bit to low
Set RS bit to logic 0 or 1 (instruction or character)
Set data to data lines (if it is writing)
Set E line to high
Set E line to low
Read data from data lines (if it is reading)
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.
Chapter 6 - Samples
One way is to check the BUSY bit found on data line D7. This is not the best method because 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'.
Chapter 6 - Samples
Connecting an LCD display to a microcontroller
File LCD.inc contains a group of macros for use when working with LCD displays.
Chapter 6 - Samples
Chapter 6 - Samples
Chapter 6 - Samples
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
Chapter 6 - Samples
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.
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.
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.
Chapter 6 - Samples
Chapter 6 - Samples
Previous page |
Table of contents |
Chapter overview |
webmaster.
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.
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.
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
Chapter 6 - Samples
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 result is 0, and for 5V it is 4095.
Chapter 6 - Samples
Chapter 6 - Samples
Previous page |
Table of contents |
Chapter overview |
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. Free line is defined as the status of logic one. Start of transmission - Start Bit, has the status of logic zero. The data bits follow the start bit (the first bit is the low significant bit), and after the bits we place the Stop Bit of logic one. The duration of the stop bit 'T' depends on the speed of transmission and is adjusted according to the needs of the transmission. For the transmission speed of 9600 baud, T is 104 uS.
1.CD (Carrier Detect)
2.RXD (Receive Data)
3.TXD (Transmit Data)
4.DTR (Data terminal Ready)
5.GND (Ground)
6.DSR (Data Set Ready)
7.RTS (Request To Send)
8.CTS (Clear To Send)
9.RI (Ring Indicator)
Pin designations on RS232 connector
In order to connect a microcontroller to a serial port on a PC computer, we need to adjust the level of the signals so communicating can take place. The signal level on a PC is -10V for logic zero, and +10V for logic one. Since the signal level on the microcontroller is +5V for logic one,
Chapter 6 - Samples
and 0V for logic zero, we need an intermediary stage that will convert the levels. One chip specially designed for this task is MAX232. This chip receives signals from -10 to +10V and converts them into 0 and 5V.
The circuit for this interface is shown in the diagram below:
Connecting a microcontroller to a PC via a MAX232 line interface chip.
File RS232.inc contains a group of macros used for serial communication.
Chapter 6 - Samples
Chapter 6 - Samples
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. After resetting a microcontroller the program sends a greeting message to PC computer: $ PIV16F84 on line $, and is ready to receive data from RX line.
We can send and receive data from PC computer from some communication program. When microcontroller receives data, it will send a message: Character received from PIC16F84: x, thus confirming that reception was successful.
Main program: