??? 03/13/08 18:31 Read: times |
#152242 - Use a loop with deglitching and averaging Responding to: ???'s previous message |
using a loop makes a more clever algorithm more practical
than within your code at http://www.8052.com/forum/read.phtml?id=152198 compile this with "sdcc -c adc.c" #define NUM_SAMPLES 6 // 6 == 4+2 (and 10 == 8+2) __sfr __at(0xab) AD1DAT2; // combination of glitch removal and averaging int adc_smoothed() { unsigned int value = 0; unsigned char index; unsigned char max = 0x00; unsigned char min = 0xff; unsigned char tmp; for (index = 0; index<NUM_SAMPLES; index++) { tmp = AD1DAT2; value += tmp; if( tmp > max ) max = tmp; if( tmp < min ) min = tmp; } // removing highest and lowest ADC reading from sum value -= min; value -= max; // return average of the rest return value/(NUM_SAMPLES-2); } and get: 91 ;------------------------------------------------------------ 92 ;Allocation info for local variables in function 'adc_smoothed' 93 ;------------------------------------------------------------ 94 ;value Allocated to registers r2 r3 95 ;index Allocated to registers r6 96 ;max Allocated to registers r4 97 ;min Allocated to registers r5 98 ;tmp Allocated to registers r7 0000 104 _adc_smoothed: 113 ; adc.c:8: unsigned int value = 0; 0000 7A 00 114 mov r2,#0x00 0002 7B 00 115 mov r3,#0x00 116 ; adc.c:10: unsigned char max = 0x00; 0004 7C 00 117 mov r4,#0x00 118 ; adc.c:11: unsigned char min = 0xff; 0006 7D FF 119 mov r5,#0xFF 120 ; adc.c:14: for (index = 0; index<NUM_SAMPLES; index++) 0008 7E 06 121 mov r6,#0x06 000A 122 00107$: 123 ; adc.c:16: tmp = AD1DAT2; 000A AF AB 124 mov r7,_AD1DAT2 125 ; adc.c:17: value += tmp; 000C 8F 00 126 mov ar0,r7 000E 79 00 127 mov r1,#0x00 0010 E8 128 mov a,r0 0011 2A 129 add a,r2 0012 FA 130 mov r2,a 0013 E9 131 mov a,r1 0014 3B 132 addc a,r3 0015 FB 133 mov r3,a 134 ; adc.c:19: if( tmp > max ) 0016 C3 135 clr c 0017 EC 136 mov a,r4 0018 9F 137 subb a,r7 0019 50 02 138 jnc 00102$ 139 ; adc.c:20: max = tmp; 001B 8F 04 140 mov ar4,r7 001D 141 00102$: 142 ; adc.c:21: if( tmp < min ) 001D C3 143 clr c 001E EF 144 mov a,r7 001F 9D 145 subb a,r5 0020 50 02 146 jnc 00104$ 147 ; adc.c:22: min = tmp; 0022 8F 05 148 mov ar5,r7 0024 149 00104$: 0024 DE E4 150 djnz r6,00107$ 151 ; adc.c:14: for (index = 0; index<NUM_SAMPLES; index++) 152 ; adc.c:26: value -= min; 0026 7E 00 153 mov r6,#0x00 0028 EA 154 mov a,r2 0029 C3 155 clr c 002A 9D 156 subb a,r5 002B FA 157 mov r2,a 002C EB 158 mov a,r3 002D 9E 159 subb a,r6 002E FB 160 mov r3,a 161 ; adc.c:27: value -= max; 002F 7D 00 162 mov r5,#0x00 0031 EA 163 mov a,r2 0032 C3 164 clr c 0033 9C 165 subb a,r4 0034 FA 166 mov r2,a 0035 EB 167 mov a,r3 0036 9D 168 subb a,r5 169 ; adc.c:30: return value/(NUM_SAMPLES-2); 0037 8A 82 170 mov dpl,r2 0039 C3 171 clr c 003A 13 172 rrc a 003B C5 82 173 xch a,dpl 003D 13 174 rrc a 003E C5 82 175 xch a,dpl 0040 C3 176 clr c 0041 13 177 rrc a 0042 C5 82 178 xch a,dpl 0044 13 179 rrc a 0045 C5 82 180 xch a,dpl 0047 F5 83 181 mov dph,a 0049 22 182 ret The round trip buffer suggested by Esko is also nice:) Greetings, Frieder |
Topic | Author | Date |
Math not functioning with proper headers? | 01/01/70 00:00 | |
horrible method | 01/01/70 00:00 | |
Lots of issues | 01/01/70 00:00 | |
Code Op at level 8 | 01/01/70 00:00 | |
Try multiple steps | 01/01/70 00:00 | |
ah hah moment arrived......omg | 01/01/70 00:00 | |
01/01/70 00:00 | ||
Here is the block which now works 100% | 01/01/70 00:00 | |
Operation question in C | 01/01/70 00:00 | |
Because | 01/01/70 00:00 | |
Simplify | 01/01/70 00:00 | |
what's the point ? | 01/01/70 00:00 | |
What's the point | 01/01/70 00:00 | |
simpler, but | 01/01/70 00:00 | |
Not right | 01/01/70 00:00 | |
use shift right instead of divide | 01/01/70 00:00 | |
If you're lucky... | 01/01/70 00:00 | |
Use a rount trip buffer | 01/01/70 00:00 | |
Bad names | 01/01/70 00:00 | |
System use names ???? | 01/01/70 00:00 | |
Common naming convention ? | 01/01/70 00:00 | |
ISO/IEC 9899:1990... | 01/01/70 00:00 | |
Comments | 01/01/70 00:00 | |
same effect as a "circular array" | 01/01/70 00:00 | |
think I like this the best | 01/01/70 00:00 | |
Use a loop with deglitching and averaging![]() | 01/01/70 00:00 |