| Swordfish Tutorial - A1102 (Hall Effect Sensor) |
|
|
| Saturday, 04 April 2009 09:10 | |
|
Hall Effect sensors allow the detection of a magnetic field. They can be extremely small, and very accurate, and often used in applications that require some sort of speed/RPM feedback. For example, you wanted to make a digital speedometer for your car, and don't want to impede on the pre-existing circuit, you could use a Hall Effect sensor to determine how many revolutions/second your wheel is doing, and calculate your speed from there.
Device = 18F452 Clock = 20 // some LCD options... #option LCD_DATA = PORTB.4 #option LCD_RS = PORTB.2 #option LCD_EN = PORTB.3 // import LCD library... Include "LCD.bas" Include "utils.bas" Include "ISRTimer.bas" Include "convert.bas" Const Timer1 = 0 Dim Hall_Sensor As PORTA.0, PreScaler As Word // Start Of Program... SetAllDigital ADCON1 = %00000110 Timer.Initialize // Activate the timer module Timer.Items(Timer1).Interval = 500 // Set the Timer for 500ms Timer.Start // Start processing all timers Input(Hall_Sensor) // Make the Hall-Effect sensor pin an input DelayMS(150) // Allow the LCD to warm up Cls // Clear the LCD, and display WriteAt(1,1,"RPM Meter") // default information While 1 = 1 // Create an infinite loop PreScaler = 0 // Reset PreScaler register Timer.Items(Timer1).Enabled = True // Enable Timer1 event Repeat Repeat // Wait for a rising edge Until Hall_Sensor = 1 Or Not Timer.Items(Timer1).Enabled Repeat // Wait for a falling edge Until Hall_Sensor = 0 Or Not Timer.Items(Timer1).Enabled If Timer.Items(Timer1).Enabled Then // If Timer1 is still enabled, PreScaler = PreScaler + 1 // then increment PreScaler If PreScaler = 84 Then // If PreScaler = 84, then Break // leave the Repeat Until loop EndIf // as 84 * 2 * 60 = 10080 EndIf Until Not Timer.Items(Timer1).Enabled // Keep looping until Timer1 times out (500mS) Timer.Items(Timer1).Enabled = False // Disable Timer1, as it may have left early PreScaler = PreScaler * 2 * 60 // * 2 = RPS, * 60 = RPM If PreScaler > 9999 Then // If the output is over 9999 PreScaler = 9999 // then display 9999 by default EndIf WriteAt(2,1,DecToStr(PreScaler, 4)) // Display the variable on the LCD // (always show 4 digits) Wend
Notice that I cap the value of PreScaler to 84, this is to ensure that my program leaves the loop if the RPM > 10000, as 84 * 2 * 60 = 10080. The value of PreScaler does not need to be capped, however if it rolls over 65535 during any math routine then your result/accuracy goes out the window. An easy fix would be defining PreScaler as a different variable type (such as LongInt for values up to 2,147,483,647).
Just to go one bit further on sample times, I have used 500mS (1/2 a second), so the slowest recordable rate = 1 sample, which equals 1 * 2 * 60 (RPS to RPM) = 120. That is also my error rate (120 RPM), as I could miss one sample each loop. If my sample time was 100mS, then my slowest sample speed would be 1 * 10 * 60 (RPS to RPM) = 600. As you can see, the error rate increases as the sample time decreases... but its a matter of how fast you need the data/want to display it that accounts for what sample speed your happy with.
Comments (4)
Joomla components by Compojoom
|
|
| Last Updated ( Wednesday, 25 November 2009 21:47 ) |
Whos Online
- Graham
- andyo
- Jon Chandler
Forum Activity
Marching LEDs - mrdeb Saturday, 13 March 2010 22:54 - [7 replies]
Credit Where Credit's Due - jon chandler Saturday, 13 March 2010 18:51 - [4 replies]
USB 8 Channel Servo Controller - andyo Saturday, 13 March 2010 01:19 - [2 replies]
Dedicated Servo Controller - graham Friday, 12 March 2010 17:58 - [0 replies]
Must have....delta temperature glowies! - graham Friday, 12 March 2010 17:31 - [3 replies]
A 'throw-away" PIC board - jon chandler Friday, 12 March 2010 13:41 - [31 replies]
iHID - graham Friday, 12 March 2010 02:04 - [5 replies]
Recent Comments
- 2010-03-13 16:36:21 andyo
Very nice - 900,000 packets and 0 errors - can't ask for better tha...
- 2010-03-13 11:59:14 Anony...
proton
- 2010-03-12 19:44:17 Jon C...
Here is a spreadsheet of compatible parts: [url]http://digital-diy...
- 2010-03-12 14:26:02 Anony...
What program do you use to capture the simulation to a videofile?
- 2010-03-12 13:15:58 Jon C...
Here's a tip for tying out SD cards. An SD card will fit the flopp...
- 2010-03-11 21:03:32 Jon C...
If this wasn't a class project that had to be done on a PIC16F690 u...
- 2010-03-11 15:30:06 uuu
I am going to send two data from ADC to USART(async mode) to serial...
- 2010-03-05 13:24:23 mrpse...
I was thinking of battery with powered routers or powered for every...




plan on using basic routine to measure wind speed.
still working on wind direction using two optisensors from a mouse driven at a 9 to one ratio