DIFFERENT BITS
Listing 1
#include <16F877A.h>
#use delay(clock=20000000)
#fuses HS,PUT,NOWDT,NOLVP,NOBROWNOUT
#use rs232(stream=1,baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)
#use rs232(stream=2,baud=9600,parity=N,xmit=PIN_C4,rcv=PIN_C5,bits=8)
#use rs232(stream=3,baud=9600,parity=N,xmit=PIN_C2,rcv=PIN_C3,bits=8)
#use rs232(stream=4,baud=9600,parity=N,xmit=PIN_C0,rcv=PIN_C1,bits=8)
Listing 2
#include “C:\Program Files\PICC\projects\hereditary language\language1.h”
#include <stdlib.h>
//port c is 4 serial ports
#define button3 PIN_B4
#define button2 PIN_B3
#define button1 PIN_B2
#define button0 PIN_B1
#define pop_size 4
#define dna_size 6 //# letters in our word
#define divisor (32,767/(pop_size-1))
//global vars
char mutation_rate=25; //%of bits to be randomly mutated
char pop[pop_size][dna_size];
char pop_new[pop_size][dna_size];
//function protos
void quit(void);
void put_values(void);
void breed(int index_winner);
Listing 3
void main() {
int i,j,in,breed_index;
long seed;
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_OFF);
setup_spi(FALSE);
setup_counters(RTCC_INTERNAL,RTCC_DIV_2);
setup_timer_1(T1_INTERNAL); //start timer 1
setup_timer_2(T2_DISABLED,0,1);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
//wait a second for spark fun splash screen
delay_ms(1500);
of options to choose from, and to
keep the cost of display components
to a minimum. Third, due to the smaller
population I am using a different
breeding method. In the prior example,
we used proportional selection: a
method whereby members of the
population have a probability of
mating proportional to their fitness.
In this example, our population is so
small I allow every member to breed
once with the winner. This makes the
winner’s traits dominant while still
allowing other characteristics to
remain to a lesser extent.
There are a few other changes:
We are using four serial ports and
four serial LCD screens instead of
just one. I created a new random seed
routine based on a suggestion from
the CCS PIC C company (which some
of you might remember from my July
column “Random Bits”). Finally, unlike
goal-driven evolution there is no end
to this process so it will continue ad
infinitum, or at least until the power
is turned off.
In brief, the project works by
creating a population of four random
words, each of which is displayed on a
text-only serial LCD screen (available
from sparkfun.com). Each screen has
an associated pushbutton, and when
you select a screen by pushing the
appropriate button, that word is
deemed the winner of the generation.
All four words then mate with the
winner using crossover and mutation
to create the next generation of
words, and the process repeats.
The Circuit
//test serial ports
//clear screens
fputc(254,1);
fputc(1,1);
fputc(254,2);
fputc(1,2);
fputc(254,3);
fputc(1,3);
fputc(254,4);
fputc(1,4);
delay_ms(25);
//test msg out
fputs(“hello”,1);
fputs(“hello”,2);
fputs(“hello”,3);
fputs(“hello”,4);
delay_ms(1500);
70 SERVO 09.2008
The circuit is simple, but the
breadboard becomes very busy with
components quickly so it is advisable
to be as neat as possible (neater than
me!) with your component placement
and hookup wires. The PIC is hooked
up as usual with a programming
connection, 10K resistor going high
on MCLR, and a crystal or resonator
hooked up appropriately. Port B is our
input port so we hook four pins
(B1–4) up to our pushbuttons going
high on the other side. We use a