milan.rajik
Banned
- Joined
- Apr 1, 2013
- Messages
- 2,524
- Helped
- 540
- Reputation
- 1,078
- Reaction score
- 524
- Trophy points
- 1,393
- Activity points
- 0
Should I measure time between two pulses (two rising edges) and calculate BPM from that or should I count number of pulses in 10 seconds and multiply it by 6
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 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 #include "float2ascii.h" sbit MAX7219_DATA at LATD0_bit; sbit MAX7219_CS at LATD1_bit ; sbit MAX7219_CLK at LATD2_bit; sbit MAX7219_DATA_Direction at TRISD0_bit; sbit MAX7219_CS_Direction at TRISD1_bit; sbit MAX7219_CLK_Direction at TRISD2_bit; const unsigned char Font_B[16] = {0x7e,0x30,0x6d,0x79,0x33,0x5b,0x5f,0x70,0x7f,0x7b,0x77,0x1f,0x4e,0x3d,0x4f,0x47}; double counter = 0, beats_per_minute = 0, time_in_seconds = 0, frequency = 0; void MAX7219_Send_Byte(unsigned char); void MAX7219_Send(char, unsigned char); void MAX7219_Init(); void MAX7219_Display(unsigned char); void Interrupt(){ if((INT0IF_bit) && (TMR1ON_bit == 0)) { TMR1ON_bit = 1; INT0IF_bit = 0; } if((INT0IF_bit) && (TMR1ON_bit)) { TMR1ON_bit = 0; INT0IE_bit = 0; INT0IF_bit = 0; } if(TMR1IF_bit){ TMR1IF_bit = 0; TMR1H = 0x3C; TMR1L = 0xB0; //Enter your code here counter = counter + 1; } } void MAX7219_Init() { MAX7219_DATA_Direction = 0; MAX7219_CS_Direction = 0; MAX7219_CLK_Direction = 0; MAX7219_DATA = 0; MAX7219_CS = 0; MAX7219_CLK = 0; MAX7219_Send(0x09, 0x00); //Decode Mode MAX7219_Send(0x0A, 0x05); //Brightness MAX7219_Send(0x0B, 0x07); //Scan limit MAX7219_Send(0x0C, 0x01); MAX7219_Send(0x0F, 0x00); } void MAX7219_Send(char digit, unsigned char data_) { MAX7219_Send_Byte(digit); MAX7219_Send_Byte(data_); MAX7219_DATA = 0; MAX7219_CS = 1; Delay_us(20); MAX7219_CS = 0; } void MAX7219_Send_Byte(unsigned char byte) { unsigned char i; for(i = 0; i < 8; i++, byte <<= 1) { if(byte & 0x80) MAX7219_DATA = 1; else MAX7219_DATA = 0; MAX7219_CLK = 1; Delay_us(50); MAX7219_CLK = 0; } } void MAX7219_Display(double myDbl) { unsigned char dispskip; unsigned char i, dp, str[30]; float2ascii(myDbl, str, 1); Ltrim(str); dispskip = strlen(str) - 1; if(dispskip >= 8)dispskip = 7; i = 0; dp = 0; while(i <= dispskip) { if((str[dispskip-i] >= 0x30) && (str[dispskip - i] <= 0x39) && (dp == 0)) { MAX7219_Send(i+1, (Font_B[str[dispskip-i] - 0x30]));//0-9 } if((str[dispskip-i] >= 0x30) && (str[dispskip - i] <= 0x39) && (dp)) { MAX7219_Send(i, (Font_B[str[dispskip-i] - 0x30]));//0-9 } if(str[dispskip-i] == '.') { MAX7219_Send(i+1, (Font_B[str[dispskip-(i+1)] - 0x30]) | 0x80);// i++; dp = 1; } i++; } while(i <= 8) { MAX7219_Send(i, 0); i++; } } void main() { CM1CON0 = 0x00; CM2CON0 = 0x00; ANSELA = 0x00; ANSELB = 0x00; ANSELC = 0x00; ANSELD = 0x00; ANSELE = 0x00; TRISA = 0xC0; TRISB = 0x01; TRISC = 0x01; TRISD = 0x00; TRISE = 0x08; PORTA = 0x00; PORTB = 0x00; PORTC = 0x00; PORTD = 0x00; PORTE = 0x00; LATA = 0x00; LATB = 0x00; LATC = 0x00; LATD = 0x00; LATE = 0x00; MAX7219_Init(); MAX7219_Display(beats_per_minute); while(1) { counter = 0; beats_per_minute = 0; time_in_seconds = 0; frequency = 0; //Timer1 //Prescaler 1:2; TMR1 Preload = 15536; Actual Interrupt Time : 100 ms //Place/Copy this part in declaration section /* TxCON bit 7-6 TMRxCS<1:0>: Timer1/3/5 Clock Source Select bits 11 =Reserved. Do not use. 10 =Timer1/3/5 clock source is pin or oscillator: If TxSOSCEN = 0: External clock from TxCKI pin (on the rising edge) If TxSOSCEN = 1: Crystal oscillator on SOSCI/SOSCO pins 01 =Timer1/3/5 clock source is system clock (F OSC ) 00 =Timer1/3/5 clock source is instruction clock (F OSC /4) bit 5-4 TxCKPS<1:0>: Timer1/3/5 Input Clock Prescale Select bits 11 = 1:8 Prescale value 10 = 1:4 Prescale value 01 = 1:2 Prescale value 00 = 1:1 Prescale value bit 3 TxSOSCEN: Secondary Oscillator Enable Control bit 1 = Dedicated Secondary oscillator circuit enabled 0 = Dedicated Secondary oscillator circuit disabled bit 2 TxSYNC: Timer1/3/5 External Clock Input Synchronization Control bit TMRxCS<1:0> = 1X 1 = Do not synchronize external clock input 0 = Synchronize external clock input with system clock (F OSC ) TMRxCS<1:0> = 0X This bit is ignored. Timer1/3/5 uses the internal clock when TMRxCS<1:0> = 1X. bit 1 TxRD16: 16-Bit Read/Write Mode Enable bit 1 = Enables register read/write of Timer1/3/5 in one 16-bit operation 0 = Enables register read/write of Timer1/3/5 in two 8-bit operation bit 0 TMRxON: Timer1/3/5 On bit 1 = Enables Timer1/3/5 0 = Stops Timer1/3/5 Clears Timer1/3/5 Gate flip-flop */ T1CON = 0x11; TMR1IF_bit = 0; TMR1H = 0x3C; TMR1L = 0xB0; TMR1ON_bit = 0; TMR1IE_bit = 1; INTEDG0_bit = 0; INT0IF_bit = 0; GIE_bit = 0; INT0IE_bit = 1; PEIE_bit = 1; GIE_bit = 1; while(!TMR1ON_bit); while(TMR1ON_bit); Delay_ms(500); time_in_seconds = ((counter / 10.0) + ((65536 - ((TMR1H << 8) + TMR1L)) * 1.52587890625e6)); frequency = 1.0 / time_in_seconds; beats_per_minute = 60 * frequency; MAX7219_Display(beats_per_minute); } }
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 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 #include "float2ascii.h" sbit MAX7219_DATA at LATD0_bit; sbit MAX7219_CS at LATD1_bit ; sbit MAX7219_CLK at LATD2_bit; sbit MAX7219_DATA_Direction at TRISD0_bit; sbit MAX7219_CS_Direction at TRISD1_bit; sbit MAX7219_CLK_Direction at TRISD2_bit; const unsigned char Font_B[16] = {0x7e,0x30,0x6d,0x79,0x33,0x5b,0x5f,0x70,0x7f,0x7b,0x77,0x1f,0x4e,0x3d,0x4f,0x47}; double counter = 0.0, beats_per_minute = 0.0, time_in_seconds = 0.0, frequency = 0.0; void MAX7219_Send_Byte(unsigned char); void MAX7219_Send(char, unsigned char); void MAX7219_Init(); void MAX7219_Display(double); void interrupt(){ if((INT0IF_bit) && (TMR1IE_bit == 0)) { TMR1IE_bit = 1; INT0IF_bit = 0; } if((INT0IF_bit) && (TMR1IE_bit)) { TMR1IE_bit = 0; INT0IE_bit = 0; INT0IF_bit = 0; } if(TMR1IF_bit) { TMR1IF_bit = 0; TMR1H = 0x3C; TMR1L = 0xB0; //Enter your code here counter = counter + 1; } } void MAX7219_Init() { MAX7219_DATA_Direction = 0; MAX7219_CS_Direction = 0; MAX7219_CLK_Direction = 0; MAX7219_DATA = 0; MAX7219_CS = 0; MAX7219_CLK = 0; MAX7219_Send(0x09, 0x00); //Decode Mode MAX7219_Send(0x0A, 0x05); //Brightness MAX7219_Send(0x0B, 0x07); //Scan limit MAX7219_Send(0x0C, 0x01); MAX7219_Send(0x0F, 0x00); } void MAX7219_Send(char digit, unsigned char data_) { MAX7219_Send_Byte(digit); MAX7219_Send_Byte(data_); MAX7219_DATA = 0; MAX7219_CS = 1; Delay_us(20); MAX7219_CS = 0; } void MAX7219_Send_Byte(unsigned char byte) { unsigned char i; for(i = 0; i < 8; i++, byte <<= 1) { if(byte & 0x80) MAX7219_DATA = 1; else MAX7219_DATA = 0; MAX7219_CLK = 1; Delay_us(50); MAX7219_CLK = 0; } } void MAX7219_Display(double myDbl) { unsigned char dispskip; unsigned char i, dp; char str[30]; float2ascii(myDbl, str, 1); Ltrim(str); dispskip = strlen(str) - 1; if(dispskip >= 8)dispskip = 7; i = 0; dp = 0; while(i <= dispskip) { if((str[dispskip-i] >= 0x30) && (str[dispskip - i] <= 0x39) && (dp == 0)) { MAX7219_Send(i+1, (Font_B[str[dispskip-i] - 0x30]));//0-9 } if((str[dispskip-i] >= 0x30) && (str[dispskip - i] <= 0x39) && (dp)) { MAX7219_Send(i, (Font_B[str[dispskip-i] - 0x30]));//0-9 } if(str[dispskip-i] == '.') { MAX7219_Send(i+1, (Font_B[str[dispskip-(i+1)] - 0x30]) | 0x80);// i++; dp = 1; } i++; } while(i <= 8) { MAX7219_Send(i, 0); i++; } } void main() { CM1CON0 = 0x00; CM2CON0 = 0x00; ANSELA = 0x00; ANSELB = 0x00; ANSELC = 0x00; ANSELD = 0x00; ANSELE = 0x00; TRISA = 0xC0; TRISB = 0x01; TRISC = 0x00; TRISD = 0x00; TRISE = 0x08; PORTA = 0x00; PORTB = 0x00; PORTC = 0x00; PORTD = 0x00; PORTE = 0x00; LATA = 0x00; LATB = 0x00; LATC = 0x00; LATD = 0x00; LATE = 0x00; MAX7219_Init(); MAX7219_Display(beats_per_minute); while(1) { counter = 0; beats_per_minute = 0; time_in_seconds = 0; frequency = 0; //Timer1 //Prescaler 1:2; TMR1 Preload = 15536; Actual Interrupt Time : 100 ms //Place/Copy this part in declaration section /* TxCON bit 7-6 TMRxCS<1:0>: Timer1/3/5 Clock Source Select bits 11 =Reserved. Do not use. 10 =Timer1/3/5 clock source is pin or oscillator: If TxSOSCEN = 0: External clock from TxCKI pin (on the rising edge) If TxSOSCEN = 1: Crystal oscillator on SOSCI/SOSCO pins 01 =Timer1/3/5 clock source is system clock (F OSC ) 00 =Timer1/3/5 clock source is instruction clock (F OSC /4) bit 5-4 TxCKPS<1:0>: Timer1/3/5 Input Clock Prescale Select bits 11 = 1:8 Prescale value 10 = 1:4 Prescale value 01 = 1:2 Prescale value 00 = 1:1 Prescale value bit 3 TxSOSCEN: Secondary Oscillator Enable Control bit 1 = Dedicated Secondary oscillator circuit enabled 0 = Dedicated Secondary oscillator circuit disabled bit 2 TxSYNC: Timer1/3/5 External Clock Input Synchronization Control bit TMRxCS<1:0> = 1X 1 = Do not synchronize external clock input 0 = Synchronize external clock input with system clock (F OSC ) TMRxCS<1:0> = 0X This bit is ignored. Timer1/3/5 uses the internal clock when TMRxCS<1:0> = 1X. bit 1 TxRD16: 16-Bit Read/Write Mode Enable bit 1 = Enables register read/write of Timer1/3/5 in one 16-bit operation 0 = Enables register read/write of Timer1/3/5 in two 8-bit operation bit 0 TMRxON: Timer1/3/5 On bit 1 = Enables Timer1/3/5 0 = Stops Timer1/3/5 Clears Timer1/3/5 Gate flip-flop */ GIE_bit = 0; //Disable Interrupts till congiguration is done T1CON = 0x11; //Configure timer 1 for 100 ms interrupt TMR1IF_bit = 0; T1GCON = 0x40; TMR1IE_bit = 0; TMR1ON_bit = 1; TMR1H = 0x3C; TMR1L = 0xB0; INTEDG0_bit = 0; //Configure INT0 interrupt to detect rising edge INT0IF_bit = 0; INT0IE_bit = 1; PEIE_bit = 1; //Enable interrupts GIE_bit = 1; while(!TMR1IE_bit); //TMR1IE_bit is set in ISR when first pulse is detected while(TMR1IE_bit); //TMR1IE_bit is cleared in ISR when 2nd pulse is detected time_in_seconds = ((counter / 10) + ((((TMR1H << 8) + TMR1L) - 15536) * 2e-6)); //time_in_seconds = ((counter * 50000 * 2e-6) + ((((TMR1H << 8) + TMR1L) - 15536) * 2e-6)); frequency = 1 / time_in_seconds; beats_per_minute = 60.0 * frequency; MAX7219_Display(beats_per_minute); } }
I fixed the code and now it works fine and precisely. Here is Proteus simulation and .hex files. My Proteus file is Proteus 8.1 SP1 format.
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 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 #include "float2ascii.h" sbit MAX7219_DATA at LATD0_bit; sbit MAX7219_CS at LATD1_bit ; sbit MAX7219_CLK at LATD2_bit; sbit MAX7219_DATA_Direction at TRISD0_bit; sbit MAX7219_CS_Direction at TRISD1_bit; sbit MAX7219_CLK_Direction at TRISD2_bit; const unsigned char Font_B[16] = {0x7e,0x30,0x6d,0x79,0x33,0x5b,0x5f,0x70,0x7f,0x7b,0x77,0x1f,0x4e,0x3d,0x4f,0x47}; double counter = 0.0, beats_per_minute = 0.0, time_in_seconds = 0.0, frequency = 0.0; void MAX7219_Send_Byte(unsigned char); void MAX7219_Send(char, unsigned char); void MAX7219_Init(); void MAX7219_Display(double); void interrupt(){ if((INT0IF_bit) && (!TMR1ON_bit)) { TMR1ON_bit = 1; INT0IF_bit = 0; } if((INT0IF_bit) && (TMR1ON_bit)) { TMR1ON_bit = 0; INT0IE_bit = 0; INT0IF_bit = 0; } if(TMR1IF_bit) { TMR1IF_bit = 0; TMR1H = 0x3C; TMR1L = 0xB0; //Enter your code here counter = counter + 1; } } void MAX7219_Init() { MAX7219_DATA_Direction = 0; MAX7219_CS_Direction = 0; MAX7219_CLK_Direction = 0; MAX7219_DATA = 0; MAX7219_CS = 0; MAX7219_CLK = 0; MAX7219_Send(0x09, 0x00); //Decode Mode MAX7219_Send(0x0A, 0x05); //Brightness MAX7219_Send(0x0B, 0x07); //Scan limit MAX7219_Send(0x0C, 0x01); MAX7219_Send(0x0F, 0x00); } void MAX7219_Send(char digit, unsigned char data_) { MAX7219_Send_Byte(digit); MAX7219_Send_Byte(data_); MAX7219_DATA = 0; MAX7219_CS = 1; Delay_us(20); MAX7219_CS = 0; } void MAX7219_Send_Byte(unsigned char byte) { unsigned char i; for(i = 0; i < 8; i++, byte <<= 1) { if(byte & 0x80) MAX7219_DATA = 1; else MAX7219_DATA = 0; MAX7219_CLK = 1; Delay_us(50); MAX7219_CLK = 0; } } void MAX7219_Display(double myDbl) { unsigned char dispskip; unsigned char i, dp; char str[30]; float2ascii(myDbl, str, 1); Ltrim(str); dispskip = strlen(str) - 1; if(dispskip >= 8)dispskip = 7; i = 0; dp = 0; while(i <= dispskip) { if((str[dispskip-i] >= 0x30) && (str[dispskip - i] <= 0x39) && (dp == 0)) { MAX7219_Send(i+1, (Font_B[str[dispskip-i] - 0x30]));//0-9 } if((str[dispskip-i] >= 0x30) && (str[dispskip - i] <= 0x39) && (dp)) { MAX7219_Send(i, (Font_B[str[dispskip-i] - 0x30]));//0-9 } if(str[dispskip-i] == '.') { MAX7219_Send(i+1, (Font_B[str[dispskip-(i+1)] - 0x30]) | 0x80);// i++; dp = 1; } i++; } while(i <= 8) { MAX7219_Send(i, 0); i++; } } void main() { CM1CON0 = 0x00; CM2CON0 = 0x00; ANSELA = 0x00; ANSELB = 0x00; ANSELC = 0x00; ANSELD = 0x00; ANSELE = 0x00; TRISA = 0xC0; TRISB = 0x01; TRISC = 0x00; TRISD = 0x00; TRISE = 0x08; PORTA = 0x00; PORTB = 0x00; PORTC = 0x00; PORTD = 0x00; PORTE = 0x00; LATA = 0x00; LATB = 0x00; LATC = 0x00; LATD = 0x00; LATE = 0x00; MAX7219_Init(); MAX7219_Display(beats_per_minute); while(1) { counter = 0; beats_per_minute = 0; time_in_seconds = 0; frequency = 0; //Timer1 //Prescaler 1:2; TMR1 Preload = 15536; Actual Interrupt Time : 100 ms //Place/Copy this part in declaration section /* TxCON bit 7-6 TMRxCS<1:0>: Timer1/3/5 Clock Source Select bits 11 =Reserved. Do not use. 10 =Timer1/3/5 clock source is pin or oscillator: If TxSOSCEN = 0: External clock from TxCKI pin (on the rising edge) If TxSOSCEN = 1: Crystal oscillator on SOSCI/SOSCO pins 01 =Timer1/3/5 clock source is system clock (F OSC ) 00 =Timer1/3/5 clock source is instruction clock (F OSC /4) bit 5-4 TxCKPS<1:0>: Timer1/3/5 Input Clock Prescale Select bits 11 = 1:8 Prescale value 10 = 1:4 Prescale value 01 = 1:2 Prescale value 00 = 1:1 Prescale value bit 3 TxSOSCEN: Secondary Oscillator Enable Control bit 1 = Dedicated Secondary oscillator circuit enabled 0 = Dedicated Secondary oscillator circuit disabled bit 2 TxSYNC: Timer1/3/5 External Clock Input Synchronization Control bit TMRxCS<1:0> = 1X 1 = Do not synchronize external clock input 0 = Synchronize external clock input with system clock (F OSC ) TMRxCS<1:0> = 0X This bit is ignored. Timer1/3/5 uses the internal clock when TMRxCS<1:0> = 1X. bit 1 TxRD16: 16-Bit Read/Write Mode Enable bit 1 = Enables register read/write of Timer1/3/5 in one 16-bit operation 0 = Enables register read/write of Timer1/3/5 in two 8-bit operation bit 0 TMRxON: Timer1/3/5 On bit 1 = Enables Timer1/3/5 0 = Stops Timer1/3/5 Clears Timer1/3/5 Gate flip-flop */ GIE_bit = 0; //Disable Interrupts till congiguration is done T1CON = 0x10; //Configure timer 1 for 100 ms interrupt TMR1IF_bit = 0; TMR1IE_bit = 1; TMR1H = 0x3C; TMR1L = 0xB0; INTEDG0_bit = 1; //Configure INT0 interrupt to detect rising edge INT0IF_bit = 0; INT0IE_bit = 1; PEIE_bit = 1; //Enable interrupts GIE_bit = 1; while(!TMR1ON_bit); //TMR1ON_bit is set in ISR when first pulse is detected while(TMR1ON_bit); //TMR1ON_bit is cleared in ISR when 2nd pulse is detected time_in_seconds = ((counter / 10) + ((((TMR1H << 8) + TMR1L) - 15536) * 2e-6)); //time_in_seconds = ((counter * 50000 * 2e-6) + ((((TMR1H << 8) + TMR1L) - 15536) * 2e-6)); frequency = 1.0 / time_in_seconds; beats_per_minute = 60.0 * frequency; MAX7219_Display(beats_per_minute); } }
Also the timer reload values are loaded for 100 ms timer interrupts.
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?