square wave pulse detector using PIC16F676

Status
Not open for further replies.
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)
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:
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?

 
I believe the switches are intended to create a 4-bit digital value of 0-15. Will the ADC resistor ladder handle that?
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..
 
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?
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;
       }}
    }
 
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:
    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.
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…