wayner Guest
|
changing tmr2 period |
Posted: Thu Aug 07, 2008 11:16 am |
|
|
hi;
i'm running a stepper motor and need to modify the timer2 period to vary the frequency of the pwm output. for testing i have hooked up a pot to the adc port to modify the pwm duty and frequency on the fly. when the output waveform is scoped, the duty changes correctly but the frequency does not change from its initialized state of 255. the adc is changing because i can sweep it from 0-255 on the lcd screen and the values display correctly but for some reason the timer2 period is not changed or it is overwritten.
does anyone know the ins and outs of modifying timer periods??, i've attempted various methods from doing it in the tmr2 isr...load the adc value into the timer2 period value.... to the code i have below doing it in the main loop and then expirementing with various delays but with no real success.
Code: | int value;
int period ;
int toggle;
int value = 255;
//Period 0.004096 sec
#int_RTCC
RTCC_isr()
{
delay_ms(50);
output_high(sclk_pin);
delay_ms(50);
output_low(sclk_pin);
}
//Period 0.1S
#int_TIMER1
TIMER1_isr()
{
}
#int_TIMER2
TIMER2_isr()
{
}
#int_AD
AD_isr()
{
}
void main()
{
setup_oscillator(OSC_8MHZ|OSC_NORMAL);
setup_adc_ports(AN0_to_AN1|VSS_VDD);
setup_adc(ADC_CLOCK_INTERNAL|ADC_TAD_MUL_0);
setup_wdt(WDT_OFF);
// setup_timer_0(RTCC_OFF);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_32|RTCC_8_BIT);
//setup_timer_1(T1_DISABLED);
//.5uS * 8 * 65536 = 0.262144S
setup_timer_1(T1_INTERNAL|T1_DIV_BY_8);
//period 0.1
set_timer1(0x9e57);
setup_timer_2(T2_DIV_BY_4, value, 1);
setup_timer_3(T3_DISABLED|T3_DIV_BY_1);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
// enable_interrupts(INT_AD);
enable_interrupts(INT_RTCC);
enable_interrupts(INT_TIMER1);
enable_interrupts(INT_TIMER2);
enable_interrupts(GLOBAL);
//===========================================
lcd_init();
while (true)
{
float lbf;
set_adc_channel(1);
delay_us(10);
value=read_adc();
duty =value/2;
set_pwm1_duty(duty);
setup_ccp1(CCP_PWM);
printf(lcd_putc, "\f%u",value);
lcd_gotoxy(1,2);
printf(lcd_putc, "%2.3f Lbf ",lbf);
}
} |
|
|