Use the print command to write out text. This can be useful when you want the microcontroller to display some information, or if you want to know what the mpy program is doing.
There are two different print commands. They are:
- print( value1, value2, … ) prints onto the computer mpyEditor window
- lcd_print( value1, value2, … ) prints onto an LCD display connected to the microcontroller
The print command uses the Launchpad pin P1_1 to send text back to the computer, and it only works when using the Launchpad board. You must make sure you do not use pin P1_1 for anything else, pin P1_1 is connected through the Launchpad USB interface so that it can send text back to the computer which is displayed on the mpyEditor window.
The print command prints in Green text onto the mpy window at the bottom of the mpyEditor. To see the print messages the Launchpad board must be connected to the computer, and the Launchpad driver must be installed (see here for instructions).
When using the print command remember to press the Launchpad RESET button in order to see the first lines. The mpyEditor does not capture the first output after the Launchpad has been programmed, therefore to see the first few lines you must reset the Launchpad so it can capture all the output lines.
Both print commands use the same format and can print out multiple values on the same line. Each value can be a variable or a string. String values must be entered as quoted strings, using a single quotes eg. ‘hello world’ or use double quotes “hello world”
x = 3 print( 'Hello World, this is the value of x', x )
Which prints out the following onto the mpyEditor
Hello World, this is the value x 3
Normally a variable is printed out as a decimal number, which is in the range –32768 to +32767.
However if the variable name ends with _hex eg. data_hex then the value will be printed out as a 4 character hexadecimal value, eg. B2D7 Also if the variable name ends with _bin eg. register_bin then the value will be printed out as a 16 character binary number eg. 0110011011100011 The binary and hexadecimal values can be more useful when you are dealing with digital type values.
data_hex = 255 print( 'Value of data_hex is ', data_hex )
Value of data_hex is 00FF
Normally each print command will be printed out on a new line on the mpyEditor window. However if the print command ends with a , character and there is no final value parameter then this will prevent the new line from being printed, and the next print command will be printed on the same line as the first print command.
print('Hello ' , ) print('world')
If a text string contains the new line character which is shown as \n, this will cause the text that follows to be printed on a new line.
The lcd_print function is used to write onto an LCD connected to the microcontroller. To use the lcd_print command the Launchpad does not need to be connected to the computer, and can be used without the Launchpad board.
The lcd_print command uses the same format as the print command described above, and it can also print multiple values on a line.
The LCD display supported is the Hitachi HD44780 type. This is a commonly available LCD. It has a 16 pin connector and has two lines of 16 characters. Each new lcd_print command will print onto the first line of the display. The second line is used only when the line extends more than 16 characters, or if the line contains a new line ‘\n’ character then the text which follows will be be displayed on the second line of the display.
With mpy there are two ways to connect the LCD to the microcontroller. See here for detailed instructions. The first way is a direct connection which uses 6 wires, 6 micrcontroller pins are connected to the LCD. The second way uses just 2 wires from the microcontroller, but requires some extra components to connect to the LCD. The 6 wire method uses most of the available pins on the microcontroller and doesn’t leave many pins left over for doing other things. The 2 wire method is much more convenient from a pin point of view but is more complicated as it requires some extra extra components and wiring.
Depending on which method is used you need to use either the lcd_enable( ) or the lcd2w_enable( ) command to setup which pins are used to control the LCD.
- lcd_enable( DB7, DB6, DB5, DB4, EN, RS ) setup pins to be used with lcd_print 6 wire interface
- lcd2w_enable( DATA, CLOCK ) setup pins to be used with lcd_print 2 wire interface
using print commands for debug
The print and lcd_print commands are very useful for debugging mpy programs. They can be a very useful aid when trying to figure out if your mpy program is working or not. You can insert print commands at various points in your code to see if your code gets to those points, or, you can use a print command to tell you the value of a variable. The print command is your friend, use it frequently to get your program working quickly.