Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

How to interface BP Transducer to PIC18F?

Status
Not open for further replies.
I ported Microchip AN1556 code to mikroC. This is a piece of my core An1556 code and I am getting errors related to Heap Memory in Proteus.

How to fix the errors related to memory?

Lines 198, 199, 200 are causing the issue. If these 3 lines are commented then I don't get any issue related to memory in Proteus.

I see that NewMovingAverage() function is not called in the code to initialize the heap. Where should I call that function?


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#define HardwareVer "B.26"
#define pressure_offset     25
 
#define Algorithm_2             //Blood pressure calculation algorithm 2 using MAP's pulse amplitude.
 
#define ADC_Sample_Rate 250     //500
 
#define BPM                 0
#define PreviousResult      1
#define CLOCK               2
#define FirmwareVersion     3
#define WiFi                4
#define MAX_STATE           5
 
#define menu_Start          0
#define menu_PumpAir        1
#define menu_Measuring      2
#define menu_ReleaseAir     3
#define menu_Result         4
 
#define Valve_OpenClose LATD7
#define open    0             //valve open
#define close   1             //valve close
#define fast    1             //pump speed fast
#define slow    2             //pump speed slow
#define Gain_CP        0.07          //pressure_reading = ADC1_temp * (3.3/4096) * 1000 * 37 / 31 / 101 * 7.5 ~= ADC1_temp * 0.07
#define pressure_buf_length          64
#define overPressure_limit          200     //in mmHg
#define MAP_Sample_Window_Interval    2     //2
#define SpeedChange_Threshold        35     //45
#define RR_Interval_Number            3     //capture 4 valid pulses that is 3 RR intervals to calculate heart rate
 
#define cmd_reboot      0
#define cmd_CMD         1
#define cmd_set         2
 
unsigned char buf[128];
 
#define CMD_HDR buf[0]
 
//Ryan Bartling filter
#define FIR_SIZE   32     //32-point moving average
#define FIR_PASSES  2     //2-stage moving average filter
 
#define CURRENT  0
#define PREVIOUS 1
 
#define UPPER_THRESHOLD 12      //for pulse detection
#define LOWER_THRESHOLD -12     //for pulse detection
#define AMPLITUDE_THRESHOLD 300
 
//*****************************************************************************
// Global Variables for BPM Demo
//*****************************************************************************
uint16_t pressure_reading0, pressure_reading, SpanVoltage_kPa;
unsigned int Sample_Window_Counter;
unsigned char overPressure;
unsigned int HR_ADCcounter;
unsigned int CP_Peak_Buf[pressure_buf_length];
unsigned char mx, xp;
unsigned char PeakFound;
unsigned int ADC_sample_counts;
unsigned char SpeedChange_value;
uint16_t ADC1_new_raw;
uint16_t ADC2_new_raw;
 
unsigned int Systolic_Pressure, Systolic_Pressure_previous;
unsigned int Diastolic_Pressure, Diastolic_Pressure_previous;
unsigned int Heart_Rate, Heart_Rate_previous;
unsigned char TMR4_counter;
unsigned int PumpSpeed;
unsigned int MAP;
double ks, kd;
unsigned char xSYS, xDIA;
 
unsigned char Target_Pressure_Set;
unsigned int MAP_temp_Max;
unsigned char MAP_Sample_Window_Counter;
unsigned char mx_temp;
uint16_t Target_Pressure, MAP_target;
unsigned char Error;
 
//filterLowPass k;
 
unsigned char HRcounterFlag, Pulse_Detection_Enable;
unsigned int wheel_counter = 0;
unsigned char w = 1;
 
unsigned int pulseHigh_Buf[pressure_buf_length];
unsigned int pulseLow_Buf[pressure_buf_length];
unsigned int PulseAmplitude_Buf[pressure_buf_length];
signed int Pulse_Amplitude;
unsigned int MAP_Amplitude;
 
//Ryan Bartling filter
int state;
int16_t dataPulse[2], dataCuff[2];
int16_t delta[2];
int16_t peakValue;
int16_t pulseHigh;
int16_t pulseLow;
int16_t rawDataPulse, rawDataCuff;
int16_t valueCuff;
 
typedef struct {
    int32_t *reg;
    uint16_t size;
    uint16_t position;
    int32_t sum;
}moving_average;
 
moving_average *maCuff[FIR_PASSES];
moving_average *maPulse[FIR_PASSES];
moving_average *maDelta;
 
////////////////////////////////////////////////////////////////////////////////
 
//Function prototypes
moving_average* NewMovingAverage(uint16_t _size);
void DestroyMovingAverage(moving_average* _this);
int16_t MoveAverage(moving_average* _this, int16_t input);
void ClearAverage(moving_average* _this);
 
sbit readBloodPressure       at flagReg.B0;
 
//Functions for 2-stage 32-point moving average filter.
moving_average* NewMovingAverage(uint16_t _size) {
     moving_average* _this;
     
     MM_Init();
 
    _this = (moving_average*)Malloc(sizeof(moving_average));
    _this->size = _size;
    _this->reg = (int32_t*)Malloc(_size * sizeof(int32_t));
    _this->position = 0;
    _this->sum = 0;
 
    return _this;
}
 
void DestroyMovingAverage(moving_average* _this) {
    Free(_this->reg, sizeof(_this->reg));
}
 
int16_t MoveAverage(moving_average* _this) {
    int16_t output;
 
    _this->sum -= _this->reg[_this->position];
    _this->sum += input;
    _this->reg[_this->position] = input;
    _this->position++;
    _this->position %= _this->size;
    output = _this->sum / (int32_t) _this->size;
 
    return output;
}
 
void ClearAverage(moving_average* _this) {
    int i;
 
    _this->sum = 0;
 
    for(i = 0; i < _this->size; i++) {
        _this->reg[i] = 0;
    }
}
 
int16_t FilterPass(int16_t input, moving_average** filters, int16_t passes) {
    int16_t output;
 
    if(passes == 0) {
        output = input;
    }
    else {
        passes--;
        output = MoveAverage(filters[passes], FilterPass(input, filters, passes));
    }
 
    return output;
}
 
void main() {
    while(1) {       
        if(readBloodPressure) {
            ADC_sample_counts = 0;
            
            while(readBloodPressure) {                               
                ADC1_new_raw = ADC_Read(0);      //Channe0(ADC1) = Cuff pressure
                Delay_us(20);
                ADC2_new_raw = ADC_Read(1);      //Channe1(ADC2) = Extracted oscillation signal
 
                dataPulse[PREVIOUS] = dataPulse[CURRENT];
                dataCuff[PREVIOUS] = dataCuff[CURRENT];
                delta[PREVIOUS] = delta[CURRENT];
                rawDataPulse = (int16_t)ADC_Read(1) << (int16_t)3;   //raw ADC data * 8
                rawDataCuff = (int16_t)ADC_Read(0) << (int16_t)3;    //raw ADC data * 8
                
                dataPulse[CURRENT] = FilterPass(rawDataPulse, maPulse, FIR_PASSES); //Retrieve and filter ADC data
                delta[CURRENT] = MoveAverage(maDelta, dataPulse[CURRENT] - dataPulse[PREVIOUS]);
                dataCuff[CURRENT] = FilterPass(rawDataCuff, maCuff, FIR_PASSES); //Retrieve and filter ADC data                
            }
        }
    }
}



- - - Updated - - -

Edit:

Issue solved.

I had missed some codes from main.c file of PIC24 An1556 code and after adding them now I have no errors related to memory or any other thing.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top