Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Reading values of frequency in PIC

Status
Not open for further replies.

Mohamed Slama

Member level 4
Member level 4
Joined
Nov 8, 2013
Messages
72
Helped
0
Reputation
0
Reaction score
0
Trophy points
6
Activity points
443
Hi friends

I have designed a system to read frequency as follow :

when a coin passed through 2-coils that connected in oscillation circuit the frequency will

change according to the position of coin .

changing of frequency occurs every a bout 10 ms .

i want to read this values and put it in a vector , i have write the code but didnt work

correctly . this is the part of code that measure frequency and put into a vector

Code:
 for(;;)
   {
        if(flag==1) //if one second elapsed
        {
           tmr1_Val = (TMR1H << 8) + TMR1L; //get high and low from timer1
           Freq = (OverNumb*65536) + tmr1_Val;  //calculate frequency
                    
              s_vector[i]=Freq;
                  Delay_ms(12); // put values every 12 ms 
                 
                  i++ ;
                  
           if (i>5)
                  {
               flag=0 ;
               i=0;

                 }

        }
        
        
        
        
        
        
        else
   {  // printing values on LCD to ensure that the values is put coorectly

     for (i=0; i<6;i++)
                      {

                      intToStr(s_vector[i],txt5);
                      Lcd_Out(2,1,txt5);
                      delay_ms(100);
                      Lcd_Cmd(_LCD_CLEAR);
                      }

     }
 

Re: Problem in reading values of frequency in PIC

hello,

Explain more what do you need...

What ref has your PIC ?
How do you handle the flag for one second ?
are you using interrupt ?
Delay 12mS * 5 => 60mS ???

if you want to compare every 10mS => interrupt every 10mS
and measure periode instead of frequency between each event
via CCP edge detection and associated timer..
 

Re: Problem in reading values of frequency in PIC

hello,

Explain more what do you need...

What ref has your PIC ?
How do you handle the flag for one second ?
are you using interrupt ?
Delay 12mS * 5 => 60mS ???

if you want to compare every 10mS => interrupt every 10mS
and measure periode instead of frequency between each event
via CCP edge detection and associated timer..

thank you for reply

im using Pic 16f877a

my code work correctly in reading frequency , i calculate frequency every 10 ms because the speed of coin through coils is very high .

the problem is :

how to put frequency every 10 ms or 12 ms into a vector to compare it with another vector for other coins .
 
Last edited:

Re: Problem in reading values of frequency in PIC

Is the speed coin allways the same ?
Is the frequency , without coin enough stable to use it as a reference ?
and use a circular buffer, maybe 100 value ~1 second , filled at 10ms intervall ..
calculate a maxima and minima to detect if a coin has been passed trought the coils.
Analyse the data to find out the correct algorythem

Have you got other sensor to detect edge of the coin..
in this case you can use another solution:
if you want to detect the delta speed of the coin

Boucle:
.......
arme Timer0 interupt for 10mS without starting it.
initailse Timer1 without interrupt
use RB0 interrupt to detect first edge of the falling coin
initialise RBO interrupt for Rising edge detection
inside RBO interrupt
put timer0 ON
put Timer1 ON
reverse IntRBO for falling edge detect
at every timer0 (10mS) interrupt ,
catch the value of timer1
count possible overflow of timer1
and store values in a table

when the next Interrupt RBO will occurs on falling edge detect,
the coin finished to pass trough coin detector
stop RB0 interrupt
stop timers
stop timer0 interrupt
.....
goto boucle , wait next coin

maybe you can also use another timer to count total elapsed time for the coin
betwen the 2 RBO edge detection
 

Re: Problem in reading values of frequency in PIC

Dear paulfjujo

i have already calculated elapsed time in other code

already i could to get frequency every 10 ms , my problem is :I need save values of frequency into table or array or any thing to compare it with other .

Complete Code
Code:
// LCD module connections
sbit LCD_RS at RC6_bit;
sbit LCD_EN at RC7_bit;
sbit LCD_D4 at RD4_bit;
sbit LCD_D5 at RD5_bit;
sbit LCD_D6 at RD6_bit;
sbit LCD_D7 at RD7_bit;

sbit LCD_RS_Direction at TRISC6_bit;
sbit LCD_EN_Direction at TRISC7_bit;
sbit LCD_D4_Direction at TRISD4_bit;
sbit LCD_D5_Direction at TRISD5_bit;
sbit LCD_D6_Direction at TRISD6_bit;
sbit LCD_D7_Direction at TRISD7_bit;
// End LCD module connections


//variables
char Freq_txt[11];
char s_vector[50];
unsigned int tmr1_Val=0;
unsigned int OverNumb=0;
unsigned int Freq=0;
unsigned short tmr0_cnt=10;
bit flag;
char txt5[7];
char txt6[7];
float TPer=0;
 unsigned char i =0 ;
  unsigned int x =0 ;
  unsigned int cnt =0 ;

void _Init()
{
  trisd.f0=0;
  lcd_init();
  Lcd_Cmd(_LCD_CURSOR_OFF);
  Lcd_Cmd(_LCD_CLEAR);

  //timer0 config
  intcon.f7=1;   //enable global interrupt
  intcon.f6=1;  //enable pref interrupt
  intcon.f5=1; //timer0 overflow interrupt
  OPTION_REG.T0CS = 0; //Internal instruction cycle clock
  OPTION_REG.PSA = 0;  //0 = Prescaler is assigned to the Timer0 module
  OPTION_REG.PS2 = 1; OPTION_REG.PS1 = 1; OPTION_REG.PS0 = 1;  //prescalar is set to 256
  tmr0_cnt=10;
  tmr0=98; //Timer0 starts counting from 98(TMR0 Preload)


  //timer1 config
  PIE1.f0=1; //Enables the TMR1 overflow interrupt
  t1con.f1=1; //External clock (on the rising edge)
  t1con.f3=1; //Oscillator is shut-off
  t1con.f2=1; //Synchronize external clock input
  tmr1l=0; tmr1h=0; //clear timer1
}
//ISR
void interrupt()
{

 if(intcon.f2==1) //if timer0 is overlfowed
 {
   if(--tmr0_cnt==0) //decrement and check tmr0_cnt until zer0 which means a 10 ms second
   {
     intcon.f7=0; t1con.f0=0;  //stop counting and global interrupts
     flag=1;  //set flag
   }
   intcon.f2=0; //reset timer0 flag
   tmr0=98;  //Timer0 starts counting from 98 again
 }

 if(pir1.f0==1) //if timer1 is overlfowed
 {
   OverNumb++;  //increment number of overflow
   pir1.f0=0; //reset timer1 flag
 }

}


void  Reset_Flags(void)
{
     tmr1l=0;
     tmr1h=0;
     tmr1_Val=0;
     OverNumb=0;
     Freq=0;
     TPer=0;             //reset variables
     tmr0_cnt=10;
     flag=0;           //reset variables
        intcon.f7=1;
      t1con.f0=1;
}

  Get_Freq(void)
{
  if(flag==1) //if one second elapsed
        {

       tmr1_Val =(TMR1H << 8) + TMR1L);  //get high and low from timer1
           Freq = (OverNumb*65536) +  tmr1_Val;  //calculate frequency
         LongWordToStr(Freq,Freq_txt);     
           Reset_Flags();
return Freq_txt;
        }


}



