Monday, December 23, 2013

3208Clock Part 4: Get started with the HT1632C display driver

There are several ways to get started with the HT1632C display driver. One attempt, and this was also my first step, is to check on Google if someone has already written a driver or a library for the HT1632C. And yes, there are a lot of them.

At first I was playing around with the Arduino library from Adafruit. After some modifications I was able to write something to the display.

A Picture from one my first tests with the Adafruit lib

I also figured out that an update of the complete display (from the frame buffer into the HT1632C display driver) took 8.3ms and was only done with a clock speed of approx 35kHz. Please keep in mind that the ATmega8 of the 3208Clock is running at only 8MHz.

Yellow: Chip Select (CS)
Blue: Clock (WR)

A closer look also showed that the duration of the clock cycles is not equal during the transmission. The frequency of the clock is increasing which means that the time for one period of the clock cycle decreases during the transmission of a 16bit value from 33.6µs to 24.8µs.

Yellow: Chip Select (CS)
Blue: Clock (WR)
Yellow: Chip Select (CS)
Blue: Clock (WR)

At this point I wanted to know 2 things.
  1. Why is the duration of the clock cycles not constant during the transmission?
  2. Why is there only a clock frequency of approx 35 kHz (Datasheet says on page 4 1MHz is ok) which causes a update time for the entire display of 8.3ms?

Investigations and benchmarks

At first I figured out that HT1632::writeScreen() and HT1632::writedata(uint16_t d, uint8_t bits) are the functions of the Adafruit library which take care for the transmission. To have something which makes it easier to investigate I have prepared some small Arduino sketches where I only implemented the corresponding functions.

writedata(uint16_t d, uint8_t bits)

In this function the bit banging except the selection of the chip (CS) is done.

