1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

square wave pulse detector using PIC16F676

Discussion in 'Homework Help' started by Fabianr18, Nov 30, 2017.

  1. Fabianr18

    Fabianr18 New Member

    Joined:
    Nov 27, 2017
    Messages:
    11
    Likes:
    0
    so using a simple if else statement on the code Pommie wrote i can use it to at least do some test runs and have something to present
    (the code insert wouldn't work)
    Code (C):
     main ()
    {
        ANSEL=0b000000;
        ADCON0bits.ADON=0;
        TRISA=0b00010000;
        TRISC=0b00000000;
        T1CON=0b00000000;

        while(1){
            unsigned int Timer1Count;
            TMR1=0;             //clear timer
            while(!RA4);        //wait for high
            T1CONbits.TMR1ON=1; //start timer
            while(RA4);         //wait low
            T1CONbits.TMR1ON=0; //stop timer
            Timer1Count=TMR1;   //copy timer
            // use the timer value here to light LEDs etc.
            if(Timer1Count<=220){
                PORTC=0;
            }
            else if(Timer1Count >= 220){
            PORTC=0b00000001;}
        }
    }  
     
    Last edited: Dec 6, 2017
  2. Inquisitive

    Inquisitive Super Moderator

    Joined:
    Aug 31, 2011
    Messages:
    947
    Likes:
    87
    Location:
    Georgia, USA
    Use

    [​IMG]
     
  3. jpanhalt

    jpanhalt Well-Known Member Most Helpful Member

    Joined:
    Jun 21, 2006
    Messages:
    6,074
    Likes:
    524
    Location:
    Cleveland, OH, USA
    ONLINE
    Ian Rogers
    I believe the switches are intended to create a 4-bit digital value of 0-15. Will the ADC resistor ladder handle that?

    upload_2017-12-6_12-35-58.png
     
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,310
    Likes:
    914
    Location:
    Rochdale UK

    ADC has 1024 resolution.... 1024 / 15 = 68bit steps.... I think so... ( actually need 16 steps, unless 0 is recognised as 0...)
    The trouble here though is he'll need a R-2R setup and switches that are double pole..
     
  6. Fabianr18

    Fabianr18 New Member

    Joined:
    Nov 27, 2017
    Messages:
    11
    Likes:
    0
    the code seems to work using the else if statements but the outputs stop working after it changes from output 0110 and forward any suggestions?
    Code (C):
     
     main ()
    {
        ANSEL=0b000000;
        ADCON0bits.ADON=0;
        TRISA=0b00010000;
        TRISC=0b00000000;
        T1CON=0b00000000;

        while(1){
            unsigned int Timer1Count;
            TMR1=0;             //clear timer
            while(!RA4){        //wait for high
            T1CONbits.TMR1ON=1;}//start timer
            while(RA4){      //wait low
            T1CONbits.TMR1ON=0; //stop timer
            Timer1Count=TMR1; }  //copy timer
            // use the timer value here to light LEDs etc.
           
            if(TMR1 <= 210){
            PORTC=0b00000000; break;}
       
            else if(TMR1 >=200 && TMR1 <=250 ){
            PORTC=0b00000001;
            }
           else if(TMR1 >250 && TMR1 <=300 ){
            PORTC=0b00000010;
           }
            else if(TMR1 >300 && TMR1 <=350 ){
            PORTC=0b00000011;
           }
            else if(TMR1 >350 && TMR1 <=410 ){
            PORTC=0b00000100;
          }
            else if(TMR1 >400 && TMR1 <=460 ){
            PORTC=0b00000101;
           }
            else if(TMR1 >450 && TMR1 <=510 ){
                PORTC=0b00000110;      
           }
           else if(TMR1 >540 && TMR1 <=570 ){
            PORTC=0b00000111;
           }
           else if(TMR1 >580 && TMR1 <=610 ){
            PORTC=0b00001000;
          }
           else if(TMR1 >640 && TMR1 <=660 ){
            PORTC=0b00001001;
           }}
        }
     
     
  7. Pommie

    Pommie Well-Known Member Most Helpful Member

    Joined:
    Mar 18, 2005
    Messages:
    10,161
    Likes:
    340
    Location:
    Brisbane Australia
    ONLINE
    To test for your values can be done with maths. Assume 25 either side of the actual value.
    So, subtract 175 and divide by 50 should give you the LED pattern.
    Code (text):

        while(1){
            unsigned int Timer1Count;
            unsigned char LED;
            TMR1=0;             //clear timer
            while(!RA4){        //wait for high
            T1CONbits.TMR1ON=1;}//start timer
            while(RA4){      //wait low
            T1CONbits.TMR1ON=0; //stop timer
            Timer1Count=TMR1; }  //copy timer
            // use the timer value here to light LEDs etc.
            Timer1Count-=175;
            LED = Timer1Count/50;
            PORTC=LED;
        }
    Edit, do you have a scope to check the transmitter is outputting the correct pulses?

    Mike.
     

Share This Page