| 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
- ta1dr
- andyo
- Jon Chandler
Forum Activity
the LCD demo hello world - jon chandler Tuesday, 09 March 2010 23:48 - [28 replies]
USB problem - jon chandler Tuesday, 09 March 2010 11:31 - [3 replies]
Servo Module - andyo Tuesday, 09 March 2010 03:39 - [3 replies]
pic to ps2 communication - roshan Friday, 05 March 2010 01:30 - [7 replies]
Way Off Topic: Worldwide Ignite Week - jon chandler Wednesday, 03 March 2010 20:33 - [4 replies]
PIC USB with C and Visual C++ Project - hop Tuesday, 02 March 2010 21:49 - [0 replies]
Function generator help - magic Tuesday, 02 March 2010 16:37 - [10 replies]
Recent Comments
- 2010-03-09 13:27:12 air j...
link:http://www.b2c2u.com/ [air jordan] [url=http://www.handba...
- 2010-03-05 13:24:23 mrpse...
I was thinking of battery with powered routers or powered for every...
- 2010-03-05 12:17:40 andyo
50 rooms - wow. The XBee modules are very easy to use but I'm not ...
- 2010-03-05 11:46:37 mrpse...
Thanks AndyO. What I have to do is design a device to monitor temp...
- 2010-03-05 03:08:58 andyo
The circuit used for the remote sensor can be found here: [url]http...
- 2010-03-04 07:19:06 renno
plz i need help do u know from where i could download it or how cou...
- 2010-03-04 07:16:05 renno
hello everyone i'm trying to finish my final project and i want to ...
- 2010-03-04 02:49:38 Saira...
Thanks that was helpful




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