Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

MPLab repetitive stimulus

Status
Not open for further replies.

jpanhalt

Well-Known Member
Most Helpful Member
I am timing a 100 Hz square wave to determine its changing duty cycle and have been using MPLab SIM for simulation. Is there any way to inject a repetitive stimulus like a square wave?

My work around has been to use the pulse feature (e.g., 5 mS high, 5 mS low) and appropriately placed breakpoints. Ideally, it would be nice to write a little PWM routine in assembly and be able to use that for stimulus.

John
 
Use the "Clock Stimulus" tab in the workbook.
Select the pin you want the signal on, RB0 for example
Inital state (hi or low)
High time and Low time in cycles
Begin at start
End Never

Apply and save the workbook


When you run the program you can see the square wave on pin RB0 in the LA
 
Talk about feeling stupid. I just found it. I hadn't downloaded the User's Manual for the SIM.

Thanks, 3v0, for the patience.

John
 
There are no stupid questions but I still seem to ask them. Luckily, not here (yet). However, in other parts of life we all ask them OR, worse still not ask and make a fool of ourselves.

Everyone should ask any question they want an answer to. How else do we learn?

Have fun and learn.

Mike.
 
Last edited:
Like I implied in the first post, I have been using the asynchronous stimulus combined with stopwatch and register watch to get most of the program working.

But, I have been messing with the clock stimulus since my early post this morning and can't get it to work. Left alone, nothing seems to happen. When I hit the "apply" button while in Run or Halt , I get the error, "Failed to send synchronous stimulus to simulator." When I apply it during Animation, I don't get the error, but nothing happens on the watch registers. They are set to update continuously, but even when halting, there are no changes. With asychronous stimulus, the registers behave as expected.

I know the program works in part with a real chip. I was capturing and printing to an LCD nice counts for the high and low periods of a PWM signal. I then added a math routine from PicList to calculate duty cycle. It compiles, and I can get the program counter in Animate to go right through as I expected. Data output to the LCD is not what I expected, so I wanted to look at the registers used in the calculation, particularly the one used for remainder, to see what's going on.

Anyway, Google found some hits. One from Microchip was particularly informational . ;) It said that the clock stimulus can be tricky to set up (surprise) and referred to a Webinar on the subject. That Webinar has been removed by Microchip.

I started a new workbook just to have a good reference point. GP1 (aka Data_Y) is the input. Chip is 12F683. Assembly language. Timer1 is used, and data are collected as 16 bit in H and L registers.

The Clock Stimulus is set up just like 3v0 described. Cycle is 5000 for both high and low. Initial is high, begin at start , end never. Optional label = Data_PWM which is not used anywhere in the program.

Do I need to do anything under Pin/Register Actions? I added a GP1 signal, but that didn't help. Are any other steps needed?

Here is a snippet of the timer/count code (sorry for the formatting that gets a little messed up between computers):

Code:
Data_start
	btfsc	Data_Y		;check if low
	goto 	$-1		;keep checking
	btfss	Data_Y
	goto	$-1			;wait for first high
	bsf	T1CON,0	        ;start Timer1
	btfsc	Data_Y		;check for low
	goto	$-1			;keep checking
	bcf	T1Con,0           ;stop timer to read H_count
	movf	TMR1H,w		;IF NEEDED: add counts equivalet to 5 fosc/4
	movwf	H_ctrH      	;to T_ctrL, add count = 5/prescale value 
	movf	TMR1L,w		;
	movwf	H_ctrL	;
	bsf	T1Con,0   	;start timer
	btfss	Data_Y		;add low to timed period
	goto	$-1			;keep checking
	bcf	T1CON,0	        ;stop Timer1

John
 
I just used a demo for a keypad I had on the system. C18 on the PIC18F2585.

With the simulator halted I created the workbook.
Recompile the program.
Opened the LA window
Added my pin RB0 to the LA signals
Hit run
Hit stop
the LA window showed the signal.

Could well be that something is different with the 12F. I doubt the actual program matters much. If you could provide a program that compiles and runs I will give it a try when I get back from brunch. Or even a project zip. That way I could look at the workbook.
 
Last edited:
Will do. Do you want the program as a PM or here? Doesn't matter to me -- it is not a secret. I will strip the math routine, as the program worked on a real chip just as expected without the math.

John
 
Here it is. Sorry it is really messy. It is a work in progress. I used goto's to avoid the sections that I am not sure work; although, they do compile when included (i.e., the 24 bit by 16 bit divide and 16 bit to unpacked BCD). The Hemsley 12 bit to BCD should run and works well.

John

View attachment 60305

Here is the complete project file zipped. Oh, by the way, ignore the Solid Works comment, if it appears. SW uses ASM files for something else and I can't get my PC to ignore that extension. The ASM file here is Assembly.

View attachment 60306
 
Last edited:
My tools are on E so after changing the tool paths and making the compile case insensitive I got it to build. The stimulus is working.

View attachment 60308

I saved the stimulus workbook as well as the HEX file. You should be able to unpack this zip and run it without building(I hope). Maybe after applying the workbook file which is stime1000square.

Edit: the Max PC address error as it was there because I tried to run with out building the project. You must be missing something in the setup. So it is down to insulting questions time. Have you clicked trace all in the LA setup. That sort of thing. Many you can find it by looking at the settings in this project.
 
Last edited:
Sorry about not warning you on the case sensitivity thing. I leave it disengaged unless I am trying to make the code pretty. This code is definitely not pretty.

On the good news side, while I was at the grocery store, I had an epiphany as too what was wrong in the code. Late night and no food led me to put the (blank) register 'H_ctrU' highest instead of lowest argument where it should have been for the 24-bit dividend of the Golovchenko code -- I wanted to multiply, not divide. Anyway the program now works and gives an output consistent with what I expected. It is amazing how such obvious things are so easily missed until you start doing something else.

That little success makes me even more anxious to get the stimulus working so I can watch and verify the calculations are correct. I will give an update after I get that done.

Thank you so much for running this code and getting the stimulus to work.

John

*H_ctrL and H_ctrH are the low and high bytes for counts while GP1 is high. Thus, they are divided by the total counts in Timer1's registers to get duty cycle. Since duty cycle is with this sensor about 0.5 ±0.1 and the changes are small, the important differences are in the remainder. That is why I multiply the dividend.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top