Skip to content

Software PWM VIII

October 12, 2014

Having eschewed premature optimization I, of course, dug in and spent a couple of hours at it. After much thought I did the obvious and moved the pwmstep routine into the pwmcycleN routine and made step a local instead of a global. I also made a local pointer to the toggle table which will go into a register and i moved the toggle table reset to before step 0 instead of after step 7. This has the effect of making analogWrite effective on the next pass. The time per pass is down to 4.3 ms/pass which I could live with. A bit over .3ms is the initialization of the toggle table from the duty cycle variables on each pass. I can fix this i’m sure but I WON’T DO IT NOW! I also note that this algorithm doesn’t seem to allow for a 100% duty cycle. The toggle always goes in the table *somewhere*. There’s not a lot of penalty for a bigger table though so maybe i’ll do something later.

void pwmcycleN(unsigned int howmany){ //run howmany*8 steps of the software pwm
	unsigned int step; //toggle table index
	unsigned char * togglep=toggle; //maybe togglep will be in a register
	while(howmany-->0){
		togglep[dct[0]]|=1; //set the end of the duty cycle for bit 0
		togglep[dct[1]]|=2; //set the end of the duty cycle for bit 1
		togglep[0]^=~PIN4; //set up with current state of port4 low bits
		togglep[0]&=0x03; //only the pwm bits are set
		for(step=0;step<8;step++){
			PIN4^=togglep[step];	//toggles the bit off if the duty cycle is over
			out(4,PIN4);
			printf("dct[0]=%cx, step %d, toggle[%d]=%cx, PIN4=%cx\n",dct[0],step, step, toggle[step],PIN4);
			togglep[step]=0;
		}
	}
}
Advertisements

From → Uncategorized

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: