EngineersGarage

  • Engineers Garage Main Site
  • Visit our active EE Forums
    • EDABoard.com
    • Electro-Tech-Online
  • Projects & Tutorials
    • Circuits
    • Electronic Projects
    • Tutorials
    • Components
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Advertise
You are here: Home / Replies / This is the code that I am

This is the code that I am

|

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
Pat ondo
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. 

RSS Recent Posts

  • WTB: "The Theory Of Servicing AM, FM, And FM Receivers" by Clarence R. Green and Robert M. Bourque November 10, 2025
  • Anyone In The US Ordered From AliExpress Recently? November 10, 2025
  • Calculation of A Class amplifier November 10, 2025
  • strange laptop problem November 10, 2025
  • restarting this Christmas project November 10, 2025

Stay Up To Date

Newsletter Signup
EngineersGarage

Copyright © 2025 WTWH Media LLC. All Rights Reserved. The material on this site may not be reproduced, distributed, transmitted, cached or otherwise used, except with the prior written permission of WTWH Media
Privacy Policy | Advertising | About Us

Search Engineers Garage

  • Engineers Garage Main Site
  • Visit our active EE Forums
    • EDABoard.com
    • Electro-Tech-Online
  • Projects & Tutorials
    • Circuits
    • Electronic Projects
    • Tutorials
    • Components
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Advertise