Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Log Data using pic16F877A

Status
Not open for further replies.
When the first pulse appears, zero a counter (as many bits as needed), then start counting in a software loop, checking the pulse again in the loop. When the next pulse appears stop counting - the counters then hold a representation of the time between pulses.

Because PIC's run so fast, and your pulses are so slow, it's best to use delay routines within the loop.

Here's a pulse measuring routine from my IR tutorial.
Code:
Read_Pulse      clrf	LoX
		btfss   IR_PORT, 	IR_In	;wait until high
            	goto    $-1
		clrf	tmp1
		movlw	0xC0			;delay to decide new keypress
		movwf	tmp2			;for keys that need to toggle

Still_High	btfss   IR_PORT, 	IR_In	;and wait until goes low
            	goto    Next
		incfsz	tmp1,f
		goto	Still_High
		incfsz	tmp2,f
		goto	Still_High
		bsf	Flags2,	New		;set New flag if no button pressed
		goto	Still_High

Next		nop
		nop
		nop
		nop
		nop				;waste time to scale pulse
		nop				;width to 8 bits
		nop
		nop
		nop
		nop
		nop
		nop
		incf	LoX,	f
        	btfss   IR_PORT, 	IR_In
        	goto    Next			;loop until input high again

The section 'Next' is the part that actually measures the width of the pulse, the first part waits for the trailing edge of the first pulse - because it's measuring the width of the pulse, and not the time between them.
 
I must say i'm a very happy camper
Nigel , i used your Ir 5.1 Pgm ,and changed it where you said to , to work with my F877A , and its responding !
i havnt hooked up the LCD yet but i can see it trying to communicate with the LCD, on port C !
yay
Thanks
willib
PS tomorrow i'll hook on the lcd and see what its trying to say
thats a pretty long program, about 93 instructions
 
i just got done working (smile) with a shift register , that i'm gonna use to talk to an lcd display it should work nicely tomorrow i'll have some photos of the lcd working hopefully
 
LCD problems

no photos
i did get it working though , sort of
The LCD is a single line type 16 chars
I had it up to HELLO WO
8 chars into the process it stopped
and the curser dissappeared , so i backed it up one space , but it still wouldnt advance past half way???
I remember somewhere that the 2 line versions handeled the second line differently , does the single line type do the same thing with the other half of the line?
 
A 1*16 display is arranged as a 2*8 so to write to the second half of the display you need to write to address 0x40. You will also have to setup the registers to tell it that it's a 2 line display.

HTH

Mike.
 
Hello williB,
It seems that i couldnt use 16f877a for R.P.M mesurement. If you could help ,please post reply. How to measure .01 u sec time diffrence using PIC16F877a it seems challenging. Unfortunately I cant.. Can you
??
 
sangram said:
Hello williB,
It seems that i couldnt use 16f877a for R.P.M mesurement. If you could help ,please post reply. How to measure .01 u sec time diffrence using PIC16F877a it seems challenging. Unfortunately I cant.. Can you
??

10nS resolution! - I should think you find it challenging!.

However, I don't see as 10nS has any connection to R.P.M. measurement, things don't spin that fast!.

What are you trying to do?.
 
ISA bus...totally bizzare....well, at least it will run on a 386/486 industrial backbone anywhere...

At least he's having fun with it :)

hehe, I remember back to when I first encountered file records and types...like the Intel, Tek, Motorola and others...my first pc prog was written in Qbasic and it's purpose was not only to strip off the record type data, but also join all the pieces of my object code together because the compiler I was using could only handle 2K, but the chip I was using had 8K onboard. Was a Texas micro if I remember correctly. I don't think I have looked at record types since :)
 
Hi every one can anybody help me out to decide prescaler and post scaler and PR2 using 4 mHZ crystal to get exact 1msec. I am doing it by trail and error...Please if you can
 
sorry sangram '
i may have been able to help ya out a couple of months ago
but i just ran some numbers on your question and got a really , really small number for PR2+1
you would think it would get easier , but , it doesnt !
as soon as you dont look at it for a while , poof its gone, for me anyway
i still have not worked out how to get RPM , but as soon as i get the time i will figure it out.
 
