48
Interrupts and Threading Example: Ultrasonic Sensor Electronics Interrupts and Threading Terry Sturtevant Wilfrid Laurier University October 30, 2017 Terry Sturtevant Electronics Interrupts and Threading

Electronics- Interrupts and Threadingdenethor.wlu.ca/pc320/lectures/interruptbeam.pdf · Interrupts allow the event to be detected by built-in hardware, and redirects program control

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Electronics- Interrupts and Threadingdenethor.wlu.ca/pc320/lectures/interruptbeam.pdf · Interrupts allow the event to be detected by built-in hardware, and redirects program control

Interrupts and ThreadingExample: Ultrasonic Sensor

ElectronicsInterrupts and Threading

Terry Sturtevant

Wilfrid Laurier University

October 30, 2017

Terry Sturtevant Electronics Interrupts and Threading

Page 2: Electronics- Interrupts and Threadingdenethor.wlu.ca/pc320/lectures/interruptbeam.pdf · Interrupts allow the event to be detected by built-in hardware, and redirects program control

Interrupts and ThreadingExample: Ultrasonic Sensor

Programming IssuesTips for interrupts

Problems

If you use an ultrasonic distance sensor, what happens if thereis no wall to detect?If you have a security system with break-in sensors, what doesyour code do most of the time?

Useful Technique: Interrupts and multiple threads allow you tocreate programs which don’t waste lots of time waiting forunpredictable events. Events include specific transitions on GPIOpins.

Terry Sturtevant Electronics Interrupts and Threading

Page 3: Electronics- Interrupts and Threadingdenethor.wlu.ca/pc320/lectures/interruptbeam.pdf · Interrupts allow the event to be detected by built-in hardware, and redirects program control

Interrupts and ThreadingExample: Ultrasonic Sensor

Programming IssuesTips for interrupts

Problems

If you use an ultrasonic distance sensor, what happens if thereis no wall to detect?

If you have a security system with break-in sensors, what doesyour code do most of the time?

Useful Technique: Interrupts and multiple threads allow you tocreate programs which don’t waste lots of time waiting forunpredictable events. Events include specific transitions on GPIOpins.

Terry Sturtevant Electronics Interrupts and Threading

Page 4: Electronics- Interrupts and Threadingdenethor.wlu.ca/pc320/lectures/interruptbeam.pdf · Interrupts allow the event to be detected by built-in hardware, and redirects program control

Interrupts and ThreadingExample: Ultrasonic Sensor

Programming IssuesTips for interrupts

Problems

If you use an ultrasonic distance sensor, what happens if thereis no wall to detect?If you have a security system with break-in sensors, what doesyour code do most of the time?

Useful Technique: Interrupts and multiple threads allow you tocreate programs which don’t waste lots of time waiting forunpredictable events. Events include specific transitions on GPIOpins.

Terry Sturtevant Electronics Interrupts and Threading

Page 5: Electronics- Interrupts and Threadingdenethor.wlu.ca/pc320/lectures/interruptbeam.pdf · Interrupts allow the event to be detected by built-in hardware, and redirects program control

Interrupts and ThreadingExample: Ultrasonic Sensor

Programming IssuesTips for interrupts

Problems

If you use an ultrasonic distance sensor, what happens if thereis no wall to detect?If you have a security system with break-in sensors, what doesyour code do most of the time?

Useful Technique: Interrupts and multiple threads allow you tocreate programs which don’t waste lots of time waiting forunpredictable events.

Events include specific transitions on GPIOpins.

Terry Sturtevant Electronics Interrupts and Threading

Page 6: Electronics- Interrupts and Threadingdenethor.wlu.ca/pc320/lectures/interruptbeam.pdf · Interrupts allow the event to be detected by built-in hardware, and redirects program control

Interrupts and ThreadingExample: Ultrasonic Sensor

Programming IssuesTips for interrupts

Problems

If you use an ultrasonic distance sensor, what happens if thereis no wall to detect?If you have a security system with break-in sensors, what doesyour code do most of the time?

Useful Technique: Interrupts and multiple threads allow you tocreate programs which don’t waste lots of time waiting forunpredictable events. Events include specific transitions on GPIOpins.

Terry Sturtevant Electronics Interrupts and Threading

