Reading frequency with a PIC16F877?

Discussion in 'Embedded Systems and Microcontrollers' started by GuitarMan216, Feb 13, 2012.

  1. GuitarMan216

    Thread Starter New Member

    Oct 6, 2011
    18
    0
    Hey guys, I've decided to start a little guitar tuner project, using assembly with a PIC16F877. I have the circuit set up, so my guitar output is essentially a square wave that I can go into the pic with. My DMM is reading the right frequencies and voltage there, so I know that's good.

    My issue is, I'm really green to this stuff, so I'm not sure how to start with the programming and input. Is there a way I could increment a timer every time the input is high, and compare say 20 input pulses with the external oscillator cycles in the time frame it took? Or is there an easier way? Any help would be appreciated. Thanks!
     
  2. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    There are several timers in there and at least one of them has a setting to use an external pin as the clock: that means it is "increment a timer every time the input is high" (actually it is edge triggered). You can cleat the timer register, wait a specific interval, then read the timer register and there you have counts per your specific interval, or the inverse of frequency.

    If you have nothing else happening just waiting a given number of statements can give you the specific interval.
     
    GuitarMan216 likes this.
  3. GuitarMan216

    Thread Starter New Member

    Oct 6, 2011
    18
    0
    Thanks for the quick reply! I just found this forum the other day, and it seems like a goldmine.

    I'm just unsure how this code would go, would I want to create some sort of loop with some set value for the TMR?:

    banksel TMR1
    movlw 0xFF00
    movwf TMR1
    btfsc TMR1
    goto whatever

    Would that then allow the timer to increment about 30 times until it's cleared, and then somehow read the interval?

    Sorry, I know I'm being incredibly vague.
     
  4. Markd77

    Senior Member

    Sep 7, 2009
    2,803
    594
    There is a capture mode on this PIC, which will store the value of timer1, and optionally trigger an interrupt, every 16 rising edges (or 1,2,4,8) of the CCP pin.
    timer1 doesen't get reset so you just subtract the previous value. Ignore the first result, it won't be accurate unless you put in extra effort.
    Have a look at section 8 of the datasheet for more info.
     
    GuitarMan216 likes this.
  5. GuitarMan216

    Thread Starter New Member

    Oct 6, 2011
    18
    0
    Thanks man. I've been reading up in section 8 for the past hour or so. This stuff is just so confusing, that I'm having a hard time getting started.
     
  6. GuitarMan216

    Thread Starter New Member

    Oct 6, 2011
    18
    0
    Ok, so I've spent some time in Section 8, and am starting to get an idea. I switched over to C, so I'm a little more comfortable. I'm using Hi-Tech C.

    Here's my code so far, but I have no clue if I'm anywhere close. Any insight?

    Thanks!

    Code ( (Unknown Language)):
    1.  
    2. #include <pic16f877.h> //Microcontroller header file
    3.  
    4. #define E_S     164.81  //Low E(2nd harmonic)
    5. #define A_S     220.00
    6. #define D_S     293.67
    7. #define G_S     392.00
    8. #define B_S     493.88         
    9. #define EH_S    659.26  //High E(2nd harmonic)
    10.  
    11. void main()
    12. {
    13.  
    14. unsigned int TIM1, TIM2, capold, capnew, diff, TMRdiff;
    15.  
    16. TRISC=0b00000000; //Setting PORTC pins to inputs
    17. TRISB=0b11111111; //Setting PORTB pins to outputs
    18. T1CON=0b00001001; //Initiating TMR1(Fosc/4)
    19. CCP1CON=0b00000110; //Capture every 4th rising edge
    20. TMR1IF=0;
    21.  
    22.  
    23. float FREQ;
    24.  
    25. Main:
    26.  
    27.  
    28. if(CCP2IF) //Timer1 Register Capture
    29.     {
    30.         capold=CCPR1H;
    31.         TIM1=TMR1;
    32. Loop:
    33.  
    34.     if(CCP2IF)
    35.         {
    36.             capnew=CCPR2H;
    37.             TIM2=TMR1;
    38.             goto StringDetect;
    39.         }  
    40. goto Loop;
    41.     }
    42.  
    43.  
    44. goto Main;
    45.  
    46. StringDetect:
    47.  
    48.     diff=capnew-capold;
    49.     TMRdiff=TIM2-TIM1;
    50.  
    51.     FREQ=(diff/TMRdiff)*1000000*4; //Multiply by 4, since it's finding every 4th trigger
    52.  
    53.     if(FREQ<((A_S+E_S)/2))
    54.         {
    55.             if(FREQ>70.00)
    56.                 {
    57.                     if(FREQ<(E_S-1.0)){
    58.                         RB0=1;
    59.                         RB1=0;
    60.                         RB2=0;
    61.                     }
    62.                     if(FREQ>(E_S+1.0)){
    63.                         RB2=1;
    64.                         RB1=0;
    65.                         RB0=0;
    66.                     }
    67.                     if((E_S-1.0)<FREQ<(E_S+1.0)){
    68.                         RB1=1;
    69.                         RB2=0;
    70.                         RB0=0;
    71.                     }
    72.                 }
    73.         }
    74.     if(FREQ<((D_S+A_S)/2))
    75.         {
    76.             if(FREQ>((A_S+E_S)/2))
    77.                 {
    78.                     if(FREQ<(A_S-1.0)){
    79.                         RB0=1;
    80.                         RB1=0;
    81.                         RB2=0;
    82.                     }
    83.                     if(FREQ>(A_S+1.0)){
    84.                         RB2=1;
    85.                         RB1=0;
    86.                         RB0=0;
    87.                     }
    88.                     if((A_S-1.0)<FREQ<(A_S+1.0)){
    89.                         RB1=1;
    90.                         RB2=0;
    91.                         RB0=0;
    92.                     }
    93.                 }
    94.         }
    95.     if(FREQ<((G_S+D_S)/2))
    96.         {
    97.             if(FREQ>((D_S+A_S)/2))
    98.                 {
    99.                     if(FREQ<(D_S-1.0)){
    100.                         RB0=1;
    101.                         RB1=0;
    102.                         RB2=0;
    103.                     }
    104.                     if(FREQ>(D_S+1.0)){
    105.                         RB2=1;
    106.                         RB1=0;
    107.                         RB0=0;
    108.                     }
    109.                     if((D_S-1.0)<FREQ<(D_S+1.0)){
    110.                         RB1=1;
    111.                         RB2=0;
    112.                         RB0=0;
    113.                     }
    114.                 }
    115.         }
    116.     if(FREQ<((B_S+G_S)/2))
    117.         {
    118.             if(FREQ>((G_S+D_S)/2))
    119.                 {
    120.                     if(FREQ<(G_S-1.0)){
    121.                         RB0=1;
    122.                         RB1=0;
    123.                         RB2=0;
    124.                     }
    125.                     if(FREQ>(G_S+1.0)){
    126.                         RB2=1;
    127.                         RB1=0;
    128.                         RB0=0;
    129.                     }
    130.                     if((G_S-1.0)<FREQ<(G_S+1.0)){
    131.                         RB1=1;
    132.                         RB2=0;
    133.                         RB0=0;
    134.                     }
    135.                 }
    136.         }
    137.     if(FREQ<((E_S+B_S)/2))
    138.         {
    139.             if(FREQ>((B_S+G_S)/2))
    140.                 {
    141.                     if(FREQ<(B_S-1.0)){
    142.                         RB0=1;
    143.                         RB1=0;
    144.                         RB2=0;
    145.                     }
    146.                     if(FREQ>(B_S+1.0)){
    147.                         RB2=1;
    148.                         RB1=0;
    149.                         RB0=0;
    150.                     }
    151.                     if((B_S-1.0)<FREQ<(B_S+1.0)){
    152.                         RB1=1;
    153.                         RB2=0;
    154.                         RB0=0;
    155.                     }
    156.                 }
    157.         }
    158.     if(FREQ>((EH_S+B_S)/2))
    159.         {
    160.             if(FREQ<1000.00)
    161.                 {
    162.                     if(FREQ<(EH_S-1.0)){
    163.                         RB0=1;
    164.                         RB1=0;
    165.                         RB2=0;
    166.                     }
    167.                     if(FREQ>(EH_S+1.0)){
    168.                         RB2=1;
    169.                         RB1=0;
    170.                         RB0=0;
    171.                     }
    172.                     if((EH_S-1.0)<FREQ<(EH_S+1.0)){
    173.                         RB1=1;
    174.                         RB2=0;
    175.                         RB0=0;
    176.                     }
    177.                 }
    178.         }
    179. }
    180.  
     
    Last edited by a moderator: Feb 14, 2012
  7. Markd77

    Senior Member

    Sep 7, 2009
    2,803
    594
    I don't know much about C, but the way I'd do it is:
    every CCP1IF (don't confuse CCP1 and CCP2, they are seperate counters):
    store CCPR1H and CCPR1L as a 16 bit C variable (capold),
    subtract that from the current value of CCPR1H and CCPR1L (I think you can use CCPR1H*256+CCPR1L),
    make sure that the case where TMR0 has changed from FFFF to 0000 during the period doesn't cause problems,
    compare that to precalculated periods, eg for 220Hz use (1000000*4)/220 = 18182
    This saves clock cycles and program memory.
     
    GuitarMan216 likes this.
  8. GuitarMan216

    Thread Starter New Member

    Oct 6, 2011
    18
    0
    Thanks again Mark! The CCPR1H*256+CCPR1L helps. I'll throw it in, do a little more research and see what pops up.

    :)
     
  9. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    Hey GuitarMan: first off, where is your jungle band?

    I'm trying to follow your work here but I'm confused what you are doing. You said you have a nice square wave of your guitar note, what pin are you applying that to the PIC?

    Why would that square wave be the 2nd harmonic as the comments in your code suggest? I would expect it to be the fundamental but I'm no player.

    Also, using HiTech C they define the symbol "CCPR2" for your processor that gives you the whole 16 bit value of Timer 1 in one shot.

    I'm still trying to figure out the settings for Timer1 to use T1CKI an the external clock If that is how you are doing it then I don't check your T1CON setting (but don't have a new value for you).
     
    GuitarMan216 likes this.
  10. GuitarMan216

    Thread Starter New Member

    Oct 6, 2011
    18
    0
    Hey Ernie, first off, thanks for the response.

    Ok, I'm about as new to this as can be, so I don't even know what a jungle band is. Whoops lol.

    My computer died, and I left the charger, so I don't have all the info right now, but I believe I was going into RC2.

    As far as the 2nd harmonic, I tune by playing a 12 fret harmonic(One octave above), which would give that frequency, with a more pure and accurate signal. The fundamental if you play it open, is less "stable", so you have more harmonics. Not sure if I made sense there.

    I don't know what you mean by: "Also, using HiTech C they define the symbol "CCPR2" for your processor that gives you the whole 16 bit value of Timer 1 in one shot."

    Again, thanks so much for your help. I can picture you guys shaking your head staring at the computer monitor, but hey, I gotta start somewhere right? :D
     
  11. Markd77

    Senior Member

    Sep 7, 2009
    2,803
    594
    This would mean you can use CCPR1 instead of CCPR1H*256+CCPR1L, good call.

    This would be for the original way that GuitarMan suggested, and would be a valid method.
    I think the easier method is setting Timer1 on the internal clock and using capture mode.
     
    GuitarMan216 likes this.
  12. GuitarMan216

    Thread Starter New Member

    Oct 6, 2011
    18
    0
    Ah, so CCPR1 stores the capture info? And can I just say: NewCap=CCPR1?

    So you're saying I don't even need the external oscillator? I'm not sure how the internal clock works...
     
  13. Markd77

    Senior Member

    Sep 7, 2009
    2,803
    594
    Sorry for confusing you, I've been less than precise with some of my descriptions.
    The 16F877 needs an external oscillator, usually a crystal.
    What I've been calling the internal clock is the instruction cycle clock, Tcy, which is 1/4 of the external oscillator.
    The method I've been describing is to set Timer1 to the instruction cycle clock and use capture mode to make it store the Timer1 value every (n) times that a CCP pin toggles.
    The other option would be to set Timer1 to counter mode (so your guitar input would become an external clock on the T1CKI pin and Timer1 would run at the frequency of the guitar) and have a timer based on the instruction cycle clock.
    Both methods would work, but I think method 1 is simpler.
    I hope I've cleared it up a bit.
     
  14. GuitarMan216

    Thread Starter New Member

    Oct 6, 2011
    18
    0
    Ok guys, I have the code "almost" working. the middle LED (RB1), is constantly on though, and the side LED's just flicker when it's out of tune. It seems to be reading the frequency right, but the output is messed up. Any suggestions?

    Code ( (Unknown Language)):
    1.  
    2. #include <htc.h> //Microcontroller header file
    3.  
    4. #define E_S     164.81  //Low E(2nd harmonic)
    5. #define A_S     220.00
    6. #define D_S     293.67
    7. #define G_S     392.00
    8. #define B_S     493.88         
    9. #define EH_S    659.26  //High E(2nd harmonic)
    10. #define OSC     4000000L
    11.  
    12. void main()
    13. {
    14.  
    15. unsigned int capold, capnew, cap, FREQ;
    16.  
    17. INTCON = 0b11000000;
    18. PIE1 = 0b00100000;
    19. PIR1 = 0b00000000;
    20. TRISC=0b00000100; //Setting RC2 to input
    21. TRISB=0b00000000; //Setting PORTB pins to outputs
    22. T1CON=0b00110001; //Initiating TMR1(Fosc/4)(1:8 prescaler)
    23.  
    24. TMR1L = 0b00000000;     // Set Timer1 register to zero  
    25. TMR1H = 0b00000000;  
    26. CCPR1L = 0b00000000;        // Set CCP register to 0  
    27. CCPR1H = 0b00000000;
    28. CCP1CON=0b00000110; //Capture every 4th rising edge
    29.  
    30. TMR0IE = 0;     // Disable interrupt on TMR0 overflow
    31. PEIE = 1;       // Enable peripheral interrupts
    32. GIE = 1;        // Global interrupt enable
    33. CCP1IF = 0;     //Clear interrupt flag
    34.  
    35. Main:
    36.  
    37. {
    38.     if (CCP1IF)               // A TMR1 register capture occurred
    39.     {
    40.         capold = capnew;
    41.         capnew = 256*CCPR1H + CCPR1L;
    42.  
    43.         if (capnew > capold)
    44.         {
    45.             cap = capnew - capold;
    46.             FREQ=(((OSC/4)*4)/(8*(cap)));
    47.  
    48.     if(FREQ<((A_S+E_S)/2))
    49.         {
    50.             if(FREQ>70.00)
    51.                 {
    52.                     if(FREQ<(E_S-1)){
    53.                         RB0=1;
    54.                         RB1=0;
    55.                         RB2=0;
    56.                     }
    57.                     if(FREQ>(E_S+1)){
    58.                         RB2=1;
    59.                         RB1=0;
    60.                         RB0=0;
    61.                     }
    62.                     if((E_S-1)<FREQ<(E_S+1)){
    63.                         RB1=1;
    64.                         RB2=0;
    65.                         RB0=0;
    66.                     }
    67.                 }
    68.         }
    69.         }
    70.  
    71.         goto Main;
    72.     }  
    73. }
    74. }
    75.  
     
  15. GuitarMan216

    Thread Starter New Member

    Oct 6, 2011
    18
    0
    Nevermind, I got it!!! Thanks so much for the help guys, it's working great! Now I might add a servo motor for automated tuning. :D
     
  16. djsfantasi

    AAC Fanatic!

    Apr 11, 2010
    2,802
    832
    Glad you got it, but could you share what you found for us following the thread?

    Thanks
     
  17. GuitarMan216

    Thread Starter New Member

    Oct 6, 2011
    18
    0
    Sure, after a lot of research and reading, as well as your guys suggestions, I messed with the code a bunch and ended up with the code below. It wasn't so much one thing that I found, but a bunch. I'm trying to make it even better, but as of now, it's working pretty well.

    Code ( (Unknown Language)):
    1.  
    2. /*********************************************
    3. /
    4. /PIC based guitar tuner
    5. /
    6. /Ryan Monteleone
    7. /
    8. **********************************************/
    9.  
    10. #include <htc.h> //Microcontroller header file
    11. #define PIC_CLK 4000000 //External Oscillator speed
    12.  
    13. #define NOTE    1.059463094 //The 12th root of 2(The difference between each note)
    14.  
    15. #define E_S     164.81  //Low E Standard(2nd harmonic)
    16. #define A_S     220.00
    17. #define D_S     293.67
    18. #define G_S     392.00
    19. #define B_S     493.88         
    20. #define EH_S    659.26  //High E Standard(2nd harmonic)
    21.  
    22. #define E_F     E_S/NOTE    //Low E Flat(2nd harmonic)
    23. #define A_F     A_S/NOTE
    24. #define D_F     D_S/NOTE
    25. #define G_F     G_S/NOTE
    26. #define B_F     B_S/NOTE       
    27. #define EH_F    EH_S/NOTE   //High E Flat(2nd harmonic)
    28.  
    29. #define OSC     4000000L //Defining a long variable for the clock speed
    30.  
    31. void main()
    32. {
    33.  
    34. unsigned int capold, capnew, cap, FREQ; //Creating unsigned variables to capture the frequency
    35.  
    36. int x=0, y=0;   //Creating delay variables 
    37.  
    38. INTCON= 0b11000000; //Enabling all masked interrupts(including peripherals)
    39. PIE1=   0b00100000; //Enabling the USART receive interrupt
    40. PIR1=   0b00000000; //
    41. TRISC=  0b00000110; //Setting RC2 to input
    42. TRISB=  0b00000000; //Setting PORTB pins to outputs
    43. TRISA=  0b00000000; //Setting PORTA pins to outputs for 7-segment display
    44. T1CON=  0b00100001; //Initiating TMR1(Fosc/4)(1:8 prescaler)
    45.  
    46. TMR1L=  0b00000000; // Set Timer1 register to zero  
    47. TMR1H=  0b00000000; //
    48. CCPR1L= 0b00000000; // Set CCP register to 0  
    49. CCPR1H= 0b00000000; //
    50. CCP1CON=0b00000110; //Capture every 4th rising edge
    51.  
    52. TMR0IE=0;   // Disable interrupt on TMR0 overflow
    53. PEIE=1;     // Enable peripheral interrupts
    54. GIE=1;      // Global interrupt enable
    55. CCP1IF=0;   //Clear interrupt flag
    56.  
    57. RB0=0;  //clearing pins
    58. RB1=0;  //
    59. RB2=0;  //
    60.  
    61. RA0=0;  //Turning on all 7 LED's of the 7-segment display
    62. RA1=0;  //
    63. RA2=0;  //
    64. RA3=0;  // 
    65. RA4=0;  //
    66. RA5=0;  //
    67.  
    68. Main:
    69.  
    70. TRISC= 0b00000110;
    71. {
    72.     if (CCP1IF) //TMR1 capture
    73.     {
    74.         capold=capnew;
    75.         capnew=256*CCPR1H+CCPR1L; //Storing capture value in capnew
    76.  
    77.         if(capnew>capold)   //Verifying both captures are independent
    78.         {
    79.             cap=capnew-capold;
    80.             FREQ=(((OSC/4)*4)/(4*(cap)));   //Calculating the frequency based on timer, and 4 rising edges
    81.     if(FREQ<((A_S+E_S)/2))
    82.         {
    83.             if(FREQ>100.00) //Filtering out any low frequency noise
    84.                 {
    85.                     if(FREQ<(E_S-3.5)){ //Setting the tolerance
    86.                         RB0=0;
    87.                         RB1=0;
    88.                         RB2=1;
    89.  
    90.                         TRISA=0b00011000;   //7-segment display
    91.  
    92.                         for(x;x<9999;x++){ //creating a delay
    93.                             for(y;y<6;y++){
    94.                             }  
    95.                         }
    96.                         goto Main;  //Start the capture again
    97.                     }
    98.                     if(FREQ>(E_S+3.5)){
    99.                         RB2=0;
    100.                         RB1=0;
    101.                         RB0=1;
    102.  
    103.                         TRISA=0b00011000;
    104.  
    105.                         for(x;x<9999;x++){
    106.                             for(y;y<6;y++){
    107.                             }  
    108.                         }
    109.                         goto Main;
    110.                     }
    111.                     if((E_S-3.5)<FREQ<(E_S+3.5)){
    112.                         RB1=1;
    113.                         RB2=0;
    114.                         RB0=0;
    115.  
    116.                         TRISA=0b00011000;
    117.  
    118.                         for(x;x<9999;x++){
    119.                             for(y;y<6;y++){
    120.                             }  
    121.                         }
    122.                         goto Main;
    123.                     }
    124.                 }
    125.         }
    126.     if(FREQ<((D_S+A_S)/2))
    127.         {
    128.             if(FREQ>((A_S+E_S)/2))
    129.                 {
    130.                     if(FREQ<(A_S-3.5)){
    131.                         RB0=0;
    132.                         RB1=0;
    133.                         RB2=1;
    134.  
    135.                         TRISA=0b00000100;
    136.  
    137.                         for(x;x<9999;x++){ //creating a delay
    138.                             for(y;y<6;y++){
    139.                             }  
    140.                         }
    141.                         goto Main;
    142.                     }
    143.                     if(FREQ>(A_S+3.5)){
    144.                         RB2=0;
    145.                         RB1=0;
    146.                         RB0=1;
    147.  
    148.  
    149.                         TRISA=0b00000100;
    150.  
    151.                         for(x;x<9999;x++){ //creating a delay
    152.                             for(y;y<6;y++){
    153.                             }  
    154.                         }
    155.                         goto Main;
    156.                     }
    157.                     if((A_S-3.5)<FREQ<(A_S+3.5)){
    158.                         RB1=1;
    159.                         RB2=0;
    160.                         RB0=0;
    161.  
    162.  
    163.                         TRISA=0b00000100;
    164.  
    165.                         for(x;x<9999;x++){ //creating a delay
    166.                             for(y;y<6;y++){
    167.                             }  
    168.                         }
    169.                         goto Main;
    170.                     }
    171.                 }
    172.         }
    173.     if(FREQ<((G_S+D_S)/2))
    174.         {
    175.             if(FREQ>((D_S+A_S)/2))
    176.                 {
    177.                     if(FREQ<(D_S-3.5)){
    178.                         RB0=0;
    179.                         RB1=0;
    180.                         RB2=1;
    181.  
    182.                         TRISA=0b00100010;
    183.  
    184.                         for(x;x<9999;x++){ //creating a delay
    185.                             for(y;y<6;y++){
    186.                             }  
    187.                         }
    188.                         goto Main;
    189.                     }
    190.                     if(FREQ>(D_S+3.5)){
    191.                         RB2=0;
    192.                         RB1=0;
    193.                         RB0=1;
    194.  
    195.                         TRISA=0b00100010;
    196.  
    197.                         for(x;x<9999;x++){ //creating a delay
    198.                             for(y;y<6;y++){
    199.                             }  
    200.                         }
    201.                         goto Main;
    202.                     }
    203.                     if((D_S-3.5)<FREQ<(D_S+3.5)){
    204.                         RB1=1;
    205.                         RB2=0;
    206.                         RB0=0;
    207.  
    208.                         TRISA=0b00100010;
    209.  
    210.                         for(x;x<9999;x++){ //creating a delay
    211.                             for(y;y<6;y++){
    212.                             }  
    213.                         }
    214.                         goto Main;
    215.                     }
    216.                 }
    217.         }
    218.     if(FREQ<((B_S+G_S)/2))
    219.         {
    220.             if(FREQ>((G_S+D_S)/2))
    221.                 {
    222.                     if(FREQ<(G_S-6.5)){
    223.                         RB0=0;
    224.                         RB1=0;
    225.                         RB2=1;
    226.  
    227.                         TRISA=0b00010001;
    228.  
    229.                         for(x;x<9999;x++){ //creating a delay
    230.                             for(y;y<6;y++){
    231.                             }  
    232.                         }
    233.                         goto Main;
    234.                     }
    235.                     if(FREQ>(G_S+6.5)){
    236.                         RB2=0;
    237.                         RB1=0;
    238.                         RB0=1;
    239.  
    240.                         TRISA=0b00010001;
    241.  
    242.                         for(x;x<9999;x++){ //creating a delay
    243.                             for(y;y<6;y++){
    244.                             }  
    245.                         }
    246.                         goto Main;
    247.                     }
    248.                     if((G_S-8.5)<FREQ<(G_S+6.5)){
    249.                         RB1=1;
    250.                         RB2=0;
    251.                         RB0=0;
    252.  
    253.                         TRISA=0b00010001;
    254.  
    255.                         for(x;x<9999;x++){ //creating a delay
    256.                             for(y;y<6;y++){
    257.                             }  
    258.                         }
    259.                         goto Main;
    260.                     }
    261.                 }
    262.         }
    263.     if(FREQ<((EH_S+B_S)/2))
    264.         {
    265.             if(FREQ>((B_S+G_S)/2))
    266.                 {
    267.                     if(FREQ<(B_S-12.5)){
    268.                         RB0=0;
    269.                         RB1=0;
    270.                         RB2=1;
    271.  
    272.                         TRISA=0b00110000;
    273.  
    274.                         for(x;x<9999;x++){ //creating a delay
    275.                             for(y;y<6;y++){
    276.                             }  
    277.                         }
    278.                         goto Main;
    279.                     }
    280.                     if(FREQ>(B_S+12.5)){
    281.                         RB2=0;
    282.                         RB1=0;
    283.                         RB0=1;
    284.  
    285.                         TRISA=0b00110000;
    286.  
    287.                         for(x;x<9999;x++){ //creating a delay
    288.                             for(y;y<6;y++){
    289.                             }  
    290.                         }
    291.                         goto Main;
    292.                     }
    293.                     if((B_S-12.5)<FREQ<(B_S+12.5)){
    294.                         RB1=1;
    295.                         RB2=0;
    296.                         RB0=0;
    297.  
    298.                         TRISA=0b00110000;
    299.  
    300.                         for(x;x<9999;x++){ //creating a delay
    301.                             for(y;y<6;y++){
    302.                             }  
    303.                         }
    304.                         goto Main;
    305.                     }
    306.                 }
    307.         }
    308.     if(FREQ>((EH_S+B_S)/2))
    309.         {
    310.             if(FREQ<1000.00)
    311.                 {
    312.                     if(FREQ<(EH_S-14.5)){
    313.                         RB0=0;
    314.                         RB1=0;
    315.                         RB2=1;
    316.  
    317.                         TRISA=0b00011000;
    318.  
    319.                         for(x;x<9999;x++){ //creating a delay
    320.                             for(y;y<6;y++){
    321.                             }  
    322.                         }
    323.                         goto Main;
    324.                     }
    325.                     if(FREQ>(EH_S+14.5)){
    326.                         RB2=0;
    327.                         RB1=0;
    328.                         RB0=1;
    329.  
    330.                         TRISA=0b00011000;
    331.  
    332.                         for(x;x<9999;x++){ //creating a delay
    333.                             for(y;y<6;y++){
    334.                             }  
    335.                         }
    336.                         goto Main;
    337.                     }
    338.                     if((EH_S-14.5)<FREQ<(EH_S+14.5)){
    339.                         RB1=1;
    340.                         RB2=0;
    341.                         RB0=0;
    342.  
    343.                         TRISA=0b00011000;
    344.  
    345.                         for(x;x<9999;x++){ //creating a delay
    346.                             for(y;y<6;y++){
    347.                             }  
    348.                         }                      
    349.                         goto Main;
    350.                     }
    351.                 }
    352.         }
    353.         }
    354.  
    355.         goto Main;
    356.     }  
    357. }
    358. }
    359.  
     
Loading...