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.

Problem measuring pulse width - 12F683 TMR1, 8MHz

Status
Not open for further replies.

jess

New Member
Edit 1: I'm starting to wonder if there is a fast transition to zero which terminates TMR1 but is too fast for me to see on the scope at 10 ms/cm. I'm going to remove the external pulse source and insert a software delay loop to test the timer independently of the external pulse source.

I have also inserted some lines of ******* in the code to mark where the actual timing begins (with comments). There is also a 10 micro sec trigger pulse to the external device which then generates the longer 40 ms pulse I'm trying to measure.

End Edit 1:

I'm trying to measure the width of a positive going pulse. My scope shows the pulse width is about 40 milliseconds. TMR1 measurements on a 12F683 with an 8MHz oscillator (0.5 us clock time) show a pulse width of h'417E' = 16766 x 0.5 = 8383us or about 8.4 milliseconds.

I have tried just turning the timer on (bsf) and off (bcf) TMR1ON. The code below uses the TMR1 gate input T1G. Both methods give the same results.

The pulse is applied to Pin 3, GPIO,T1GATE. The code waits for GPIO,T1GATE to go Hi and sets Pin 5, GP2 Hi. The timer is then started. The reverse process occurs at the end of the pulse. I used GP2, Pin 5 just to confirm that the pulse was getting into the PIC correctly by looking at GPIO,T1GATE and Pin 5 simultaneously with a 2 channel scope. Both waveforms were identical.

Where am I going wrong? All help would be appreciated.

Thanks, Jess

Code:
;12F683 excerpt - Measure Pulse Width

MAIN	movlw	b'00010111'	; Set all Analog(GP0, GP1, GP2, GP4) 
							; as digital I/O					 
		movwf	CMCON0		; digital I/O			 			 
		bsf		STATUS,RP0	; bank 1			 				 
		clrf	ANSEL		; digital I/O			 			 
		movlw	b'00011010'	; GP1, GP3, GP4 as Digital inputs	 
		movwf	TRISIO		; setup TRISIO as desired	 		 
;
;  setup 8-MHz INTOSC
;
		movlw	b'01110000'	;				 					 
		movwf	OSCCON		; 8-MHz INTOSC system clock	 		 
Stable	btfss	OSCCON,HTS	; oscillator stable?		 		 
		goto	Stable		; no, branch			 			 
		bcf		STATUS,RP0	; bank 0			 				 
;
;  initialize RS-232 Serial I/O
;
		call	Init232			

		bsf		INTCON,GIE	; turn on interrupts for RS232 		 

		; Clear Screen and Show Title
				
		_Title	"Measure Pulse Width\r\n\n"
		
		bcf		PIR1,TMR1IF	; clear TMR1 overflow flag
		
		bsf		STATUS,RP0	; bank 1
		movf	OSCCON,W
		bcf		STATUS,RP0	; bank 0
		call 	PutByte		; Print OSCCON value		
		_Print	" OSCCON value\r\n\n"
		movf	PIR1,W
		call 	PutByte		; Print PIR1 value
		_Print	" PIR1 value\r\n\n"

		
MT		_Print	"\n MT - Press a key to send a Ping: \r\n"		
		call	Get232
		_Print	"\nConfirm Key Pressed\r\n\n"

; ************* Measure Pulse Width ************
;		
;  turn off RS232 interrupts
;		
		bcf		INTCON,GIE	; turn off interrupts		 		 
;
;  setup TMR1	Turn Off Timer1
;
		clrf	CMCON1		;									 
		bsf		CMCON1,T1GSS; Enable T1G gate input				 
		movlw	b'11000100'	; Set up TMR1 for T1G input			 
		movwf	T1CON		; 			  TMR1 Off				 
        ;bcf		T1CON,TMR1ON; TMR1 Off		 				 
        bcf		PIR1,TMR1IF	; clear TMR1 overflow flag			 
		clrf	TMR1L		; Clear Lo counter register			 
		clrf	TMR1H		; Clear Hi counter register			 
;*********************************************************
;*********************************************************
;  		Trigger Hi duration time >= 10uS
;		
		bsf		GPIO,TRIGOUT; Generate Trigger
		call	Dly10		; 10 uS
		bcf		GPIO,TRIGOUT
;The above 3 lines create a trigger pulse to another device which then generates the 
;40 ms pulse.
;
;*********************************************************
;*********************************************************
;
EchoWt	btfss	GPIO,T1GATE	; wait for PULSE Hi
		goto	EchoWt		; wait til Hi
;********************************************************		
; PULSE STARTS HERE - MEASURE Pulse duration
;********************************************************
		bsf		T1CON,TMR1ON; TMR1 ON
		bsf		GPIO,2		; Pin 5 Hi when PULSE Hi
;
; wait for Hi to Lo transition
;		
EW2		btfsc	GPIO,T1GATE	; wait for PULSE Lo
		goto	EW2			; wait til Lo
		bcf		T1CON,TMR1ON; stop TMR1
