#define ACT_PWM PIN_B1
#define ACT_EXTEND
output_bit(ACT_DIR,1); \
output_bit(ACT_PWM,1);
#define ACT_RETRACT
output_bit(ACT_DIR,0); \
output_bit(ACT_PWM,1);
#define ACT_STOP
output_bit(ACT_PWM,0)
void main()
{
setup_adc_ports(NO_ANALOGS|VSS_VDD);
setup_adc(ADC_OFF|ADC_TAD_MUL_0);
setup_spi(SPI_SS_DISABLED);
setup_wdt(WDT_OFF);
setup_timer_0(RTCC_OFF);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
set_tris_b(0b11111100); //set RB0 and RB1 as outputs in binary
ACT_RETRACT;
delay_ms(3000);
ACT_STOP;
do{
ACT_EXTEND;
delay_ms(2000);
ACT_STOP;
delay_ms(2000);
ACT_RETRACT;
delay_ms(2000);
ACT_STOP;
delay_ms(2000);
}while(1);
}
//make sure actuator is retracted
//retract for 3 seconds
//stop the actuator
//extend the actuator
//extend for 2 seconds
//stop the actuator
//stop for 2 seconds
//retract the actuator
//retract for 2 seconds
//stop the actuator
//stop for 2 seconds
I don’t think you’re having any trouble
following the logic of the Firgelli linear actuator
driver code we’ve assembled. Once all of the
housekeeping is done, our C actuator driver
makes sure that the actuator begins in a
retracted position. The C code contained
within the do/while loop executes forever as
while(1) is always TRUE. The actuator will
extend for two seconds, stop for two seconds,
retract for two seconds, and stop again for
two seconds before repeating the sequence.
C(ing) the Light
The real beauty of our C project is that
anyone can cold-read our C source code and
immediately know what the application is
doing. Our liberal use of #define commands,
statement shorthand, and C macros makes our
Firgelli linear actuator driver application easy to
read and understand. The code we’ve written
is almost self-commenting. Who says C is hard
to learn? SV
SERVO 08.2010 73