void main()
{
char  stringArray[40] ;
   trisb.f0=1;
   trisb.f1=0;
   _Init();

    for(;;)
   {

if (portb.f0==1){
   for (i=0;i<40;i++)   {
  
      stringArray[i]=Get_Freq();
     delay_ms(1); // because the freq is captured every 10ms
       intToStr( stringArray[i],txt5);
        lcd_out(1,1,txt5);

 
                          }

                       }

         else
                       {
                       for (x=0;x<40;x++)   {
                        intToStr( stringArray[x],txt6);
                         lcd_out(2,1,txt6);

                          delay_ms(100);
                                       }


                        }
}}
 
Last edited:

Re: Problem in reading values of frequency in PIC

use capture and compare mode of pic16f877a.... and pass the result in an array... it is very simple
 

Re: Problem in reading values of frequency in PIC

use capture and compare mode of pic16f877a.... and pass the result in an array... it is very simple

in the code above i have already used capture , my problem is how to save values of frequency that i have already got into an array .
 

Re: Problem in reading values of frequency in PIC

use eeprom with spi or i2c protocol and save them.... then u can easily read them compare them or do what ever operations we need... i hope it will be useful to you sir
 
Re: Problem in reading values of frequency in PIC

use eeprom with spi or i2c protocol and save them.... then u can easily read them compare them or do what ever operations we need... i hope it will be useful to you sir

Thank u so much
 

Re: Problem in reading values of frequency in PIC

hello,


I suspect some probleme for storing the values of frequencies...

Code:
  Get_Freq(void)
{
  if(flag==1) //if one second elapsed
        {

       tmr1_Val =(TMR1H << 8) + TMR1L);  //get high and low from timer1
           Freq = (OverNumb*65536) +  tmr1_Val;  //calculate frequency
         LongWordToStr(Freq,Freq_txt);     
           Reset_Flags();
return Freq_txt;
        }

