Translate

Monday, July 27, 2015

How to modify analog output range of Arduino Due

Arduino Due does not have an analog output voltage from 0 V to Vref, but from 1/6 to 5/6 of the reference voltage, that is, 0.55 V and 2.75V with Vref = 3.3 V. This is also confirmed by the Atmel (see bibliography).
The output voltage range of the DAC is only 2.75-0.55  = 2.2 V, with a resolution of 2.2/4095 = 0.5372 mV.
A simple differential amplifier, realized with an op amp, is used to remove the 0.55 V offset and to amplify the output signal in order to reach the desired value. The following figure shows the scheme of this circuit:

To generate the voltage Vp = 0.55 V, that is, the minimum value of the DAC, is used the divider formed by R3, R4 and Rp. The trimmer pot Rp is used to adjust Vp in order to have an output value close to 0 V in correspondence with the number 0 sent to the DAC. With a single power supply you can never get exactly 0V but you approach some mV, about 8 in my prototype.
The differential amplifier has the following gain:
G = Vo/(Vi-Vp) = R2 / R1
Just change the gain, for a different output voltage, but less than 4 V,.
With a gain G = 3.3 /2.2 = 1.5, the output of this circuit has  the desired range from 0 to 3.3V.

It is recommended to use resistors with a tolerance of 1% or less, particularly for the four resistors of the amplifier. Don’t use operational amplifier as LM741, LM1458, TL081 and other that are not suitable for single-supply.


References

1)      “Atmel ARM Cortex-M3 Product Family (SAM3)”, Atmel application note 42187A−SAM−10/2013

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