Page 7: Electronics- Interrupts and Threadingdenethor.wlu.ca/pc320/lectures/interruptbeam.pdf · Interrupts allow the event to be detected by built-in hardware, and redirects program control

Interrupts and ThreadingExample: Ultrasonic Sensor

Programming IssuesTips for interrupts

Polling is the process of checking in software for an event; i.e.a transition on an input.

Polling precludes other operations while it is happening.Interrupts allow the event to be detected by built-in hardware,and redirects program control to code which deals with itautomatically.

The main program and the interrupt service routine are calleddifferent threads of execution.

Terry Sturtevant Electronics Interrupts and Threading

Page 8: Electronics- Interrupts and Threadingdenethor.wlu.ca/pc320/lectures/interruptbeam.pdf · Interrupts allow the event to be detected by built-in hardware, and redirects program control

Interrupts and ThreadingExample: Ultrasonic Sensor

Programming IssuesTips for interrupts

Polling is the process of checking in software for an event; i.e.a transition on an input.Polling precludes other operations while it is happening.

Interrupts allow the event to be detected by built-in hardware,and redirects program control to code which deals with itautomatically.

The main program and the interrupt service routine are calleddifferent threads of execution.

Terry Sturtevant Electronics Interrupts and Threading

Page 9: Electronics- Interrupts and Threadingdenethor.wlu.ca/pc320/lectures/interruptbeam.pdf · Interrupts allow the event to be detected by built-in hardware, and redirects program control

Interrupts and ThreadingExample: Ultrasonic Sensor

Programming IssuesTips for interrupts

Polling is the process of checking in software for an event; i.e.a transition on an input.Polling precludes other operations while it is happening.Interrupts allow the event to be detected by built-in hardware,and redirects program control to code which deals with itautomatically.

The main program and the interrupt service routine are calleddifferent threads of execution.

Terry Sturtevant Electronics Interrupts and Threading

Page 10: Electronics- Interrupts and Threadingdenethor.wlu.ca/pc320/lectures/interruptbeam.pdf · Interrupts allow the event to be detected by built-in hardware, and redirects program control

Interrupts and ThreadingExample: Ultrasonic Sensor

Programming IssuesTips for interrupts

Polling is the process of checking in software for an event; i.e.a transition on an input.Polling precludes other operations while it is happening.Interrupts allow the event to be detected by built-in hardware,and redirects program control to code which deals with itautomatically.

The main program and the interrupt service routine are calleddifferent threads of execution.

Terry Sturtevant Electronics Interrupts and Threading

Page 11: Electronics- Interrupts and Threadingdenethor.wlu.ca/pc320/lectures/interruptbeam.pdf · Interrupts allow the event to be detected by built-in hardware, and redirects program control

Interrupts and ThreadingExample: Ultrasonic Sensor

Programming IssuesTips for interrupts

Tips for interrupts

Don’t have waiting in interrupt routines.Create flags to communicate between threads.Make interrupt routines as short as possible; have mostprocessing done in the main thread.

Terry Sturtevant Electronics Interrupts and Threading

Page 12: Electronics- Interrupts and Threadingdenethor.wlu.ca/pc320/lectures/interruptbeam.pdf · Interrupts allow the event to be detected by built-in hardware, and redirects program control

Interrupts and ThreadingExample: Ultrasonic Sensor

Programming IssuesTips for interrupts

Tips for interrupts

Don’t have waiting in interrupt routines.

Create flags to communicate between threads.Make interrupt routines as short as possible; have mostprocessing done in the main thread.

Terry Sturtevant Electronics Interrupts and Threading

Page 13: Electronics- Interrupts and Threadingdenethor.wlu.ca/pc320/lectures/interruptbeam.pdf · Interrupts allow the event to be detected by built-in hardware, and redirects program control

Interrupts and ThreadingExample: Ultrasonic Sensor

Programming IssuesTips for interrupts

Tips for interrupts

Don’t have waiting in interrupt routines.Create flags to communicate between threads.

Make interrupt routines as short as possible; have mostprocessing done in the main thread.

Terry Sturtevant Electronics Interrupts and Threading

Page 14: Electronics- Interrupts and Threadingdenethor.wlu.ca/pc320/lectures/interruptbeam.pdf · Interrupts allow the event to be detected by built-in hardware, and redirects program control

