Simple four channel signal plot Version 2.1
(c) 2006 by Malte Marwedel
Use this programs under the terms of the GPL version 2. See gpl-license.txt for
more details.

This program has four (five) different operating modes.
Mode 1: The AVR reads four channels digitally and transmits them to the PC
        program. The PC program provides some features like trigger, zoom
        e.c.t This mode was the prime idea why I started programming.
Mode 2: Provides similar functions as Mode 1 but uses an A/D converter, the
        disadvantage is the much lower sampling rate of only 3000 samples/s
        per channel. The resolution is 7 Bit.
Mode 3: Provides four voltage meters, the resolution is 12 Bit generated by
        oversampling of the 10Bit A/D converter.
Mode 4: Provides a signal generator. You have to connect a 6 Bit R2R resistor
        network as D/A converter. You can produce the waveform with your
        mouse. If you port this program to an AVR with a full PORTB
        (like the ATMEGA16), you could easily use a 8 instead of 6 Bit D/A
        converter.
AVR main mode: Waits for commands described in the sample-avr/main.c file.
        If the LED is on, the AVR is in this mode. If the LED is flashing,
        the operating voltage is too high or too low but the AVR is still in
        this mode. The LED is off in any of the four other operating modes.

===== Some notes about Mode 1 (which was the only mode in the program version 1.0) =====
The AVR program reads data from four digitally ports and transmit them via
RS232.
The PC program reads the data a RS232 interface and plots them on the screen.

The protocol format is the following:
Every read byte contains the status of all four input pins (lower 4 Bits) and
a value how often this input states were the same (upper 4 Bits).
Examples:
0xf8 was read: In this case input 1,2 and 3 were low and input 4 is
     high. This was the same for the previous 15 samples.
0x3f was read: All four pins are high and this was the same three times in a
    row.
0x0y was read: If the first part is zero, the data should be ignored,
    regardless what y is.
By transmitting the times all read inputs were equal to the previous read, it
is possible to transmit a higher sample rate than the interface could
do without this simple compression. The disadvantage is that data will get lost
when the input pins change very often, resulting in a transmit after every input
read. There is a FIFO in the transmitter which make short high input switching
rate possible without data loss. But after a while the FIFO get filled and data
will get lost.

==== AVR-PC interface ====
The PC program searches automatically on the following ports for a sampler circuit:
/dev/ttyUSB0, /dev/ttyS0, /dev/ttyUSB1, /dev/ttyS1, /dev/ttyS2, /dev/ttyS3
if you use an other port as one of them above you have to specify them in the
source code file simple232.c and compile again. Please note that it take some
seconds if your circuit is not connected to the first available port in the list.

==== Known problems with the PC program ====
The drawing of the lines by the PC takes quite some time. More input switches
result in more lines to draw. The PC seems to support only 256 Bytes of FIFO
which were read by the PC program not often enough if there is much to draw.
If the FIFO overflows the received data gets lost of course.

If you use a graphic card with only poor 2D hardware acceleration the CPU has to
do this work for the card. This takes A LOT of CPU power, making some operating
modes nearly unuseable (especially the mode 2).
I had good experience with:
Xorg 7.0 using the 'fglrx' driver and a Radeon Xpress 200M card
Xfree 6.8 using the 'fglrx' driver and a Radeon 9600
I had bad experience with:
Xorg 7.0 using the 'radeon' driver and a Radeon Xpress 200M card

On very few program starts my X-Server (x.org 6.9, fglrx driver) hung fully.
During, I guess some, hundreds of program starts this happend two times to me.
If you want to be safe, better save all files before starting the program.
If this happens to you please report this to me together with your operating
system, graphic driver, graphic system and graphic card you are using because
I would like to find out if this is a problem with this program or a problem
with a special driver/x-server/graphiccard problem.

If you terminate the program the hard way (eg CONTROL+C) the sampler circuit
is not set back to 9600 baud. As result you have to power off/on the
circuit before starting the program again. Solution: Using the key 'q' or
the 'X' of the window manager to quit the program sets the circuit back
to 9600 before exiting.

The original RS232 interfaces (accessible by /dev/ttySx) does not seem to
support a speed of 230500 Baud as required for the high data-rate. Use
a RS232<->USB Converter in this case. If you only need Mode 3 and Mode 4
(these do not require a high data rate) you could change the sourcecode
of the PC-program so that it stays always with 9600Baud. The file for doing
so is plot-pc/main.c somewhere around line 108 (the 'if' statement).

