View previous topic :: View next topic |
Author |
Message |
luckyluke
Joined: 18 Apr 2006 Posts: 45
|
is these same? |
Posted: Sun Feb 17, 2019 4:34 am |
|
|
hello
i am controlling two motors with h-bridge controller.
i want to ask a question about frequent update of pwm duty.
Code: |
#include <18f2520.H>
#fuses XT, NOWDT,NOBROWNOUT,PUT,NOLVP,NOMCLR
#use delay(clock=4000000)
void main(){
setup_timer_2(T2_DIV_BY_4,200,6);
setup_timer_1 ( T1_INTERNAL | T1_DIV_BY_1 );
setup_ccp1(CCP_PWM);
setup_ccp2(CCP_PWM);
while(1){
while (!interrupt_active(INT_TIMER1));
clear_interrupt(INT_TIMER1);
set_timer1(64536);
set_pwm1_duty(200);
set_pwm2_duty(200);
}
|
Code: |
#include <18f2520.H>
#fuses XT, NOWDT,NOBROWNOUT,PUT,NOLVP,NOMCLR
#use delay(clock=4000000)
void main(){
setup_timer_2(T2_DIV_BY_4,200,6);
setup_timer_1 ( T1_INTERNAL | T1_DIV_BY_1 );
setup_ccp1(CCP_PWM);
setup_ccp2(CCP_PWM);
set_pwm1_duty(200);
set_pwm2_duty(200);
while(1){
while (!interrupt_active(INT_TIMER1));
clear_interrupt(INT_TIMER1);
set_timer1(64536);
}
|
so is these code same for motor? |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19496
|
|
Posted: Sun Feb 17, 2019 5:35 am |
|
|
The PWM gets updated on the next cycle, and since the update is to the same number it'll remain unchanged.
Honestly 'better' to make your indentation reflect actual operations:
Code: |
while(1)
{
while (!interrupt_active(INT_TIMER1))
; //This is the operation being handled by the 'while'
clear_interrupt(INT_TIMER1); //these lines are not
set_timer1(64536);
set_pwm1_duty(200);
set_pwm2_duty(200);
}
|
Otherwise it is terribly easy to forget which operations are in which loop |
|
|
luckyluke
Joined: 18 Apr 2006 Posts: 45
|
|
Posted: Sun Feb 17, 2019 6:15 am |
|
|
It was for fixed 1 ms interval.
I intended to fill 1ms with\
Code: | while (!interrupt_active(INT_TIMER1)) ; |
Actual program is like:
Code: |
while(1)
{
while (!interrupt_active(INT_TIMER1))
;
clear_interrupt(INT_TIMER1);
set_timer1(64536);
sensor_read();
decision();
motor();
}
|
Is this wrong logic or bad for readability? |
|
|
|