Interrupts and ThreadingExample: Ultrasonic Sensor

Programming IssuesTips for interrupts

Tips for interrupts

Don’t have waiting in interrupt routines.Create flags to communicate between threads.Make interrupt routines as short as possible; have mostprocessing done in the main thread.

Terry Sturtevant Electronics Interrupts and Threading

Page 15: Electronics- Interrupts and Threadingdenethor.wlu.ca/pc320/lectures/interruptbeam.pdf · Interrupts allow the event to be detected by built-in hardware, and redirects program control

Interrupts and ThreadingExample: Ultrasonic Sensor

Sensor InterfacingUsing Interrupts

Example: Ultrasonic Sensor

Here’s an example of using interrupts with the SR-04 ultrasonicdistance sensor.

Terry Sturtevant Electronics Interrupts and Threading

Page 16: Electronics- Interrupts and Threadingdenethor.wlu.ca/pc320/lectures/interruptbeam.pdf · Interrupts allow the event to be detected by built-in hardware, and redirects program control

Interrupts and ThreadingExample: Ultrasonic Sensor

Sensor InterfacingUsing Interrupts

Example: Ultrasonic Sensor

Here’s an example of using interrupts with the SR-04 ultrasonicdistance sensor.

Terry Sturtevant Electronics Interrupts and Threading

Page 17: Electronics- Interrupts and Threadingdenethor.wlu.ca/pc320/lectures/interruptbeam.pdf · Interrupts allow the event to be detected by built-in hardware, and redirects program control

Interrupts and ThreadingExample: Ultrasonic Sensor

Sensor InterfacingUsing Interrupts

Voltage levels

The ultrasonic sensor is a 5V device.A 3.3V trigger pulse from the Pi may work.A 5V echo pulse to the Pi is not OK!It could destroy the GPIO pin.

Optoisolating both signals takes care of both problems.

Terry Sturtevant Electronics Interrupts and Threading

Page 18: Electronics- Interrupts and Threadingdenethor.wlu.ca/pc320/lectures/interruptbeam.pdf · Interrupts allow the event to be detected by built-in hardware, and redirects program control

Interrupts and ThreadingExample: Ultrasonic Sensor

Sensor InterfacingUsing Interrupts

Voltage levels

The ultrasonic sensor is a 5V device.

A 3.3V trigger pulse from the Pi may work.A 5V echo pulse to the Pi is not OK!It could destroy the GPIO pin.

Optoisolating both signals takes care of both problems.

Terry Sturtevant Electronics Interrupts and Threading

Page 19: Electronics- Interrupts and Threadingdenethor.wlu.ca/pc320/lectures/interruptbeam.pdf · Interrupts allow the event to be detected by built-in hardware, and redirects program control

Interrupts and ThreadingExample: Ultrasonic Sensor

Sensor InterfacingUsing Interrupts

Voltage levels

The ultrasonic sensor is a 5V device.A 3.3V trigger pulse from the Pi may work.

A 5V echo pulse to the Pi is not OK!It could destroy the GPIO pin.

Optoisolating both signals takes care of both problems.

Terry Sturtevant Electronics Interrupts and Threading

Page 20: Electronics- Interrupts and Threadingdenethor.wlu.ca/pc320/lectures/interruptbeam.pdf · Interrupts allow the event to be detected by built-in hardware, and redirects program control

Interrupts and ThreadingExample: Ultrasonic Sensor

Sensor InterfacingUsing Interrupts

Voltage levels

The ultrasonic sensor is a 5V device.A 3.3V trigger pulse from the Pi may work.A 5V echo pulse to the Pi is not OK!

It could destroy the GPIO pin.

Optoisolating both signals takes care of both problems.

Terry Sturtevant Electronics Interrupts and Threading

Page 21: Electronics- Interrupts and Threadingdenethor.wlu.ca/pc320/lectures/interruptbeam.pdf · Interrupts allow the event to be detected by built-in hardware, and redirects program control

Interrupts and ThreadingExample: Ultrasonic Sensor

Sensor InterfacingUsing Interrupts

Voltage levels

The ultrasonic sensor is a 5V device.A 3.3V trigger pulse from the Pi may work.A 5V echo pulse to the Pi is not OK!It could destroy the GPIO pin.

Optoisolating both signals takes care of both problems.