void writedata(uint16_t d, uint8_t bits)
  pinMode(_data, OUTPUT);
  for (uint8_t i=bits; i > 0; i--) {
    digitalWrite(_wr, LOW);
    if (d & _BV(i-1)) { digitalWrite(_data, HIGH); }
    else { digitalWrite(_data, LOW); }
    digitalWrite(_wr, HIGH);
  pinMode(_data, INPUT);

 Answer for Question No 1

In the line "if (d & _BV(i-1))" happens the magic which causes that the clock cycles are varying during the transmission.

_BV(i-1) is the same like (1<<(i-1)), i is the iterator of the for-loop which is initialized with the number of bits. In the worst case the the iterator i is initialized with 16 which causes that in the check for the if-statement a "left shift operation" is executed 15 times. Because that i gets smaller every bit it has to do less and less shift operations to check the if-statement. And this causes that the clock cycle is not equal over the transmission of multiple bits.

I have fixed the problem with the clock speed by the following code modification:

void writedata(uint16_t d, uint8_t bits)
  uint16_t compareBit = 0;
  // 0x8000 -> set MSB
  compareBit = bits == 16 ? 0x8000 : compareBit |= (1<<(bits-1)); 
  pinMode(_data, OUTPUT);
  for (uint8_t i=bits; i > 0; i--)
    digitalWrite(_wr, LOW);
    if (d & compareBit) { digitalWrite(_data, HIGH); } // 1
    else { digitalWrite(_data, LOW); } // 0
    compareBit = compareBit >> 1;
    digitalWrite(_wr, HIGH);
  pinMode(_data, INPUT);

At the beginning of the function I have made a new variable for the comparison if the DATA pin has to be set. These are some lines more code but in this case the bit shift is only executed once in the for-loop. I also set the compareBit variable in a clever way because the most data packages are 16bit in this case the variable is preset with 0x8000 which is nothing else than setting the MSB true. Otherwise we have a repetitive shift operation for the amount of bits that have to be sent.

After this modification the frequency of the clock has been increased to approx 41.5kHz and the duration for a complete screen refresh is now 6.9ms this is 83% of the time it took before the modification.

Yellow: Chip Select (CS)
Blue: Clock (WR)

Yellow: Chip Select (CS)
Blue: Clock (WR)

Answer for Question No 2

After the initial improvement of the clock speed to 41.5kHz it still felt to slow for me. So I have removed all the Arduino specific stuff from the corresponding functions and replaced it by native "avr gcc" code.

void writedata(uint16_t d, uint8_t bits)
  uint16_t compareBit = 0;
  // 0x8000 -> set MSB
  compareBit = bits == 16 ? 0x8000 : compareBit |= (1<<(bits-1));
  DDRB |= (1<<DATA);
  for (uint8_t i=bits; i > 0; i--)
    PORTB &= ~(1<<WR);
    if (d & compareBit) { PORTB |= (1<<DATA); } // 1
    else { PORTB &= ~(1<<DATA); } // 0
    compareBit = compareBit >> 1;
    PORTB |= (1<<WR);
  DDRB &= ~(1<<DATA);

I was not aware that the speed improvement would be factor 10!

Yellow: Chip Select (CS)
Blue: Clock (WR)

Yellow: Chip Select (CS)
Blue: Clock (WR)

All three sketches can be downloaded from my GitHub repository.
Slow is the original code, medium is the code with the bugfix for the varying clock cycle and fast is the code for with all improvements.

Friday, December 20, 2013

The Idea, the Design, the Mistake

I know, it's been a long time since my last post. But I was busy with other stuff and also my laziness (a skill which I have trained close to perfection) has prevent me from doing a new post.

This time I want to write about the good, the bad and the ugly the idea, the design and the mistake.

The Idea

While using an Arduino MEGA 2560 with the Ethernet shield I've discovered that there are still a lot of unused and free pins available.

It covers the pins 14-53 and A8-A15 (A6,A7 are also free but difficult to reach with a additional board).

These pins include:
  • USART 1-3 (14-19)
  • I2C (20,21)
  • PWM (44-46)
  • SPI (50-53)
  • ATmega2560 external memory interface (22-37, 39-41)
  • +5V and GND
This brought me to the idea to make a small prototyping shield for these pins.

The Design

For aesthetic reasons I wanted to make a *duino shaped board so I've downloaded the Arduino Mega 2560 reference design form the Arduino homepage to get the exact measurements for the pins and the board outline. Based on that I was able to create a EAGLE library with some templates (board outlines and connectors) to create own shields for the Arduino Mega.

The Mistake

After a lot of checks of the EAGLE *.brd file and additional checks of each of the Gerber layers I've ordered the the PCB at OSH Park. 24 Days later I've found the boards in a nice purple envelope in my mailbox.

I quickly unpacked the PCBs, fired up my soldering station and searched for some pin headers in my assortment boxes. Just some seconds after I have found the pin headers I have discovered that they do not fit trough the holes of the vias - I experienced a strong feeling of disappointment/frustration :-(

A cigarette later I decided to investigate what went wrong. I have discovered that the drilling of all vias in my design of the board where only 0.8mm and a diameter of at least 1.0mm is necessary to put pin header trough the holes. I've decided to make the holes of the vias for the connectors to the Arduino 1.2mm and the rest of the holes of the prototyping area 1.1mm.
I also have updated all packages in my EAGLE Arduino library

But even if the pin headers do not fit to the board I was able to see that it would fit nicely. So I have ordered the updated revision of the board directly again at OSH Park. In 3-4 weeks I will see the final result.

Tuesday, September 3, 2013

This is NOT an Oscilloscope!

What has happened?

At the moment I'm playing around with a Wien Bridge Oscillator, or to say it better I have tried the last two days to get it working.

I have found an application note from Analog Devices (AN580) which describes an circuit of the Wien Bridge Oscillator with two diodes for the stabilization of the output swing. In the schematic of the application note are also two digital potentiometers used to set the frequency and to manipulate the size of the output swing. This two potentiometers could be replaced by common potentiometers.

On this page I have found additional informations about this circuit which also helped me to finally get it run.

The Circuit

Some notes for the schematic:
  • The values for R1 and R2 must be equal and also the values for C1 and C2.
  • By increasing or decreasing the values of C1,C2 or R1, R2 the frequency can be changed. I would recommend a linear stereo potentiometer for R1 and R2 this makes it easy to change the values simultaneously.
  • If the capacitance or the resistance gets larger the frequency becomes slower.
  • LED1 and LED2 control the amplitude of the output swing. As higher the forward voltage of the LEDs the higher the amplitude of the output swing.
  • Set the gain with the potentiometer R4 so that the LEDs (both) just begin to light up. If the gain is to large the distortion will be very high.
In the schematic I also have implemented a voltage divider to connect my Arduino Leonardo. ATTENTION both, the Arduino and the Wien Bridge Oscillator are powered form independent and insulated power sources. Only in this case it is possible to connect the negative 12V to the GND of the Arduino.


To make sure that both power supplies are independent from each other Measure the voltage between the GND pin of the Arduino while it is connected to its power source (USB-Port) and the three supply rails (+12V GND -12V) of the Wien Bridge Oscillator. The Voltage in this case should be very close to 0 (I have measured values below 20mV for each line).


The next important part is to make sure that the output of the voltage divider (R6, R7) never can have more than 5V otherwise you are going to kill the Arduino.

The calculation for the voltage divider is quite simple. To calculate the voltage divider the maximum possible voltage (even in a case of failure) between the GND of the Arduino and the the OUT of the Oscillator must be known. In this case the GND of the Arduino is connected to the -12V of the Oscillator. The maximum positive voltage is +12V both together have 24V.

Vmax / (R6+R7) * R7
24V / (39k+10k) * 10k = 4.9V

With a maximum output voltage of 4.9V after the voltage divider it would be no problem to connect the Arduino.

The Problem

The main problem was that I was not able to see the waveform of the output, which makes it more or less impossible to see if the output swing of the OPAMP goes to its positive or negative maximum values so that the result is more like a square wave with sinus parts at the rise and fall.

Due to the fact that I only want to use very low frequencies (<500Hz and in this sample approximately 160Hz) I thought it could be possible that the analog inputs of an AVR are fast enough to get a rough look at the waveform.

After first tests I have figured out that I'm able to measure approximately 8 times per ms (each 120ns) which is a sampling rate of 8330 kHz. For me it would be OK to use this for frequencies below 500Hz because at 500Hz we still have more than 10 Measure points per period. With 10 measured points per period a sinus signal does not look very beautiful but it could be identified as a sinus signal.

I assume that the speed could be increased if the analog value is measured directly without the Arduino libraries. The datasheet of the ATmega32U4 says that a sample rate of up to 15k samples per second is possible at full resolution (10Bit). And in the datasheet of the ATmega328 is the same statement for the full 10Bit measurements.

The Waveform Viewer

To make it short: Here is the code it can be downloaded from my Github repository.

The code itself is quite simple. My first attempt was to sample 255 times the analog value into an array and then send the array formatted to the Arduino serial monitor so that I only have to make copy and paste into the spreadsheet.

This has worked very well but I am lazy. So I remembered that I have an Arduino Leonardo in the drawer which could type the values directly into the spreadsheet.
I've added the keyboard code to the sketch and programmed it to my Arduino Leonardo and it worked more or less instantly.

Now the life got very easy, I only have to push one button to get all the values including the time stamp into the spreadsheet. The next step would be to just look onto the Arduino to start the sampling :-)

After the values have been entered into the spreadsheet I have made a graph from the values to watch the waveform.

Here I have the OpenOffice file which I have used to show the graph. To let the Arduino enter the values into the sheet just select the box A1 and press the startButton (pull pin 7 to GND). You can repeat this as often as you want because the graph automatically changes after the values have been entered, just push the button again.

Monday, August 19, 2013

3208Clock Part 3: Implement the board into the Arduino IDE

General overview

In this post we take a closer look into the configuration of the Arduino IDE and we also upload the first test sketch to the 3208Clock.


24. Jul.2016:
I figured out that the following descriptions does not work anymore for the actual Arduino IDEs (> 1.5.x). I have no clue how its done now and i assume its a similar way to implement the new board but at the moment i dont have the time to do it.

Add the new board to the configuration files of the Arduino IDE

There is a configuration file called boards.txt in the program folder of the Arduino IDE. It can be found here .../arduino-1.0.5/hardware/arduino/boards.txt. Open this file in a text editor and add the following lines at the end of the file and save the file. Then start/restart the Arduino IDE. Now there is an entry called "Lattice Clock w/ ATmega8" in the Arduino IDE which can be found in the menu Tools - Boards. Clock w/ ATmega8

The entries in the boards.txt are more or less self explaining but we still want to venture a further look on the parameters. Clock w/ ATmega8

With this parameter the name which will be shown in the Arduino IDE under the boards section can be set. In this case its "Lattice Clock w/ ATmega8"


This parameter sets the maximum upload size of the hex-file. in this case we can use the complete 8192 bytes (8kB) of the ATmega8L program space. It is not necessary to install a boot loader on the device because the ICSP connection will be used.


With this two parameters the two bytes of the fuses will be set. The fuses are the internal configuration of the Atmel chips.
With this fuse settings the reservation for the boot loader in the flash space is disabled and it is also set to a clock frequency of 8MHz from the internal oscillator.

IMPORTANT! If you don't know exactly what you are doing with the fuse settings then it is very easy to reduce the usage of the device to a paperweight. Changes are at your own risk.

This parameter tells the compiler of the Arduino IDE which micro controller is used.

This parameter sets the information of the clock speed (in this case 8000000Hz or 8MHz). This parameter is used by the compiler to calculate some constants which will be used by some of the internal hardware of the ATmega e.g. usart / serial connection.

I'm not sure what this two parameters are doing. If someone has an idea please leave me a message in the comments.

Burn the fuses

Select the board "Lattice Clock w/ ATmega8" in the boards menu and select the programmer (If you use an Arduino as a programmer then select ArduinoISP). Both can be found in the Tools menu.

IMPORTANT! If the wrong board is selected for example the Arduino NG or older w/ ATmega8 then you will brick the 3208Clock because the fuses for this board configure the clock source to external 16MHz quartz. The 3208Clock does not have an external 16MHz quartz.

After the correct programmer and board has been selected go again to the tool menu and click on Burn Bootloader. In our case it only flashes the fuses and erases the chip.

Build a "Test LED" and flash the test sketch to the device

I guess a picture says more than thousand words. I have used a 1kOhm resistor. Everything between 330 and 1000 Ohm will be fine. Also the selection of the LED is not critical any common LED which emits visible light will fit. It is also not necessary to solder the LED to the board for the first test it only needs to be plugged through the holes of the DS18x20 footprint.

If the LED is prepared so far then please download the test sketch from my github repository or use the blink sketch from the examples and change the ledPin from 13 to 15.

To burn the test sketch to the 3208Clock you must hold the shift key while clicking the upload button. Otherwise the Arduino IDE tries to use the serial connection to upload the sketch which will cause an error message.

In the next posts we will take a closer look on how to get in contact with the Holtec HT1632C display driver to get something shown on the display.

Sunday, August 18, 2013

3208Clock Part 2: Prepare the Hardware to get connected with the Arduino IDE

Hardware connection

The 3208Clock has of cause no Arduino boot loader installed and it also has no serial or USB connection on pin headers. For this reason it is only possible to program the device over the ICSP connector with an programmer like an AVR ISPmk2.

Don't be scared. If you have an Arduino (I only tested it with the UNO but it should also work with older versions) then you also have an ISP Programmer. Just flash the ArduinoISP sketch which is delivered with the examples of the Arduino IDE to your Arduino.

Here are the parts that are needed to make the connection.
  • 1 Arduino (in this case its an Arduino UNO others should work as well)
  • 6 Jumper wires female to male
  • 1 2*3 pin header OR I would recommend a boxed header

The pin header / boxed header must be soldered to the 3208Clock board. To make sure that the boxed header is mounted in the correct direction take a closer look on the picture below.

And here is a picture with the finished setup. Please double check the connections before you power on the Arduino programmer just to make sure that the Arduino and the 3208Clock is still alive after our first try.

If there is already an ISP programmer by hand then of cause this can be used. I personally use a USBprog which works as an AVR ISPmkII clone.

In the next post I will show a closer look into the configuration of the Arduino IDE. Especially the way how a new device can be added so that it can be selected under Tools - Board in the Arduino IDE.

Tuesday, August 13, 2013

The modified 3208Clock Part 1 the beginning

I stumbled upon this lattice clock while I was surfing through the products of one of this Chinese Internet sellers with worldwide free shipping. I thought "OK, 12$ (~9,0 EUR) what can I loose?" So I've ordered one of this devices.

I have stolen this Picture from

After only four weeks the package has arrived. My first thought was to quickly unwrap it and connect it to power to see what it is doing...

It took me $12.30 and a lot of patience (waiting for delivery) to learn that this device can not be powered directly by 12V DC. After the magic smoke has disappeared and I have turned the PCB I have seen that this device is labeled with DC 5V

From this setback, I did not let me discourage and I ordered a new unit. During the delivery time for the new device I had some time to do some reverse engineering. So I unmounted all the parts which hide the direct view to the PCB.

The contents of the 3208Clock

This device consists of an ATmega8L which is the brain of the Clock and a Holtec HT1632C Display controller paired with four 8x8 LED segments.

Optional components

During my investigation I have discovered that the PCB was in general developed in a very clever way. The following parts can be retrofitted to the board.
  • Mini USB port
  • TSOP infrared receiver
  • DS18x20 1-Wire temperature sensor / LM35 temperature sensor
  • Bi-color LED (color changing by reversing polarity)
  • 6-Pin ICSP header to reprogram the ATmega8L
  • Buzzer/Speaker (can not be mounted if ICSP header is mounted)
  • On the backside is a place to mount a RTC IC and a coin cell battery to power the clock IC if the main power is not connected.
But due to the Problem that the ATmega8L has only 8KB of program space I'm not sure if all of the above components can be implemented into the software.

PCB Overview

The most interesting part seems to be the right part of the front side. Here are a lot of footprints of the unpopulated optional parts which can be used to interact with the real world.

I have prepared a picture which shows which pins of the ATmega8L are connected to the footprints. The numbers are the Arduino pin numbers. I was to lazy to also write the original port pin numbers like "PB3" to the Picture. But here you can see a very good pinout diagram made by Pighixxx. I know its for a ATmega168 but the Pinout is more or less compatible.

Here is a picture of the backside of the PCB. We see here that three ICs can be mounted to the board. From left to right we have at first the footprint for the ATmega8L with its TQFP32 package. The next ones are the footprint for a battery and a SO16 IC where a RTC can be mounted.
The last one is the Footprint 52 pin QFP footprint for the Holtec HT1632C driver IC for the 8x8 LED displays.

I also have made a picture with some additional descriptions for some pins. During my reverse engineering I also painted some pads red to make it easier for me to track the +5V line. Also take a look at the top left of the picture there is a very important information written which I have ignored at my first try.

My actual setup

The only parts from the optional components which I have installed are the ICSP header for programming the device and a bi-color LED which was laying around for debugging purposes.

With the Setup in the picture above I'm now able to send a zero terminated string to the device over Bluetooth which will then scroll through the display. Here is a short video which I have made.

The next steps

In the next posts I will show a more insight view about the single steps which I have made to get this device up and running with the Arduino IDE and a couple of libraries. I also want to upload a well commented version the code to GitHub so that everyone can get started with playing around with this cheap and versatile device.

I'm not sure which topic the next posts will have but they will be more or less related to the 3208Clock device. It is also possible to post requests for additional informations of specific parts of this device or the procedure of hacking it to the comments.

Just some info about me and how do I got addicted to playing with electronics

The early days

I was born in the early 80s in the near of Frankfurt Germany and I soon have discovered that it is very interesting to disassemble things to more or less (more less than more) understand how they are working.

I guess I was 10 as I got my first own IBM compatible PC. This device laid the foundation stone for my later life.

The youth / I survived the 90s

It was a hard time. At the beginning of the 90s there was no Internet, no smart phones and even no cell phone. (OK, there were cell phones but they where not affordable to common people, you also have needed a briefcase or a car to transport them) We have used phones which where connected with a cable to a special outlet on the wall in our house.

At the mid of the 90s I gained access to the Internet. At this time the Internet looked like an LSD trip. Especially under some enthusiasts which have created their own private pages. There where some unwritten rules to design private web pages. I just want to point out some of these rules here:
  1. Use as many different colors as possible. There where 16 different colors. With some patience and training you where able to implement all of them into one page.
  2. Try to bring some harmony into your page. E.g. Blue text on green background. Yellow text on blue background was also very popular.
  3. Use frames, at least three.
  4. Use plenty of animated GIFs which do not belong to the topic of the page. (If there was a topic) 
  5. The last but also one of the most important point was to upload a lot of large pictures to show the visitor that you either have a lot of patience to upload the pictures or to blame all your visitors which where visiting your page with an 28.8kbps modem which was pretty slow.
During this time I also played a lot of computer games and also played with some electronic kits.

The first decade of the new century

School was finished and I had to start with working / learning a job. I my case I have learned electrician at construction sites. I general I would say that working is a nice activity but you definitely loose to much leisure time.

I've done this job until the mid of this decade. Then I have decided to go back to school for two years to improve my skills and to rise my value to the job marked.

One of the teachers has developed the PICee development and learning platform for PIC microcontrollers. This was also part of the lessons, so I got the first contact to C and the world of programmable micro controllers. I was interested in electronics for a long time but I have not got the final push into this topic until the beginning of this lessons.

And today its a hobby for me which is inevitable.

Until today

Since this time I'm playing with micro controllers. The first time I used the PIC16Fxxx from Microchip but then I switched to Atmel because they have full support for Linux. And about a year ago I have discovered the Arduino plattform which makes my life a lot easier.

Here is a short list about my skills (or things which I believe I'm skilled at)

German, English, Perl, C, HTML, Linux, Arduino, Electronics (More the digital part, for the analog stuff I would need better math skills), Eagle CAD Software, Beckhoff PLC, CNC-Programming (DIN code)

Hope that was enough info about me

Sunday, August 11, 2013

The first test entry


So this is just the first test entry to get some look and feel of this Blogger engine.
A RaspberryPi project which I already have published on Github.

And here is a Picture of this Project: