PWM Output

Dependencies:   mbed

Committer:
woodbed
Date:
Fri Dec 02 10:52:29 2016 +0000
Revision:
0:64f91b130af9
PWM Output (3port : Heart beat,Respiration,Bodymove(snore)?)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
woodbed 0:64f91b130af9 1 #include "mbed.h"
woodbed 0:64f91b130af9 2
woodbed 0:64f91b130af9 3 //PWM
woodbed 0:64f91b130af9 4 PwmOut signal_1(p21);
woodbed 0:64f91b130af9 5 PwmOut signal_2(p22);
woodbed 0:64f91b130af9 6 PwmOut signal_3(p23);
woodbed 0:64f91b130af9 7
woodbed 0:64f91b130af9 8 //Ticker
woodbed 0:64f91b130af9 9 Ticker timer;
woodbed 0:64f91b130af9 10
woodbed 0:64f91b130af9 11 //Timer
woodbed 0:64f91b130af9 12 //Timer ta;
woodbed 0:64f91b130af9 13
woodbed 0:64f91b130af9 14 //Digital Out
woodbed 0:64f91b130af9 15 DigitalOut test(p20);
woodbed 0:64f91b130af9 16
woodbed 0:64f91b130af9 17 //Cos Wave
woodbed 0:64f91b130af9 18 float coswave[256];
woodbed 0:64f91b130af9 19
woodbed 0:64f91b130af9 20 //User set Wave Paramater
woodbed 0:64f91b130af9 21 int sample_dt = 100; //microsec 10kHz
woodbed 0:64f91b130af9 22 float hb_carr_freq = 8.7; //Hz
woodbed 0:64f91b130af9 23 float hb_mod_freq = 1; //Hz
woodbed 0:64f91b130af9 24 float hb_carr_level = 0.083; //min=0 max=0.5 0.5以上ではMOD 50%以上で振幅が飽和する
woodbed 0:64f91b130af9 25 float hb_mod_ratio = 100; //0-100 %
woodbed 0:64f91b130af9 26 float resp_freq = 0.3; //Hz
woodbed 0:64f91b130af9 27 float resp_level = 0.56; //min=0 max=1
woodbed 0:64f91b130af9 28 float snore_freq = 150; //Hz
woodbed 0:64f91b130af9 29 float snore_level = 0.23; //min=0 max=1
woodbed 0:64f91b130af9 30 int snore_oncycle = 10000; //On Time = snore_oncycle * sample_dt
woodbed 0:64f91b130af9 31 int snore_allcycle = 20000; //ALL Time = snore_allcycle * sample_dt , snore_oncycle < snore_allcycle;
woodbed 0:64f91b130af9 32
woodbed 0:64f91b130af9 33 //Set wave paramater
woodbed 0:64f91b130af9 34 //HB_carr
woodbed 0:64f91b130af9 35 float hb_carr_period = 1000000*1/hb_carr_freq; //microSec
woodbed 0:64f91b130af9 36 float hb_carr_n = 1;
woodbed 0:64f91b130af9 37 float hb_carr_n_sample = 0; //The maximum number of sampling points in the signal
woodbed 0:64f91b130af9 38 float hb_carr_tend = 0; //The remainder of sampling points in the signal
woodbed 0:64f91b130af9 39 float hb_carr_tstart = 0; //start offset of sampling time
woodbed 0:64f91b130af9 40 float hb_carr_t_samplepoint = 0; //
woodbed 0:64f91b130af9 41 float hb_carr_table_dt = hb_carr_period/255;
woodbed 0:64f91b130af9 42 float hb_carr_n_samplepoint = 0;
woodbed 0:64f91b130af9 43 float hb_carr_rem_samplepoint=0;
woodbed 0:64f91b130af9 44 float hb_carr_v_samplepoint = 0;
woodbed 0:64f91b130af9 45
woodbed 0:64f91b130af9 46 //HB_mod
woodbed 0:64f91b130af9 47 float hb_modp_period = 1000000*1/(hb_carr_freq + hb_mod_freq);
woodbed 0:64f91b130af9 48 float hb_modp_n = 1;
woodbed 0:64f91b130af9 49 float hb_modp_n_sample = 0; //The maximum number of sampling points in the signal
woodbed 0:64f91b130af9 50 float hb_modp_tend = 0; //The remainder of sampling points in the signal
woodbed 0:64f91b130af9 51 float hb_modp_tstart = 0; //start offset of sampling time
woodbed 0:64f91b130af9 52 float hb_modp_t_samplepoint = 0; //
woodbed 0:64f91b130af9 53 float hb_modp_table_dt = hb_modp_period/255;
woodbed 0:64f91b130af9 54 float hb_modp_n_samplepoint = 0;
woodbed 0:64f91b130af9 55 float hb_modp_rem_samplepoint= 0;
woodbed 0:64f91b130af9 56 float hb_modp_v_samplepoint = 0;
woodbed 0:64f91b130af9 57
woodbed 0:64f91b130af9 58 float hb_modm_period = 1000000*1/(hb_carr_freq - hb_mod_freq);
woodbed 0:64f91b130af9 59 float hb_modm_n = 1;
woodbed 0:64f91b130af9 60 float hb_modm_n_sample = 0; //The maximum number of sampling points in the signal
woodbed 0:64f91b130af9 61 float hb_modm_tend = 0; //The remainder of sampling points in the signal
woodbed 0:64f91b130af9 62 float hb_modm_tstart = 0; //start offset of sampling time
woodbed 0:64f91b130af9 63 float hb_modm_t_samplepoint = 0; //
woodbed 0:64f91b130af9 64 float hb_modm_table_dt = hb_modm_period/255;
woodbed 0:64f91b130af9 65 float hb_modm_n_samplepoint = 0;
woodbed 0:64f91b130af9 66 float hb_modm_rem_samplepoint= 0;
woodbed 0:64f91b130af9 67 float hb_modm_v_samplepoint = 0;
woodbed 0:64f91b130af9 68
woodbed 0:64f91b130af9 69 float hb_mod_ra = hb_mod_ratio / 100;
woodbed 0:64f91b130af9 70 float hb_signal = 0;
woodbed 0:64f91b130af9 71
woodbed 0:64f91b130af9 72 //RESP
woodbed 0:64f91b130af9 73 float resp_period = 1000000*1/resp_freq; //microSec
woodbed 0:64f91b130af9 74 float resp_n = 1;
woodbed 0:64f91b130af9 75 float resp_n_sample = 0; //The maximum number of sampling points in the signal
woodbed 0:64f91b130af9 76 float resp_tend = 0; //The remainder of sampling points in the signal
woodbed 0:64f91b130af9 77 float resp_tstart = 0; //start offset of sampling time
woodbed 0:64f91b130af9 78 float resp_t_samplepoint = 0;
woodbed 0:64f91b130af9 79 float resp_table_dt = resp_period/255;
woodbed 0:64f91b130af9 80 float resp_n_samplepoint = 0;
woodbed 0:64f91b130af9 81 float resp_rem_samplepoint = 0;
woodbed 0:64f91b130af9 82 float resp_v_samplepoint = 0;
woodbed 0:64f91b130af9 83 float resp_signal = 0;
woodbed 0:64f91b130af9 84
woodbed 0:64f91b130af9 85 //SNORE Paramater
woodbed 0:64f91b130af9 86 float snore_period = 1000000*1/snore_freq; //microSec
woodbed 0:64f91b130af9 87 float snore_n = 1;
woodbed 0:64f91b130af9 88 float snore_n_sample = 0; //The maximum number of sampling points in the signal
woodbed 0:64f91b130af9 89 float snore_tend = 0; //The remainder of sampling points in the signal
woodbed 0:64f91b130af9 90 float snore_tstart = 0; //start offset of sampling time
woodbed 0:64f91b130af9 91 float snore_t_samplepoint = 0; //
woodbed 0:64f91b130af9 92 float snore_table_dt = snore_period/255;
woodbed 0:64f91b130af9 93 float snore_n_samplepoint = 0;
woodbed 0:64f91b130af9 94 float snore_rem_samplepoint = 0;
woodbed 0:64f91b130af9 95 float snore_v_samplepoint = 0;
woodbed 0:64f91b130af9 96 int snore_cycle = 0;
woodbed 0:64f91b130af9 97 float snore_signal = 0;
woodbed 0:64f91b130af9 98
woodbed 0:64f91b130af9 99 int pwidth_1 = 0;
woodbed 0:64f91b130af9 100 int pwidth_2 = 0;
woodbed 0:64f91b130af9 101 int pwidth_3 = 0;
woodbed 0:64f91b130af9 102
woodbed 0:64f91b130af9 103 //Signal Culcurate
woodbed 0:64f91b130af9 104 void signal_culc(){
woodbed 0:64f91b130af9 105
woodbed 0:64f91b130af9 106 // ta.reset();
woodbed 0:64f91b130af9 107 // ta.start();
woodbed 0:64f91b130af9 108
woodbed 0:64f91b130af9 109 //HB section
woodbed 0:64f91b130af9 110 //HB_carr
woodbed 0:64f91b130af9 111 hb_carr_n_sample = (hb_carr_period - hb_carr_tstart) / sample_dt;
woodbed 0:64f91b130af9 112 hb_carr_tend = fmod((hb_carr_period - hb_carr_tstart) , sample_dt);
woodbed 0:64f91b130af9 113 hb_carr_t_samplepoint = hb_carr_tstart + sample_dt * hb_carr_n; //time of sampling point
woodbed 0:64f91b130af9 114 hb_carr_n_samplepoint = hb_carr_t_samplepoint / hb_carr_table_dt;
woodbed 0:64f91b130af9 115 hb_carr_rem_samplepoint = fmod(hb_carr_t_samplepoint , hb_carr_table_dt);
woodbed 0:64f91b130af9 116 hb_carr_v_samplepoint = (hb_carr_rem_samplepoint/hb_carr_table_dt)*(coswave[(int)hb_carr_n_samplepoint+1]-coswave[(int)hb_carr_n_samplepoint])+coswave[(int)hb_carr_n_samplepoint];
woodbed 0:64f91b130af9 117 if(hb_carr_n == (int)hb_carr_n_sample) {
woodbed 0:64f91b130af9 118 hb_carr_n =1;
woodbed 0:64f91b130af9 119 hb_carr_tstart = sample_dt - hb_carr_tend;
woodbed 0:64f91b130af9 120 }
woodbed 0:64f91b130af9 121 else {
woodbed 0:64f91b130af9 122 hb_carr_n++;
woodbed 0:64f91b130af9 123 }
woodbed 0:64f91b130af9 124 //HB_mod
woodbed 0:64f91b130af9 125 //Acosωct+(Ama/2)cos(ωc+ωm)t+(Ama/2)cos(ωc-ωm)t fcarr(t) = Acosωct,ma = modulation ratio,
woodbed 0:64f91b130af9 126 hb_modp_n_sample = (hb_modp_period - hb_modp_tstart) / sample_dt;
woodbed 0:64f91b130af9 127 hb_modp_tend = fmod((hb_modp_period - hb_modp_tstart) , sample_dt);
woodbed 0:64f91b130af9 128 hb_modp_t_samplepoint = hb_modp_tstart + sample_dt * hb_modp_n; //time of sampling point
woodbed 0:64f91b130af9 129 hb_modp_n_samplepoint = hb_modp_t_samplepoint / hb_modp_table_dt;
woodbed 0:64f91b130af9 130 hb_modp_rem_samplepoint = fmod(hb_modp_t_samplepoint , hb_modp_table_dt);
woodbed 0:64f91b130af9 131 hb_modp_v_samplepoint = hb_mod_ra*((hb_modp_rem_samplepoint/hb_modp_table_dt)*(coswave[(int)hb_modp_n_samplepoint+1]-coswave[(int)hb_modp_n_samplepoint])+coswave[(int)hb_modp_n_samplepoint]);
woodbed 0:64f91b130af9 132 if(hb_modp_n == (int)hb_modp_n_sample) {
woodbed 0:64f91b130af9 133 hb_modp_n =1;
woodbed 0:64f91b130af9 134 hb_modp_tstart = sample_dt - hb_modp_tend;
woodbed 0:64f91b130af9 135 }
woodbed 0:64f91b130af9 136 else {
woodbed 0:64f91b130af9 137 hb_modp_n++;
woodbed 0:64f91b130af9 138 }
woodbed 0:64f91b130af9 139
woodbed 0:64f91b130af9 140 hb_modm_n_sample = (hb_modm_period - hb_modm_tstart) / sample_dt;
woodbed 0:64f91b130af9 141 hb_modm_tend = fmod((hb_modm_period - hb_modm_tstart) , sample_dt);
woodbed 0:64f91b130af9 142 hb_modm_t_samplepoint = hb_modm_tstart + sample_dt * hb_modm_n; //time of sampling point
woodbed 0:64f91b130af9 143 hb_modm_n_samplepoint = hb_modm_t_samplepoint / hb_modm_table_dt;
woodbed 0:64f91b130af9 144 hb_modm_rem_samplepoint = fmod(hb_modm_t_samplepoint , hb_modm_table_dt);
woodbed 0:64f91b130af9 145 hb_modm_v_samplepoint = hb_mod_ra*((hb_modm_rem_samplepoint/hb_modm_table_dt)*(coswave[(int)hb_modm_n_samplepoint+1]-coswave[(int)hb_modm_n_samplepoint])+coswave[(int)hb_modm_n_samplepoint]);
woodbed 0:64f91b130af9 146 if(hb_modm_n == (int)hb_modm_n_sample) {
woodbed 0:64f91b130af9 147 hb_modm_n =1;
woodbed 0:64f91b130af9 148 hb_modm_tstart = sample_dt - hb_modm_tend;
woodbed 0:64f91b130af9 149 }
woodbed 0:64f91b130af9 150 else {
woodbed 0:64f91b130af9 151 hb_modm_n++;
woodbed 0:64f91b130af9 152 }
woodbed 0:64f91b130af9 153
woodbed 0:64f91b130af9 154 //HB_AM MOD
woodbed 0:64f91b130af9 155 hb_signal = hb_carr_level*(hb_carr_v_samplepoint + (0.5*hb_modp_v_samplepoint)+(0.5*hb_modm_v_samplepoint))+0.5;//0-1 -> 0V-3.3V
woodbed 0:64f91b130af9 156
woodbed 0:64f91b130af9 157 //RESP section
woodbed 0:64f91b130af9 158 resp_n_sample = (resp_period - resp_tstart) / sample_dt;
woodbed 0:64f91b130af9 159 resp_tend = fmod((resp_period - resp_tstart) , sample_dt);
woodbed 0:64f91b130af9 160 resp_t_samplepoint = resp_tstart + sample_dt * resp_n; //time of sampling point
woodbed 0:64f91b130af9 161 resp_n_samplepoint = resp_t_samplepoint / resp_table_dt;
woodbed 0:64f91b130af9 162 resp_rem_samplepoint = fmod(resp_t_samplepoint , resp_table_dt);
woodbed 0:64f91b130af9 163 resp_v_samplepoint = resp_level*((resp_rem_samplepoint/resp_table_dt)*(coswave[(int)resp_n_samplepoint+1]-coswave[(int)resp_n_samplepoint])+coswave[(int)resp_n_samplepoint]);
woodbed 0:64f91b130af9 164 resp_signal = resp_v_samplepoint+0.5; //0-1 -> 0V-3.3V
woodbed 0:64f91b130af9 165 if(resp_n == (int)resp_n_sample) {
woodbed 0:64f91b130af9 166 resp_n =1;
woodbed 0:64f91b130af9 167 resp_tstart = sample_dt - resp_tend;
woodbed 0:64f91b130af9 168 }
woodbed 0:64f91b130af9 169 else {
woodbed 0:64f91b130af9 170 resp_n++;
woodbed 0:64f91b130af9 171 }
woodbed 0:64f91b130af9 172
woodbed 0:64f91b130af9 173 //SNORE section
woodbed 0:64f91b130af9 174 snore_n_sample = (snore_period - snore_tstart) / sample_dt;
woodbed 0:64f91b130af9 175 snore_tend = fmod((snore_period - snore_tstart) , sample_dt);
woodbed 0:64f91b130af9 176 snore_t_samplepoint = snore_tstart + sample_dt * snore_n; //time of sampling point
woodbed 0:64f91b130af9 177 snore_n_samplepoint = snore_t_samplepoint / snore_table_dt;
woodbed 0:64f91b130af9 178 snore_rem_samplepoint = fmod(snore_t_samplepoint , snore_table_dt);
woodbed 0:64f91b130af9 179 snore_v_samplepoint = snore_level*((snore_rem_samplepoint/snore_table_dt)*(coswave[(int)snore_n_samplepoint+1]-coswave[(int)snore_n_samplepoint])+coswave[(int)snore_n_samplepoint]);
woodbed 0:64f91b130af9 180 if(snore_n == (int)snore_n_sample) {
woodbed 0:64f91b130af9 181 snore_n =1;
woodbed 0:64f91b130af9 182 snore_tstart = sample_dt - snore_tend;
woodbed 0:64f91b130af9 183 }
woodbed 0:64f91b130af9 184 else {
woodbed 0:64f91b130af9 185 snore_n++;
woodbed 0:64f91b130af9 186 }
woodbed 0:64f91b130af9 187 if (snore_cycle <= snore_oncycle) {
woodbed 0:64f91b130af9 188 snore_cycle++;
woodbed 0:64f91b130af9 189 }
woodbed 0:64f91b130af9 190 else if (snore_cycle>snore_oncycle && snore_cycle <= snore_allcycle){
woodbed 0:64f91b130af9 191 snore_v_samplepoint = 0;
woodbed 0:64f91b130af9 192 snore_cycle++;
woodbed 0:64f91b130af9 193 }
woodbed 0:64f91b130af9 194 else {
woodbed 0:64f91b130af9 195 snore_cycle = 0;
woodbed 0:64f91b130af9 196 }
woodbed 0:64f91b130af9 197
woodbed 0:64f91b130af9 198 //test
woodbed 0:64f91b130af9 199 // test = !test;
woodbed 0:64f91b130af9 200
woodbed 0:64f91b130af9 201 snore_signal = snore_v_samplepoint+0.5; //0-1 -> 0V-3.3V
woodbed 0:64f91b130af9 202
woodbed 0:64f91b130af9 203 //PWM Output Pulse width
woodbed 0:64f91b130af9 204 pwidth_1 = hb_signal * sample_dt;
woodbed 0:64f91b130af9 205 pwidth_2 = resp_signal * sample_dt;
woodbed 0:64f91b130af9 206 pwidth_3 = snore_signal * sample_dt;
woodbed 0:64f91b130af9 207
woodbed 0:64f91b130af9 208 //ta.stop();
woodbed 0:64f91b130af9 209 //printf("time= %d \n",ta.read_us());
woodbed 0:64f91b130af9 210 }
woodbed 0:64f91b130af9 211
woodbed 0:64f91b130af9 212 void attime(){
woodbed 0:64f91b130af9 213 signal_culc();
woodbed 0:64f91b130af9 214 }
woodbed 0:64f91b130af9 215
woodbed 0:64f91b130af9 216
woodbed 0:64f91b130af9 217 int main() {
woodbed 0:64f91b130af9 218
woodbed 0:64f91b130af9 219 //Make Cos wave
woodbed 0:64f91b130af9 220 int i;
woodbed 0:64f91b130af9 221 for(i=0;i<=255;i++){ //256->255
woodbed 0:64f91b130af9 222 coswave[i]=0.5*(cos(2.0*3.1415*i/256)); //256->256
woodbed 0:64f91b130af9 223 }
woodbed 0:64f91b130af9 224 i = 0;
woodbed 0:64f91b130af9 225
woodbed 0:64f91b130af9 226 signal_1.period_us(sample_dt); //Pulse_cycle = 100usec = 10kHz
woodbed 0:64f91b130af9 227 signal_2.period_us(sample_dt);
woodbed 0:64f91b130af9 228 signal_2.period_us(sample_dt);
woodbed 0:64f91b130af9 229
woodbed 0:64f91b130af9 230 int j;
woodbed 0:64f91b130af9 231 j = sample_dt;
woodbed 0:64f91b130af9 232 timer.attach_us(&attime,j);
woodbed 0:64f91b130af9 233
woodbed 0:64f91b130af9 234 while(1) {
woodbed 0:64f91b130af9 235 signal_1.pulsewidth_us(pwidth_1);
woodbed 0:64f91b130af9 236 signal_2.pulsewidth_us(pwidth_2);
woodbed 0:64f91b130af9 237 signal_3.pulsewidth_us(pwidth_3);
woodbed 0:64f91b130af9 238 }
woodbed 0:64f91b130af9 239 }