Terry Sturtevant Electronics Interrupts and Threading

Page 22: Electronics- Interrupts and Threadingdenethor.wlu.ca/pc320/lectures/interruptbeam.pdf · Interrupts allow the event to be detected by built-in hardware, and redirects program control

Interrupts and ThreadingExample: Ultrasonic Sensor

Sensor InterfacingUsing Interrupts

Voltage levels

The ultrasonic sensor is a 5V device.A 3.3V trigger pulse from the Pi may work.A 5V echo pulse to the Pi is not OK!It could destroy the GPIO pin.

Optoisolating both signals takes care of both problems.

Terry Sturtevant Electronics Interrupts and Threading

Page 23: Electronics- Interrupts and Threadingdenethor.wlu.ca/pc320/lectures/interruptbeam.pdf · Interrupts allow the event to be detected by built-in hardware, and redirects program control

Interrupts and ThreadingExample: Ultrasonic Sensor

Sensor InterfacingUsing Interrupts

"!#

Terry Sturtevant Electronics Interrupts and Threading

Page 24: Electronics- Interrupts and Threadingdenethor.wlu.ca/pc320/lectures/interruptbeam.pdf · Interrupts allow the event to be detected by built-in hardware, and redirects program control

Interrupts and ThreadingExample: Ultrasonic Sensor

Sensor InterfacingUsing Interrupts

"!#

Sensor side; 5V logic pulses

Terry Sturtevant Electronics Interrupts and Threading

Page 25: Electronics- Interrupts and Threadingdenethor.wlu.ca/pc320/lectures/interruptbeam.pdf · Interrupts allow the event to be detected by built-in hardware, and redirects program control

Interrupts and ThreadingExample: Ultrasonic Sensor

Sensor InterfacingUsing Interrupts

����

Terry Sturtevant Electronics Interrupts and Threading

Page 26: Electronics- Interrupts and Threadingdenethor.wlu.ca/pc320/lectures/interruptbeam.pdf · Interrupts allow the event to be detected by built-in hardware, and redirects program control

Interrupts and ThreadingExample: Ultrasonic Sensor

Sensor InterfacingUsing Interrupts

����

Raspberry Pi side; 3.3V pulsesTerry Sturtevant Electronics Interrupts and Threading

Page 27: Electronics- Interrupts and Threadingdenethor.wlu.ca/pc320/lectures/interruptbeam.pdf · Interrupts allow the event to be detected by built-in hardware, and redirects program control

Interrupts and ThreadingExample: Ultrasonic Sensor

Sensor InterfacingUsing Interrupts

Interrupt sample code (1/4)

i m p o r t RPi . GPIO as GPIOi m p o r t d a t e t i m ei m p o r t t ime##s t u f f o m i t t e d h e r e#GPIO . s e t u p ( TRIGGER PIN , GPIO .OUT)GPIO . s e t u p (ECHO PIN , GPIO . IN )##s t u f f o m i t t e d h e r e#

Terry Sturtevant Electronics Interrupts and Threading

Page 28: Electronics- Interrupts and Threadingdenethor.wlu.ca/pc320/lectures/interruptbeam.pdf · Interrupts allow the event to be detected by built-in hardware, and redirects program control

Interrupts and ThreadingExample: Ultrasonic Sensor

Sensor InterfacingUsing Interrupts

Interrupt sample code (1/4)

i m p o r t RPi . GPIO as GPIOi m p o r t d a t e t i m ei m p o r t t ime##s t u f f o m i t t e d h e r e#GPIO . s e t u p ( TRIGGER PIN , GPIO .OUT)GPIO . s e t u p (ECHO PIN , GPIO . IN )##s t u f f o m i t t e d h e r e#

Terry Sturtevant Electronics Interrupts and Threading

Page 29: Electronics- Interrupts and Threadingdenethor.wlu.ca/pc320/lectures/interruptbeam.pdf · Interrupts allow the event to be detected by built-in hardware, and redirects program control

Interrupts and ThreadingExample: Ultrasonic Sensor

Sensor InterfacingUsing Interrupts

Interrupt sample code (2/4)

##s t u f f o m i t t e d h e r e#d e f U l t r a s o n i c S e n d P u l s e ( ) :

