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 C18, Added a Nop() to loop, decreased execution time?

Status
Not open for further replies.

Fred.Amoson

New Member
I'm writing a program that outputs a sine wave to DAC. I'm trying to add a function that allows me to set at what frequency the loop that makes the sine wave runs at, so I can set the frequency. In testing, I simply added a Nop() command to the loop. On my first attempt, it reduced the frequency by .6 Hz, when I added another one it reduced it another .6 Hz. It is now at 203.1 Hz. I added another and it went to 204.6 Hz! It actually sped up.

Can someone explain that one to me. Do I need to turn off some sort of code enhancing or something to get the timing function to work right?

Thanks!
 
Does this Nop() command take parameters like Nop(4)? What ever is compiling the code may treat successive equal commands differently from what you imagine.

Look at the ASM equivalent. The first spot to check is the Program Window "MPLab > View > Program Memory", then the .lst file. The Program Window is the live asm breakout box equivalent of the static .lst file.
 
Things can get faster when you add more nops if you have something that gets in sync with something else. Eg, if you are transmitting over RS232 you may miss the buffer free flag by reading it one cycle early and have to do another loop. Is there anything like that in your code? Can you post the code?

Mike.
 
I recall reading about this over on Forum.Microchip a long while ago. Seems if you add a single ASM instruction to a routine the compiler starts generating bunches of "load bank select register" instructions within that code.

Mike
 
donniedj - The Nop() node doesn't actually take parameters, and is (As far as I know) exactly equivalent to the ASM Nop() command, which is just a no operation clock cycle.

Thanks for the info Mike, that is good to know. I've looked at the dissasembly for this particular program, and it appears to be correct, just a bunch of Nop() codes.

Pommie - You probably hit the nail on the head. I am using a MCP4922 DAC chip, with an SPI interface in the loop, and it is probably what is causing the issue. I've attached the code incase anyone has any suggestions on how to fix this problem:

Code:
        for (x = 0; x < 127;  x++)
	{
		output = sinWave[x];
		byte1 = output >> 8;
		byte1 = byte1|0b00110000;
		byte2 = output&0x00ff;

		CS = 0;
		SSPBUF = byte1;
		while(!SSPSTATbits.BF);
		byte1 = SSPBUF;
		SSPBUF = byte2;
		while(!SSPSTATbits.BF);
		byte2 = SSPBUF;
		CS = 1;	

		Nop();
		Nop();
		Nop();
		Nop();
		Nop();
		Nop();
		Nop();
		Nop();
		Nop();
		Nop();
	}
 
The simplest way to fix the problem would be a timer. Do you know how many cycles are required between outputs? If you have Timer2 free then you can set PR2 to the number of cycles (probably with a postscaler) and just wait for PIR1bits.TMR2IF at the top of your loop.

Mike.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top