This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

How to use time0 as event counter on AT89S52 ?

Guys,

I'm trying to count a pulse from T0 input on P3.4,

Here's the code :
sbit counter_input=P3^4; //input pulse from op-amp
 void init_timer()
{
   TL0=0x00;
   //TH0=0x00;

   counter_input = 1; //set it as input to read a pulse from op-amp
   counter_input = 0x00; //clear the counter

   TMOD=0x05;
   P0=0x00;
}
main :
.
.
.
unsigned int counter=0,d1,d2,d3,dx,dy,dz,x;
  lcd_ini(); //init LCD
  init_timer();//init timer0
  TR0=1;

   while (1)
    {
      counter=TL0;
      P0=TL0;
      x=counter/10;
      d1=counter%10;
      d2=x%10;
      d3=x/10;

      dx=d1+48;
      dy=d2+48;
      dz=d3+48;
   lcd_data(dx); lcd_data(dy);     lcd_data(dz);

}


I got the output already, but it's not consistent yet,
781, 881,321, 050...etc,
I tested with putting input to the sensor it's responding on the display according to the input, but...it's not yet displaying for example 000, 030, 050,090, etc...

Any ideas on this case ?
I'm abit confuse on using timer and counter ....

Thanks for the ideas

  • counter_input = 1; //set it as input to read a pulse from op-amp
    counter_input = 0x00; //clear the counter
    

    HUH?

  •       x=counter/10;
          d1=counter%10;
          d2=x%10;
          d3=x/10;
    
          dx=d1+48;
          dy=d2+48;
          dz=d3+48;
       lcd_data(dx); lcd_data(dy);     lcd_data(dz);
    

  • so dz,dy,dx ?
    any other concern ?
    thanks

  • When you have a source line like:

    dx=d1+48;
    


    It is very hard to see what it does. Exactly what does the value 48 mean to a reader?

    If you have a source line looking like:

    dx=d1+'0';
    


    it is way easier to spot that you are converting an integer value 0 .. 9 into the characters '0' .. '9'.

    Try to avoid the use of magic numbers in your code. It is a great strength in C that you can make use of character constants in expressions - a strength wasted if not used.

    Another interesting thing is that you used the variable names dx, dy, dz. The names have zero meaning - they don't help a reader to understand the code. And more importantly, they did not help you keep track of which variable is counting hundreds, tens or ones. So you managed to print the most significant digit last and the least significant digit first. There really is an important lesson there, about the choice of variable names.

    But the main issue here - exactly what does your debugging process look like? Exactly how can your debugging fail to spot that you perform two different assigns to the same variable while the comment following the two assigns gives completely different descriptions of what assigns to the variable actually means? Have you actually tried to debug the code?

  • I have no idea yet on how to debug it ?
    How can I see my counter doing what I want ?

    I did already on the board and op amp has given me a pulse, but I'm not sure if I'm doing the right code for it...
    That's why I asked to this forum.