Microcontroller › PIC › How to Connect a reed sensor to a PIC24 or any PIC?
- This topic has 4 replies, 2 voices, and was last updated 10 years ago by
AJISH ALFRED.
Viewing 5 posts - 1 through 5 (of 5 total)
-
AuthorPosts
-
November 14, 2013 at 7:03 pm #2668
Pat ondo
ParticipantI am working on a project where I have to use two reeds sensors in order to get the velocity of a object travelling horizontally. The reed sensors are switches that close or open when they are near of a magnet. I was planing to use the ADC on the PIC but I read somewhere that in my case I do not have to do it because I would not need any voltage values between 0V and 5V. I will only need the minimum voltage and the maximum voltage value. So, I want to know how I can actually connect those sensors to the micro controller. And what can I do to get the time between the two microcontroller when they close or open?Thanks
November 19, 2013 at 5:15 am #10619AJISH ALFRED
ParticipantHi,
You can make any IO pin as input and conncet the Reed swicth just like any normal switch.
November 19, 2013 at 5:13 pm #10631Pat ondo
ParticipantThank you,
I did it that way but my I am trying to get the time between the two events. I am using timer and timer interrupt. what will be the best way to do it?
November 22, 2013 at 8:30 am #10636Pat ondo
ParticipantThis is the code that I am using to get the time between reed sensor 1 and 2 and reed sensor 2 and 3.
#include “p24fj64ga002.h”#include <stdio.h>_CONFIG1( JTAGEN_OFF & GCP_OFF & GWRP_OFF &BKBUG_ON & COE_OFF & ICS_PGx1 &FWDTEN_OFF & WINDIS_OFF & FWPSA_PR128 & WDTPS_PS32768 )_CONFIG2( IESO_OFF & SOSCSEL_SOSC & WUTSEL_LEG & FNOSC_PRIPLL & FCKSM_CSDCMD &OSCIOFNC_OFF &IOL1WAY_OFF & I2C1SEL_PRI & POSCMOD_XT )#define XTFREQ 7372800 // On board Crystal frequency#define PLLMODE 4 // On Chip PLL setting (Fosc)#define FCY (XTFREQ*PLLMODE)/2 // Intruction Cycle Frequency#define BAUDRATE 115200#define BRGVAL ((FCY/BAUDRATE)/16)-1int main(void){LATA = 0;LATB = 0;//TRISB controls direction for all PortB pins, where 0 -> Output, 1 -> input.TRISAbits.TRISA0 = 1;TRISAbits.TRISA1 = 1;TRISBbits.TRISB0 = 1;int time1; int time2;TMR1 = 0; TMR2 = 0;// Setup Timer 1 control register (T1CON) to:// TON = 1 (start timer)// TCKPS1:TCKPS2 = 00 (set timer prescaler to 1:1)// TCS = 0 (Fosc/2)T1CON = 0x1000;T2CON = 0x1000;while(1){if(TRISAbits.TRISA0 == 1) //Switch on reed 1 opens{TMR1 = 0;PR1 = 0;if(TRISAbits.TRISA1 == 1) //Switch on reed 2 opens{PR1 = time1;}}if(TRISAbits.TRISA1 == 1) //Switch on reed 2 opens{TMR2 = 0;PR2 = 0;if(TRISBbits.TRISBO == 1) //Switch on reed 3 Opens{PR2 = time2;}}}}is it the right way to write it? I want to get the time between three switches.November 22, 2013 at 1:14 pm #10638AJISH ALFRED
ParticipantHi,
Make your code to wait till the switch open like the following;
while(1){while (TRISAbits.TRISA0 == 0); //wait till Switch on reed 1 opensTMR1 = 0;PR1 = 0;while(TRISAbits.TRISA1 == 0) //wait till Switch on reed 2 opensPR1 = time1;while(TRISAbits.TRISA1 == 0) //wait till Switch on reed 2 opensTMR2 = 0;PR2 = 0;while(TRISBbits.TRISBO == 0); //wait till Switch on reed 3 OpensPR2 = time2;}I hope this may work!! -
AuthorPosts
Viewing 5 posts - 1 through 5 (of 5 total)
- You must be logged in to reply to this topic.