Microcontroller › PIC › How to Connect a reed sensor to a PIC24 or any PIC? › This is the code that I am
November 22, 2013 at 8:30 am
#10636
Participant
This 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)-1
int 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.