|
DPRG: Keil Brief Software Review
Subject: DPRG: Keil Brief Software Review
From: Jim Brown
jbrown at why.net
Date: Wed Feb 26 18:35:39 CST 1997
The day before yesterday, I installed the Keil Software. I had
no problems at all. I was up and running in a matter of a minute or two.
It appears that all of the examples are there, unlike the problem we
had with the dongle version. Compiling, linking, and debugging are a
breeze. I looked through the examples and tried them out. The debugger is
great! The debugger has a serial window too so that you can simulate
talking to your program with a serial tty. When you hit "Go" in the
debugger, it outputs text to the serial window, you also can input
text on the serial window, just as if you were on a tty. It's really slick.
After looking over the examples and feeling pretty comfortable with
it, I decided to rewrite the Baby Robot code in Keil C. Below you'll
find the source attached. After doing a "make", the hex output file
was about 400 bytes! I was expecting much more! Maybe my 8K EPROM has
a chance after all! I tried optimizing for size, but it didn't seem
to change at all, so I switched it back to optimizing for speed.
BTW, I haven't tried my code yet to see if it will run on my
Baby Robot yet, so if you see any bugs in the code below, it's
because it's not tested out. It compiled though, so there's either no
syntax errors, or else Keil C didn't notice them. Feel free to
"fagan" it if you want! <grin>
The Keil C looks like a real goldmine!!!!!!!!
----------- my sample source code follows -----------------
/*---------------------------------------------------------------+
| Baby Robot two |
| |
| Author: Jim Brown |
| |
| Revision History: |
| 02/25/97 Created |
| |
+---------------------------------------------------------------*/
/*
p1.0 = mot1 (left) forward
p1.1 = mot1 (left) backward
p1.2 = mot2 (right) backward
p1.3 = mot2 (right) forward
LEFT MOTOR RIGHT MOTOR
+---------------+ +---------------+
| | | |
p1.0 ------+--O O--|--+ p1.3 ------+--O O--|--+
| | | | | | | |
+--(MOT)--+ | | +--(MOT)--+ | |
| | | | | | | |
p1.1 ------+--O O--+ | p1.2 ------+--O O--+ |
| | | |
+------------------+ +------------------+
*/
#include <reg51.h>
/* one second = ~125 ticks */
#define ONE_SECOND 125
/* define motor masks for direction */
#define FORWARD 0x09
#define BACKWARD 0x06
#define LEFT 0x0a
#define RIGHT 0x05
#define STOP 0x00
/* global variables */
int ticker = 0; /* count down timer */
int mask = STOP; /* current direction */
int on_or_off = 0; /* current pwm state */
int pwm_on_time = 0; /* amount of pwm on_time left */
int pwm_off_time = 0; /* amount of pwm off_time left */
int on_time = 0; /* amount of pwm on_time to use */
int off_time = 0; /* amount of pwm off_time to use */
/*----------------------------------------------------------------+
| This function is called when the 8051 overflow interrupt 0 |
| occurs. On a 12mhz clock in 13bit overflow mode the overflow & |
| interrupt will occur every 8192 us or about 125 times a second. |
| This function will be used to keep track of the current count |
| down time and for PWM timing of the motors. |
+----------------------------------------------------------------*/
void timer_interrupt(void) interrupt 0
{
/* count down timer */
if (ticker>0) ticker--;
/* is it currently on or off? */
if (on_or_off == 1)
{
/* it's on */
/* have we run out of on time? */
if (--pwm_on_time <= 0)
{
/* switch to off */
pwm_off_time = off_time;
on_or_off = 0;
P1 = STOP;
}
} else {
/* it's off */
/* have we run out of off time? */
if (--pwm_off_time <=0)
{
/* switch to on */
pwm_on_time = on_time;
on_or_off = 1;
P1 = mask;
}
}
}
/* set up timer 0 for 13 bit overflow and interrupt every 8192 us */
void init(void)
{
P3 = 0x00; /* turn port 3 off */
P1 = 0x00; /* turn motors off */
TMOD = 0x00; /* set timer 0 for 13 bit overflow */
TCON = 0x10; /* start the timer 0 going */
/* overflow will occur every 8192 us */
IE = 0x82; /* make the overflow cause an interrupt! */
/* 80H means to allow interrupts */
/* 02H means to enable timer 0 interrupts */
}
/* determine the direction mask and PWM speed */
void set_direction_n_speed(int direction, int percent_speed)
{
mask = direction;
on_time = percent_speed >> 4; /* gives 7 possible speeds 0 16 32 48 64 80
96 */
/* 0% 17% 33% 50% 67% 83% 100% */
off_time = 6 - on_time;
ticker = ONE_SECOND >> 2;
while (ticker > 0){}
mask = STOP;
}
void main (void)
{
int i;
init();
set_direction_n_speed(STOP, 0);
while (1) /* forever */
{
for (i = 0; i<100; i+=16) /* speed up */
set_direction_n_speed(FORWARD, i);
for (i-=16; i>= 0; i-=16); /* slow down */
set_direction_n_speed(FORWARD, i);
}
}
-------------------------------------------------------------------------
Jim Brown jbrown at why.net or jbrown at spdmail.spd.dsccc.com
http://users.why.net/jbrown
http://www.dprg.org (next meeting is March 22nd) Bring robots!
Rom 3:23,24 http://users.why.net/jbrown/pcb.htm
More information about the DPRG mailing list
|