/*************************************************************************
Fibonacci
In a Fibonacci series each number is the sum of the two previous numbers.
This program calculates Fibonacci numbers within an 8-bit range,
Files c018i.o and p18f2420.lib are included by the Linker Script.
Program intended for simulation only, hence no input/output.
TJW 21.10.05 retested 23.5.09
;************************************************************************/
#include
//these memory locations hold the Fibonacci series
unsigned char fib0; //lowest number
//(oldest when going up, newest when reversing down)
unsigned char fib1; //middle number
unsigned char fib2; //highest number
unsigned char fibtemp; //temporary location for newest number
unsigned char counter; //indicates which value series has reached
void main (void)
{
fib0 = 0;
fib1 = 1;
fib2 = 1;
counter = 3; //have preloaded the first three numbers, so start at 3
loop:
do
{
fibtemp = fib1 + fib2;
counter = counter + 1;
//now shuffle numbers held, discarding the oldest
fib0 = fib1; //first move middle number, to overwrite oldest
fib1 = fib2;
fib2 = fibtemp;
}
while (counter<12);
//when reversing down, we will subtract fib0 from fib1 to form new fib0
do
{
fibtemp = fib1 - fib0; //latest number now placed in fibtemp
counter = counter - 1;
//now shuffle numbers held, discarding the oldest
fib2 = fib1; //first move middle number, to overwrite oldest
fib1 = fib0;
fib0 = fibtemp;
}
while (fib0>0);
goto loop;
}