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 CCS Technical Support

Speed sensor detection

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



Joined: 21 Jul 2008
Posts: 8
Location: Fresno

View user's profile Send private message

Speed sensor detection
PostPosted: Tue Dec 31, 2013 7:55 pm     Reply with quote

Hi All, I'm working on a project that requires me to design a speed sensor for a flywheel connected to a drive belt. I'm somewhat of a beginner programmer with PICs and need some advice.

The PIC is receiving its input from a Hall Effect sensor connected to the PIC and a magnet on the flywheel. The Hall Effect emits a 5V signal until disrupted by the magnetic field and then drops to about 0.2 volts. The flywheel spins from 2000 up to 4500 rpms.

My question is this: what is the best approach to detect this signal from the Hall Effect sensor? There are several interrupts options (external interrupts, Port change interrupts) along with other options. I don't want to reinvent the wheel, but just need some recommendations from experts out there.

Please advise.
Gabriel



Joined: 03 Aug 2009
Posts: 1074
Location: Panama

View user's profile Send private message

PostPosted: Tue Dec 31, 2013 8:02 pm     Reply with quote

Read the pics datasheet.
Make sure it has a capture and compare hardware
Search the forum for examples
Do math..

You are done.
_________________
CCS PCM 5.078 & CCS PCH 5.093
jgd10



Joined: 21 Jul 2008
Posts: 8
Location: Fresno

View user's profile Send private message

PostPosted: Wed Jan 01, 2014 9:58 pm     Reply with quote

This seems like a great choice. Reviewing the datasheet this is what I came up with, let me know if I'm on the right track.

1. Place CCP1 in capture mode
2. When an event occurs on pin CCP1, capture time from TMR1 store in variable prevTime
3. Next event on pin CCP1, capture time and store in currTime
4. Use prevTime and currTime to calculate the speed

Thanks
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Jan 01, 2014 11:34 pm     Reply with quote

Yes and there are many threads in the forum archives which show how
to do this and discuss every possible problem.
jgd10



Joined: 21 Jul 2008
Posts: 8
Location: Fresno

View user's profile Send private message

PostPosted: Sat Jan 11, 2014 10:43 pm     Reply with quote

Thanks for all the help, I've made lots of progress over the past week. I did find quite a few code examples.

I did have a few questions though. On post http://www.ccsinfo.com/forum/viewtopic.php?t=29963
there was a question in regards to the L in the frequency equation.
Quote:
frequency = (int16)(1000000L / current_ccp_delta);

but the question wasn't answered. I've searched various references and couldn't find the purpose of the "L".
Can someone fill me in on its use or where I can get information about its use?

Final question, on the same ref thread listed above, PCM programmer posted a demo program, the program sets up and uses timer1 successfully (it works, I tried it). To my surprise no where in the code is #INT_TIMER1 declared?!?
Can someone explain why this still works? I can only postulate that the compiler creates the function behind the scenes...
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sat Jan 11, 2014 11:20 pm     Reply with quote

Data sheet.
http://ww1.microchip.com/downloads/en/DeviceDoc/30292D.pdf

Quote:

8.1 Capture Mode

When a capture is made, the interrupt request flag bit CCP1IF
(PIR1<2>) is set.

It doesn't say anything about the Timer1 interrupt flag being set.
jgd10



Joined: 21 Jul 2008
Posts: 8
Location: Fresno

View user's profile Send private message

PostPosted: Sun Jan 12, 2014 12:54 am     Reply with quote

I understand, but it also states that:
Quote:
8.1 Capture Mode
In Capture mode, CCPR1H:CCPR1L captures the
16-bit value of the TMR1 register when an event occurs
on pin RC2/CCP1.

What's throwing me is that the TMR1 value is still being captured.

Looking at the data sheet for TIMER1 MODULE it states:
Quote:
6.0 TIMER1 MODULE
.... The TMR1 Interrupt, if enabled,
is generated on overflow, which is latched in interrupt
flag bit TMR1IF (PIR1<0>).

Since the TMR1 is not enabled, an interrupt doesn't occur, but is TMR1 still running in the background which allows CCP1 to capture the time?

Also, can you tell me anything on the L in the freq equations?
temtronic



Joined: 01 Jul 2010
Posts: 9594
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Sun Jan 12, 2014 6:30 am     Reply with quote

PIC peripherals, like the timers and UARTs ,will work without their interupts enabled. However if the interupt is enabled then a bit(flag) is set in a register and the PIC is made aware of it.
Similar to getting mail delivered.The mailman puts letters(usually bills!) in your mailbox, so your mail has come. If he doesn't raise the flag, you don't know there's mail unless you look.
Interupts are a fast,automatic way to KNOW some action has been done instead of having the PIC in a continous 'is it done yet ?' loop.


The 'L' indicates that the variable is a Long( 16 bits).The timer register is 10 bits wide, so without the 'L', 2 bits are missing,giving wrong results and actions.

hth
jay
jgd10



Joined: 21 Jul 2008
Posts: 8
Location: Fresno

View user's profile Send private message

PostPosted: Sun Jan 12, 2014 11:59 am     Reply with quote

I see said the blind man.

Thank you.
asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

PostPosted: Sun Jan 12, 2014 12:17 pm     Reply with quote

there are actually 3 ways to deal with an INT flag

1 - set up interrupt handler, and accept the side effects of doing so

2- POLL INT flag in main or in dedicated functions, where the main side effect is latency proportional ( roughly) to the inverse polling frequency

3- ignore ( and in the case of a timer for instance, read the timer value directly - to make process decisions )

each of these approaches has its uses
Very Happy Very Happy Very Happy Very Happy
gpsmikey



Joined: 16 Nov 2010
Posts: 588
Location: Kirkland, WA

View user's profile Send private message

PostPosted: Sun Jan 12, 2014 12:46 pm     Reply with quote

A couple of other things to consider in your program - think about adding an "average" of the last 8 or so readings so that your "speed" is not jumpy if there are slight variations in the sensor signal. Also, if you are going to be displaying the result, keep your display update rate (if digital) to less than 2 or 3 times/second. Too many people try to update a digital display 100 times / second - the end result is if there is any variation, your eyes can't read the silly thing.

mikey
_________________
mikey
-- you can't have too many gadgets or too much disk space !
old engineering saying: 1+1 = 3 for sufficiently large values of 1 or small values of 3
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Jan 12, 2014 3:44 pm     Reply with quote

Quote:

What's throwing me is that the TMR1 value is still being captured.

Since the TMR1 (interrupt) is not enabled, an interrupt doesn't occur, but is TMR1 still running in the background which allows CCP1 to capture the time?

The CCP pin sees an incoming edge. This causes the current value of
Timer1 to be loaded into the CCP registers. At this point, a CCP interrupt
is generated. The CCP has "captured" (ie., loaded) the Timer1 value
at the time of the incoming edge. Timer1 interrupts are not involved in
this process at all.
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