Math functions

These programs are intended for an ATMEGA8 CPU, but it should not be too difficult to adapt them for other AVR CPU's.

divide by 10 and modulo 10


#include <stdio.h>
#include <stdint.h>

int divmod10(uint8_t x)
{
	int8_t quot, rest;
	uint16_t r0r1;

	r0r1=x*0x1a;
	quot=(r0r1 >> 8);
	r0r1=quot*10;
	rest=x;
	rest-=(r0r1 & 0xff);
	if(rest < 0) {
		quot--;
		rest+=10;
	}
	printf("%i / 10 = %i, rest %i\n", x, quot, rest);
}

int main(void)
{
	int i;

	for(i=0; i<256; i++) {
		divmod10(i);
	}
	return 0;
}