Fratello,
I would suggest downloading and looking at the original for 18F PICs here:
http://www.picbasic.co.uk/forum/show...271#post130271
Robert
Fratello,
I would suggest downloading and looking at the original for 18F PICs here:
http://www.picbasic.co.uk/forum/show...271#post130271
Robert
My Creality Ender 3 S1 Plus is a giant paperweight that can't even be used as a boat anchor, cause I'd be fined for polluting our waterways with electronic devices.
Not as dumb as yesterday, but stupider than tomorrow!
Tried hard to solve this problem, using a timer ... but without succes.
Any advice will be very appreciated !
Code:@ __config _XT_OSC & _WDT_OFF & _PWRTE_ON & _MCLRE_ON & _BODEN_ON DEFINE OSC 4 CMCON = 7 TRISIO = %00001011 INTCON = 0 IOC = 0 GPIO = 0 ANSEL = %00110001 ADCON0.7 = 1 nr var word adca var word adcb var word ADCValue var WORD ' Final ADC Result CounterA var BYTE ' Just a BYTE Temporary working variable DataW var WORD ' Just a WORD Temporary working variable RawData var word [10] ' Array holding ADC Result TIP_press VAR BIT j VAR word GPIO.2 = 0 GIE VAR INTCON.7 GPIE VAR INTCON.3 GPIF VAR INTCON.0 Timer1 var word EXT :@Timer1 = TMR1L TMR1ON VAR T1CON.0 TMR1IF VAR PIR1.0 LONGPRESS VAR BIT T1CON = %00110100 T1COUNT VAR BYTE GIE =0 GPIE=0 IOC.0 = 1 ok var bit ;============================================================================ Main: ok=0 tip_press=0 TIMER1 =3037 T1COUNT=0 LONGPRESS=0 nr=1 ADCON0 = %10000001 Pauseus 50 ADCON0.1 = 1 While ADCON0.1=1:Wend DataW.HighByte=ADRESH DataW.LowByte=ADRESL if dataw < 631 then TMR1ON=1 pause 1 WHILE dataw < 631 T1COUNT=T1COUNT + 1 CALL CHECK WEND IF LONGPRESS = 1 THEN TIP_press = 1 ELSE TIP_press = 0 ENDIF T1COUNT = 0 pause 1 call achizitie endif gosub efectuez gpif = 0 GOTO MAIN achizitie: for nr = 1 to 2 For CounterA=0 to 9 ADCON0 = %10000001 Pauseus 50 ' Wait for channel to setup ADCON0.1 = 1 ' Start conversion While ADCON0.1=1:Wend ' Wait for conversion DataW.HighByte=ADRESH ' Read variable from ADC and save DataW.LowByte=ADRESL RawData(CounterA)=DataW Next CounterA CounterA=0 Gosub Getsort next nr return CHECK: IF TMR1IF then TMR1IF = 0 Timer1 = 3037 T1Count = T1Count + 1 if T1Count = 2 then TMR1ON = 0 T1Count = 0 LongPress = 1 endif ENDIF RETURN ;============================================================================ GetSort: If RawData(CounterA+1) < RawData(CounterA) then DataW=RawData(CounterA) RawData(CounterA)=RawData(CounterA+1) RawData(CounterA+1)=DataW If CounterA>0 then CounterA=CounterA-2 endif CounterA=CounterA+1 If CounterA<10 then goto GetSort DataW=0 For CounterA=4 to 7 DataW=DataW+RawData(CounterA) Next CounterA ADCValue=DataW>>2 if nr = 1 then adca = adcvalue else adcb = adcvalue endif if adcb=adca then ok = 1 Return ;============================================================================ efectuez: if ok=1 then IF adcb > 300 AND adcb < 350 THEN if tip_press = 1 then gpio.2 = 1 : pause 2000 : gpio.2 = 0 else gpio.2 = 1 : pause 500 : gpio.2 = 0 endif endif endif Return ;============================================================================![]()
Why do you insist on sorting the ADC readings? What problem is that trying to solve?
I'd get rid of the "bubble sort" and try starting with Richard's code.
The schematic is working into "hard" environment ... I tried different variants before this. THIS is the only way to have accurate readings of ADC.
I see now... the code isn't just a bubble sort. It sorts a set of adc readings and then averages the "inner most" set of them which would tend to exclude the outliers.
What if you take this portion of Richard's codeand replace that with the result of your existing sort/average routine?Code:chk_sw: ADCON0 = %10000001 ;ch0 Pauseus 50 ' Wait for channel to setup ADCON0.1 = 1 ' Start conversion While ADCON0.1=1:Wend ' Wait for conversion DataW.HighByte=ADRESH ' Read variable from ADC and save DataW.LowByte=ADRESL
Already tried ... it's act like I always press SHORT !
the dataset you are trying to infer information from consists of two pieces of data
1. the adc reading
2. the timestamp of the reading
The periods you are interested in are between .2 and 1 sec. if you sample the input volts at 200 times / sec (5mS)
then you need to store at least 200 samples and their timestamps . when you have those samples you could analyse the dataset
for press/longpress data. you need to understand that if you sort the data on the adc value then the time stamp value needs to be included in the sort otherwise the data becomes useless [all the time info is lost].
If the data is so noisey that my method fails the the above method could work but it would not be possible on a 12f675 [insufficient ram]
lowering the input impedence, altering the sample rate or increasing the "pressed"/"not pressed" tolorance may help.
Write a pgm to capture and print the adc reading at a 5mS sample rate .then you can examine the data statistically for a fundamental noise frequency that may be possible to filter out or maybe some other ways to mitigate the noise problem. without real data i'm just speculating.
my method could also be turned around to count the number of "valid" reads in a period rather than the number of "periods" of valid reads ,
which may perform better in noisy environments
Bookmarks