this function return a char pointer Freq_txt, so declare as
(char *) get_Freq(void)

char stringArray[40]; is a table of 40 cars !
you must use an array of pointer
char stringArray [40][11];


do you want to store Text or decimal value ?
maybe it is more usefull to store decimal 32 bits value in a array of
unsigned long int StoreArray[40];

Freq = (OverNumb*65536) + tmr1_Val; //calculate frequency
StoreArray=Freq;
 
Re: Problem in reading values of frequency in PIC

hello,


I suspect some probleme for storing the values of frequencies...

Code:
  Get_Freq(void)
{
  if(flag==1) //if one second elapsed
        {

       tmr1_Val =(TMR1H << 8) + TMR1L);  //get high and low from timer1
           Freq = (OverNumb*65536) +  tmr1_Val;  //calculate frequency
         LongWordToStr(Freq,Freq_txt);     
           Reset_Flags();
return Freq_txt;
        }

this function return a char pointer Freq_txt, so declare as
(char *) get_Freq(void)

char stringArray[40]; is a table of 40 cars !
you must use an array of pointer
char stringArray [40][11];


do you want to store Text or decimal value ?
maybe it is more usefull to store decimal 32 bits value in a array of
unsigned long int StoreArray[40];

Freq = (OverNumb*65536) + tmr1_Val; //calculate frequency
StoreArray=Freq;


Thank you
yes , you are right so i have modified code , but is necessary to use I2C ?

- - - Updated - - -

there are a problem in saving valus in this code

what is wrong in code paulfjujo

Code:
  for(;;)
   {
       if(flag==1) //if one second elapsed
        {

         tmr1_Val =(TMR1H << 8) + TMR1L;  //get high and low from timer1
            Freq =(OverNumb*65536) + tmr1_Val ;  //calculate frequency
                  Freq/=100;
           
        //  LongWordToStr(Freq,Freq_txt); //convert Frequency to string
          // floatToStr(TPer,TPer_txt);   //convert Period to string
                 if(portb.f0==1){


          for(x=0;x<10;x++){
          
          if (cnt != Freq){
  StoreArray[x]=Freq;
 intToStr( StoreArray[x],txt5);

  
        lcd_out(1,1,txt5);
     
      delay_ms(100);
      cnt=(char)Freq;
                       }

                     }
                        Reset_Flags();
                       }

                  else {
                    for (i=0;i<10;i++)   {
                        intToStr(StoreArray[i] ,txt5);
                         lcd_out(2,1,txt5);
                 
                        delay_ms(100);
                              }
                              i=0;
                              }








        }
 

Re: Problem in reading values of frequency in PIC

Hello,

i don't understand what you do you want to do with "Cnt" value..
don't forget to use a 32 bits (unsigned long for "Freq" value.
so with "cnt" as an unsigned char
you will get the LSB of the 32 bits word Freq

exemple :
if Freq=123546 (decimal value)
Cnt=(unsigned char) Freq ; gives 154 (decimal value)
because Freq in Hexa = 0x001E289A
and cnt in Hexa = 0x9A and 0x9A -> 154 decimal.
 

Re: Problem in reading values of frequency in PIC

thank u paulfjujo

suppose you can read frequency every 10 ms by your PIC Okay , i have already done this part .

can you write a code in mikroC Pro to place 50 value of frequency into a Vector (one dimensional array )

and display this vector value by value every 1 second on LCD .
 

Re: Problem in reading values of frequency in PIC

is this code correct
suppose the frequency value is Freq

Code:
 if (portb.f0==1)
        {
                s_vector[i]=Freq;
                 Delay_ms(30);
                longwordToStr(Freq,Freq_txt); //convert Frequency to string
                      Lcd_Out(1,1,Freq_txt);
                      i++;

        Reset_Flags();
        }
      else
        for (x=1;x<=50;x++) {
       // s_vector[x]=x;
                          longwordToStr(s_vector[x],Freq_txt);
                             Lcd_Out(2,1,Freq_txt);
                             delay_ms(1000); 

                            }
                            x=0;
   }
}
 

Re: Problem in reading values of frequency in PIC

hello,

initialisation must be like this...
unsigned long Freq;
unsigned long s_vector[50]; // it means 50x4=200 bytes of ram

your program must work if you have enough RAM !
check avaibility ...
if tested it only with a size of 4 elements instead of 50 ..
but i only have MikroC demo and tested on a PIC12F1840..

nota: Pic18F26K22 has 3K of RAM !!!
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top