tachometer with pic16f887

Discussion in 'Embedded Systems and Microcontrollers' started by soulraven, Mar 2, 2009.

  1. soulraven

    Thread Starter New Member

    Mar 2, 2009
    3
    0
    please help me with this code, trying to make a tachometer with pic16f887. only show me all the time 330.
    Set CCP1 capture but in vain ....
    where is wrong?
    I put in main.c
    ccp1_isr ();
    turometru ();
    where I wrong?
    Code ( (Unknown Language)):
    1.  
    2. #include "main.h"
    3.  
    4. #priority CCP1, TIMER1
    5.  
    6. #define BytePtr(var, offset)   (char *)(&(char *)var + offset)
    7.  
    8. #byte PIR1 = 0xF9E
    9. #bit  TMR1IF = PIR1.0
    10.  
    11. int8  gc_timer1_extension = 0;
    12. int8  gc_capture_flag = FALSE;
    13. int32 g32_ccp_delta;
    14.  
    15. //------------------------------------------------------
    16. #int_timer1
    17. void timer1_isr(void)
    18. {
    19. gc_timer1_extension++;
    20. }
    21.  
    22. //------------------------------------------------------
    23.  
    24. #int_ccp1
    25. void ccp1_isr(void)
    26. {
    27. char timer_ext_copy;
    28. int32 current_ccp;
    29. static int32 old_ccp = 0;
    30.  
    31. gc_capture_flag = TRUE;      
    32.  
    33. current_ccp = (int32)CCP_1;  
    34.  
    35. // Get local copy of the timer ext.
    36. timer_ext_copy = gc_timer1_extension;
    37.  
    38.  
    39. if(TMR1IF)
    40.   {
    41.    if(*BytePtr(current_ccp, 1) < 2)  // Was CCP captured after Timer1 wrapped?
    42.       timer_ext_copy++;  // If so, inc the copy of the timer ext.
    43.  
    44.    // Since we know a timer interrupt is pending, let's just
    45.    // handle it here and now.  That saves a little load off
    46.    // the processor.
    47.    gc_timer1_extension++;  // Increment the real timer extension
    48.    TMR1IF = 0;     // Then clear the Timer1 interrupt
    49.   }
    50.  
    51. // Insert the timer extension into the proper place in the 32-bit
    52. // CCP value.
    53. // ie.,  Insert it into location "EE" as follows: 0x00EEnnnn
    54. // (nnnn = the CCP).
    55. *BytePtr(current_ccp, 2) = timer_ext_copy;
    56.  
    57. g32_ccp_delta = (current_ccp > old_ccp) ? current_ccp - old_ccp : current_ccp + (0x1000000 - old_ccp);
    58.  
    59. // Save the current ccp value for next time.
    60. old_ccp = current_ccp;
    61.  
    62. }
    63.  
    64. //=======================
    65. void turometru()
    66. {
    67. int16 frecventa;
    68. int16 frequency;
    69. int32 current_ccp_delta;
    70.  
    71. set_timer1(0);          
    72. setup_timer_1(T1_INTERNAL | T1_DIV_BY_1);  
    73. setup_ccp1(CCP_CAPTURE_RE);  
    74.  
    75. // Enable interrupts.
    76. clear_interrupt(INT_TIMER1);
    77. enable_interrupts(INT_TIMER1);
    78.  
    79. clear_interrupt(INT_CCP1);
    80. enable_interrupts(INT_CCP1);
    81. enable_interrupts(GLOBAL);
    82.  
    83. //while(1)
    84. //  {
    85.    disable_interrupts(GLOBAL);
    86.    current_ccp_delta = g32_ccp_delta;;
    87.    enable_interrupts(GLOBAL);
    88.  
    89.    if(gc_capture_flag == TRUE)
    90.      {
    91.     frequency = (int16)((5000000L + (current_ccp_delta >> 1)) / current_ccp_delta);
    92.     frecventa = (frequency * 60)/2;
    93.    
    94.     lcd_gotoxy(5,1);
    95.     printf(lcd_putc,"%lu\n", frecventa);
    96.     printf("%lu\n\r", frecventa);
    97.      // printf(lcd_putc,"%lu Hz, delta = %lx \n\r", frequency, current_ccp_delta);
    98.  
    99.       gc_capture_flag = FALSE;
    100.     }
    101.   else
    102.     {
    103.     lcd_gotoxy(5,1);
    104.     printf(lcd_putc,"0  \n");
    105.     printf("0  \r");
    106.     }
    107.  
    108.   delay_ms(100);
    109.  //}
    110.  
    111. }
    112.  
     
Loading...