
261
Chapter 13, How to Make RC Cars Play Laser Tag
Exhibit B: RC Car Hack Software Code
// radio control car hack
// (4/26/03 Scott Fullam
// feel free to modify this code
#include <P16F876A.h>
#define var_res_clk 0x01
void gen_2khz(int cycles);
void init_code();
void check_hit();
void check_fire();
char hit_count;
char shot_count;
void update_score(char num);
#pragma CLOCK_FREQ 1600000
// #pragma RESERVE_ADDR_0 // add this line back in if you are using the ICD2 and debugging code
void main()
{
init_code();
while(1)
{
check_fire();
check_hit();
}
}
void init_code()
{
// set pr2 divider to 25. this plus the prescaler value of 4 will generate
// a 38461 kHz out from CPP1 using a system crystal of 16 MHz
pr2 = 25;
ccpr1l = 0x0D;
// set the diver bits but do not set PWM mode yet
ccp1con = 0x00;
// set up portc 2 as an output, 0,1 as inputs
trisc = 0x03;
// set prescaler to 4 and switch timer 2 on
t2con = 0x05;
// porta 2-5 as outputs. the others as inputs
// set adcon1 so that all pins are digital I/O
adcon1 = 0x07;
trisa = 0x00;
// set up timer1 for 2 kHz (or 3 kHz) modulation generation
t1con = 0x20;
hit_count = 0;
Exhibit B
ch13_rc.indd 261
1/22/2002 4:44:43 PM