HAPSIM - Helmi's AVR Periphery Simulator V2.20
LCD (HD44780U), Terminal (USART and TWI), LEDs, Push-Buttons and Keypad Simulator Extension
for the AVR Simulator of Atmel's AVR Studio 4
Table of content:
- supports Atmel AVR Studio 4, Version 4.10 up to Version 4.19, build 730.
AVR Studio Version 4.12.452 or higher strongly recommended!
- simultaniously simulates one or more of each of the following components:
- HD44780U compatible LC-Display with up to 40 x 2 or 20 x 4 characters.
- Pushbuttons connected to Input Port Pins.
- LEDs connected to Output Port Pins.
- Terminal connected to USART, UART or TWI(I2C) ports.
- 4x4 Matrix Keypad.
- Watch Cycle Counter and Simulation Speed.
- easy configuration of the components.
- configuration files are stored in future-save XML format.
- target device selection with all devices supported by the AVR Simulator.
- acts like a real HD44780U.
- up to 4 Display lines.
- 8 to 40 characters per line.
- 4bit or 8bit I/O mode support.
- supports the complete original character set (5 x 8 dot)
including all the 'weird' characters above 0x80 ;-)
- support for 8 user defined characters.
- supports blinking cursor, display shifting, cursor movement.
- selectable background color and display size (zoom factor).
- LCD write only mode: R/W line may be hardwired to '0', no portpin necessary.
This application demonstrates the usage of a 16x2 LCD control, a 4-Button control and a 8-LED control.
- Download and unzip HAPSIM and LCDTest.
- Build the LCDTest project by executing 'build.bat' (maybe edit the 'winavr' path first).
- Start AVR Studio 4
- In the Project Wizard select the 'lcdtest.elf' file and press 'next>>'
- Select Debug Platform: 'AVR Simulator' and Device: 'ATmega128'.
- Click 'Finish'.
- Start 'hapsim.exe'. It automatically detects the running AVR Studio and connects to it.
- Verify the connection to AVR Studio by clicking the Options menu:
The 'AVRStudioHook' Option must be checked.
- An LCD1 Window with a 16 x 2 LCD appears by default.
- Now open the 'LCDTest.xml' file in HAPSIM.
An LCD, a Button window and a LEDs window appear.
Each of them can be configurated by selecting it and then clicking the appropriate
Settings button or select '...Settings' from the 'Options' menu.
But that's not necessary now because they are already pre-configured for the LCDTest program.
- Hit the Run (F5) button in AVR Studio, sit back and watch as the LCD shows the welcome text.
It's quite slow, I know, but that's the simulation speed of the AVR Simulator with a little
slowdown by HAPSIM, of course...
- When the text display has finished, the first green LED in the LEDs window, named 'Mode0' lights up.
For explanation: the 4 green LEDs show the current mode of the Test program, I'll explain later.
The red LEDs below show the state of the 4 buttons of the buttons window.
- First, let's test the cursor modes of the LCD:
Press the first of the 4 buttons, the 'Cursor' button and hold it down. As you see the
'Button1' LED lights up!
Release the button. The underlined cursor appears at the current cursor position of the LCD which
should be at the 'T' of "Test" if you have not pushed any other buttons yet.
Press and release the 'Cursor' button once more and you get the blinking cursor with underline.
Next time you get the blinking cursor without underline. Once more and the cursor disappears.
Select a (visible) cursor of your choice for the following steps.
- Test mode 0: Press and release - slowly - the 'Right' button. On every edge of the button signal
one (increasing) character code is written to the display while the cursor moves to the right.
Note that you have to do it slowly, because the buttons are only polled by the AVR and the
character display generates a little delay. When you click too quick, the AVR will loose some
'button events'.
When the cursor reaches the right edge, the display shifts to the left in order to keep the cursor
visible at all time.
With the 'Left' button the (still increasing) character codes are written from right to left and
the display shifts to the right when the cursor reaches the left edge.
- Now switch to test mode 1 by pressing the 'Mode' button. (what a surprise ;-)
The 2nd green LED 'Mode1' lights up.
- Test mode 1: In this mode the cursor can be moved with the 'Left' and 'Right' button without writing
characters and without shifting the display. So you can also move the cursor out of the visible
display area.
- Press the 'Mode' button once more and you get to test mode 2: Now the display is shifted left and right
without moving the cursor.
- Finally switch to test mode 3: This is a test for the user defined characters, the "CGRAM Test" which
is displayed in the 2nd line. The first display line shows the character codes 0 to 15 which contains
the 8 user defined characters twice; character 0-7 are the same as 8-15.
At the first appearance you should see there random bit patterns (= the current content of the CGRAM)
except for the Copyright (c) Symbol which was defined during display of the intro text.
With the 'Left' and 'Right' button you can now write (increasing) bit patterns to increasing (right)
or decreasing (left) CGRAM addresses and watch them on the display.
- That's all! Pressing the 'Mode' button once more leads to the mode 0 again...
Note: | If you want to run this LCDTest program on a 'real' AVR you have to remove the definition of
FASTSIM by removing the line
"set DEBUG=-D FASTSIM" from the build.bat batchfile.
This #define shortens out the delay() function and therefore speeds up the simulation! |
The Terminal Simulation for USART and TWI is a new feature in HAPSIM V2.0.
There is also a small test application for this new contol, namely a USART <-> TWI Converter
which is used for this tutorial:
- Download and unzip Usart2Twi (and of course HAPSIM if you haven't done yet).
- Build usart2twi project by executing 'build.bat' which generates a '.elf' file for the ATMega8
this time for variety (and also to show the Device selection feature of HAPSIM ;-)
Note: | usart2twi is prepared to be built for ATMega128 too, by just running the 'build128.bat',
if you prefer... |
- Start AVR Studio 4
- In the Project Wizard select the 'u2twi8.elf' file and press 'next>>'
- Select Debug Platform: 'AVR Simulator' and Device: 'ATmega8'.
- Click 'Finish'.
- Start 'hapsim.exe'. It automatically detects the running AVR Studio and connects to it.
- Verify the connection to AVR Studio by clicking the Options menu:
The 'AVRStudioHook' Option must be checked.
- Now open the 'Usart2Twi.xml' file in HAPSIM.
You get two Terminal windows. The upper one named USART is attached to the USART interface without
local echo because USART echo is done by the test application.
The other Terminal named 'I2C-Terminal' is attached - what a surprise - to the TWI interface
this time with local echo.
- Hit the Run (F5) button in AVR Studio, sit back and watch as the USART terminal shows the welcome text.
- When the text display has finished, you can start typing in any of the two windows and watch as the
text is echoed and appears in the other window... :-0
- Have fun!
As an alternative to the following here is a nice tutorial written by Oliver Müller which uses the open-source com0com instead of Virtual Serial Port Kit, which I used below...
The used demo application utrans is quite simple: It uses both of the USART interfaces of an ATMega128:
Whatever is received on USART0 is forwarded to USART1 and vice versa.
For this demonstration we need two COM ports of our PC where we can redirect the AVR USARTs. At these COM ports we
connect nullmodem cables with terminal programs attached.
So in fact we need two pairs of COM ports each pair looped with a serial cable! Due to the fact that it is unlikely
you have 4 COM ports available on your computer you can either use a second computer running the terminal programs
or you can use a virtual serial port driver like the Virtual Serial Port Kit (unfortunately there's only a 15 days trial version for free) which I used here:
- Make sure everything is installed: AVR Studio, HAPSIM, utrans demo-app and Virtual Serial Port Kit.
- Create two virtual connections with the Virtual Serial Port Kit: COM3 to COM4 and COM5 to COM6
For our application it's wise to enable bitrate emulation in the properties dialogs of the virtual connections.
- Run Build.Bat to build the application
- Start AVR Studio, load utrans.elf, select AVR Simulator and ATMega128.
- Start HAPSIM, load utrans.xml. You get two terminal windows: USART0 ==> COM3 and USART1 ==> COM5
- Start two terminal programs like HyperTerminal. Attach the first one to COM4 and the second one to COM6. Set the baudrates to 1200 bps for both terminals.
Your first HyperTerminal is now virtually connected to USART0, your second one to USART1. Because of the utrans application with connects the both USARTs you have now the following connection between the two HyperTerminals:
HyperTerminal1 <-> COM4 <-> virtual connection <-> COM3 <-> HAPSIM <-> USART0 <-> AVR Simulator running utrans app
<-> USART1 <-> HAPSIM <-> COM5 <-> virtual connection <-> COM6 <-> HyperTerminal2!
- Run the application in AVR Studio. You should see the short welcome text 'USART transceiver!' in all of the four terminal windows!
- Now let's play: everything you type into one HyperTerminal will appear on the other one and vice versa.
- But you are not limited to typing text. You can use every feature the terminal program provides.
- Send a text file.
- Use the 'Capture text' option of HyperTerminal to log the output of your AVR application to a file.
- How about a file transfer using Zmodem protocol? It works! Cool, uhh? Just make sure you take a short file. Otherwise you will get a timeout of the sender Terminal because the effective transfer speed is much slower than 1200 bps due to the AVR Simulator.
TestKeypad is an application for Atmega16 to test a 4 x 4 keypad connected to PortC.
The program detects the pressed key by reading the row and column. The row is read from the input lines by setting the strobe lines to all 0. Afterwards the column is read by reverting the data direction, set all input lines (now output) to 0 and read the column from the strobe lines (now input). Open input lines are pulled to high by using the AVR internal pullups.
To prevent key bounce keypadtest starts a 25ms timer and retests the button is still pressed.
Running TestKeypad using HAPSIM
- Run Build.Bat to build the application
- Open testkeypd.elf in the AvrStudio Project Wizard and select AVR Simulator with ATmega16 as target.
- Start HAPSIM and open TestKeypad.xml
- Run the application in AvrStudio Simulator.
The Terminal screen will display a message "Test Keypad Program"
Press the buttons on the keypad or use the Keyboard to activate the hotkey links.
The keys pressed are echoed to the terminal screen.
- no timing conditions are checked or considered.
e.g. the LCD even works if you don't wait for the busy bit to clear.
- DDR (data direction) programming of the AVR ports are neither checked nor considered.
- avoid double connections, e.g. connecting the same port pin to the enable pin of the LCD
and to a LED will cause problems.
- don't change the port settings (of any controls) during a running Debug session.
Hit the Break Button in the AVR Studio first! Anyway you don't have to stop the debugger completely.
- no memory mapped IO support for the LCD.
- TWI terminal only supports master-transmit and slave-receive modes
and doesn't check the slave-address.
- incomplete PartDescriptionFiles for ATMega8 (maybe others too)
supplied with AVR Studio Versions below 4.11, resulting in TWI terminal problems.
- USART2 and USART3 on the "big" devices ATmegaXXX0 don't support interrupt handling because of a bug in the AVR Simulator of AVR Studio 4.12.490 and below.
Hopefully this will be corrected by Atmel sometime.
Andrew Macdonald for programming the 4x4 Keypad control and the corresponding demo-app "TestKeypad", andrew.macdonald@au.fujitsu.com.
Peter Fleury for his LCD library, used by the LCDTest program, http://jump.to/fleury
Iuri Apollonio for the 'CoolMenu' Support,
http://www.codeguru.com/Cpp/controls/menu/bitmappedmenus/article.php/c177/
Maurizio Pisano for the MessageBoxTimeout API, http://www.codeproject.com/cpp/MessageBoxTimeout.asp
James Clark for his Expat XML parser library, http://www.libexpat.org and
Tim Smith for the C++ Wrappers for the Expat XML Parser, http://www.codeproject.com/soap/expatimpl.asp
Oliver Müller for his tutorial about "Simulation of Serial Connections in AVR Studio 4", http://www.cogito-ergo-sum.org/com0com-hapsim.shtml.en
And last but not least, Atmel for their AVR microcontrollers which are the reason for all this ;-)
and of course for their free AVR Studio Software, look at http://www.atmel.no/beta_ware/ for the latest (Beta-)Releases.
Look at http://www.helmix.at/hapsim
Note: |
Some of these Demo-Applications are using the #define FASTSIM to speed up the simulation.
If you want to run them on 'real' AVR hardware you have to remove the definition of
FASTSIM by removing the line
"set DEBUG=-D FASTSIM" from the build.bat batchfiles! |
Some internal information on HAPSIM:
- MFC VC++6.0 application.
- document/view architecture where every control (LCD, Buttons, LEDs, ...) has it's own DocTemplate so it's quite modular and simple to add new controls.
- the interface to AvrStudio is done by a rather crude hooking mechanism. But this functionality is centralized so every control view gets windows messages for the desired AVR Port changes and may set AVR Input Ports via simple API calls.
If you want to add new functionallity and therefore collaborate in the further development of HAPSIM then read this HAPSIM Programmer's Tutorial.
V2.20 - 16.2.2012
- Crash with ATtiny devices (without UART) fixed.
- Corrupted port scan table fixed.
- Ignore port writes with unchanged content (performance improvement).
V2.19 - 21.1.2012
- 4x4 Keypad Control redesigned and fixed. bidirectional access of row and column lines.
V2.18 - 29.10.2011
- Update for AVR Studio Version 4.19, build 730
- 4x4 Keypad Control redesigned and fixed.
V2.17 - 6.12.2009
- Update for AVR Studio Version 4.18, build 684
- Device list handling fixed (search 'simulatorparts.cac' on the right place).
V2.16 - 7.9.2009
- Update for AVR Studio Version 4.17, build 666 (the number of the beast ;-)
- Crashes with the "big" devices ATmega256x fixed.
- More informative error message on missing device list.
V2.15 - 23.5.2009
- Update for AVR Studio Version 4.16 SP1, build 638
V2.14 - 6.12.2008
- Update for AVR Studio Version 4.15 Final, build 623
V2.13 - 25.10.2008
- Update for AVR Studio Version 4.15, build 619 (Beta)
V2.12 - 18.3.2008
- Update for AVR Studio Version 4.14, build 584 (RC1)
Note: The new AVR Simulator 2 is (still) not supported!
Note (24.10.2008): Support for Versions up to 4.14.603 (SP1) also confirmed!
V2.11 - 18.2.2008
- Terminal-bug on multicore machines - repetitive transfer of same characters - fixed!
The processor affinity mask for hapsim is set identical to AVRStudio.
AVRStudio only uses one core! Thanks to Heiko Nocon for analyzing the problem!
V2.10 - 5.5.2007
- New control "AvrCycleView" for watching the Cycle Counter and Simulation Speed added.
- Output of Error messages on Windows 2000 fixed.
- "Language-Mix" german-english of some buttons fixed (all buttons should have english names now)!
- Support of the SPI registers added as a prerequisite for an upcoming SPI terminal control.
Note (9.1.2008): Support for Version 4.13.571 (SP2) also confirmed!
V2.09 - 20.2.2007
- Update for AVR Studio up to Version 4.13, build 524 (Release candidate)
Note: The new AVR Simulator V2 (Preview) is not supported!
Note (2.3.2007): Support for Version 4.13.528 (Release) also confirmed!
V2.08 - 10.10.2006
- Bugfix: Terminal control crashes when no COM ports available!
V2.07 - 16.8.2006
- Support of 4 line LCDs with up to 20 characters per line.
- Support for the "big" devices ATmega1280 and ATmega2560 with up to 11 I/O registers and 4 USARTS.
- Update for AVR Studio up to Version 4.12.490 (SP3)
Note (22.10.2006): Support for Version 4.12.498 (SP4) also confirmed!
- Handling of 'active low' pushbuttons corrected.
V2.06 - 21.10.2005
- 4x4 Keypad Control added by Andrew Macdonald.
- Terminal Emulation improvements: Redirection to COM ports.
- Update for AVR Studio up to Version 4.12.456 (RC4)
- Support for older Studio Versions than 4.11.404 slowly discontinued.
- Bugfixes (and probably some new bugs added :-)
V2.05 - 15.3.2005
- Update for AVR Studio 4.11 Service Pack 1, Build 404 and 405.
Remark 25.3.2005: V2.05 also supports Service Pack 2, Build 406!
- Performance improvement!
V2.04 - 3.3.2005
Update for AVR Studio 4.11 Servicepack 1, build 403.
V2.03 - 10.2.2005
'Stay On Top' - Option added.
V2.02 - 7.2.2005
- Trinary encoding support for Button Control
This feature allows to connect two buttons at one I/O line.
For details see AVR Freaks Design Note #044 by Luke Sangalli
Using Trinary Encoding to Reduce Pin Wastage
- Configuration file loading via parameter at program start.
V2.01 - 1.2.2005
- UART support for devices like ATMega163 (UART instead of USART).
- GDI resource leakage fixed.
- PartDescriptionFiles: handling of '$' as hex identifier added.
V2.00 - 26.1.2005
- Terminal simulation for USART and TWI(I2C) implemented.
- Target device selection.
- LCD write only mode:
R/W line may be hardwired to '0', no portpin necessary.
- Configuration file format changed to XML.
- Update for AVR Studio 4.11, build 401.
V1.02 - 6.9.2004
LCD supports up to 40 characters per line.
V1.01 - 28.8.2004
Update for AVR Studio 4.10, build 356.
V1.0 - 23.8.2004
HAPSIM is both Freeware and Open Source!
Everyone can use it without any restrictions.
And everyone is invited to extend the functionality of HAPSIM by contributing new controls or features.
In this case I only want to receive the modifications aswell and get the permission to add them completely, or partly, into my "official" HAPSIM release.
The name of the contributing author is then mentioned on the HAPSIM homepage and/or in the program itself.
If you simply like HAPSIM, or even increase your productivity and commercial success by using it,
donations to a poor, underpaid programmer are very much appreciated ;-)
Look at http://www.helmix.at/hapsim
Please e-mail any suggestions, complaints and other feedback to
helmix@gmx.at
http://www.helmix.at/hapsim
Helmut Wallner, 16.2.2012