;********************************************************
; PULSE STOPS HERE
;********************************************************		
		bcf		GPIO,2		; Pin 5 Lo when PULSE Lo
;		
; Enable interrupts
;		
		bsf		INTCON,GIE	; turn on RS232 interrupts
;
		movf	PIR1,W
		call 	PutByte		; Print PIR1 value
		_Print	" Overflow if PIR1 is odd value\r\n\n"
		
		bsf		STATUS,RP0	; bank 1
		movf	OSCCON,W
		bcf		STATUS,RP0	; bank 0
		call 	PutByte		; Print OSCCON value		
		_Print	" OSCCON value\r\n\n"
		
		movf	TMR1H,W		;				 					 
		movwf	WH
		call	PutByte		;				 					 
		
		movf	TMR1L,W		;				 					 
		movwf	WL
		call	PutByte		;				 					 
		
		_Print  " Hex 500 nS periods\r\n\n"
				
;  divide count by two to get microSec (8 MHz clock)
		
		bcf		_C			; clear carry
		RRF		WH,f		; rotate Hi right, bit 0 into C
		RRF		WL,f		; rotate Lo right, C into bit7,
		bcf		_C			; discard Lo carry bit
			
		movf	WH,W		; counter high byte
		call	PutByte
		movf	WL,W		; counter low byte
		call	PutByte		
		
		_Print " Pulse Width , Hex Microseconds\r\n\n"
		
		goto MT

		end
 
Last edited:
Hi Jess,

I suspect the 16-bit Timer 1 registers are rolling over at 32.768-msecs and you're seeing the balance (40-msecs minus 32.768-msecs). So, you need to check for Timer 1 overflow periodically and supplement the 16-bit Timer 1 registers with an additional bit to measure a pulse up to 65-msecs. Make sense?

Mike
 
I would give the best solution to the problem but Mike has already done the deed with the suggestion of expanding to 17-bits, 17th bit assigned to another variable with each overflow of Timer1.

Second best. Is 8MHz necessary? A change in frequency would suffice yet requires alteration of delays and such.
 
Thanks Mike - I totally missed the 8 msec difference possibility. I had intended on listing the numbers that were printed. They are:

h'0B' = PIR1 (bit 0 = TMR1IF interrupt or overflow flag), 0B is even so TMR1IF = 0
h'7C' = OSCCON
h'417E' = 500ns count
h'20BF' = 1/2 500ns count

So, if TMR1IF is zero, then apparently no overflow ocurred.

What am I missing?

Jess
 
Hi donniedj, I'm using Mike's 9600 baud RS232 to communicate with the PC and it uses an 8 MHz clock.

I suppose I could switch clock speeds but I also want to understand the timer stuff.

I thought I was checking for an overflow and getting none. If that's true then a USER, timer or hardware error is the problem. See my post after yours.

Thanks for your comments.

Jess
 
jess said:
Thanks Mike - I totally missed the 8 msec difference possibility. I had intended on listing the numbers that were printed. They are:

h'0B' = PIR1 (bit 0 = TMR1IF interrupt or overflow flag), 0B is even so TMR1IF = 0
h'7C' = OSCCON
h'417E' = 500ns count
h'20BF' = 1/2 500ns count

So, if TMR1IF is zero, then apparently no overflow ocurred.

What am I missing?

Jess
Actually, 0Bh = 11 (decimal) = 00001011 (binary) so bit 0 (TMR1IF) is set and indicates that "rollover" condition we were talking about.
 
You might let us know what kind of ranges and echo times you're expecting from the sonar sensor. That will help us determine if 17 bits of timer data are enough.

BTW, I think your use of the Timer 1 "gate" is very intuitive and insightful.

May I impose to ask what is the formula used to determine distance from the time measurement? Can you use that formula to determine distance resolution when using an 8-MHz clock (Tcy = 500-nsec) or a 20-MHz clock (Tcy = 200-nsec)? And is distance resolution an important factor in your design?

Mike
 
Last edited:
Dang, if only I could count! Had the answer in front of me all the time. Next time I'll have the computer check to see if its odd or even.

Thanks for your patience Mike.

Jess
 
For what I'm doing Mike, resolution is not particularly important - a 4 MHz clock would be quite adequate. The transducers in the SRF04 are 40KHz. Sound travels about 1100 feet/sec or about 1.1 ft/msec but varies with temperature and perhaps other ambient variables. If you wanted better accuracy you could use two devices a known distance apart and have them time a ping from one device to another and calibrate the distance/msec to be used in ranging. Also, remember the transducers are electromechanical. They "ring up and ring down", are slow to start and continue to vibrate and after the drive has been removed. So, when did the Ping start and end, exactly?

For my purposes now, I'm rounding off to 1 foot/msec. Is the car in the garage? Is the garage door open or closed? Is your robot going to hit the wall? I'm just playing and learning more about PICs, and counting.

