Quantcast
Channel: MCS Electronics Forum
Viewing all articles
Browse latest Browse all 20748

BASCOM-AVR : Any Port of Arduino CapSense library to Bascom-AVR : REPLY

$
0
0
Hello, I tried to adopt the Arduino's CapSense library for Bascom-AVR. And modified it (removed some options like calibration). It is working well. The code is given bellow. Take 2 pins of AVR, one is send pin and the other is receive pin. Connect send pin and receive pin with a 1M resistor (more gives better result, tried with 2M). Connect an aluminium foil to the receive pin with wire. Use/call 'Capget()' function to get the result. Either call it once or more for higher resolution. In the code bellow it is called for 200 times. For bigger capacitance change the timeout value (variable Tout), default is 2000 ms. Send pin is PORTD.4, and receive pin is PORTC.0. Without aluminium foil for touch sensing, I also put a 22pF capacitor to receive pin & GND to check, value changes. Since I am not an expert, either for microcontroller or Bascom or Arduino, there might be mistake in my code. If you find any, or have suggestion to improve it, please let me know. This would help me to learn more. Thank you. Image: [img:37e2de41ba]http://farm9.staticflickr.com/8545/8613480583_951a304e10.jpg[/img:37e2de41ba] [code:1:37e2de41ba] $regfile = "m8def.dat" $crystal = 16000000 $hwstack = 40 $swstack = 16 $framesize = 32 '$sim 'Set Send Pin And Receive Pin Sendpin Alias Portd.4 'Send Pin Receivepin Alias Pinc.0 'Receive Pin Config Sendpin = Output Config Receivepin = Input Receivepin = 0 'pullup off Sendpin = 0 'output low Declare Function Capget() As Long 'Configure LCD Config Lcd = 16x2 Config Lcdpin = Pin , Db4 = Portd.5 , Db5 = Portd.6 , Db6 = Portd.7 , Db7 = Portb.0 , E = Portd.3 , Rs = Portd.2 Dim Millis As Long 'Long instead of integer, if cap is big Dim Cnt As Long Dim Tmpcnt As Long Dim Tout As Long 'Long instead of integer, if cap is big Dim Captotal As Long Dim Y As Integer Dim Samples As Integer Dim Minidelay As Integer Tout = 2000 'Timeout millisecond. Change if needed Samples = 200 'How many samples to get. Change if needed. Minidelay = 100 'uSec delay during sample. Change if needed Millis = 0 Cnt = 0 Cls Lcd "Start..." Wait 2 Cls ' Timer settings were made by Ian Dobson's AVR Timer Calculator (plus Bascom code generator). The best one. ' The calculator can be found at http://www.planet-ian.com/start_e.htm Const Timer1reload = 2000 Config Timer1 = Timer , Prescale = 8 Load Timer1 , Timer1reload On Ovf1 Timer1_isr 'enable timer1 'start timer1 Enable Interrupts ''///////////Main////////// Main: Cnt = 0 Tmpcnt = 0 For Y = 1 To Samples Tmpcnt = Capget() If Tmpcnt < 0 Then Cnt = Tmpcnt Exit For Else Cnt = Cnt + Tmpcnt End If Next Y 'Cnt = Capget() Lcd Cnt Wait 1 Cls Goto Main ''///////////End of Main////////// Timer1_isr: Load Timer1 , Timer1reload Millis = Millis + 1 Return Function Capget() As Long 'Local Bitdelay As Long Millis = 0 'Make zero Captotal = 0 Load Timer1 , Timer1reload 'Load timer pre value Enable Timer1 'Timer1 enable Start Timer1 'Timer1 start Sendpin = 1 'output high send pin While Receivepin = 0 And Millis < Tout 'When rcv pin is low, and not timeout Captotal = Captotal + 1 Wend If Captotal >= Tout Then 'timeout occured Disable Timer1 'Timer1 disable Stop Timer1 'Timer1 stop Millis = 0 'Make zero Capget = -1 'Error Exit Function End If ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' '''Following lines can be removed from here to '''A Delay (this can be removed). Makes reading stable. Stop timer, as there is waitms. Millis will remain same as timer is stopped Disable Timer1 'Timer1 disable Stop Timer1 'Bitdelay = Captotal / 4 'Make 4 or 2 or other. If larger number Bitdelay is, it takes a while to output on LCD 'Waitms Bitdelay Waitus Minidelay '100 uSec.Good result so far. Load Timer1 , Timer1reload 'Load timer pre value Enable Timer1 'Timer1 enable Start Timer1 'Timer1 start '''Stop Delay. Timer starts. Millis will increase '''Lines can be removed upto here. But result is not good. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Sendpin = 0 'output low send pin, make it as GND While Receivepin = 1 And Millis < Tout 'When rcv pin is high, and not timeout Captotal = Captotal + 1 Wend If Captotal >= Tout Then 'timeout occured Disable Timer1 'Timer1 disable Stop Timer1 'Timer1 stop Millis = 0 'Make zero Capget = -2 'Error Exit Function End If Disable Timer1 'Timer1 disable Stop Timer1 'Timer1 stop Capget = Captotal End Function [/code:1:37e2de41ba]

Viewing all articles
Browse latest Browse all 20748

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>