├── .gitattributes ├── main.c ├── Makefile ├── TinyTouchLib.h ├── TinyTouchLib.c ├── .gitignore └── README.md /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | *.sln merge=union 7 | *.csproj merge=union 8 | *.vbproj merge=union 9 | *.fsproj merge=union 10 | *.dbproj merge=union 11 | 12 | # Standard to msysgit 13 | *.doc diff=astextplain 14 | *.DOC diff=astextplain 15 | *.docx diff=astextplain 16 | *.DOCX diff=astextplain 17 | *.dot diff=astextplain 18 | *.DOT diff=astextplain 19 | *.pdf diff=astextplain 20 | *.PDF diff=astextplain 21 | *.rtf diff=astextplain 22 | *.RTF diff=astextplain 23 | -------------------------------------------------------------------------------- /main.c: -------------------------------------------------------------------------------- 1 | /* 2 | v0.1 06/10/2013 3 | 4 | TinyTouchLibrary Example 5 | 6 | Hardware: 7 | - Attiny 13A/25/45/85 8 | - LED on PB4 9 | - Touchbutton on PB3 10 | */ 11 | 12 | 13 | #include 14 | #include 15 | #include "tinytouchlib.h" 16 | 17 | int main(void) 18 | { 19 | CLKPR=_BV(CLKPCE); 20 | CLKPR=0; // set clock prescaler to 1 (attiny 25/45/85/24/44/84/13/13A) 21 | 22 | DDRB=_BV(PB4); // Enable LED output pin 23 | 24 | tinytouch_init(); 25 | 26 | while(1) { 27 | if (tinytouch_sense()==tt_push) {PORTB^=_BV(PB4);} // Toggle LED on touch event 28 | _delay_ms(10); 29 | } 30 | } 31 | 32 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | 2 | DEVICE = attiny85 3 | F_CPU = 16000000 # in Hz 4 | AVRDUDE = avrdude -c usbasp -p t85 -V -U flash:w:main.hex 5 | 6 | CFLAGS = -I. -DDEBUG_LEVEL=0 7 | CFLAGS += -Wno-deprecated-declarations -D__PROG_TYPES_COMPAT__ 8 | CFLAGS += -Wl,--gc-sections 9 | CFLAGS += -fdata-sections -ffunction-sections 10 | OBJECTS = main.o tinytouchlib.o 11 | COMPILE = avr-gcc -Wall -Os --std=gnu99 -DF_CPU=$(F_CPU) $(CFLAGS) -mmcu=$(DEVICE) 12 | 13 | 14 | # symbolic targets: 15 | help: 16 | @echo "make hex ....... to build main.hex" 17 | @echo "make flash ..... to flash the firmware using avrdude/usbasp" 18 | @echo "make clean ..... to delete objects and hex file" 19 | 20 | hex: main.hex 21 | 22 | # rule for uploading firmware: 23 | flash: main.hex 24 | $(AVRDUDE) -U flash:w:main.hex:i 25 | 26 | # rule for deleting dependent files (those which can be built by Make): 27 | clean: 28 | rm -f main.hex main.lst main.obj main.cof main.list main.map main.eep.hex main.elf *.o 29 | 30 | # Generic rule for compiling C files: 31 | .c.o: 32 | $(COMPILE) -c $< -o $@ 33 | 34 | # Generic rule for assembling Assembler source files: 35 | .S.o: 36 | $(COMPILE) -x assembler-with-cpp -c $< -o $@ 37 | # "-x assembler-with-cpp" should not be necessary since this is the default 38 | # file type for the .S (with capital S) extension. However, upper case 39 | # characters are not always preserved on Windows. To ensure WinAVR 40 | # compatibility define the file type manually. 41 | 42 | # Generic rule for compiling C to assembler, used for debugging only. 43 | .c.s: 44 | $(COMPILE) -S $< -o $@ 45 | 46 | # file targets: 47 | 48 | main.elf: $(OBJECTS) 49 | $(COMPILE) -o main.elf $(OBJECTS) 50 | 51 | main.hex: main.elf 52 | rm -f main.hex main.eep.hex 53 | avr-objcopy -j .text -j .data -O ihex main.elf main.hex 54 | avr-size -C --mcu=$(DEVICE) main.elf 55 | 56 | # debugging targets: 57 | 58 | disasm: main.elf 59 | avr-objdump -d main.elf 60 | 61 | cpp: 62 | $(COMPILE) -E main.c 63 | -------------------------------------------------------------------------------- /TinyTouchLib.h: -------------------------------------------------------------------------------- 1 | /* 2 | * TinyTouchLib.h 3 | * 4 | * Created: 09.06.2013 17:25:47 - v0.1 Initial release (Attiny 10) 5 | * 10.06.2013 - v0.2 ported to ATtiny 25/45/85 and ATtiny13 6 | * Author: Tim (cpldcpu@gmail.com) 7 | */ 8 | 9 | #ifndef TINYTOUCHLIB_H_ 10 | #define TINYTOUCHLIB_H_ 11 | #include 12 | 13 | enum {tt_off=0,tt_on,tt_push,tt_release,tt_timeout}; 14 | 15 | ////////////////////////////////////////////////////////////////////////// 16 | // 17 | // User definable settings 18 | // 19 | ////////////////////////////////////////////////////////////////////////// 20 | 21 | // Define upper and lower threshold for the touch sensing. You may have to 22 | // change these depending on the geometry of your touch button. 23 | // Setting the "on" value lower will make the touch button more sensitive. 24 | // Setting the "off" value higher will make the touch button less likely 25 | // to be "stuck". Too high values can lead to oscillations. 26 | 27 | #define touch_threshold_on 60 28 | #define touch_threshold_off 20 29 | 30 | // If the touch button is pressed, the bias value is not updated 31 | // to prevent detection of multiple touches. In some cases this may 32 | // lead to a "stuck button" situation. Therefore an update of the bias 33 | // is forced after a certain time unless this function is deactivated. 34 | // 35 | // The value of timeout corresponds to the number of calls to tinytouch_sense(). 36 | // The maximum is 255. 37 | // Setting timeout to 0 will turn the functionality off. 38 | 39 | #define touch_timeout 255 40 | //#define touch_timeout 0 // turn off timeout functionality 41 | 42 | 43 | // Define pins to use for the reference input and the touch button 44 | // The reference pin is used to charge or discharge the internal 45 | // sample&hold capacitor. This pin is used in output mode and should 46 | // not be shorted to VCC or GND externally. 47 | // The sense pin is connected to the touch-button. To improve noise immunity 48 | // a series resistor can be used. 49 | 50 | // The pin number must match the corresponding analog input number ADCx. 51 | // Default port is PORTB. (ATtiny 5/10/13/25/45/85) 52 | 53 | #define tt_refpin 2 // Use PB2 as reference pin 54 | #define tt_refadc 1 // Use ADC1 as reference ADC input 55 | #define tt_sensepin 3 // Use PB3 as sense pin 56 | #define tt_senseadc 3 // Use ADC3 as sense ADC input 57 | 58 | ////////////////////////////////////////////////////////////////////////// 59 | // 60 | // Library functions 61 | // 62 | ////////////////////////////////////////////////////////////////////////// 63 | 64 | // Library initialization 65 | // Call this once to initialize the library functions and the ADC converter 66 | void tinytouch_init(void); 67 | 68 | // The sense function evaluates the button state and performs internal 69 | // housekeeping. It should be polled at least 30 times per second to 70 | // update the internal logic. Please note that each call performs 32 71 | // analog to digital conversions with active waiting. This may take 72 | // several ms. 73 | 74 | // Possible return values are: 75 | // tt_off=0 No touch sensed 76 | // tt_on Touch button is active and touch is sensed. 77 | // tt_push Touch button is pushed. Use this to initiate one time events. 78 | // tt_release Touch button is released. Use this to initiate one time events. 79 | // tt_timeout Touch button has been active too long and internal bias was reset. 80 | uint8_t tinytouch_sense(void); 81 | 82 | #endif -------------------------------------------------------------------------------- /TinyTouchLib.c: -------------------------------------------------------------------------------- 1 | /* 2 | * tinytouchlib.c 3 | * 4 | * Created: 09.06.2013 17:25:47 - v0.1 Initial release (ATtiny 10) 5 | * 10.06.2013 - v0.2 ported to ATtiny 25/45/85 and ATtiny13 6 | * Author: Tim (cpldcpu@gmail.com) 7 | */ 8 | 9 | 10 | #include "TinyTouchLib.h" 11 | #include 12 | 13 | // Internal function to read the adc input 14 | uint8_t tinytouch_adc(void); 15 | 16 | uint16_t bias; 17 | uint8_t touch; 18 | #if touch_timeout>0 19 | uint8_t timer; 20 | #endif 21 | /* 22 | Capacitive sensing using charge sharing between 23 | the S/H capacitor and an external sensing pad 24 | */ 25 | 26 | void tinytouch_init(void) { 27 | 28 | #ifndef __AVR_ATtiny13__ 29 | PRR &=~_BV(PRADC); 30 | #endif 31 | ADCSRA =_BV(ADEN)|_BV(ADPS2)|_BV(ADPS1); // Enable ADC, Set prescaler to 64 32 | 33 | bias=tinytouch_adc()<<8; 34 | touch=0; 35 | } 36 | 37 | uint8_t tinytouch_sense(void) { 38 | uint8_t i; 39 | uint16_t tmp; 40 | int16_t delta; 41 | 42 | tmp=0; 43 | for (i=0; i<16; i++) { 44 | tmp+=tinytouch_adc(); // average 16 samples 45 | _delay_us(100); 46 | } 47 | 48 | delta=tmp-(bias>>4); 49 | 50 | if (!touch) { 51 | if (delta>touch_threshold_on) { 52 | touch=1; 53 | #if touch_timeout>0 54 | timer=0; 55 | #endif 56 | return tt_push; 57 | } 58 | 59 | // update bias only when touch not active 60 | bias=(bias-(bias>>6))+(tmp>>2); // IIR low pass 61 | return tt_off; 62 | } else { 63 | if (delta0 69 | if (timer==touch_timeout) { 70 | bias=tinytouch_adc()<<8; 71 | return tt_timeout; 72 | } 73 | timer++; 74 | #endif 75 | return tt_on; 76 | } 77 | } 78 | 79 | uint8_t tinytouch_adc(void) { 80 | 81 | uint8_t dat1,dat2; 82 | 83 | // Precharge Low 84 | ADMUX =tt_refadc; // connect S/H cap to reference pin 85 | PORTB |= _BV(tt_refpin); // Charge S/H Cap 86 | PORTB &=~_BV(tt_sensepin); // Discharge Pad (0) 87 | DDRB |= _BV(tt_refpin)|_BV(tt_sensepin); 88 | 89 | _delay_us(32); 90 | 91 | DDRB &=~(_BV(tt_sensepin)); // float pad input, note that pull up is off. 92 | 93 | #ifdef __AVR_ATtiny10__ 94 | ADMUX =tt_senseadc; // Connect sense input to adc 95 | #else 96 | ADMUX =tt_senseadc|_BV(ADLAR); // Connect sense input to adc 97 | #endif 98 | 99 | ADCSRA |=_BV(ADSC); // Start conversion 100 | while (!(ADCSRA&_BV(ADIF))); 101 | ADCSRA |=_BV(ADIF); // Clear ADIF 102 | 103 | #ifdef __AVR_ATtiny10__ 104 | dat1=ADCL; 105 | #else 106 | dat1=ADCH; 107 | #endif 108 | 109 | // Precharge High 110 | ADMUX =tt_refadc; // connect S/H cap to reference pin 111 | PORTB &=~_BV(tt_refpin); // Discharge S/H Cap 112 | PORTB |= _BV(tt_sensepin); // Charge Pad 113 | DDRB |= _BV(tt_refpin)|_BV(tt_sensepin); 114 | 115 | _delay_us(32); 116 | 117 | DDRB &=~(_BV(tt_sensepin)); // float pad input input 118 | PORTB &=~_BV(tt_sensepin); // pull up off 119 | 120 | #ifdef __AVR_ATtiny10__ 121 | ADMUX =tt_senseadc; // Connect sense input to adc 122 | #else 123 | ADMUX =tt_senseadc|_BV(ADLAR); // Connect sense input to adc 124 | #endif 125 | 126 | ADCSRA |=_BV(ADSC); // Start conversion 127 | while (!(ADCSRA&_BV(ADIF))); 128 | ADCSRA |=_BV(ADIF); // Clear ADIF 129 | 130 | #ifdef __AVR_ATtiny10__ 131 | dat2=ADCL; 132 | #else 133 | dat2=ADCH; 134 | #endif 135 | 136 | 137 | return dat2-dat1; 138 | } 139 | 140 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ################# 2 | ## Eclipse 3 | ################# 4 | 5 | *.pydevproject 6 | .project 7 | .metadata 8 | bin/ 9 | tmp/ 10 | *.tmp 11 | *.bak 12 | *.swp 13 | *~.nib 14 | local.properties 15 | .classpath 16 | .settings/ 17 | .loadpath 18 | 19 | # External tool builders 20 | .externalToolBuilders/ 21 | 22 | # Locally stored "Eclipse launch configurations" 23 | *.launch 24 | 25 | # CDT-specific 26 | .cproject 27 | 28 | # PDT-specific 29 | .buildpath 30 | 31 | 32 | ################# 33 | ## Visual Studio 34 | ################# 35 | 36 | ## Ignore Visual Studio temporary files, build results, and 37 | ## files generated by popular Visual Studio add-ons. 38 | 39 | # User-specific files 40 | *.suo 41 | *.user 42 | *.sln.docstates 43 | 44 | # Build results 45 | 46 | [Dd]ebug/ 47 | [Rr]elease/ 48 | x64/ 49 | build/ 50 | [Bb]in/ 51 | [Oo]bj/ 52 | 53 | # MSTest test Results 54 | [Tt]est[Rr]esult*/ 55 | [Bb]uild[Ll]og.* 56 | 57 | *_i.c 58 | *_p.c 59 | *.ilk 60 | *.meta 61 | *.obj 62 | *.pch 63 | *.pdb 64 | *.pgc 65 | *.pgd 66 | *.rsp 67 | *.sbr 68 | *.tlb 69 | *.tli 70 | *.tlh 71 | *.tmp 72 | *.tmp_proj 73 | *.log 74 | *.vspscc 75 | *.vssscc 76 | .builds 77 | *.pidb 78 | *.log 79 | *.scc 80 | 81 | # Visual C++ cache files 82 | ipch/ 83 | *.aps 84 | *.ncb 85 | *.opensdf 86 | *.sdf 87 | *.cachefile 88 | 89 | # Visual Studio profiler 90 | *.psess 91 | *.vsp 92 | *.vspx 93 | 94 | # Guidance Automation Toolkit 95 | *.gpState 96 | 97 | # ReSharper is a .NET coding add-in 98 | _ReSharper*/ 99 | *.[Rr]e[Ss]harper 100 | 101 | # TeamCity is a build add-in 102 | _TeamCity* 103 | 104 | # DotCover is a Code Coverage Tool 105 | *.dotCover 106 | 107 | # NCrunch 108 | *.ncrunch* 109 | .*crunch*.local.xml 110 | 111 | # Installshield output folder 112 | [Ee]xpress/ 113 | 114 | # DocProject is a documentation generator add-in 115 | DocProject/buildhelp/ 116 | DocProject/Help/*.HxT 117 | DocProject/Help/*.HxC 118 | DocProject/Help/*.hhc 119 | DocProject/Help/*.hhk 120 | DocProject/Help/*.hhp 121 | DocProject/Help/Html2 122 | DocProject/Help/html 123 | 124 | # Click-Once directory 125 | publish/ 126 | 127 | # Publish Web Output 128 | *.Publish.xml 129 | *.pubxml 130 | 131 | # NuGet Packages Directory 132 | ## TODO: If you have NuGet Package Restore enabled, uncomment the next line 133 | #packages/ 134 | 135 | # Windows Azure Build Output 136 | csx 137 | *.build.csdef 138 | 139 | # Windows Store app package directory 140 | AppPackages/ 141 | 142 | # Others 143 | sql/ 144 | *.Cache 145 | ClientBin/ 146 | [Ss]tyle[Cc]op.* 147 | ~$* 148 | *~ 149 | *.dbmdl 150 | *.[Pp]ublish.xml 151 | *.pfx 152 | *.publishsettings 153 | 154 | # RIA/Silverlight projects 155 | Generated_Code/ 156 | 157 | # Backup & report files from converting an old project file to a newer 158 | # Visual Studio version. Backup files are not needed, because we have git ;-) 159 | _UpgradeReport_Files/ 160 | Backup*/ 161 | UpgradeLog*.XML 162 | UpgradeLog*.htm 163 | 164 | # SQL Server files 165 | App_Data/*.mdf 166 | App_Data/*.ldf 167 | 168 | ############# 169 | ## Windows detritus 170 | ############# 171 | 172 | # Windows image file caches 173 | Thumbs.db 174 | ehthumbs.db 175 | 176 | # Folder config file 177 | Desktop.ini 178 | 179 | # Recycle Bin used on file shares 180 | $RECYCLE.BIN/ 181 | 182 | # Mac crap 183 | .DS_Store 184 | 185 | 186 | ############# 187 | ## Python 188 | ############# 189 | 190 | *.py[co] 191 | 192 | # Packages 193 | *.egg 194 | *.egg-info 195 | dist/ 196 | build/ 197 | eggs/ 198 | parts/ 199 | var/ 200 | sdist/ 201 | develop-eggs/ 202 | .installed.cfg 203 | 204 | # Installer logs 205 | pip-log.txt 206 | 207 | # Unit test / coverage reports 208 | .coverage 209 | .tox 210 | 211 | #Translations 212 | *.mo 213 | 214 | #Mr Developer 215 | .mr.developer.cfg 216 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | TinyTouchLib 2 | ============ 3 | 4 | This library provides functions for touch-button sensing on ATtiny microcontrollers with integrated ADC. 5 | It implements a charge sharing approach similar to Atmels Qtouch ADC that does not require additonal external components. 6 | The library performs automatic bias adjustment to the touch button capacitance and should therefore be immune to drift. 7 | 8 | You can find an example app [here](https://github.com/cpldcpu/TinyTouchButton). 9 | 10 | #### Supported Devices #### 11 | 12 | | Device | Support | No Support | Comment | 13 | | ------------- |:-------:| :-----: | ------ | 14 | | ATtiny 4/9 | | X | No ADC | 15 | | ATtiny 5/10 | X | | | | 16 | | ATtiny 13A | X | | | 17 | | ATtiny 25/45/85 | X | | | 18 | | ATtiny 24/44/84 | | X | may be supported later | 19 | | ATtiny 261/461/861 | | X | may be supported later | 20 | 21 | 22 | ## Library Functions ## 23 | 24 | 25 | #### initialization #### 26 | 27 | ``` 28 | void tinytouch_init(void); 29 | ``` 30 | 31 | Call this once to initialize the library functions and the ADC converter. 32 | 33 | 34 | #### Sensing #### 35 | 36 | ``` 37 | uint8_t tinytouch_sense(void); 38 | 39 | Possible return values are: 40 | tt_off=0 No touch sensed 41 | tt_on Touch button is active and touch is sensed. 42 | tt_push Touch button is pushed. Use this to initiate one time events. 43 | tt_release Touch button is released. Use this to initiate one time events. 44 | tt_timeout Touch button has been active too long and internal bias was reset. 45 | 46 | ``` 47 | 48 | This function performs touch pad sensing and internal housekeeping. It should 49 | be called at least 30 times per second. Please note that each call performs 32 50 | analog to digital conversions with active waiting. This may take several ms. 51 | 52 | ## Library Configuration 53 | 54 | ``` 55 | ////////////////////////////////////////////////////////////////////////// 56 | // 57 | // User definable settings 58 | // 59 | ////////////////////////////////////////////////////////////////////////// 60 | 61 | // Define upper and lower threshold for the touch sensing. You may have to 62 | // change these depending on the geometry of your touch button. 63 | // Setting the "on" value lower will make the touch button more sensitive. 64 | // Setting the "off" value higher will make the touch button less likely 65 | // to be "stuck". Too high values can lead to oscillations. 66 | 67 | #define touch_threshold_on 60 68 | #define touch_threshold_off 20 69 | 70 | // If the touch button is pressed, the bias value is not updated 71 | // to prevent detection of multiple touches. In some cases this may 72 | // lead to a "stuck button" situation. Therefore an update of the bias 73 | // is forced after a certain time unless this function is deactivated. 74 | // 75 | // The value of timeout corresponds to the number of calls to tinytouch_sense(). 76 | // The maximum is 255. 77 | // Setting timeout to 0 will turn the functionality off. 78 | 79 | #define touch_timeout 255 80 | //#define touch_timeout 0 // turn off timeout functionality 81 | 82 | 83 | // Define pins to use for the reference input and the touch button 84 | // The reference pin is used to charge or discharge the internal 85 | // sample&hold capacitor. This pin is used in output mode and should 86 | // not be shorted to VCC or GND externally. 87 | // The sense pin is connected to the touch-button. To improve noise immunity 88 | // a series resistor can be used. 89 | 90 | // The pin number must match the corresponding analog input number ADCx. 91 | // Default port is PORTB. (ATtiny 5/10/13/25/45/85) 92 | 93 | #define tt_refpin 2 // Use PB2 as reference pin 94 | #define tt_refadc 1 // Use ADC1 as reference ADC input 95 | #define tt_sensepin 3 // Use PB3 as sense pin 96 | #define tt_senseadc 3 // Use ADC3 as sense ADC input 97 | ``` 98 | 99 | ## Example ## 100 | 101 | This example implements a touch button on PB3 to toggle a LED connected to PB4. 102 | 103 | ``` 104 | #include 105 | #include 106 | #include "tinytouchlib.h" 107 | 108 | int main(void) 109 | { 110 | CLKPR=_BV(CLKPCE); 111 | CLKPR=0; // set clock prescaler to 1 (attiny 25/45/85/24/44/84/13/13A) 112 | 113 | DDRB=_BV(PB4); // Enable LED output pin 114 | 115 | tinytouch_init(); 116 | 117 | while(1) { 118 | if (tinytouch_sense()==tt_push) {PORTB^=_BV(PB4);} // Toggle LED on touch event 119 | _delay_ms(10); 120 | } 121 | } 122 | 123 | ``` 124 | --------------------------------------------------------------------------------