problem in my code ???

Discussion in 'Embedded Systems and Microcontrollers' started by sayf alawneh, Oct 2, 2014.

  1. sayf alawneh

    Thread Starter New Member

    Aug 4, 2014
    guys i really need help my project is to make a frequency counter (frequency meter) can cout frequency up to 5Mhz using TMR1 or TMR0 i am lookig since 2 weeks in every where but still cant understand cause every body explain it different way than the other ,,,, i just need to know the maximum frequency i can count using both timers and whatt is the relation between the max frequency i can count and the crystal oscillator ?
    also lot of guys told me to use TMR1 but why TMR1 and not TMR0 ??? i know TMR1 is 16 bit and TMR0 is 8 bits
    for my project i used TMR1 and i really dont know why :(
    as a test i used PWM from pic16f877a and sent the pWM pulse to T1CkI in another 16f877a
    but the accuracy wasnt good !! for example a pwm of 15 k HZ was read 15252 HZ an error of 252 HZ ???
    also i couldnt read more than 30Khz i couldnt get the needed range from 1 hz to 5Mhz !! also i need good accuracy is the problem in the code??? here is the counter code guys

    Code ( (Unknown Language)):
    2. sbit LCD_RS at RB4_bit;
    3. sbit LCD_EN at RB5_bit;
    4. sbit LCD_D7 at RB0_bit;
    5. sbit LCD_D6 at RB1_bit;
    6. sbit LCD_D5 at RB2_bit;
    7. sbit LCD_D4 at RB3_bit;sbit LCD_RS_Direction at TRISB4_bit;
    8. sbit LCD_EN_Direction at TRISB5_bit;
    9. sbit LCD_D7_Direction at TRISB0_bit;
    10. sbit LCD_D6_Direction at TRISB1_bit;
    11. sbit LCD_D5_Direction at TRISB2_bit;
    12. sbit LCD_D4_Direction at TRISB3_bit;
    13. void main() {
    14. int c=0,i=0,n=0;
    15. char txt[5],txt1[5],*res;;
    16. lcd_init();
    17. lcd_cmd(_LCD_CLEAR);
    18. lcd_cmd(_LCD_CURSOR_OFF);
    19. TRISC=1;
    20. TRISB=0;
    21. TRISD=0;
    22. PORTD= 0;
    23. TMR1L=0;
    24. TMR1H=0;
    25. LCD_OUT(1,1,"WORKING..");
    26. delay_ms(500);
    27. lcd_out(1,1," WORKING.");
    28. delay_ms(500);
    29. lcd_out(1,1," WORKING ");
    30. T1CON= 0B00000011;delay_ms(1000);
    31. T1CON= 0B00000010;
    32. c= TMR1L;
    33. i= TMR1H * 256;
    34. n= c+i;
    35. IntToStr(n,txt);
    36. lcd_cmd(_LCD_CLEAR);
    37. delay_ms(100);
    38. lcd_out(1,1," FREQUENCY IS");
    39. delay_ms(100);lcd_out(2,0,txt);
    40. lcd_out(2,7," Hz ");
    41. }
    and the PWm code is simple just to generate a pulse with 15Khz
    guys am really a newbie please i need help i really need it please explain to me as a newbie
    i only have 5 days to finish this project :(
    Last edited by a moderator: Oct 2, 2014
  2. shteii01

    AAC Fanatic!

    Feb 19, 2010
    A) wall of text
    B) no punctuation
    C) code not commented
  3. jjw


    Dec 24, 2013
    - oscillator frequency ?, is it crystal oscillator ?
    - does the compiler know what the frequency is ?
    - Timer1 overflows at 65536 and you should count the number of overflows, if the measuring time is 1000ms and want to measure higher frequencies
    - signed integer overflows at 32768
    - you should zero Timer1 just before enabling it
    - are you sure that the PWM-frequency is exatly 15000Hz