8051 tacho

This is a discussion on 8051 tacho within the C Programming forums, part of the General Programming Boards category; I,ve made a "tacho" using an 8051 , a 4511 decoder and 4 digit 7 seg display. its not very ...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    15

    8051 tacho

    I,ve made a "tacho" using an 8051 , a 4511 decoder and 4 digit 7 seg display. its not very accurate where am i going wrong with my code.

    Code:
    #include <reg66x.h>
    
    #define on 1
    #define off 0
    
    void delay();
    
    unsigned int F,number,i,count,overflow,digit1,digit2,digit3,digit4;
     float T;
    
    sbit enable_digit1 = P1^0;
    sbit enable_digit2 = P1^1;
    sbit enable_digit3 = P1^2;
    sbit enable_digit4 = P1^3;
    
    void an_interrupt() interrupt 0 using 1
    {
     TR0 =on;
      if (count<3)
      {count++;}
      else
       TR0 = off;
       T = overflow* 65536 ;
       T = T + ( TH0 *256 ) + TL0 ;
       T = T * 0.000000543;
       F = 1/T;
       number = F * 60 ;
       digit1 = (number/1000);
       digit2 = ( (number%1000)/100 );
       digit3 = ( (number%100)/10 );
       digit4 = ( (number%10) );
       count = 0;
       overflow = 0;
       TH0 = 0x00;
      TL0 = 0x00;
        }
    
    void timing ()interrupt 1 using 2
     {
      overflow++;
     }
    
    void main (void)
    {
      count = 0; // set count zero
      IT0 =on;  // make interrupt edge triggered
      TMOD = 0x01;   //mode 1
      TH0 = 0x00;
      TL0 = 0x00;
      TR0 = off; //timer0 off
      EA = on;   //global enable all
      ET0 = on;  //enable timer zero
      EX0 = on;    //enable external interrupt
     
      while (on)
        {
       P2 = digit1;         //multiplex digits on and off
       enable_digit1 = on;
       delay();
       enable_digit1 = off;
       P2 = digit2;
       enable_digit2 = on;
       delay();
       enable_digit2 = off;
       P2 = digit3;
       enable_digit3 = on;
       delay();
       enable_digit3 = off;
       P2 = digit4;
       enable_digit4 = on;
       delay();
       enable_digit4 = off;
        }
    }
    
    
    void delay()
       {
         for(i=0; i<255; i++)
        {
        }
       }

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    2,765
    How large in bits is an "unsigned int" on your micro-controller?
    I think you are overflowing it with this statement "T = overflow* 65536 ;"

    Correction, I was right the first time. Try casting by using this code.

    Code:
    T = (float)overflow* 65536 ;
    Is the value you are getting too large or too small? Or both?


    Tim S.
    Last edited by stahta01; 04-04-2011 at 12:58 PM.

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    15
    Thanks for pointing that out. i'll give it a try as soon as i can.
    Any more advice is more than welcome. Thanks again

  4. #4
    Registered User
    Join Date
    Oct 2010
    Posts
    15
    The value shown on the dislay is too high and is fluctuating

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,822
    > else
    > TR0 = off;
    > T = overflow* 65536 ;
    Your indentation suggests that the whole code should belong to the else, but in reality, only one line is part of the else.

    Everything else happens all the time (possibly fluctuating).

    ALWAYS use braces, then you always know what you mean.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Oct 2010
    Posts
    15
    ive changed code a little values on the display are changing by 60 each time, the reading are quite good between 1-5Hz and 56 -165 Hz Between 5 to 56 the reading fluctuates
    Code:
    #include <reg66x.h>
    #define on 1
    #define off 0
    
    void delay();
    
    unsigned int number,i,count, digit1,digit2,digit3,digit4;
    float T,F,overflow;
    
    sbit enable_digit1 = P1^0;
    sbit enable_digit2 = P1^1;
    sbit enable_digit3 = P1^2;
    sbit enable_digit4 = P1^3;
    
    void an_interrupt() interrupt 0 using 1
      {
      TR0 = on;
      if (count == 0)
        {
         count++;}
      else
      {
      TR0 = off;
       T = overflow* 0x10000 ;
       T = T + ( TH0 *256 ) + TL0 ;
       T = T * 0.000000543;
       F = 1/T;
       number = F * 60* count ;
       digit1 = (number/1000);
       digit2 = ( (number%1000)/100 );
       digit3 = ( (number%100)/10 );
       digit4 = ( (number%10) );
       count = 0;
       overflow = 0;
       TH0 = 0x00;
      TL0 = 0x00;
       
      }
    }
    
    void timing ()interrupt 1 using 2
     {
      overflow++;
     }
    
    void main (void)
    {
      count = 0; 
      IT0 =on;    
      TMOD = 0x01;   
      TH0 = 0x00;
      TL0 = 0x00;
      TR0 = off; 
      EA = on;   
      ET0 = on;  
      EX0 = on;    
      
      while (on)
        {
      
    
       P2 = digit1;         
       enable_digit1 = on;
       delay();
       enable_digit1 = off;
       P2 = digit2;
       enable_digit2 = on;
       delay();
       enable_digit2 = off;
       P2 = digit3;
       enable_digit3 = on;
       delay();
       enable_digit3 = off;
       P2 = digit4;
       enable_digit4 = on;
       delay();
       enable_digit4 = off;
        }
    }
    
    
    void delay()
       {
         for(i=0; i<55; i++)
        {
        }
       }

  7. #7
    Registered User
    Join Date
    Oct 2010
    Posts
    15
    The display is now not jumping up in steps of sixty, but using an input signal of less than 60 Hz, i am still getting the wrong readings on the display.Could this be due to the micro I am using

  8. #8
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    Well you did multiply by 60 for some reason.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 8051 microcontrollers
    By ygfperson in forum Tech Board
    Replies: 5
    Last Post: 12-02-2002, 12:29 PM
  2. Writing C code to control I/O of 8051 chip
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 08-12-2002, 08:28 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21