Thanks a lot

Thanks a lot for reply.. I am feeling lonely in this problem.. You give me relief.. See i did it using timer 1 as counter and timer 2 as timer to measure 1ms i did it by trial and error..Now i am getting count with some errror for low HZ 2 to 200 hz its +/_ 1 error. But as i move towards high frequency it starts to increment
I am sending you sample code you can find it on your own.. Still iam not able to calculate pr2 for 1ms delay.. How to decide do you have any idead.
I assume following methos
Say for 4 Mhz crystal basic clock frequency will become 1 Mhz i.e. ! u second.. With prescalar of 1:1 it will remain 1 u second.. And for post scaler of 1:4 and PR2=199 it will become 199* 1usecond = 199 u second...Ok
So for post scaler of 1:4 it will become 199*4=796 msec..but it is not i hope i am writing correct calculation.. send me your calculations..

check the code
it is written in mikroc.

void init_timer2(void);
void init_ports(void);
void init_timer1(void);
void hex_2_BCD(unsigned long ) ;

unsigned int sec_cnt=0;
unsigned int cnt=0,int_cnt=0,ext_int_cnt=0;
unsigned char res[5];

/***************************************************************************/
void interrupt()
{
if(INTCON.INTF)
{
PORTC.F4 = ~PORTC.F4;
if(ext_int_cnt==0)
{
++ext_int_cnt;
T2CON.TMR2ON =1 ;
T1CON =0X07;
INTCON.INTE =0 ;
}
INTCON.INTF = 0 ;
}
else if(PIR1.TMR2IF==1)
{
++sec_cnt;
if(sec_cnt>=1004)
{
PORTB.F6 = !PORTB.F6;
T2CON.TMR2ON = 0 ;
T1CON =0X06;
}
PORTB.F7 = !PORTB.F7;
PIR1.TMR2IF=0;
}
}
/***************************************************************************/
void main(void)
{
init_ports();
init_timer1();
init_timer2();
USART_Init(9600); // Initalize USART (9600 baud rate, 1 stop bit, ...
USART_Write('M');
INTCON =0XC0;
INTCON.INTE =1;



while(1)
{
if(sec_cnt>=1004)
{


int_cnt = TMR1H <<8;
int_cnt = int_cnt | TMR1L ;

hex_2_BCD(int_cnt) ;

int_cnt = 0;
TMR1H = 0;
TMR1L = 0;
sec_cnt = 0;
ext_int_cnt = 0;
INTCON.INTE = 1;
T1CON = 0X07;
}
};

}
/***************************************************************************/
void init_timer1()
{
T1CON = 0X06;
}
/***************************************************************************/
void init_timer2()
{
T2CON = 0x20; //TIMER IS OFF | PRESCALER 1:1 | POST SCALER 1:8
INTCON = 0x00;
TMR2 = 0x00;
PIR1.TMR2IF = 0 ;
PIE1.TMR2IE = 1 ; //ENABLE TIMER INTERRUPT
PR2 = 199 ;
}
/***************************************************************************/
void init_ports()
{
TRISB = 0x01;
TRISC = 0XC1;
PIE1 = 0x01; // enable interrupts
OPTION_REG = 0xC0;
INTCON.INTE = 0 ;
}
/***************************************************************************/
void hex_2_BCD(unsigned long int_cnt)
{

unsigned int j=10000,i=0;
int_cnt=int_cnt;
if(int_cnt<10000)
{

USART_Write('S');
USART_Write(' ');
for(i=0;i<5;i++)
{
res =(int_cnt/j)|0x30;
int_cnt=int_cnt%j;
j =j/10;
}//END for

for(i=0;i<5;i++)
{
USART_Write( res);
}
USART_Write(' ');
}//END if
else
{
USART_Write('O');
USART_Write(' ');
for(i=0;i<5;i++)
{
res =(int_cnt/j)|0x30;
int_cnt=int_cnt%j;
j =j/10;
}//END for

for(i=0;i<5;i++)
{
USART_Write(res);
}
USART_Write(' ');
}//END else
}
/***************************************************************************/



send me your comments
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top