2007 19 Nov
Now that my lab tools are all set I need to test my Pic16F628A to get a PWM signal. This signal will later be applied to the H-Bridge circuit that will control DC motors power. For the test I’ve used this circuit:
For the XSC1 I’ve connected my Velleman Instruments Digital Oscilloscope PCS500.
I’ve used the following code:
-
#include <16F628A.h>
-
#use delay(clock=4000000)
-
#fuses NOWDT, HS, PUT, NOPROTECT, BROWNOUT, MCLR, NOLVP, NOCPD
-
//————————————————————————————————————————————————————————————
-
// PWM signal to be generated on pin9 -> RB3/CCP1
-
//————————————————————————————————————————————————————————————
-
void main(void)
-
{
-
// Value = 0 Duty-Cycle (pulse width) = 0
-
// Value = 64 Duty-Cycle (pulse width) = 50%
-
// Value = 128 Duty-Cycle (pulse width) = 100% -> DC Signal
-
int8 value = 0; delay_ms(100); // power up delay
-
-
// will hang on debug but necessary to turn of comparators
-
setup_comparator(NC_NC_NC_NC);
-
setup_vref(FALSE); // turn of ADC channels
-
set_tris_b(0b11110000);
-
setup_ccp1(CCP_PWM); // Configure CCP1 as a PWM
-
// (1/clock_freq)*4*Divider*128 = cycle time
-
// (1/4000000)*4*16*128= 2.048 ms or 488.281 Hz
-
setup_timer_2(T2_DIV_BY_16, 127, 1);
-
-
while( TRUE )
-
{
-
set_pwm1_duty(value);
-
// only for debug - will blink led every 0.5 second
-
// and increment Duty Cycle every second
-
output_low(PIN_B0);
-
delay_ms(500);
-
output_high(PIN_B0);
-
delay_ms(500);
-
value = value + 1;
-
}
-
}
-
//————————————————————————————————————————————————————————————
This code will generate a 2.048 ms square wave (488.281 Hz) with adjustable Duty-Cycle (pulse width) from 0% to 100%.(DC)