Skip to content

Software PWM V – Toggle Table in Action

October 10, 2014

Here’s the results of the software pulse width management viewed from the saleae logic analyzer. The top trace, PWMB was set for 25% duty cycle. The bottom trace, PWMA was set for 50% duty cycle. So it works – Yay. A couple of things jump out though: one pass through the toggle table is taking about 6ms – that’s 1500 instructions. I was hoping it would be more like 250 instructions – 1ms. Secondly, the beginning state of the lines is 1 rather than 0 – that wouldn’t work well for a motor driver which would interpret it as full speed ahead – i’m not sure what’s going on there.
14-10-10 saleae

Here’s the code as it stands at close of day today:

#include <olduino.h>
#include <nstdlib.h>
#define putc(x) out(7,x)
#include "softpwm.h"
//softpwm.c routines for olduino software pulse width management.
void analogWrite(unsigned int bit, unsigned int val){
	if (bit<2){
		dct[bit]=val>>5; //step down range to 0..7
void pwmstep(){	//run one step of the software pwm cycle
	PIN4^=toggle[step]; out(4,PIN4); //toggles the bit off if the duty cycle is over
	if (8==step){//if we're done a full pass
		step=0;	//reset the index
		toggle[dct[0]]=1; //set the end of the duty cycle for bit 0
		toggle[dct[1]]=2; //set the end of the duty cycle for bit 1
		toggle[0]^=~PIN4; //set up with current state of port4 low bits
		toggle[0]&=0x03; //only the pwm bits are set

void pwmcycleN(unsigned int howmany){ //run howmany*8 steps of the software pwm
	unsigned int i; //temp

void main(){
	analogWrite(0,64);//25% duty cycle
	analogWrite(1,128);//50% duty cycle
	asm("	seq\n");
	analogWrite(0,128);//50% duty cycle
	asm("	req\n");
#include <olduino.c>
#include <nstdlib.c>

From → Uncategorized

Leave a Comment

Leave a Reply

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

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

Google photo

You are commenting using your Google 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 )

Connecting to %s

%d bloggers like this: