![]() | ![]() | ![]() |
| | |||||||
| Micro Controllers Discuss all aspects of micro controllers - building them, coding them, etc. All controllers are welcome - PIC, BASIC, Z8 Encore!, etc. |
| | Thread Tools | Display Modes |
| | (permalink) |
| 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 by jess; 3rd February 2007 at 02:05 PM. |
| | |
| | (permalink) |
| New Member | Please see: Edit 1 |
| | |
| | (permalink) |
| Experienced Member | 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 |
| | |
| | (permalink) |
| Experienced Member | 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.
__________________ I can still log in! Delete my account and all my post now. |
| | |
| | (permalink) |
| New Member | 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 |
| | |
| | (permalink) |
| New Member | 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 |
| | |
| | (permalink) | |
| Experienced Member | Quote:
| |
| | |
| | (permalink) |
| Experienced Member | 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 by Mike, K8LH; 3rd February 2007 at 10:30 PM. |
| | |
| | (permalink) |
| New Member | 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 |
| | |
| | (permalink) |
| New Member | 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 by jess; 3rd February 2007 at 11:26 PM. |
| | |
| | (permalink) |
| Experienced Member | 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 |
| | |
| | (permalink) | |
| Super Moderator | Quote:
Interesting you mention 'ground effect', I know you get it with helicopters, how does it work with an airplane?. | |
| | |
| | (permalink) |
| Experienced Member | 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. |
| | |
| | (permalink) |
| New Member | 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: http://www.silcom.com/~pelican2/Pico...BOUT_DOPP.html 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 |
| | |
| | (permalink) |
| New Member | 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 |
| | |
| Bookmarks |
| Thread Tools | |
| Display Modes | |
| |
| | ||||
| Thread | Thread Starter | Forum | Replies | Latest |
| small problem with tmr1 16f87 | zkt_PiratesDen | Micro Controllers | 7 | 3rd July 2006 05:55 PM |
| Measuring Pulse width modulation PWM | walters | General Electronics Chat | 44 | 12th June 2005 08:37 PM |
| detecting pulse width using PIC | mr. mister | Micro Controllers | 4 | 11th April 2005 01:16 PM |
| measure pulse width with a counter...other ways?? | xmat | Electronic Projects Design/Ideas/Reviews | 7 | 6th April 2005 08:05 AM |
| Pulse Width Calculation | waqar | Micro Controllers | 1 | 25th June 2003 11:53 AM |