MORON'S GUIDE TO THE BUTTERFLY JOYSTICK & LCD v1.1
TABLE OF CONTENTS:
THE BUTTERFLY JOYSTICK
THE LCD SCREEN
THE JOYSTICK TESTER PROGRAM
THE MAIN LOOP
THE MESSAGES DEFINED
TRANSFERING DATA TO OUR BUFFER
THE DISPLAY ROUTINE
THE LCD DATA REGISTERS
BIT MANIPULATION GYMNASTICS
INTIALIZING THE LCD MODULE
THE LOOKUP TABLE
A SOUND EFFECT AND A PAUSE
FINAL JOYSTICK TESTER PROGRAM LISTING
AN LCD SCROLLING PROGRAM
LCD SCROLLING PROGRAM LISTING
The joystick is a little switch that can be used for input on the Butterfly Boards. The Liquid Crystal Display (LCD) is used as output, to display up to six characters. We are going to use these two devices to create a joystick tester in assembly language that will display which position of the joystick is active at any time.
Then we make a few changes and re-use most of the same code to create a program that will scroll a long message across the small LCD screen.
THE BUTTERFLY JOYSTICK
The Joystick is a combination of five switches in one; one for each of four directions and a centre switch which is activated by pressing down in the middle position. A quick look at the schematics for the board shows that it is connected to input pins of both Port B and Port E.
MIDDLE SWITCH ____-____ PinB,4 UP SWITCH ____-____ PinB,6 DOWN SWITCH ____-____ PinB,7 LEFT SWITCH ____-____ PinE,2 RIGHT SWITCH ____-____ PinE,3
The joystick switches are pulled up by the pull-up resisters and are read as ones when not in use and are shorted to ground and read as zero when pressed.
+3VDC +3VDC | | Z Z Z Z OPEN Z CLOSED Z SWITCH: _ | SWITCH: | .--- ---+---- PINx .-------+---- PINx | | V V GROUND GROUND
Since the joystick is an input device we use the PINx command to read them and not the PORTn form. To catch all the possibilities we might have code that resembles the following:
SBIS PINB,4 ;JOYSTICK PRESS RJMP JOYMID SBIS PINB,6 ;JOYSTICK UP RJMP JOYUP SBIS PINB,7 ;JOYSTICK DOWN RJMP JOYDOWN SBIS PINE,2 ;JOYSTICK LEFT RJMP JOYLEFT SBIS PINE,3 ;JOYSTICK RIGHT RJMP JOYRIGHT
Note that while three inputs are to Port B pins, the Left and Right switches are connected to Port E pins. The Skip if Bit is Set command (SBIS) test the indicated pin and if it is still set (indicating not pressed) it will skip the RJMP command that follows it. When the associated pin line is pressed (reads zero) the program jumps to the correct routine.
THE LCD SCREEN
The LCD is created by long crystals mounted behind polarized glass. In their normal state they are transparent and the grey back of the LCD can be seen. When a voltage is applied, the crystals bend enough that light cannot be transmitted through the polarized glass, the associated segment then appears black to the viewer.
RELAXED VOLTAGE APPLIED POLARIZED GLASS: - - - - - - - - - - - - - - - - - - - - LIQUID CRYSTALS: | | | | | | | | ==> / / / / / / / / / / BACKGROUND: ----------------- ---------------------
The LCD characters are made from fourteen segments (a to n). To create a character we need to activate the segments that make up the character. For example To create the letter I we might activate segments j and n; and if you look for the letter I in the table below, you see that there is a one in the position for n and j, and the letter C uses segments d,e,f,a:
; mpnd legc jfhb k a <------> LCD SEGMENTS .DW 0b_0011_1001_1001_0001 ;B -----a----- .DW 0b_0001_0100_0100_0001 ;C | \ | / | .DW 0b_0011_0001_1001_0001 ;D f h j k b .DW 0b_0001_1110_0100_0001 ;E | \ | / | .DW 0b_0000_1110_0100_0001 ;F --g-- --l-- .DW 0b_0001_1101_0100_0001 ;G | / | \ | .DW 0b_0000_1111_0101_0000 ;H e p n m c .DW 0b_0010_0000_1000_0000 ;I | / | \ | .DW 0b_0001_0101_0001_0000 ;J -----d-----
The above is part of a look-up table we use to convert values and ASCII characters to LCD Segments. Later you can modify it to create your own character set.
The LCD segments are memory mapped to twenty memory locations LCDDR0 to LCDDR19 as shown below in a small subroutine that clears all the segments. The Y-Pointer is set to the first memory location LCDDR0, a zero is written to that location and the pointer is increased by one, and then next is cleared until we reach LCDDR19, at which point we stop:
;---------------------------; ; CLEAR ALL SEGMENTS ON LCD ; ;---------------------------; LCD_CLR: LDI YL,LOW(LCDDR0) CLR YH CLRLUPE: ST Y+,ZERO CPI YL,LCDDR18+1 BRNE CLRLUPE RET
The LCD Module is quite complex, but as long as we initialize and configure it correctly, all we need to do is convert numbers and ASCII characters to LCD segments, write them to the appropriate memory locations and they will display on the LCD. The LCD Module takes care of things such as duty cycle, frame rates, etc.
FOR THE REMAINING CHAPTERS I INVITE YOU TO DOWNLOAD THE FOLLOWING PDF FILE:
(You must login to see the Files)