GPIO . output ( TRIGGER PIN , GPIO . HIGH)t ime . s l e e p ( PULSE TIME )GPIO . output ( TRIGGER PIN , GPIO .LOW)g l o b a l t r i g g e r t i m et r i g g e r t i m e=d a t e t i m e . d a t e t i m e . now ( )

Terry Sturtevant Electronics Interrupts and Threading

Page 30: Electronics- Interrupts and Threadingdenethor.wlu.ca/pc320/lectures/interruptbeam.pdf · Interrupts allow the event to be detected by built-in hardware, and redirects program control

Interrupts and ThreadingExample: Ultrasonic Sensor

Sensor InterfacingUsing Interrupts

Interrupt sample code (2/4)

##s t u f f o m i t t e d h e r e#d e f U l t r a s o n i c S e n d P u l s e ( ) :

GPIO . output ( TRIGGER PIN , GPIO . HIGH)t ime . s l e e p ( PULSE TIME )GPIO . output ( TRIGGER PIN , GPIO .LOW)g l o b a l t r i g g e r t i m et r i g g e r t i m e=d a t e t i m e . d a t e t i m e . now ( )

Terry Sturtevant Electronics Interrupts and Threading

Page 31: Electronics- Interrupts and Threadingdenethor.wlu.ca/pc320/lectures/interruptbeam.pdf · Interrupts allow the event to be detected by built-in hardware, and redirects program control

Interrupts and ThreadingExample: Ultrasonic Sensor

Sensor InterfacingUsing Interrupts

Interrupt sample code (3/4)

d e f U l t r a s o n i c P u l s e R e c e i v e d ( c h a n n e l ) :e c h o t i m e=d a t e t i m e . d a t e t i m e . now ( )d e l t a=echo t ime −t r i g g e r t i m ef l i g h t t i m e=d e l t a . t o t a l s e c o n d s ( )p r i n t ” R i s i n g edge d e t e c t e d on ECHO PIN . ”p r i n t f l i g h t t i m e ∗ speed

Terry Sturtevant Electronics Interrupts and Threading

Page 32: Electronics- Interrupts and Threadingdenethor.wlu.ca/pc320/lectures/interruptbeam.pdf · Interrupts allow the event to be detected by built-in hardware, and redirects program control

Interrupts and ThreadingExample: Ultrasonic Sensor

Sensor InterfacingUsing Interrupts

Interrupt sample code (3/4)

d e f U l t r a s o n i c P u l s e R e c e i v e d ( c h a n n e l ) :e c h o t i m e=d a t e t i m e . d a t e t i m e . now ( )d e l t a=echo t ime −t r i g g e r t i m ef l i g h t t i m e=d e l t a . t o t a l s e c o n d s ( )p r i n t ” R i s i n g edge d e t e c t e d on ECHO PIN . ”p r i n t f l i g h t t i m e ∗ speed

Terry Sturtevant Electronics Interrupts and Threading

Page 33: Electronics- Interrupts and Threadingdenethor.wlu.ca/pc320/lectures/interruptbeam.pdf · Interrupts allow the event to be detected by built-in hardware, and redirects program control

Interrupts and ThreadingExample: Ultrasonic Sensor

Sensor InterfacingUsing Interrupts

Interrupt sample code (4/4)

GPIO . a d d e v e n t d e t e c t (ECHO PIN , GPIO . FALLING ,c a l l b a c k=U l t r a s o n i c P u l s e R e c e i v e d )

t r y :w h i l e True :

U l t r a s o n i c S e n d P u l s e ( )p r i n t ” Wait ing . . . ”t ime . s l e e p ( 5 )p r i n t ”Done w a i t i n g . . . ”

e x c e p t K e y b o a r d I n t e r r u p t :GPIO . c l e a n u p ( ) # CTRL+C e x i t

Terry Sturtevant Electronics Interrupts and Threading

Page 34: Electronics- Interrupts and Threadingdenethor.wlu.ca/pc320/lectures/interruptbeam.pdf · Interrupts allow the event to be detected by built-in hardware, and redirects program control

Interrupts and ThreadingExample: Ultrasonic Sensor

Sensor InterfacingUsing Interrupts

Interrupt sample code (4/4)

GPIO . a d d e v e n t d e t e c t (ECHO PIN , GPIO . FALLING ,c a l l b a c k=U l t r a s o n i c P u l s e R e c e i v e d )

