View Full Version : MPLAB simulation
Chippie
Nov 03, 2005, 02:01 PM
Does anyone have a step by step tutorial for running a pic program in simulation mode in MPLAB please?
I'm having probs with my code and although it compiles my tacho circuit isnt working...
I've tried some other code in my pic, so my programmer and pic are ok...the hardware I've built on breadboard is ok too...I've verified that too by using the code and modding the hardware slightly( using Com Anode 7seg displays in place of com K )
Acetronics
Nov 04, 2005, 03:02 AM
Hi, Chippie
Did you try at first the NOT MODIFIED code with original drawing ??? one can find tacho programs full of bugs, on the Web :D .
seriously, MPLAB simulator is not so handy for such debugging ... in lasts MPLAB releases ( 7.2x ) it became usable.
The ICD2 Debugger would be much much better for this ...
May be you could give me your download address ... i'd have a look to it.
;) No problem : as you could have seen, I've developped my own tacho ... totally bug free !!!
Alain
TugBoat
Nov 05, 2005, 05:43 AM
Actually, I find the MPLABS simulator quite good. The most difficult part is to get the input signals correct. It is fairly intuititive once you get going. Here is what I do:
1) With your workspace active use Debugger->Select Tool->MPLAB SIM to activate the simulator.
2) Get the debugger settings Debugger->Settings and then click on 'Break Options'. Make sure that the following are set: 'Global Break Enable', 'Enable WDT Expiration Warning', 'Stack Enable', 'Break on Stack Full/Underflow', 'Break on WDT timeout' and click OK. (This makes sure the simulator stops on most unusual conditions that you normally want to know about.)
2a) You can also set the clock speed under 'Clock', just remember that if you alter this from 4MHz then the following simulation stimulus examples will require different numbers.)
3) Now the tricky part. You need to setup the input signals for the simulation. Bring up Debugger->Stimulus. You can configure two types of inputs, 'sync' signals or 'async' signals. The async ones happen when you click the button on the Stimulus window, the sync ones just toggle away based on the simulation clock.
3a) So let's suppose you want a pin (say GP4) to be high for 18.5msec and low for 1.5msec. In the Stimulus pane click 'Add Row', the click on the 'Async' of the added row and change it to 'sync', then make sure the signal is enabled by clicking the check box, then set the pin to GP4, then type 18500 into the high cycles and 1500 into the low cycles (this assumes 4MHz clock). Then click 'Edit Complete'.
3b) You can also add stimulus inputs that change when you click. For example, let's say we want to be able to force GP3 input high and low to simulate a push button switch. Click 'Add Row', click in the 'pin' column just after th word 'fire' and then select GP3 as the pin set the action to 'high', then click 'Edit Complete'. Now repeat this process but select action 'low' to get another input. You should now have two stimulus lines, each with a 'fire' button that allows you to trigger the action. (ie. one for GP3 high and one for GP3 low).
4) Now make sure that the main program source module window is selected and push 'F10'. The source will be assembled and you should see a green arrow pointing at the instruction in location 0000h of your program. The green arrow is the current PC.
5) You can step your program one instruction at a time by pressing F7. You should see the arrow move one instruction at a time.
6) If you hold the cursor over a RAM variable in your program the debugger will pop up a little 'balloon' with the current value of the variable. This also works for the special registers as well. If you just step through the first few instructions of your program and hold the mouse over variables as it goes you will see what is happening.
7) You can insert a break-point by just double left clicking on a line of source. A 'B' in a red hexagon will appear on the left. (Where the green arrow lives.) Once you have a number of break-points inserted you can get the simulator to run until a breakpoint by pressing F9. (Double left click a second time will remove the breakpoint.)
7a) While the programming is running you can fire async stimulus (say toggle GP3) by clicking on the 'Fire' button in the Stimulus window. You can also edit the 'sync' stimulus on the fly and change the input waveform, just click in the 'High cycles' (or 'low cycles'), change the number and then click 'Edit Complete'.
7b) If the program just keeps running and you want to know where it 'is' just press F5 to halt execution at the current point.
8) There is a useful 'stopwatch' window available under Debugger->Stopwatch. You can zero the stopwatch when some even occurs, say you get to breakpoint A, then when the program stops a breakpoint B you know how long it has taken.
9) Under 'View' there are a number of other useful windows for simulation:
9a) View->File Registers shows the current RAM and special registers in Hex format.
9b) View->EEPROM shows the EEPROM (if you are using it)
9c) View->Special Function Register shows the CPU registers in a more friendly format. You can edit the data in the registers by clicking in the appropriate area (say GPIO in Binary) and just altering the value.
9d) View->Watch This is a nice function that allows you to add variables (select them in the list and click 'Add Symbol') to a panel of variables to watch. There are 4 sets of watch variables.
Notes: The A/D is not supported and if you are debugging programs that use A/D you need to insert a breakpoint in your code where it triggers the A/D and manually edit the CPU registers with the A/D value you want to use.
I hope this helps. I am sure if you just play with it a bit you will get the hang in no time.
Tim
Chippie
Nov 05, 2005, 06:47 AM
Excellent info Tim! ;) many thnx.
Off to play...... :)
later that day...........
guyz,
thnx for all the help....I've sorted it...nothing wrong with the code.
I had a few changes to make to my breadboard and put a few wires in the right place, add a pulse signal and all is well...oh joy!
Just need to make a few tweeks here and there to the code....Now watch me screw it up!!
vBulletin® Copyright ©2000-2009, Jelsoft Enterprises Ltd.