Anyone interested in higher resolution might want to go to a higher ultrasonic frequency. The SRF04 ($25) I'm playing with claims a distance range of 3 inches to 10 feet. Other devices, SRF05, SRF08, SRF10 are more sophisticated but cost more. I think there are some which use transducer frequencies of several hundred KHz to perhaps MHz. Also, if you really want to refine resolution you may need to replace the PIC in the SRF04, its a 12F509, so you can use a higher frequency device and know what kind of resolution limitations the software has.

Anyone interested can Google SRF04 and a number of hits will pop up probably related to hobby robotics. The circuits are interesting too. The SRF04 uses a RS232 chip to drive the ultrasonic transducer push - pull fashion for higher output.

Remember the distance traveled by the sound (echo) is twice the distance to the object.

Jess
 
Last edited:
Sorry to pester you. It's just an interesting subject. I've been wondering how that highway speed sign measures my speed as I'm approaching it. I'm also a Private Pilot (as is my lovely wife) and I always wondered if I could put together something that would give a nice little LED indicator when coming into ground effect and time to flare while landing.

Take care. Regards, Mike
 
Mike said:
Sorry to pester you. It's just an interesting subject. I've been wondering how that highway speed sign measures my speed as I'm approaching it. I'm also a Private Pilot (as is my lovely wife) and I always wondered if I could put together something that would give a nice little LED indicator when coming into ground effect and time to flare while landing.

They usually use doppler shift radar - using a microwave radio beam.

Interesting you mention 'ground effect', I know you get it with helicopters, how does it work with an airplane?.
 
The reduction in drag when you're in ground effect provides very smooth floating sensation. It's very useful on short field take offs or when taking off from a grass strip as you can get the plane off the ground and in ground effect before the normal takeoff speed then speed up more rapidly while in that reduced drag ground effect zone.
 
Mike, I don't consider interest as being pestered. Glad I have something to share.

As Nigel indicated its some sort of radar. Radar and sonar are similar conceptually as I'm sure you know. The speed of signal travel is vastly different (RF versus sound). With radar (speed of light transmission) some sort of phase comparison between incident and reflected signals is used to extract distance and speed (velocity) information. As with the train whistle example of doppler, the change of tone or frequency relates to speed.

An excelent discussion of doppler, as related to ham radio "fox hunting" (direction finding, not animals), can be found at:
**broken link removed**
Be sure to check out the link More About Doppler at the bottom of the page.

Ramsey Electronics used to carry some microwave "radar" kits, rather crude, which were supposed to be able to measure speed of vehicles.

I'm not sure how any of this might relate to "ground effect" since I'm not familiar with it.

Jess
 
Ground effect as you describe it brings to mind a build up of an air cushion or pressure under the wings. Is it a pressure effect? If so should be measurable.

Jess
 
Sorry we got a little off subject Jess.

BTW, my MOS (military occupational specialty) in the Army long ago was WEAPON Support RADAR Repair (an electronic technician with some specialized RADAR electronics training). But I ended spending most of my tour in Germany as the Chief Operator and Custodian of MARS Radio Station AE1MAN (DL5MA) near Stuttgart.

Later, Mike
 
jess said:
Ground effect as you describe it brings to mind a build up of an air cushion or pressure under the wings. Is it a pressure effect? If so should be measurable.

With a helicopter it's the effect where the downwards air from the rotor actually pushes against the ground - this helps the helicopter leave the ground more easily. Once you get to about the diameter of the rotors off the ground it then starts to fly properly, and you need a lot more power to get higher.
 
Nigel Goodwin said:
With a helicopter it's the effect where the downwards air from the rotor actually pushes against the ground - this helps the helicopter leave the ground more easily. Once you get to about the diameter of the rotors off the ground it then starts to fly properly, and you need a lot more power to get higher.

Nigel, do you fly a helicopter?

With a fixed wing, the downdraft from the wings generates a high pressure bubble behind the plane which "pushes" it along. The net result is an apparent reduction in drag and an aircraft that can glide further.

It does make me wonder if a pressure differential could be detected between the front and back when in ground effect. I don't think a second static vent and a mechanical system would be sensitive enough but digital pressure sensors might be worth a try.

Mike.
 
Pommie said:
Nigel, do you fly a helicopter?

No, but one landed a number of times on the football pitch just across from the workshop (which is upstairs) - and once when he took off he barely cleared the workshop roof!. Next time he came I spoke to him about it, he explained about ground effect and said the reason for the previous 'exciting' take off was that the turbo was slow cutting in - and until it did he didn't have enough power to get higher.

He was an interesting guy, he was a professional pilot, and actually bringing a man for a series of meetings. He explained that once he had to make an emergency landing in a field near a road - and a policeman on a pushbike saw him come down. The policeman came across and gave him a ticket ordering him to produce his documents at the police station - just as they would with a car!. Despite the stupidness of this, he had to comply - or risk arrest, so he loaded all his helicopter documents in a large wheelbarrow and took them to the police station! - there are a LOT of documents associated with a helicopter!. The police station were NOT pleased, it caused them a lot of totally needless work - and the PC concerned got reprimanded! :p
 
Status
Not open for further replies.

Latest threads

Back
Top