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

Counter problem

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








Counter problem
PostPosted: Fri Aug 18, 2006 11:07 am     Reply with quote

I'm trying to control my motor counting pulses on encoder:

//#device PIC18F252
#include <18F252.h>
#include <math.h>

#use delay(clock=20000000)
#fuses NOWDT,WDT128,HS, NOPROTECT, NOOSCSEN, BROWNOUT, BORV20, PUT, STVREN, NODEBUG, NOLVP, NOWRT, NOWRTD, NOWRTB, NOWRTC, NOCPD, NOCPB, NOEBTR, NOEBTRB

#byte ADCON1 = 0x0FC1

#byte T0CON = 0x0FD5
#byte T1CON = 0X0FCD

#define M1Enable PIN_C2

//global variables

int1 Flag_direction; // BIT - indicates direction of motors - forward or ;
int16 Counter


void main()
{

setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_OFF);
setup_spi(FALSE);
setup_wdt(WDT_OFF);
setup_timer_0(RTCC_EXT_L_TO_H);
setup_timer_1(T1_EXTERNAL_SYNC);
setup_timer_2(T2_DISABLED,0,1);
setup_timer_3(T3_DISABLED|T3_DIV_BY_1);


SET_TRIS_C( 0x01 ); // 'set all the PORTC pins to outputs, RC0 - input
SET_TRIS_B( 0xFF ); // 'set all the PORTB pins to inputs
//PORT_B_PULLUPS(TRUE); //internal pull-ups
SET_TRIS_A( 0xFF );


do {

output_high(M1Enable ); //start motor

if (get_timer0() == 50)

{
output_low(M1Enable); //stop motor after 50 pulses
set_timer0(0);
}

} while (true);

} //main

but motor never stops
rnielsen



Joined: 23 Sep 2003
Posts: 852
Location: Utah

View user's profile Send private message

PostPosted: Fri Aug 18, 2006 11:19 am     Reply with quote

There are Counters and there are Timers inside the PIC. They use the same hardware but are used very differently. A Counter will count pulses that come in from the outside world. Timers will 'count' clock cycles of the oscillation circuit that is Inside the PIC. You, apparently, will have the encoder output connected to one of the pins of the PIC. You need to configure one of the Counters to count the encoder pulses. Look at your PIC spec. sheet to see which pins you need to use. You can't just hook up to any pin and expect a counter to see the pulses.

You will need to have an ISR (Interrupt Service Routine) that will be used to count the puses too, if you want to have any kind of accuracy. If you simply try to have your main() program poll the counter register you could miss a pulse or two, depending on how busy the program is. Have the ISR do something when it counts 50 pulses, like set the motor enable Low. Doing a get_timer() might miss a pulse.

Also, when the enable line goes low the inertia of the motor will keep it turning a little so you will over-shoot.

Ronald
Guest








PostPosted: Fri Aug 18, 2006 11:36 am     Reply with quote

ok - i've modified my code

do {

output_high(M1Enable ); //start motor

if (get_timer0() >= 50)

{
output_low(M1Enable); //stop motor after 50 pulses
set_timer0(0);
}

} while (true);

} //main

so motor should stop anywhere - unfortunatelly it doesn't.
rnielsen



Joined: 23 Sep 2003
Posts: 852
Location: Utah

View user's profile Send private message

PostPosted: Fri Aug 18, 2006 4:00 pm     Reply with quote

Look at your code real close. You have your do loop. Inside it you make M1Enable High. You then check to see if the timer is >= 50. If it's not, then you loop back up and make M1Enable High. You then check if the timer is >= 50. If it is, then you make M1Enable Low, set the timer counter to zero(0) and then, immediately, loop back up and make M1Enable High, check the timer, and so on. You are looping over and over. You don't have anything to keep the motor from starting up again. This happens so fast that the motor won't have time to even Start slowing down before you tell it to go again.

Try rethinking how to make this work. You tell the motor to run, you check to see if it's reached it's destination, if it has you tell it to stop. NOW, don't tell it to start up again until you want it to. What should you put into your code to accomplish this? Also, try registering on the board so we know who we're talking to.

Ronald
Guest








PostPosted: Sat Aug 19, 2006 6:08 am     Reply with quote

Thanks rnielsen, i reconsider my algorithm.

BTW.I'm registered user, but when i try to log in i get message - incorrect password or user name.
theteaman



Joined: 04 Aug 2006
Posts: 98

View user's profile Send private message

PostPosted: Sat Aug 19, 2006 8:28 am     Reply with quote

(wow, i dont remember the last time i saw a 'do' loop ;) )
rnielsen



Joined: 23 Sep 2003
Posts: 852
Location: Utah

View user's profile Send private message

PostPosted: Tue Aug 22, 2006 1:05 pm     Reply with quote

theteaman, you must not be married. I'm continually seeing do loops....

DO THIS for me!

DO THAT for me!

DO you know how to listen to me?

DO NOT go out with the boys!

.....

Twisted Evil
theteaman



Joined: 04 Aug 2006
Posts: 98

View user's profile Send private message

PostPosted: Fri Aug 25, 2006 10:21 pm     Reply with quote

rnielsen wrote:
theteaman, you must not be married. I'm continually seeing do loops....

DO THIS for me!

DO THAT for me!

DO you know how to listen to me?

DO NOT go out with the boys!

.....

Twisted Evil


hehe Laughing Laughing

seriously though in general its considered bad programming practice although... its all subjective i guess Smile
drh



Joined: 12 Jul 2004
Posts: 192
Location: Hemet, California USA

View user's profile Send private message

PostPosted: Sat Aug 26, 2006 12:16 pm     Reply with quote

Quote:

seriously though in general its considered bad programming practice although... its all subjective i guess


Do/while loops evaluate at the end of the loop. The code in the body of the loop will always execute at least once.

While loops evaluate at the beginning of the loop. The code in the body of the loop may or may not execute.
_________________
David
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Sat Aug 26, 2006 1:26 pm     Reply with quote

drh wrote:
Quote:

seriously though in general its considered bad programming practice although... its all subjective i guess


Do/while loops evaluate at the end of the loop. The code in the body of the loop will always execute at least once.

While loops evaluate at the beginning of the loop. The code in the body of the loop may or may not execute.
The use of do/while has nothing to do with bad programming practices. I agree with DRH that a do/while is a well allowed language construct which works slightly different than a while loop and in some situations is even the preffered choice.
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