Course materials · Microprocessor Applications

GPIO

English translation

Learning objectives

Use timestamped records to examine multiple signals from a single button press, and define input handling rules.

Execution conditions

Use the recorded waveform for the baseline exercise. For the hardware extension, compare the BCM numbers and input/output settings in the original RPi.GPIO code with the board you use.

GPIO Edge Detection

Starting conditions

(t_ms,level)=[(0,0),(10,1),(12,0),(14,1),(50,0),(80,1)]. Between records, the preceding level is maintained.

Procedure

  1. Reconstruct 0~100 ms at 1 ms intervals and draw a step plot.
  2. Find the times of the rising and falling edges in the original record.
  3. Record the cumulative count with only rising edges counted by the counter.

Results to retain

import numpy as np
import matplotlib.pyplot as plt
events=[(0,0),(10,1),(12,0),(14,1),(50,0),(80,1)]
rising=[t for (t,x),(_,previous) in zip(events[1:],events[:-1]) if previous==0 and x==1]
print('raw rising',rising)
# TODO: Record the waveform, falling edges, and cumulative Counter.

Record results and self-check

t [ms]:

level:

Edge type:

Cumulative rising count:

Points to check

Effects of polling intervals

Starting conditions

Read the same waveform at Δt∈{1,10,20}\Delta t\in\{1,10,20\} ms intervals. Keep the original edge record unchanged.

Procedure

  1. At each polling time, read the level of the preceding event.
  2. Recount rising edges in the sampled data and compare with the original record.
  3. Identify the actual intervals of the short pulses that disappeared.

Results to retain

def sample_level(t):
    return next(level for ts,level in reversed(events) if ts<=t)
for dt in [1,10,20]:
    times=np.arange(0,101,dt);levels=[sample_level(t) for t in times]
    print(dt,len(times))
# TODO: Count transitions in the samples and compare with the original record.

Record results and self-check

Polling interval [ms]:

Number of samples:

Detected rising count:

Missed intervals:

Points to check

Filter contact bounce

Starting conditions

Set the minimum accepted interval between rising edges to 20 ms. Evaluate each edge relative to the last accepted edge.

Procedure

  1. From the raw rising list, ignore subsequent edges less than 20 ms apart.
  2. Change the limit to 5,20,50 ms and compare the number of accepted edges.
  3. Read the GPIO.setmode and time.sleep arguments in Week6-1_GPIO_LED_Test.ipynb, and explain how its pin numbering scheme and time units relate to this activity's records. Complete this by reading the code only.

Results to retain

def debounce(times,min_gap):
    accepted=[]
    for t in times:
        if not accepted or t-accepted[-1]>=min_gap:accepted.append(t)
    return accepted
print(debounce(rising,20))
# TODO: Compare 5, 20, and 50 ms.

Record results and self-check

Minimum interval [ms]:

raw count:

Accepted count:

Accepted times [ms]:

Points to check