Take the Rough with the Smooth Part 5 (PIC Microcontroller)

Example 14.4

As part of a smart biomedical monitor, the peak analog value of an electro-cardiogram (ECG) signal is to be determined anew for each cycle. This R-point (see Fig. 7.1) maximum value is to be output from PortB and RA5 is to be pulsed high whenever this value is being updated. Assuming that a PIC16C73/4 is used to implement the intelligence, the the ECG signal (conditioned as shown in Fig. 14.15) connected to Channel 1 RA1, devise a possible strategy. Timer 0 is being used to interrupt the processor at nominally 100 times per second (see Program 13.2) design a suitable ISR to implement your strategy.

Solution

As in any biomedical parameter the ECG signal will vary from cycle to cycle in gain, shape and period. Even if this were not so, imperfections in the data acquisition system, notably the skin electrodes, can cause slow baseline (dc) drift. Thus the threshold at which the signal is to be tracked to its peak R-value must be reset at some sensible fraction of its previous peak during the period following the last update.

One possibility is shown in Fig. 14.16. Here the threshold is slowly decremented after the peak to ensure that a following peak of lower amplitude is not missed. On the basis of a lowest ECG rate of 40 beats per minute (period 1.5 s) if we reduce the threshold by one bit each two samples then the maximum reduction would be a count of 75 at a sample rate of 100 per second. To do this the threshold value THRESHOLD in Program 14.8 is stored as a double-byte number of form integer:fraction and half an integer (i.e. fraction = 10000000b) subtracted in each sample where the peak value MAXIMUM is not updated.


The task list implemented by this listing is:

1. DO a conversion to get ANALOG.

2. Vin must be attenuated by the gain of the system G, where the input range istmpA86_thumb[2][2][2][2]In our case G = 2. Thus using the potential divider relationship:

tmpA87_thumb[2][2][2][2]

After some manipulation we have:

tmpA88_thumb[2][2][2][2]

Program 14.8 ECG peak picking.

Program 14.8 ECG peak picking. ECG detection strategy.

Fig. 14.16 ECG detection strategy.

2. IF (ANALOG > THRESHOLD)

• MAXIMUM = ANALOG.

• THRESHOLD = ANALOG.

• PORTB = ANALOG.

• RA5 = 1.

3. ELSE

• Reduce THRESHOLD by 0.5.

• RA5 = 0.

In updating THRESHOLD where ANALOG > THRESHOLD the integer byte takes the new value of MAXIMUM whilst the fractional byte is zeroed. Treating this byte pair as a 16-bit word, this effectively equates the threshold as MAXIMUM x 256 or THRESHOLD = MAXIMUM << 8, where MAXIMUM has been shifted left eight places. We are assuming that THRESHOLD has been zeroed in the background program during the initialization phase.

If the digitized analog sample is less than the threshold trip value then 80h = 10000000b is subtracted from the lower byte at THRESHOLD+1 and if this produces a borrow, then the upper byte at THRESHOLD is decremented. This subtract \ routine is skipped if the threshold has reached zero thus preventing underflow.

Although not shown, Program 14.8 uses the subroutine GET_ANALOG of Program 14.1 and its associated 12 is delay subroutine.

Program 14.9 gives the C coded version implementing our task list. The #int_rtcc directive tells the compiler to treat the following function as a Real-Time Counter Clock (Timer 0) ISR. In function ecg_isr(), the variables threshold and maximum are declared static. This means that their value will be retained after the function has exited and will be available next time on entry. The default way of treating C function variables is to hold their value only for the duration of the function. An alternative way of dealing with this problem is to declare such variables outside any function, in which case they will be global and retain their value indefinitely.

Threshold is defined as a long int and the CCS compiler will then treat this datum as a 16-bit variable as required. The definition in equating threshold to zero will only initialize it once when the programbegins its run, as the variable is static. Again this is not the normal behavior of the default auto variable.

