For information about the generic components of the IAR C-SPY Intel 8051 Simulator, refer to the Readme file for IAR C-SPY Debugger
This document describes how to use the macro language to simulate different peripherals, such as timers. The package is only an example of how to use the macro language. You can freely change these macro files to fit your needs.
For information on how the different peripherals work on the chip, refer to the hardware manual for your processor.
The macro language is described in the manual. You can also see all available system and user macros listed in C-SPY. In the Options menu, select "Load Macro". At the bottom of the dialog box you can see all registered macros.
All examples described in this text file are located in the \8051\TUTOR directory.
At the moment there are two different examples of
macro-simulated peripherals, both timers. There are also two macro
files simulating external interrupts. These macro files do not
come with any corresponding C source file.
Files included in this project:
- timer.c
- timer.mac
Building the project:
- Create a new, empty project in the IAR Embedded
Workbench.
- Include the TIMER.C file in the project.
- In the project option, C-SPY category, use the TIMER.MAC
file as the setup file.
- Build the project and start C-SPY.
How it works:
When loaded into C-SPY, the TIMER.MAC will start
by setting breakpoints at - TCON that will execute
function _edit_TCON(); - TMOD that will execute function
_edit_TMOD(); It also sets a 4 byte wide breakpoint
starting at TL0. This breakpoint will cover TL0, TH0, TL1,
and TH1, and will execute the function _edit_TH_TL() when hit.
Finally, it also sets breakpoints at:
Timer0_int , that will
execute the function _check_timer( 0
);
Timer1_int , that will execute the function _check_timer( 1 );
For more information about the C-SPY macro functions,
please read the description below.
The C source file:
The TIMER.C file only consists of the main()
function.
First, the TRx is set to zero. This will enable the
timer until we have set the proper values in the timer-control SFRs.
Then, we set the timer values. In this
example, we set up timer 0 in mode 1, and timer 1 in mode 2.
TMOD = 0x21;
We also load the TLx and THx SFRs with the timer count
for our selected timers. To do this, we use the C macro
define GTV_Mm, where m is the mode.
tmp = GTV_M1( arr[cnt--] );
TL0 = 0xFF & tmp;
TH0 = 0xFF & (tmp>>8);
and
tmp = GTV_M2( arr2[cnt2--] );
TL1 = 0xFF & tmp;
TH1 = 0xFF & tmp;
Now, we enable timer 0, timer 1, and the global interrupt.
ET0 = 1;
ET1 = 1;
EA = 1;
Finally, we enable the timers to start the timer simulation.
TR0 = 1; /* Start timer 0. */
TR1 = 1; /* Start timer 1. */
The C-SPY macro file
TIMER.MAC consists of eight different functions:
_newInterrupt(timer,mode,spec)
Cancels old interrupts, and orders a new one for the timer
and mode selected. It checks the TLx and THx values to
determine the interrupt time. The spec parameter is set
if timer 1 runs in mode 3.
_updateCounter(TL,TH,mode,cnt,tl,th)
Updates the TLx and THx values when changes have been
done to the timer settings.
_edit_TCON()
Updates timers when a new value has been written to the
TCON sfr.
_edit_TMOD()
Updates timers when a new value has been written to the
TMOD sfr.
_edit_TH_TL()
Updates timers when a new value has been written to the
TLx or THx sfr.
_check_timer( timer )
Called when timer interrupts occur. Checks the timer
settings to see if a new interrupt time should be set for
the timer.
execUserSetup()
On setup the breakpoints are set at TCON, TMOD, TL0, TH0,
TL1, TH1, Timer0_int, and Timer1_int.
execUserReset()
On reset, all current interrupts are canceled.
Files included in this project:
- timer0m2.c
- timer0m2.mac
Building the project:
- Create a new, empty project in the IAR Embedded
Workbench.
- Include the TIMER0M2.C file in the project.
- In the project option, C-SPY category, use the TIMER0M2.MAC
file as setup file.
- Build the project and start C-SPY.
How it works:
This is a very simple example of how to use the C-SPY
macro language. The C source only enables the timer 0
interrupt, and starts running in a while( 1 ) loop. The C-SPY
macro file will do all the work as the timer setup and
timer simulator.
The TIMER0M2.MAC file consists of nine functions.
_timer0Mode2()
Reloads TL0 with TH0 and orders a new interrupt.
_timerSetBreakpoints()
Sets a breakpoint at the timer_interrupt() function.
_initTimer0Mode2()
Initializes TL0 and TH0, and sets the TR0 bit in the TCON sfr.
_timerInit()
Initializes the timer simulation.
_timerShutdown()
Cancels all interrupts and breakpoints.
execUserInit()
Called at C-SPY initialization.
execUserSetup()
Called at C-SPY setup.
execUserReset()
Called at C-SPY reset.
execUserExit()
Called at C-SPY exit.
Copyright 2001 IAR Systems. All rights reserved.