Mohamed Slama
Member level 4
- Joined
- Nov 8, 2013
- Messages
- 72
- Helped
- 0
- Reputation
- 0
- Reaction score
- 0
- Trophy points
- 6
- Activity points
- 443
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);
}
}
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..
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
// 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);
}
}
}}
use capture and compare mode of pic16f877a.... and pass the result in an array... it is very simple
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
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;
}
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;
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;
}
}
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;
}
}
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?