CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

is these same?

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
luckyluke



Joined: 18 Apr 2006
Posts: 45

View user's profile Send private message

is these same?
PostPosted: Sun Feb 17, 2019 4:34 am     Reply with quote

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: 19238

View user's profile Send private message

PostPosted: Sun Feb 17, 2019 5:35 am     Reply with quote

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

View user's profile Send private message

PostPosted: Sun Feb 17, 2019 6:15 am     Reply with quote

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?
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group