Skip to content

Accommodating different processor speeds

July 25, 2014

The Bagels Server runs very well on the 4mhz membership card and hardware spi. It brings up the question though of how to deal with the same code running on different processors. The Arduino has a “boards.txt” file that specifies things like cpu speed and eventually translates into a C++ #define like cpuspeed 16000000l for 16 mhz. The way the delay() function in my “olduino.c” works is to call an assembly routine “onems()” that takes one millisecond by chewing through 100 instructions including call/return overhead.

My first pass ugly idea is to have an optional define #4mhz or similar that could change this behaviour like

void delay(unsigned int howlong){
	unsigned int i;
#ifdef mhz4
	for (i=1;i!=howlong;i++){

That would work although it’s syntactically ugly but there’s one big issue: A 16 bit unsigned int has a maximum of 65,536 so a delay of >26 seconds would give anomalous results. I can explain a limit of 65 seconds, but not 26!

So I have to affect the onems() routine. Now that’s in the file and looks like this

_oneMs:		;execute 100 instructions including call(15)/return(10) sequence. takes about 1 ms
	ldi	(100-15-10-2)/2
$$mslp:	smi	1
	bnz	$$mslp

The assembler has an IF/ELSE/ENDIF construct so I could change that to something like:

_oneMs:		;execute 100 instructions including call(15)/return(10) sequence. takes about 1 ms
	SET	howmany	(250-15-10-2)/2
	SET	howmany(100-15-10-2)/2
	ldi	howmany
$$mslp:	smi	1
	bnz	$$mslp

For a tryout I can set that variable from an asm(“SET MHZ4 1\n”); in open code anywhere in the C program. In principle, this has a gotcha limit too but getting howmany up above 255 would take a 32mhz 1802 so i think i’m safe.


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 )

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: