Translate

Monday, July 13, 2015

Teensy rev.3.1 and KS0108 Graphic LCD library

Giovanni Carrera 13/07/2015

This very interesting library, is still incompatible with the last version of Teensy.  The causes are at least two: the data ports of the MK20DX256 processor on Teensy are 32 bit against the 8 of ATmega328 on Arduino Uno, the second is the mapping used by the add-on program Teensyduino for the Arduino Ide, in order to make the compatibility with Arduino ONE, that has not a complete 8 bit I/O port available. For this reason the KS0108 library divides the GLCD data bus of eight bits in two nibbles: PortB (0:3) and PortD(4:7). PortC(0:4) is used for the control pins. The following table, shows the Arduino pins used for GLCD interfacing.

Arduino to GLCD wiring table
Arduino
 pin
GLCD signal
ATmega328
signal
8
DB0
PB0
9
DB1
PB1
10
DB2
PB2
11
DB3
PB3
4
DB4
PD4
5
DB5
PD5
6
DB6
PD6
7
DB7
PD7
A0
CS1
PC0
A1
CS2
PC1
A2
R/W
PC2
A3
D/I
PC3
A4
E
PC4
The use of two nibbles also causes a greater transfer time.
The teensyduino tries to make compatible Teensy with the Arduino I /O pins, but the hardware is completely different, since there is no  8-bit wise port. For this reason the KS0108 library is not compatible with Teensy 3.1.
Before starting to modify this library, I tried to see how the pin are organized with respect to the CPU port. The following table shows the CPU bit name and the corresponding Teensy pin name, that try to follow the Arduino Uno names. But the port bits are very different.
CPU
Teensy
Other
signal
 name
function
DAC0
DAC/A14

PTA12
3
CAN TX
PTA13
4
CAN RX
PTB0
16 /A2
SCL0/Touch
PTB1
17 /A3
SDA0/Touch
PTB16
0
RX1/Touch
PTB17
1
TX1/Touch
PTB2
19 /A5
SCL0/Touch
PTB3
18 /A4
SDA0/Touch
PTC0
15 /A1
Touch/CS
PTC1
22 /A8
Touch/CS/PWM
PTC2
23 /A9
Touch/CS/PWM
PTC3
9
RX2/CS/PWM
PTC4
10
TX2/CS/PWM
PTC5
13 (Led)
SCK
PTC6
11
DOUT
PTC7
12
DIN
PTD0
2
CS
PTD1
14 /A0
SCK
PTD2
7
RX3/DOUT
PTD3
8
TX3/DIN
PTD4
6
CS/PWM
PTD5
20 /A6
CS/PWM
PTD6
21 /A7
RX1/CS/PWM
PTD7
5
TX1/PWM





























In order to overcome this problematic, I observed that there were two complete 8 bit port available, so I used the PORTD for data bus of the GLCD display, and I modified the library. In addition to the increased CPU speed, now the transfer is done with a single instruction.
The following table shows the pin assignment that have decided for the wiring with the display.
CPU
Teensy
Used by GLCD
signal
 name
PTA13
4
GLCD – R/W
PTB1
17 /A3
GLCD – E
PTB2
19 /A5
GLCD – CS1
PTB3
18 /A4
GLCD – CS2
PTC2
23 /A9
GLCD – /RST
PTC3
9
GLCD – D/I
PTD0
2
GLCD – DB0
PTD1
14 /A0
GLCD – DB1
PTD2
7
GLCD – DB2
PTD3
8
GLCD – DB3
PTD4
6
GLCD – DB4
PTD5
20 /A6
GLCD – DB5
PTD6
21 /A7
GLCD – DB6
PTD7
5
GLCD – DB7
As you can see, the order of the pins (2,14,7,8,6,20,21,5) does not respect one of the bits of the CPU portD.
After some test on I/O portD with 8 LEDs connected to port, I noted that they don’t respond to the logic of my program but they comply with the scrambled order of the table.
After an internet research, I found a good solution reading the hints of immortalSpirit (1). So I modify the I/O initialization in the library in the file ks0108_Teensy.h and in the main program ks0108.cpp, where I changed almost completely the function WriteData.
The system run now the graphic test GLCDexample.pde, as we can see in the following figure.

I have designed and made a very flexible project; in addition to the graphic display (a Winstar WG12864A, with RGB backlit), I added a SD card module and a real time clock, mounting a slim 32.768kHz quartz. On the top we can see a battery power bank that supplies the system for hours. An acrylic front panel preserves the GLCD .. and also creates the bad reflection on the photo.
With few changes I also ran on my Teensy GLCD, the program arduino-oscillo.pde of Noriaki Mitsunaga (3), an interesting oscilloscope which uses the same library and display. In the following figure the photo taken during the test.
References
  1. “Tutorial on digital I/O, ATMega PIN/PORT/DDR D/B registers vs. ARM GPIO_PDIR / _PDOR” - immortalSpirit - Jan 2013.
  2. "K20 Sub-Family Reference Manual”- Freescale - Document Number: K20P64M72SF1RM, Rev. 1.1, Dec 2012.
  3. “Arduino Oscilloscope”, Noriaki Mitsunaga, http://n.mtng.org/ele/arduino/oscillo.html



1 comment:

  1. It is really a helpful blog to find some different source to add my knowledge. I came into aware of new professional blog and I am impressed with suggestions of author. 5 inch tft lcd display

    ReplyDelete