EXERCISE 3: String Variables and ASCII Code EXERCISE OBJECTIVE When you have completed this exercise, you will be able to describe the use of string variable and ASCII code. You will use Flowcode and the FACET board to store sequences using the Keypad. EXERCISE DISCUSSION Using String Variables Strings are collections of Byte variables. A string can be used to store a sequence of numbers or a sequence of letters. When you declare a string of a certain length, you actually declare an array of Byte variables where the number of bytes corresponds to the string length. Every character of the string you declare is stored as an 8 bit value in a separate byte in the RAM memory. For example, if you declare this: text[4], then the text in the string variable 'text' can be 4 characters long. In fact, this string variable could be used to store the word 'JOHN' in. If you don't declare a size next to the name of your string variable, Flowcode will set it to the default size of 20. Be aware of the maximum of 368 8-bit variables that can be stored in the PIC16F877A. Arrays You may have come across Array variables before. In Flowcode 3 Strings and Arrays are equivalent. Arrays are 2 or more variables with the same name and a different 'member number' between square braces. For example, if you declare a string as: MY_ARRAY[8], then 8 different 8-bit variables are declared at once. They can all be addressed separately by adding the correct member number between square braces: in this case MY_ARRAY[0] to MY_ARRAY[7]. Strings / Arrays are used in cases where a number of similar variables are needed. 200 FACET by Lab-Volt
Decisions, Macros, and String Variables ASCII Where strings are used to store a sequence of letters they will be represented in 'ASCII' format. ASCII stands for American Standard Code for Information Interchange and it is an internationally recognized standard for using numbers to represent letters. Although each letter in the string is represented by an 8 bit byte, in fact only 7 of the bits are used with the spare bit left as 0. You can look up the corresponding numbers for each character in an ASCII table below. The following tableshows the ASCII characters or symbols and their binary, decimal and Hex equivalents. FACET by Lab-Volt 201
EEPROM This is a convenient point to introduce another facility that the PICmicro can offer you: Electrically Erasable Programmable Read Only Memory, or EEPROM. All PICmicro devices have Read Only Memory (ROM), Random Access Memory(RAM), and Electrically Erasable Programmable Read Only Memory (EEPROM). ROM is where your program is stored and can not be altered while the program runs. RAM is where your variables are stored and this can be written to and read from as your program runs, but the contents are lost as soon as the power is removed from the chip. EEPROM can be written to and read from as your program runs, but the contents are preserved when the power is removed. This means that the contents are available again the next time you use the device. EEPROM is useful in many situations where the settings a user makes need to be preserved for the next time the system is used. For example, remembering the frequency a television channel is tuned to. EXERCISE QUESTIONS Collections of Byte variables that can be used to store a sequence of numbers or letters are a. ASCII code. b. Arrays. c. Strings. Two or more variables with the same name and a different "member number" between square braces are a. ASCII code. b. Arrays. c. Strings. In Flowcode, Strings and Arrays are equivalent. a. True b. False EXERCISE PROCEDURE Set up the equipment The following is the equipment you will need for this exercise: Multiprogrammer Hardware: Setting: PICmicro Device Toggle Switch (Fast/Slow) Toggle Switch (RC/XTAL) XTAL PORTA PORTB PORTC PORTD PORTE 16F877A N/A XTAL 19,660,800 Hz N/A LCD Block N/A Keypad N/A 202 FACET by Lab-Volt
Decisions, Macros, and String Variables Exploring the keypad In this section we will take a look at how the keypad works. Using the keypad to enter only a single digit is easy enough, but using it to enter numbers larger than 10 introduces some complexities. First the basics though: 1. Start a new flowchart using a 16F877A device. 2. Onto your flowchart put a keypad component and an LCD display. Connect the keypad to port D and the display to port B. 3. Select EDIT...VARIABLES from the menu and assign a new Byte variable 'key'. 4. Assemble the following flowchart: FACET by Lab-Volt 203
In top to bottom icon order this is explained as follows: start the LCD display, an endless loop, using the Cursor 0.0 macro set the LCD cursor to 0,0 (top left), call the GetKeypadNumber macro and put the result in the variable 'key'. 204 FACET by Lab-Volt
Decisions, Macros, and String Variables Last display variable 'key' on the LCD using the LCD PrintNumber macro. 5. Add comments to each Icon as shown in the graphic of the program. 6. Run this program in the simulator, then save it as Ex 3 Keypad with your initials to your folder before running it on the FACET Board. When you run the program you will see that, to start with, the LCD displays the number 255. This is what the keypad returns when there is no button pressed. When you press a key, the display will print the number of the key pressed. However, you will notice that it does not erase all of the old number. To modify the program, enter a new LCD Component macro with the function Clear after the cursor 0,0 macro and you will see only the number pressed. FACET by Lab-Volt 205
On the keypad there are the numbers 0 to 9 as well as # and * keys. These are allocated the numbers 10 and 11. You can use the and * keys as control characters to allow humans to indicate that the number is complete a bit like the Enter key on your computer keyboard. 7. Save this program as Ex 3.1 Keypad with your initials to your folder. Run this program first in the simulator, then on the FACET Board. NOTE: When you run this program using the simulator, the numbers will be flashing. This is due to the refresh rate of the monitor, the clock speed, and the program size as the program clears the numbers through each loop. This effect will not be as bad when you download the program to the FACET Board. Seeing ASCII 1. In your previous program, alter the Keypad Component macro to use the GetKeypadASCII macro rather than the GetKeypadNumber macro. This will put an ASCII number into the variable 'key'. Save this as Ex 3.2 Keypad with your initials. 2. Run your program again. You will notice that the ASCII for the key '1' is decimal 49, '2' is 50 etc. No key pressed still returns value 255. 3. In the LCD Component macro, alter the macro PrintNumber to PrintASCII. Save this as Ex 3.3 Keypad with your initials. 4. Run the program again. You will see that the display once again displays the numbers '1' to '9', '*' and '#'. The no key pressed displays a 'y' with an umlaut - the LCD characters set includes many characters other than just ASCII. So the interesting thing here is that the ASCII system allows you to store numbers as the ASCII equivalent and then display them as the number equivalent. Here is a standard we can use for storing both numbers and letters at the same time. What we need to do next is implement a system that allows us to gather a string of numbers from the keypad. Getting the phone number One good example of gathering long numbers is getting a multiple digit number. This could be a phone number, a password, or a number between 0 and 1023. 1. Start a new program and save it as Ex3 Getting the Phone Number with your initials. 2. Create the program shown next. 206 FACET by Lab-Volt
Decisions, Macros, and String Variables Page1 FACET by Lab-Volt 207
Page2 208 FACET by Lab-Volt
Decisions, Macros, and String Variables Page3 3. The following is an explanation of this program: The first 4 Component macros set up the LCD display. Then there is a Calculation icon which sets up the variables used in the program. The variable 'KEY' is used to store the key pressed in ASCII. The string PHONE_NO is used to store the numbers - a maximum of 11 digits. Next there is a Connection point A, followed by the Keypad macro that assigns the variable 'KEY' to the keypad input. The first decision box detects the condition that no key is pressed. This goes to a point at the end of the program where the variable 'OLD_KEY' is assigned to the input. 'OLD_KEY' is used as a flag to allow us to monitor whether the input has changed. This helps de-bounce the input so that we do not get, say, 6 digit 1's when we press the '1' key. The next decision box detects if the new input is the same as the old input (KEY=OLD_KEY), and returns the program to the point A if the keypad number is the same. FACET by Lab-Volt 209
The next decision box looks for ASCII 42 - the '*' key. This is used to denote a cancel operation. If the '*' key is pressed the first byte of the array is set to 0 and the macro is sent to the end point. The next decision box looks for ASCII 35 - the '########' key. This is used to denote an Enter operation. If the '' key is pressed the last byte of the array is set to 0 and the program is routed to the end point. During the program we use the variable PTR to allow us to track which digit in the string is being entered. The string has 10 characters: 0 to 9. The variable PTR must be increased each time a digit is entered allowing each digit to be stored in a separate character of the string. The next decision box tests whether this is the first character or not. If it is the first character then the display is cleared. Next a Calculation icon declares: PHONE_NO = KEY. This assigns the current character of the string to be the value entered on the keypad. The next icon in the main loop then displays the key pressed on the LCD so that the user has visual confirmation that the system has detected the key press. This is followed by a Calculation icon where the value PTR is increased by 1. This means that the next character entered on the keypad will be stored in the subsequent character of the array. At the end of the main loop we store the key pressed in OLD_KEY as described above, then wait 100ms, and then loop back to the beginning of the macro. Try this program in simulation, then on the FACET Board. If your program does not work, see your instructor for a copy of a working program to compare. EEPROM With reference to the previous example: 1. Add an EEPROM component to the workspace. 2. Add a new Component macro to your program just after the LCD is updated with the key the user presses. In this Component macro use the EEPROM WriteEEPROM macro using the variables PTR as the memory location, and KEY as the data. You can see the window shown here. 210 FACET by Lab-Volt
Decisions, Macros, and String Variables 3. Simulate your program. If you watch the EEPROM Flowcode component then you should see that as you enter numbers they are stored in the EEPROM memory. 4. Save this new program and try it first in simulation, then on the Microcontroller System Development FACET Board. Reading data back from the EEPROM is very similar to writing data from it: you simply specify a variable where you want to place the data, specify the address you want to read it from. EXERCISE QUESTIONS 1. When no number on the keypad was pressed, the LCD displayed a. nothing. b. 000 c. 255 2. Without Strings and ASCII, the largest number you could display was a. 10 b. 11 c. 100 3. Using NUMARRAY[10] you can store a maximum of a. 11 digits b. 10 digits. c. 10 decimal places. 4. The Get a Phone Number program relies on a number of jump points and subroutines to provide the end result. a. True b. False FACET by Lab-Volt 211
EXERCISE CONCLUSION Strings are collections of Byte variables. A string can be used to store a sequence of numbers or a sequence of letters. Every character of the string is stored as an 8 bit value in a separate byte in the RAM memory. Where strings are used to store a sequence of letters, the letters will be represented in ASCII format. EEPROM memory is useful where settings need to be preserved for the next time a system is used. REVIEW QUESTIONS 1. Strings are: a. collections of Byte variables. b. different from an array in Flowcode. c. used to store only sequences of numbers. d. All of the above are correct. 2. There is a limit to the number of 8-bit variables that can be stored in the PIC16F877A. a. True b. False 3. Where strings are used to store a sequence of letters, they are represented in format. a. ASCE b. ASCI c. ASCII d. None of the above is correct. 4. can be written to and read from as the program runs, and the contents are preserved when the power is removed. a. RAM b. ROM c. EPROM d. EEPROM 5. To read data back from the EEPROM, a. specify a variable where you want to place the data. b. specify the address you want to read from. c. it is not possible to read data back from the EEPROM. d. Both a and b are required. CMS AVAILABLE FAULTS AVAILABLE 212 FACET by Lab-Volt