The fixed messages are generated from program data tables using ADDWF PCL to jump into the table using the number in W, and RETLW to return the ASCII codes.. To fetch the required code,
Trang 1The fixed messages are generated from program data tables using ADDWF PCL to jump into the table using the number in W, and RETLW to return the ASCII codes The assembler generates the ASCII code in response to the sin-gle quotes which enclose the character To fetch the required code, the table pointer is added to the program counter at the top of the table The table pointer
is also checked each time to see the end of the table has been reached The output variable is just a count from 0 to 9, but to obtain the correspon-ding ASCII code, 30h must be added, because the ASCII for 0 is 30h, for 1 is 31h and so on The ASCII code table is shown in Table 4.4 A 250 ms delay is executed between each output to make the count visible
; Output variable count to display (0-9) endlessly -
OutVar CLRF Var ; Clear variable number MOVLW 0X30 ; Load offset to be added ADDWF Var ; to make ASCII code (30-39)
Next MOVF Var,W ; Load the code BSF Select,RS ; Select data mode CALL Send ; and send code
MOVLW 0xCB ; code to move cursor back BCF Select,RS ; Select command mode CALL Send ; and send code MOVLW D'250' ; Load count to wait 250ms CALL Xms ; so numbers are visible
MOVF Var,W ; Load number SUBLW 0x3A ; Check for last (10=A) BTFSS STATUS,Z ; and skip if last GOTO Next ; or do next number GOTO OutVar ; Repeat from number Z
; MAIN PROGRAM ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Start CALL Init ; Initialise the display CALL OutMes ; Display fixed characters GOTO OutVar ; Display an endless count
Mess1 MOVF Point,W ; and load it CALL Line1 ; Get ASCII code from table CALL Send ; and do it
INCF Point ; point to next character MOVF Point,W ; and load the pointer SUBLW D'16' ; check for last table item BTFSS STATUS,Z ; and finish if 16 done GOTO Mess1 ; Output character code
MOVLW 0xC0 ; Move cursor to line 2 BCF Select,RS ; Select command mode CALL Send ; and send code CLRF Point ; Reset table pointer Mess2 MOVF Point,W ; and load it CALL Line2 ; Get fixed character BSF Select,RS ; Select data mode CALL Send ; and send code INCF Point ; next character MOVF Point,W ; Reload pointer SUBLW D'11' ; and check for last BTFSS STATUS,Z ; Skip if last GOTO Mess2 ; or send next
Program 4.4 Continued
Trang 2Project LCD
Program to demonstrate fixed and variable output of alphanumeric characters
to 16x2 LCD (simulation only)
HARDWARE
ISIS simulation file LCD.DSN MCU 16F877A
Clock = XT 4MHz LCD Data = RD4 – RD7
RS = RD1
E = RD2
RW = 0
FIRMWARE Initialise
LCD output = Port D Wait 100ms for LCD to start LCD: 4-bit data, 2 lines, auto cursor Reset LCD
Display message line 1
Reset table pointer REPEAT
Get next code Send ASCII code UNTIL 16 characters done
Display message line 2
Position cursor Reset table pointer REPEAT
Get next code
Send ASCII code
UNTIL 11 characters done
Display incrementing count
REPEAT Set Count = 0 LOOP
Calculate ASCII
Send ASCII code
Increment Count Reset cursor Delay 250ms UNTIL Count = 9 ALWAYS
Send ASCII code
Mask low nibble Output high nibble Pulse E
Wait 1ms Swap nibbles Output low nibble Pulse E
Wait 1ms
Return
Figure 4.8 LCD program outline
Trang 3The primary set of characters consists of upper and lower case letters, num-bers and most of the other characters found on a standard keyboard This comes to 95 characters, represented by codes from 3210 to 12610 inclusive With an 8-bit code, 256 characters are possible, so the remaining ones are used for special language character sets, for example accented characters, Greek, Arabic or Chinese The character set can be programmed into the display RAM
as required, or ordered as a ROM implementation Further details can be ob-tained from the display data sheet
SUMMARY 4
• Switch inputs generally use pull-ups and debouncing
• The hardware timer operates independently of the program
• The keypad is a set of switches scanned in rows and columns
• 7-segment LEDs are the simplest form of alphanumeric display
• The alphanumeric LCD receives ASCII codes for display in rows
• The LCD also needs command codes for control operations
High Bit
Table 4.4 ASCII character codes
Trang 4ASSESSMENT 4
1 Explain why a pull-up resistor needed with a switch input (3)
3 Briefly explain why hardware timers are useful in MCUs (3)
4 Explain how a pre-scaler extends the timer period (3)
5 Explain why the plain 7-segment LED display needs a code table (3)
6 Explain why a BCD encoded display does not need a code table (3)
7 Briefly explain the scanning process used to read a keypad button (3)
8 Explain briefly how the LM016L LCD can be driven with only six outputs (3)
9 State the function of inputs RS and E in the LM016L LCD (3)
11 Draw a block diagram of the circuit with a keypad and 7-segment display,
indicating the main components and signals in the system (5)
12 Describe the software precautions needed to obtain correct operation
of the LCD in 4-bit mode when the same port is used for control and
ASSIGNMENTS 4
4.1 Keypad Test
Run the keypad system simulation, KEYPAD.DSN Ensure that the correct display is obtained when the keypad is operated Represent the program using
a flowchart and pseudocode Explain the advantages of each Explain why switch debouncing is not necessary in this particular application
4.2 LCD Test
Run the LCD system simulation, LCD.DSN Ensure that correct operation is obtained; fixed messages should be followed by an incrementing count Enable debug mode by hitting pause instead of run, and select PIC CPU Source Code
in the debug menu; also select PIC CPU Registers Ensure that the debug win-dows are opened
Trang 5Single step in the source code window using the ‘step into’ button Follow through the initialisation until the delay routine is entered Why is the ‘step out’ button now useful? Start again using ‘step over’ – note the effect Why is
‘step over’ useful?
Single step through the output sequence, displaying the fixed characters one
at a time, then the count Note the codes output at Port D and check them against the ASCII table given
4.3 LCD Control
Construct a timing diagram for the output sequence obtained to the LCD in LCD.DSN for one complete character, by recording the changes at Port D, and referring to the simulation timer Show traces for all six outputs Obtain a tim-ing diagram ustim-ing the simulation graph feature, and check that this is consis-tent with your manually constructed version
Trang 6Data Processing
Most microcontroller programs need to process data using arithmetic or logi-cal operations The main types of data in general processor systems are num-bers and characters Characters are not too much of a problem as there are only
26 letters in the alphabet and 10 numerical characters Even allowing for upper and lower case, that is only 62 codes are required The basic ASCII character set thus only requires a 7-bit code, which also provides for most other key-board symbols (see previous chapter for table of ASCII codes) Use of the eighth bit allows special character sets to be added
Numerical data is a bit more of a problem, as an 8-bit code can only represent numbers 0–255; so some additional methods are needed to handle larger (and smaller) numbers, so that calculations can be performed with a useful degree of precision
Number Systems
Computers work with binary numbers, but humans prefer to work in decimal However, these number systems both follow the same basic rules:
1 Select a range of digit symbols to use (e.g 10 symbols in decimal)
2 Count from zero up to the maximum value in single digits (0–9 in decimal)
Trang 73 Increment the digit to the left of the first digit (to 1)
4 Reset the previous column to zero
5 Count up again in the units column to the maximum
6 Increment the next digit again
7 When each column is at the maximum value, increment the next and reset
8 Use more columns as necessary for larger numbers Hopefully we all know how to count in decimal (denary is the official name) The base of the number system is the number of digits used (10) Binary is base 2, hex base 16, octal base 8 Any base can be use in theory, but in prac-tice some are more useful than others
Historically, base 12 has been used extensively (hours, minutes, angles, old English money), and is useful because it can be divided by 2, 3, 4 and 6 But this is not a true number system because there are no discrete symbols for 10 and 11 Similarly, BCD is not a proper number system (see later for details) because its binary count stops at 9 Hexadecimal is a true number system because it uses discrete symbols for 10(A) – 15(F) It is useful because it provides a compact way of representing binary, the native number system of all digital computers and controllers
Denary
Zero was a very important invention, because number systems depend on it Another important idea is column weighting, and you can see the significance
by simply analysing how the denary system works (Table 5.1)
The number is calculated as follows:
Total value (column weighting digit value) [ sum of] This may seem to be obvious, but it is important to state it clearly, as we will be implementing numerical processing which uses this structure Denary
is also the reference system, that is other systems are described by reference
to it
Total value 7000 300 90 5 7395
Table 5.1 Structure of a denary number
Trang 8Computers work in binary because it was found to be the most efficient and precise way to represent numbers electronically Before the necessary digital hard-ware was developed, computers were used in which analogue voltages represented denary values Linear amplifier (op-amp) circuits provided the processing, and these are well suited to mathematical processes such as integration and differen-tiation, but the accuracy was limited by the signal quality On the other hand, the accuracy of the digital computer can be increased (in theory) to any required degree of precision by simply increasing the number of binary digits Processing
in 32 bits (now common), for example, provides a potential degree of precision of
1 part in 232 (4294967296), and even a modest 8 bits gives an accuracy of 1 part
in 256, better than 0.5% error at full scale (Table 5.2)
The result of the analysis of the structure of a typical binary number shows that the decimal value can be worked out as follows:
Total value Σ(column weight of non-zero bits) Notice the pattern of the column weightings – it is the base number to the power 0, 1, 2, 3, etc This is the meaning of the base number
The maximum number for a given number of bits is obtained when all the bits are 1 In a 8-bit number, the maximum value is 111111112 25510(the subscript indicates the number base) This is calculated as 281, that is, two to the power of the base minus 1
The number of different codes, including zero, is 28 256 This is impor-tant in defining memory capacity, where an extra bit on the address doubles the memory Important reference points are 210 1024 bytes (1 kb), 216(64 kb),
220(1 Mb) and 230(1 Gb) The highest address in a 1 kb memory, for example,
is 1023
Hexadecimal
The same principle is applied to the number system with base 16 The problem here was that extra numerical symbols were required, so symbols which are
Total value 8 0 0 1 9
Table 5.2 Structure of a binary number
Trang 9normally used as letters were simply adopted as numbers: A16 1010 to
F16 1510(Table 5.3)
Note the pattern in the progression of the hex column weight – the weighting is 016, 1016, 10016, 100016, etc This will apply to all number systems – the column weight is a progression of 0n, 10n, 100n, 1000n, etc
where n is the base It can also be seen that the conversion from hex to denary
is not simple, but the conversion from hex to binary is, which is why hex is useful
Other Number Systems
Numbers can be represented using any base by following the rules outlined above Octal (base 8) is sometimes used in computing, but will not be consid-ered here Numbers with a base greater than 16 would need additional symbols, so, in theory, one could carry on using letters up to Z
Binary Coded Decimal
As mentioned previously, BCD is not a proper number system, but it is useful as an intermediate system between binary and decimal In BCD, the numbers 0–9 are represented by their binary equivalent, and stored as 4-bit numbers These may then be converted into ASCII code to send to a display (e.g the alphanumeric LCD seen later) or into pure binary for processing Alternatively, the data can be processed in BCD by using appropriate algo-rithms (Table 5.4)
By studying these examples, general rules for BCD processing can
be deduced When adding two single digit numbers (Table 5.5), the first (Num1) incremented and the second (Num2) decremented If the Num2 reaches zero, Num1 can be stored as the single digit result However, if Num1 reaches 10, Num2 is stored as the least significant digit of the result, and the MSD set to 1
This type of process can be devised and implemented for all arithmetic operations The advantage is that the results can be input from a keyboard
as BCD, and output as ASCII without conversion However, if the arithmetic is
Column weight 100016 4096 16 3 10016 256 16 2 1016 16 16 1 116 1 16 0
Total value 36864 2816 0 15 = 39695
Table 5.3 Structure of a hex number
Trang 10more complex, conversion into pure binary or floating point (FP) format may
be necessary
Floating Point Formats
Numbers have a limited range in the context of computer storage and process-ing, depending on how they are represented and stored The number of bytes which will be allocated to store a number must be set up in advance of running
a program For example, a plain 16-bit binary number ranges from 0 to 65535 (2161), and requires 2 bytes oxf memory Since negative numbers are usually required, the MSB 1 may be used to indicate a negative integer, leaving only 15 bits for the number The range is then from 32767 to 32767
Table 5.4 BCD calculations
BCDADD
Process to add two single digit BCD numbers with one or two digit result
REPEAT
Table 5.5 Process for BCD add with carry