USB Physical Hit Counter Based on AVR ATtiny25

ATtiny25 MAX7219

This is an USB Physical Hit Counter.This version requires one MCU to communicate between PC and 7-segments display driver.I decided using AVR and this is my first AVR project.The purpose of this project still same as previous version.Therefore,it used to count and display the total page hits on any website instead of using only general scripts such as PHP,Java,CGI etc.The host software no USB driver programming required.

USB Physical Hit Counter Based on AVR ATtiny25

Hardware and software on Display side


Click to enlarge

The display I have used come from version 2 which it is MAX7219 and 7-segment x 8. MAX7219 requires only 3 I/O from MCU.ATtiny25 is the heart of this project.The firmware for this project written in AVR-GCC.This firmware modified from EasyLogger example which developed by Christian Starkjohann from Objective Development.Also,Christian Starkjohann developed an excellent USB firmware called AVR-USB which is a firmware-only implementation of a USB 1.1 compliant low-speed device for Atmel’s AVR microcontrollers.With this AVR-USB driver, it is easy to implement USB interface on AVR microcontroller project without dedicated USB hardware or no requires any USB to RS232 devices such as FT232.
From the circuit above this circuit requires at least 300 mA , 9-12 V DC or AC external power supply for MAX7219 to avoid overloading on USB bus.The regulator(IC1) needs an appropriate heat sink.The IC3 use bus-powered.

I use MAX7219 in decode-mode to reduce program memory space(2k for ATtiny25) because of this mode no program memory space to keep 7-segment font required. Here is the code inside the function usbFunctionSetup in firmware which is the importance code to receive command from PC.

USB_PUBLIC uchar usbFunctionSetup(uchar data[8])
{
static uchar replyBuf[2];
uchar Dlen;
usbMsgPtr = replyBuf;
switch (data[1]) {
case 0: // data format => data[3]:data[2]:data[5]:data[4]
MAX7219_DisplayChar(1,data[3] >> 4 & 0×0F); // display digit 0
MAX7219_DisplayChar(2,data[3] & 0×0F); // display digit 1
MAX7219_DisplayChar(3,data[2] >> 4 & 0×0F); // display digit 2
MAX7219_DisplayChar(4,data[2] & 0×0F); // display digit 3
MAX7219_DisplayChar(5,data[5] >> 4 & 0×0F); // display digit 4
MAX7219_DisplayChar(6,data[5] & 0×0F); // display digit 5
MAX7219_DisplayChar(7,data[4] >> 4 & 0×0F); // display digit 6
MAX7219_DisplayChar(8,data[4] & 0×0F); // display digit 7
Dlen = 0;
break;
case 1:
MAX7219_Clear();
Dlen=0;
break;
case 2:
MAX7219_Init();
Dlen=0;
break;
default:
Dlen=0;
break;
}
return Dlen;
}

Host Software
I have written the host software in Delphi 6 and Indy 10 component installed on my PC as previous version.But this version requires a spacial library called “LibUSB-Win32” which is a port of the USB library libusb to the Windows operating systems (Win98SE, WinME, Win2k, WinXP). The library allows user space applications to access any USB device on Windows in a generic way without writing any line of kernel driver code. Also,this host software requires CoolTrayIcon and TextTrayIcon component for delphi.This component allow this host software minimize to the system tray as show in the picture below.My host software is just example and there are no some features such as no checking internet connection or not,no checking text file(hits.txt) exists or not.But,you can be modified or write your new software.
As before,the host sofware will read text file(hits.txt) from web server every 10 seconds or more(timer1 interval) then send data to display circuit via USB.

This Delphi component can be downloaded from the link below or search on google for “Delphi CoolTrayIcon”.
Web server script

On the my web server,I create a text file to keep the hits number(e.g.hits.txt) with set file permission to chmod 755 or 777 then place the PHP script below on any page that you need to count the page hits,sidebar.php in my case.The hits.txt will be fetched by host software.

$hitfile = $_SERVER[’DOCUMENT_ROOT’].”/counter/hits.txt”;
$hits = @file_get_contents($hitfile);
file_put_contents($hitfile,$hits+1,LOCK_EX);
echo ($hits+1); // comment this line if do not want to display
?>

AVR Firmware
Firmware source code
Delphi 6 source code
Include source code and execute file(exe)
CoolTrayIcon component
System tray component

The host software tested on Windows XP only.Please don’t ask me about MAX7219 PCB,I have designed about two years ago and I cann’t find the orginal PCB file.I use AVR ISP programmer and PonyProg to experiment this project.