Here is the solution to my problem.
I'm using a Chinese Mega1280 Arduino clone to work like an oscilloscope with my LC7981 based 240x128 display. I have modified the u8glib entry for the U8GLIB_LC7981_160X80 by changing the width and height in u8g_dev_lc7981_160x80.c as follows:
#define WIDTH 240 //this was 160
#define HEIGHT 128 //and this was 80
With those two changes my Hitachi SP14N003 works perfectly.
The following code reads analog(0) 240 times and displays it as an oscilloscope would show it (or if you dig in there you'll find an alternative bar-chart display). The top three text lines can display info on the last set of 240 readings. Try it - you'll see what I mean.
#include "U8glib.h"
U8GLIB_LC7981_160X80 u8g(26, 27, 28, 29, 22, 23, 24, 25, 34, 30, 31, 33, 32); // 8Bit Com: D0..D7: 8,9,10,11,4,5,6,7 en=34, cs=30 ,di=31,rw=33, reset = 32
/*
N.B. The u8glib I'm using has been modified to change the screen
width and height of the LC7981_160X80 panel to 240x128. My panel
is the Hitachi SP14N003. The pinouts in the above constructor just
reflect the way I connected it up to my Chinese Mega1280 Arduino
clone. If you wire it up differently, you will need to change the
above to match your wiring.
*/
int analogIn0[241]; //An array to hold the analog readings.
int amplitudeScaled[241]; //Another array to hold the scaled version of the above array.
int sampleNum; //The index for the above arrays
long cumulator; //Used in calculating the average value of analog readings
int averageVal; //integer version of the average reading value
String averageValStr; //receives the string version of the calculated average analog readings
int maxSampNumber; //The index of the first occurrence of the highest reading in the arrays
int minSampNumber; //The index of the last occurrence of the lowest reading in the arrays
int maxim; //The highest reading found in the analog reading array
int minim; //The lowest reading found in the analog reading array
String highestValStr; //The string version of maxim
String lowestValStr; //The string version of minim
String maxSampNumStr; //The string version of the maxSampNumber index
String minSampNumStr; //The string version of the minSampNumber index
int height; //the on-screen height of the displayed reading in pixels
int voltage; //holds the calculated average voltage in mV
String voltStr; //The string version of voltage
void setup(void) {
u8g.setFont(u8g_font_6x10); //This size works well - it allows three lines of legible text above the graph.
u8g.setRot180(); //Because my screen works better upside-down
u8g.setColorIndex(1); // Mono. Set to 0 before a write if you want to clear a pixel
}
void draw(void) {
for (sampleNum=0; sampleNum <= 239; sampleNum++)
{
amplitudeScaled[sampleNum] = analogIn0[sampleNum]/10; //This just divides the 0 to 1023 readings by 10 so it will fit in the bottom 103 pixels of the display
//amplitudeScaled[sampleNum] = analogIn0[sampleNum]- averageVal + 60; //Rough and ready way to zoom in - needs work
height = amplitudeScaled[sampleNum]; //not sure if I need this
//u8g.drawVLine(sampleNum, 127-height, height); //BarChart - slower but prettier?
u8g.drawPixel(sampleNum, 127-amplitudeScaled[sampleNum]); //Oscilloscope trace - quicker but basic
}
//The following lines just print some info on the array data
u8g.setPrintPos(0,7);
u8g.print( "Av'ge reading = " + averageValStr + " (= " + voltStr +"mV)");
u8g.setPrintPos(0,15);
u8g.print( "Max reading = " + highestValStr);
u8g.setPrintPos(121,15);
u8g.print( "first @ sample # " + maxSampNumStr);
u8g.setPrintPos(0,23);
u8g.print( "Min reading = " + lowestValStr);
u8g.setPrintPos(127,23);
u8g.print( "last @ sample # " + minSampNumStr);
}
void buildArray(void) {
cumulator=0;
maxim=0;
minim=1023;
for (sampleNum=0; sampleNum <= 239; sampleNum++)
{
analogIn0[sampleNum]=analogRead(A0);
cumulator = cumulator + analogIn0[sampleNum];
if (analogIn0[sampleNum] > maxim) { //looking for the first highest instance in the array
maxim=analogIn0[sampleNum]; //sets 'maxim' to the highest value found in the array
maxSampNumber=sampleNum; //saves the index for the first occurrence of it
}
if (analogIn0[sampleNum] <= minim) { //looking for the last lowest instance in the array
minim=analogIn0[sampleNum]; //sets minim to the lowest value found in the array
minSampNumber=sampleNum; //saves the index for the last occurrence of it.
}
}
averageVal = cumulator/240;
averageValStr = String(averageVal);
voltage = cumulator*1000/49104; //mV actually, not V. Avoids problems converting floats to strings.
voltStr = String(voltage); // again, it is mV.
highestValStr = String(maxim); // The next few lines just convert integers to strings ready for display
maxSampNumStr = String(maxSampNumber);
lowestValStr = String(minim);
minSampNumStr = String(minSampNumber);
}
void loop(void) {
// picture loop
u8g.firstPage(); // See the u8glib documentation
buildArray(); // Fill the array with analog readings and calculate some statistics etc.
do {
draw(); // Draw the screen
}
while( u8g.nextPage() ); // See the u8glib documentation
// rebuild the picture after some delay - useful in debugging
//delay(3000);
}
This works! I'm getting nearly 2 fps which is enough for my ultimate purpose (watch this space).
Enjoy!
Andy.