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

How to build a multi-threading project.

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



Joined: 24 Jun 2004
Posts: 285

View user's profile Send private message

How to build a multi-threading project.
PostPosted: Wed Jun 15, 2005 11:45 am     Reply with quote

I am designing a control system. the system measure the speed ( I am thinking using the timer1 interrupt) and with different speed, I should send out different pulses (very low frwquency, 20-100hz), any suggestion how to do it.
young



Joined: 24 Jun 2004
Posts: 285

View user's profile Send private message

PostPosted: Wed Jun 15, 2005 12:29 pm     Reply with quote

what I am thinking right now is using the timer1 interrupt to measure the speed by counting pulses from a encoder, and send out high and low output every delays (ms delay), however, I am afraid that while I am send high and low pulses by using delay, I will lose control of measuring the encoder input time. will this be a problem?
libor



Joined: 14 Dec 2004
Posts: 288
Location: Hungary

View user's profile Send private message

PostPosted: Thu Jun 16, 2005 1:49 am     Reply with quote

If you're counting pulses in the interrupt service routine, it will be triggered if enabled no matter what you are doing in your main loop. So you will not lose pulses. The only problem is that the instruction under execution in tha main block gets delayed, you should consider if this small jitter in the length of you output pulses cause problem. You can eliminate most of it if you use a counter to set the length of the output pulses in the main block, and you adjust this counter in the ISR catch up with the time the processor spends in the ISR.

You van do it using separate timers for both tasks, in this case you might get only a small jitter in your output pulses if both events fire at the same time.
You can use the 18F two-level interrupt system also for this.

Another option would be to use those PICs with built-in encoder inputs. Those do the quadrature counting in hardware.
valemike
Guest







PostPosted: Thu Jun 16, 2005 7:28 am     Reply with quote

I'm doing the same thing right now: rb0 is an interrupt, and i send out pulses, whose varying duty cycle will control the motor speed.

You have to use PWM with the CCP module. That way, the rb0 ISRs won't interfere with your pulses' duty cycle. If you don't use PWM, it gets even more indeterminate if you have other ISRs running such as timer isrs.

In fact, if you only have rb0 interrupts, then you won't be able to 'latch' the timer1 value automatically. You'd have to save in in your rb0 isr. Think of the interrupt latency involved. If you only have 1 isr, then your interrupt latency will be consistent. If you have other isrs, and you are servicing one of them, and all of a sudden an rb0 interrupt hits, there is still the latency of finishing off the current isr, restoring registers, then the pending rb0 isr gets serviced, saves registers, runs the isr, restores registers, exits the isr, etc.

I have been able to minimize the redundant enters and exits of isrs by calling the rb0 isr function from within other isrs. This is possible for rb0 only because i think a dummy read of rb0 will automatically clear the pending rb0 interrupt flag.

But libor is right, you're better off using the quadrature encoder on some PICs, or you can do it also with flip flops and feeding the encoder bits into your external Timer1 pins rather than rb0. This way you'll latch the time without interrupt latency, and without the need of wasting cpu cycles in isrs. This is especially if your encoder rate is too high.
Neutone



Joined: 08 Sep 2003
Posts: 839
Location: Houston

View user's profile Send private message

PostPosted: Thu Jun 16, 2005 11:10 am     Reply with quote

young wrote:
what I am thinking right now is using the timer1 interrupt to measure the speed by counting pulses from a encoder, and send out high and low output every delays (ms delay), however, I am afraid that while I am send high and low pulses by using delay, I will lose control of measuring the encoder input time. will this be a problem?


I would expect using the PWM module would not be an option because of the very low frequency you want at the output. I would setup timer2 to interupt at a fixed interval and use timer1 to count pulses. When timer2 interupts read timer1 and then clear timer1. Keep the count of time untill PWM output toggles. That means all your code would be within the interupt for timer2.

A more acurate method would be to setup the timers but not enable the interupts. Then within main create a loop starting with a while (not timer2 interupt) folowed by clearing the interupt and the code to keep all timing. This keeps from having to jump to an interupt vector.
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