Program 14.9 An implementation of the ECG peak picker in C.

Program 14.9 An implementation of the ECG peak picker in C.

 

In equating threshold to the new maximum value, the latter is multiplied by 256 by shifting left eight times – a good compiler will automatically change a N*256 to N<<8. This double-byte form allows for the reduction by half a bit 0080h to give the specified falling trip level.

Both implementations assume that the analog module, ports and interrupt mask bits have been set up at the beginning of the background program as described earlier in the topic.

Self-assessment questions

14.1 In Example 14.4 the decay of the threshold level was linear. Although this is fairly effective for situations where the nominal period is known a priori and does not vary greatly, an exponential decay would be better suited where this is not the case. To generate this type of relationship a fixed percentage of the value at each sample point should be subtracted to give the new outcome rather than a constant. Show how you could modify Programs 14.8 and 14.9 to decrement at a rate of approximately 0.4% 256) on each sample and determine the time constant in terms of the number of samples.

14.2 A programmer writing an ISR-based handler for an analog module has replaced the retfie instruction in Program 14.2 by return. What effect will this have?

14.3 Real world analog signals are noisy. In practice this means that some form of filtering or smoothing is frequently required. In any circumstance noise coming in from outside should not have any appreciable frequency components above half the sampling rate since such noise will be frequency shifted back into the baseband as shown in Fig. 14.4. Such low-pass filtering must be applied to the signal before the A/D conversion.

Although this external anti-alias filter must by definition be implemented using hardware circuitry (such as a CR network), noise within the passband can be smoothed out using software filtering routines. One simple approach to digital filtering is to take multiple readings and average them to give a composite outcome. For example, 16 readings summed and shifted right four times (^16) would reduce random noise by a factor of ^16 = 4.

Another approach well known to staticians, is to take a moving average; for example, of a stock price over a month interval. An efficient algorithm of this type is a 3-point average:

tmpA92_thumb[2][2][2][2]

where Sn is the nth sample from the analog module.

Show how you could modify the GET_ANALOG subroutine to remember the last samples from the two previous calls and return the smoothed value.

14.4 It has been suggested that as part of the ECG monitor of Example 14.4 that a MAX506 DAC be used to introduce an automatic gain control function preceding the PIC’s analog input. The aim of the AVC is to keep the peak of the analog input between 4 and g full scale. How might you go about implementing this subsystem? Hint: Remember that each channel of a MAX506 is the product of its digital input and Vref and that the latter can vary between 0 V and VDD.

14.5 How could the time between ECG peaks be measured with a resolution of 10 ms and output at one of the parallel ports as an extension of Example 14.4?

14.6 An input analog sinusoid signal, conditioned as shown in Fig. 14.15, is to be full-wave rectified; that is voltages that were originally negative are to have their sign changed. Design a routine to do this assuming that the input voltage is available at ADRES and the processed output is to be presented via Port B to a DAC.

14.7 Write a C coded program after Program 14.5 to compare two analog voltages at Channel 0 and 1 bringing RA2 high when V0 > V2, RA3 likewise for V0 == V1 and RA4 for V0 < V1. Assume a PIC16C71 device with a clock frequency of 10 MHz. The PIC16C71 can be set to make RA0 and RA1 analog (RA0_RA1_ANALOG) with the rest digital.

14.8 Figure 14.17 is based on Fig. 10 of Microchip’s application note AN546 Using the Analog-to-Digital (A/D) Converter as a means of providing an external voltage reference source for power-sensitive applications. How do you think the circuit works and what factors govern the choice of current limiting resistor?

 A controllable external voltage circuit.

Fig. 14.17 A controllable external voltage circuit.

14.9 Microchip recommend that where possible channle 0 RA0/AN0 should not be used in the PIC16C71 due to possible noise problems. Can you see why this is so and can you think of any way around this problem?

Pinning for the PIC16C71.

Fig. 14.18 Pinning for the PIC16C71.

Next post:

Previous post: