Файл: The quintessential PIC microcontroller (S. Katzen, 2000).pdf
ВУЗ: Не указан
Категория: Не указан
Дисциплина: Не указана
Добавлен: 15.06.2025
Просмотров: 5331
Скачиваний: 0
14. Take the Rough with the Smooth 425
Program 14.8 ECG peak picking.
; ***********************************************************
; * |
FUNCTION: |
ISR to |
update the ECG |
parameters |
* |
||
; |
* |
ENTRY |
: |
On a Timer0 interrupt |
* |
||
; |
* |
EXIT |
: |
Update |
MAXIMUM and THRESHOLD:THRESHOLD+1 |
* |
|
;***********************************************************
;First save context
ECG_ISR movwf |
_work |
; |
Put |
away W |
swapf |
STATUS,w |
; |
and |
the Status register |
movwf |
_status |
; ************************************************************
btfss |
INTCON,T0IF |
; |
Was this a Timer0 interrupt? |
|
goto |
ECG_EXIT |
; |
IF not THEN exit |
|
bcf |
INTCON,T0IF |
; ELSE clear flag |
||
movlw |
1 |
; |
Initiate a conversion of |
|
call |
GET_ANALOG |
; |
Channel 1 |
|
movwf |
TEMP |
; |
Save digitized value |
|
subwf |
THRESHOLD,w |
; |
THRESHOLD - ANALOG |
|
btfsc |
STATUS,C |
; |
IF no Borrow THEN |
|
goto |
BELOW |
; |
don’t update MAXIMUM |
|
movf |
TEMP,w |
; |
ELSE get digitized value |
|
movwf |
MAXIMUM |
; |
which is the new MAXIMUM |
|
movwf |
PORTB |
; |
made visible to outside |
|
bsf |
PORTA,5 |
; |
which is signalled |
|
movwf |
THRESHOLD |
; |
Now update double-byte |
|
clrf |
THRESHOLD+1 |
; |
threshold |
|
goto |
ECG_EXIT |
; |
and finish |
|
; Land here if the input is below the threshold |
||||
BELOW |
bcf |
PORTA,5 |
; |
Signal no update |
; Now reduce the threshold by 0.5 unless it is zero |
||||
movf |
THRESHOLD,f |
; |
Is integer threshold zero? |
|
btfsc |
STATUS,Z |
; |
Skip if not |
|
goto |
ECG_EXIT |
; |
IF it is THEN leave alone |
|
movlw |
80h |
; |
0.5 = 10000000b |
|
subwf |
THRESHOLD+1,f ; |
Take away from fraction byte |
||
btfss |
STATUS,C |
; |
Skip if no borrow |
|
decf |
THRESHOLD,f |
; |
ELSE decrement integer thres |
|
; ************************************************************
ECG_EXIT swapf |
_status,w |
; Untwist the original Status reg |
||
movwf |
STATUS |
|||
swapf |
_work,f |
; Get |
the original W reg back |
|
swapf |
_work,w |
; |
leaving STATUS unchanged |
|
retfie |
; |
and |
return from interrupt |
|
426 The Quintessential PIC Microcontroller
Vref |
THRESHOLD |
0V
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 e ectively equates the threshold as MAXIMUM × 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 12 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 µs 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 program begins
14. Take the Rough with the Smooth 427
Program 14.9 An implementation of the ECG peak picker in C.
#int_rtcc ecg_isr()
{
unsigned int analog;
static unsigned long int threshold = 0; static unsigned int maximum;
analog = read_adc();
if(analog > threshold>>8) |
|||
{ |
|||
maximum |
= analog; |
/* New maximum value |
*/ |
PORT_B |
= analog; |
/* Show the outside world |
*/ |
threshold = maximum << 8; |
/* New 2-byte threshold |
*/ |
|
output_bit(PIN_A5,1); |
/* Tell outside world |
*/ |
|
} |
|||
else |
|||
{ |
|||
threshold = threshold - 0x0080; /* Reduce by 0.5 |
*/ |
||
output_bit(PIN_A5,0); |
/* Signal no update |
*/ |
|
} |
|||
} |
|||
its run, as the variable is static. Again this is not the normal behavior of the default auto variable.
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 chapter.
Self-assessment questions
14.1 In Example 14.4 the decay of the threshold level was linear. Although this is fairly e ective 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% (≈ 2561 ) on each sample and determine the time constant in terms of the number of samples.