Sounds very good!!! I like it Mike...BUT won't be easy for me but I think I can try to cook something...Use a four (4) element array of "duty cycle" and TRIS values for the four (4) unique steps... 60 steps gets you 360 degress... Maintain a "position" variable and increment or decrement it by one, according to a "direction" flag, when you get a new "step" flag. Use "position % 4" as the array index...
#int_timer2 // 125 Hz
tmr2_isr()
{
tick = 1;
}
main(){
int ct, stepc;
set_tris_a(0b00111111); //
set_tris_b(0b00010000); //
set_tris_c(0b00000000); //
ansel = 0b00000000;
anselh = 0; // No analogs
bit_clear(option_reg, 7); // Global enable for weak pullups
setup_timer_2( T2_DIV_BY_16, 100, 5); // 125 Hz (1000000/(16 * 100 * 5))
bit_set(intcon, 6); // Periph enable
enable_interrupts(INT_TIMER2); // Interrupt on timer 2
enable_interrupts(GLOBAL);
ccp1con = 0b00001101; // PWM, B and D are inverted
portc = 0;
while (1)
{
restart_wdt(); // Feed the watchdog
if (!tick)
continue; // Should be 125 times/sec
tick = 0;
if (++ct < 125)
continue;
ct = 0; // We get here once per second
switch(stepc)
{
case 4:
pstrcon = 0b00000011;
ccpr1l = 100;
break;
case 3:
pstrcon = 0b00000011;
ccpr1l = 25;
break;
case 2:
pstrcon = 0b00000110;
ccpr1l = 58;
break;
case 1:
pstrcon = 0b00001100;
ccpr1l = 58;
break;
case 0:
pstrcon = 0b00001001;
ccpr1l = 25;
}
if (++stepc >= 5)
stepc = 0;
}
}
setup_timer_2(T2_DIV_BY_16, 100, 5); // 125 Hz (1000000/(16*100*5)
/*
* setup TMR2 for 1.6 ms PWM period and 8 ms interrupts (4 MHz clock)
*/
T2CON = 0b00100110; // -0100--- TOUTPS<3:0> postscale 5
// -----1-- TMR2ON "on"
// ------10 T2CKPS<1:0> prescale 16
// generate 16 us timer 'ticks'
PR2 = 100-1; // period = 100 x 16 us 'ticks' (625 Hz)
Rich (BB code):/* * setup TMR2 for 1.6 ms PWM period and 8 ms interrupts (4 MHz clock) */ T2CON = 0b00100110; // -0100--- TOUTPS<3:0> prescale 16 // -----1-- TMR2ON "on" // ------10 T2CKPS<1:0> postscale 5 // generate 16 us timer 'ticks' PR2 = 100-1; // period = 100 x 16 us 'ticks' (625 Hz)
Yes, you're correct... the comments are backwards... will fix it in other post...Mike, are you sure of your T2CON configuration?
isn' it TOUTPS<3:0> for postcale and T2CKS<1:0> for prescale?
main
call pwm_dutystep
0000 00527 .................... setup_timer_2( T2_DIV_BY_16, 100, 5); // Should be 125 Hz (1000000/(16 * 100 * 5))
006F 3020 00528 MOVLW 20
0070 00F8 00529 MOVWF 78
0071 0878 00530 MOVF 78,W
0072 3806 00531 IORLW 06
0073 0092 00532 MOVWF 12
0074 3064 00533 MOVLW 64
0075 1683 00534 BSF 03,5
0076 0092 00535 MOVWF 12
There's nothing magic about the 625Hz frequency. It was just convenient for the code, and I thought it was as good as anything. That audible noise isn't very pleasant, and now I'm wondering if a higher frequency would be better. But then, that would be tougher on the driver components, and whatever else it is, the ULN2003 isn't a sophisticated chip, and I don't know what its limits are. But those percentages are what Microsoft Excel calculated for me, and the video suggests they're pretty much right. They wouldn't change even if you use a different frequency. And yes, bits 2-5 of Port C are the pins you need to connect to.@John P: Bravo!! i watched that video...good job!
so if getting this right, i should use a 625Hz PWM frequency and 100%, 25%, 58%, 58%, 25% for step1, step2, step3, step4, step5 respectively, ?
but the output/input pins configuration for the above steps still stands?
regards,
by Duane Benson
by Duane Benson
by Aaron Carman
by Jake Hertz