Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Low rpm tacho
09-10-2016, 02:12 PM,
#1
Brick  Low rpm tacho
Hello everybody.

This is my first "big" project, the good news is: it works.

This is a slow rpm tacho, ranging from +-5 rpm to 90. Not confirmed

Please feel free to make comments, any.

Code:
/* Tachometer for low RPM
       fpeixeiro (Fernando Peixeiro) - 2016

 The circuit:
 Pinguino 2550 v4.14
 max7219 with 3 blue 7seg cc
 FC-51 obstacle detector

*/
// delay to give time to see the display
#define DELAYTIME 150

// input from tacho sensor
#define tachopin 7

// counter for tmr1 overflow
unsigned int tmr1intcount = 0;

// port from where to listen
unsigned char tacho = HIGH;

// variable for count overflows between slot
unsigned int ovfbtslot = 0;

// variable for total time between slot
long timebtslot = 0;

// variable for rpm
long rpm = 0;

// variable to determine if 7seg needs to be cleared
unsigned char flag = 0;
   
void setup()
{
 
 /*
  Connections Pinguino -> MAX7219
  pin 2 is connected to the DataIn (pin1)
  pin 3 is connected to the CLK (pin13)
  pin 4 is connected to LOAD (pin12)
  1 : We have one MAX7219. */
LedControl.init(2,3,4,1);            // setup of max7219
LedControl.shutdown(0,false);   // Command for wake up
LedControl.clearDisplay(0);       // Clears the digits
LedControl.setIntensity(0,8);     //Set intensity for the digits (depends on the value of the resistor Iset, from 0 to 15)
 
 // -----FC-51 Setup -----
 pinMode(7, INPUT);
     
 // ----- timer 1 Setup -----
 // sets timer 1
 TMR1H = 0;
 TMR1L = 0;
 // timer1 register is 16 bits
 T1CONbits.RD16=1;
 // clock source for timer 1 is derived from clock
 T1CONbits.T1RUN=0;
 // prescale value 1/8
 T1CONbits.T1CKPS1=1;
 T1CONbits.T1CKPS0=1;
 // timer 1 oscillator is shut off ( internal clock )
 T1CONbits.T1OSCEN=0;
 // T1SYNC is not used ( internal clock )
 // timer clock is internal clock ( Fosc/4 )
 T1CONbits.TMR1CS=0;
 // timer 1 is On
 T1CONbits.TMR1ON=1;
 // FOSC is 20 Mhz => Internal clock = 20 Mhz / 4 = 5 Mhz
 // clock period is 200 ns
 // prescale for timer 1 is 8 => clock period for timer 1 is 1,6 us
 // timer 1 is incremented every 1,6 us
 // timer 1 is a 16 bits register, it will overflow every 1,6 us*65536 = 104 ms
 // when timer 1 overflow it increments a variable (tmr1intcount)
 // peripheral interrupt
 INTCONbits.PEIE=0;
 // global enable interrupt
 INTCONbits.GIE=0;

}
       //section in test
void print7Seg()        // print to 7segments
  {
  unsigned int decimal;      // decimals
  unsigned int unit;            // units
  unsigned int tunit;           // tens of units

  decimal = rpm%10;
  rpm=rpm/10;
  unit=rpm%10;
  rpm = rpm/10;
  tunit = rpm;

  LedControl.setDigit(0,0,(byte)decimal,false);     // decimal
  LedControl.setDigit(0,1,(byte)unit,true);            // unit
  LedControl.setDigit(0,2,(byte)tunit,false);          // tens of unit

}

void loop()
{

  // routine to clear the 7seg, when flag is reset
  if(flag==0)
    {
      LedControl.clearDisplay(0);       // Clears the digits
      flag=1;
    }

 // timer 1 interrupt flag in register PIR1
 if (PIR1bits.TMR1IF)
   {
      // reset interrupt flag
      PIR1bits.TMR1IF=0;
      // count overflows
      tmr1intcount ++;
      }

                   // reads FC-51
                   tacho = digitalRead(tachopin);
                   
                   // if LOW
                   if (tacho == LOW)
                     {
                       // copys overflow count to variable
                       ovfbtslot = tmr1intcount;
                       
                       // resets overflow count
                       tmr1intcount = 0;
                       
                       // calculates time between slot
                       timebtslot = ovfbtslot*104;
                       
                       // calculates rpm
                       rpm = 600000/timebtslot;
                       
                       // send information to max7219 (7seg display)
                       print7Seg();
                       
                       // a slight delay to see result on 7seg
                       delay(DELAYTIME);
                       
                       // resets some variables
                       ovfbtslot = 0;
                       timebtslot = 0;
                       rpm = 0;
                       }
                       
       // if no interruption of the beam, cleans off 7seg display
       if (tmr1intcount >=100)
       {
       flag = 0;
       }
                       if (tmr1intcount >= 250)
                       {
                       // if no interruption of the beam in aprox 4 s
                       // the max is switched off to save energy
                        LedControl.shutdown(0,true);
                       }
       if (tmr1intcount)
       {
       // if an interruption of the beam happens
       // max will be on again
       LedControl.shutdown(0,false);
       }

}

Tanks for everyone who helped, Heart

fpeixeiro
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)