t r y :w h i l e True :

U l t r a s o n i c S e n d P u l s e ( )p r i n t ” Wait ing . . . ”t ime . s l e e p ( 5 )p r i n t ”Done w a i t i n g . . . ”

e x c e p t K e y b o a r d I n t e r r u p t :GPIO . c l e a n u p ( ) # CTRL+C e x i t

Terry Sturtevant Electronics Interrupts and Threading

Page 35: Electronics- Interrupts and Threadingdenethor.wlu.ca/pc320/lectures/interruptbeam.pdf · Interrupts allow the event to be detected by built-in hardware, and redirects program control

Interrupts and ThreadingExample: Ultrasonic Sensor

Sensor InterfacingUsing Interrupts

Interrupt Usage

GPIO.add event detect()set up interruptECHO PINpin to monitorGPIO.FALLINGtransition (i.e. “event”) to watch forcallback=Ultrasonic Pulse Receivedfunction to execute when event happens

Terry Sturtevant Electronics Interrupts and Threading

Page 36: Electronics- Interrupts and Threadingdenethor.wlu.ca/pc320/lectures/interruptbeam.pdf · Interrupts allow the event to be detected by built-in hardware, and redirects program control

Interrupts and ThreadingExample: Ultrasonic Sensor

Sensor InterfacingUsing Interrupts

Interrupt Usage

GPIO.add event detect()set up interrupt

ECHO PINpin to monitorGPIO.FALLINGtransition (i.e. “event”) to watch forcallback=Ultrasonic Pulse Receivedfunction to execute when event happens

Terry Sturtevant Electronics Interrupts and Threading

Page 37: Electronics- Interrupts and Threadingdenethor.wlu.ca/pc320/lectures/interruptbeam.pdf · Interrupts allow the event to be detected by built-in hardware, and redirects program control

Interrupts and ThreadingExample: Ultrasonic Sensor

Sensor InterfacingUsing Interrupts

Interrupt Usage

GPIO.add event detect()set up interruptECHO PINpin to monitor

GPIO.FALLINGtransition (i.e. “event”) to watch forcallback=Ultrasonic Pulse Receivedfunction to execute when event happens

Terry Sturtevant Electronics Interrupts and Threading

Page 38: Electronics- Interrupts and Threadingdenethor.wlu.ca/pc320/lectures/interruptbeam.pdf · Interrupts allow the event to be detected by built-in hardware, and redirects program control

Interrupts and ThreadingExample: Ultrasonic Sensor

Sensor InterfacingUsing Interrupts

Interrupt Usage

GPIO.add event detect()set up interruptECHO PINpin to monitorGPIO.FALLINGtransition (i.e. “event”) to watch for

callback=Ultrasonic Pulse Receivedfunction to execute when event happens

Terry Sturtevant Electronics Interrupts and Threading

Page 39: Electronics- Interrupts and Threadingdenethor.wlu.ca/pc320/lectures/interruptbeam.pdf · Interrupts allow the event to be detected by built-in hardware, and redirects program control

Interrupts and ThreadingExample: Ultrasonic Sensor

Sensor InterfacingUsing Interrupts

Interrupt Usage

GPIO.add event detect()set up interruptECHO PINpin to monitorGPIO.FALLINGtransition (i.e. “event”) to watch forcallback=Ultrasonic Pulse Receivedfunction to execute when event happens

Terry Sturtevant Electronics Interrupts and Threading

Page 40: Electronics- Interrupts and Threadingdenethor.wlu.ca/pc320/lectures/interruptbeam.pdf · Interrupts allow the event to be detected by built-in hardware, and redirects program control

Interrupts and ThreadingExample: Ultrasonic Sensor

Sensor InterfacingUsing Interrupts

Interrupt sample code

w h i l e True :U l t r a s o n i c S e n d P u l s e ( )p r i n t ” Wait ing . . . ”t ime . s l e e p ( 5 )p r i n t ”Done w a i t i n g . . . ”

Note the sleep instruction after the pulse is sent.

Terry Sturtevant Electronics Interrupts and Threading

Page 41: Electronics- Interrupts and Threadingdenethor.wlu.ca/pc320/lectures/interruptbeam.pdf · Interrupts allow the event to be detected by built-in hardware, and redirects program control

Interrupts and ThreadingExample: Ultrasonic Sensor

Sensor InterfacingUsing Interrupts

Interrupt sample code

w h i l e True :U l t r a s o n i c S e n d P u l s e ( )p r i n t ” Wait ing . . . ”t ime . s l e e p ( 5 )p r i n t ”Done w a i t i n g . . . ”

Note the sleep instruction after the pulse is sent.

Terry Sturtevant Electronics Interrupts and Threading

Page 42: Electronics- Interrupts and Threadingdenethor.wlu.ca/pc320/lectures/interruptbeam.pdf · Interrupts allow the event to be detected by built-in hardware, and redirects program control

Interrupts and ThreadingExample: Ultrasonic Sensor

Sensor InterfacingUsing Interrupts

Interrupt sample code

w h i l e True :U l t r a s o n i c S e n d P u l s e ( )p r i n t ” Wait ing . . . ”t ime . s l e e p ( 5 )p r i n t ”Done w a i t i n g . . . ”

Note the sleep instruction after the pulse is sent.

Terry Sturtevant Electronics Interrupts and Threading

Page 43: Electronics- Interrupts and Threadingdenethor.wlu.ca/pc320/lectures/interruptbeam.pdf · Interrupts allow the event to be detected by built-in hardware, and redirects program control

Interrupts and ThreadingExample: Ultrasonic Sensor

Sensor InterfacingUsing Interrupts

Transitions detected during sleep.

Terry Sturtevant Electronics Interrupts and Threading

Page 44: Electronics- Interrupts and Threadingdenethor.wlu.ca/pc320/lectures/interruptbeam.pdf · Interrupts allow the event to be detected by built-in hardware, and redirects program control

Interrupts and ThreadingExample: Ultrasonic Sensor

Sensor InterfacingUsing Interrupts

Possible Improvements

There are many ways this code can be improved:The time for the trigger pulse hasn’t been optimizedThe time between trigger pulses hasn’t been optimized

No doubt there are many other possibilities as well.

Terry Sturtevant Electronics Interrupts and Threading

Page 45: Electronics- Interrupts and Threadingdenethor.wlu.ca/pc320/lectures/interruptbeam.pdf · Interrupts allow the event to be detected by built-in hardware, and redirects program control

Interrupts and ThreadingExample: Ultrasonic Sensor

Sensor InterfacingUsing Interrupts

Possible Improvements

There are many ways this code can be improved:

The time for the trigger pulse hasn’t been optimizedThe time between trigger pulses hasn’t been optimized

No doubt there are many other possibilities as well.

Terry Sturtevant Electronics Interrupts and Threading

Page 46: Electronics- Interrupts and Threadingdenethor.wlu.ca/pc320/lectures/interruptbeam.pdf · Interrupts allow the event to be detected by built-in hardware, and redirects program control

Interrupts and ThreadingExample: Ultrasonic Sensor

Sensor InterfacingUsing Interrupts

Possible Improvements

There are many ways this code can be improved:The time for the trigger pulse hasn’t been optimized

The time between trigger pulses hasn’t been optimized

No doubt there are many other possibilities as well.

Terry Sturtevant Electronics Interrupts and Threading

Page 47: Electronics- Interrupts and Threadingdenethor.wlu.ca/pc320/lectures/interruptbeam.pdf · Interrupts allow the event to be detected by built-in hardware, and redirects program control

Interrupts and ThreadingExample: Ultrasonic Sensor

Sensor InterfacingUsing Interrupts

Possible Improvements

There are many ways this code can be improved:The time for the trigger pulse hasn’t been optimizedThe time between trigger pulses hasn’t been optimized

No doubt there are many other possibilities as well.

Terry Sturtevant Electronics Interrupts and Threading

Page 48: Electronics- Interrupts and Threadingdenethor.wlu.ca/pc320/lectures/interruptbeam.pdf · Interrupts allow the event to be detected by built-in hardware, and redirects program control

Interrupts and ThreadingExample: Ultrasonic Sensor

Sensor InterfacingUsing Interrupts

Possible Improvements

There are many ways this code can be improved:The time for the trigger pulse hasn’t been optimizedThe time between trigger pulses hasn’t been optimized

No doubt there are many other possibilities as well.

Terry Sturtevant Electronics Interrupts and Threading