On some systems (I guess older ones)
(seen on: Debain Sarge with kernel 2.6.12 / 2.6.8
          Knoppix with kernel 2.4.2x)
the USB<->Rs232 converter are working only once. Before restarting the
PC-Program you have to pull-out and pull back in the USB<->RS232 converter.
This might be a bug of my program, the kernel or of the hardware. I did came
so far that a write() to the filedescriptor of the /dev/ttyUSBx device more or
or less decided wether I could talk to the circuit on the next PC program start
or not. I know that this annoying especially with the problems of the /dev/ttySx
devices mentioned before.
 

==== Known problems with the AVR program ====
none so far

==== Known problems with the Hardware ====
The used MAX232N is specified for 120kBaud, but, I am using it with
230500kBaud, this could result in problems, however I did not encounter any.

Mode 4 is able to generate very high output frequencies, however the used
amplifier LM258 does not support such high frequencies well. In the case
very high frequencies with a wide output voltage swing are needed an other
amplifier should be used.


==== Hardware needed ====
An AVR ATMEGA8 or better with a typical serial interface. The read input pins
were PINC 0, PINC 1, PINC 2 and PINC 3. Because of the very high baudrate
(230400baud), you need a crystal optimised for right baudrates. By default I
assume you use a 14.7456 MHZ crystal. If you would like to use a 11.0592 MHZ
crystal you have to correct the UBRRL value in the source. See datasheet how to
calculate this value. The sample rate you get is the crystal frequency divided
by the OCR2 incremented by one.
Example: You have a 11.0592 MHZ crystal and OCR2 is set to 73, then you get a
sample rate of 11059200/(73+1)=149448.6 samples per second.
So by modifying the OCR2 value you could increment or decrement your sample rate.
With a 18.432MHZ crystal you might get a sample rate up to 250000 samples/sec.
But remember that the PC interface and the PC program are not faster by using a
faster crystal.

For using mode four you have to add a R2R resistor network to PORTB.

==== How to compile the programs ====
In the sample-avr directory run make and then upload the hex file with your
favourite programmer.
There is an already compiled binary for the PC in the plot-pc directory called
simpleplot. But I suggest you to compile your own binary. Do this by deleting the
a.out file and then type make to compile. The program needs the glut library
for the graphical interface. The glut package names for Ubuntu are freeglut3,
freeglut3-dev and libglut3. Install all three. I guess the package names were
equal or similar on other systems.
Then run /.simpleplot . By running in a console you will see some useful information.

==== Help needed, port to Windows ====
Since I do not use Win32 (except for gaming), it would be nice if someone would
port this program to windows. It guess this would be not too difficult since
the used grapic library is already ported:
http://www.xmission.com/~nate/glut.html
So the only thing which has to be ported is the RS232 communication. All RS232
communication is done in simplers232.c. Those functions have to be rewritten for
windows OS.

==== Testing example ====
Mode 1: Connect a TSOP1738 to one of the AVR input pins and watch the signals of 
        various infrared remote controls.
Mode 2: Connect a potentiometer to one of the AVR input pins and watch the signal
Mode 3: Same as Mode 2.
Mode 4: Connect a small loudspeaker or a moving coil mechanism and have fun ;-)

==== Changelog ====
2006-12-25 Version 2.10
   * Better makefile for the PC program
   * Lots of compiler warnigs fixed. PC program compiles now without warnings.
   * Closing the PC program by the window manager is now handeled correctly
   * By default the AVR and PC program expect a 14.7456 MHZ crystal connected
     to the AVR instead of a 11.0592 MHZ one as the previous version expect.
   * Improvements for the PC program to update the screen more frequently
   * AVR supports status LED
   * Some small bugs are fixed

2006-09-11 Version 2.00, providing three more operating modes.

2006-04-23 Version 1.10
   * Three times higher sample rate of the AVR, as disadvantage the FIFO on the
     AVR has been reduced down to 256 Byte.
   * The PC Application now uses an additional software FIFO and is
     multithreaded which reads the hardware FIFO much more frequently providing
     higher sample rates. As disadvantage the PC CPU is now used permanently by
     100% as long as the PC program is running.

2006-03-28 Version 1.00 is out

END OF FILE