waicon
Active Member
Offline
Posts: 134
Thank You
-Given: 57
-Receive: 79
I'm learning to Dream...not Relaxing...zzZ
|
|
« on: April 01, 2013, 05:28:59 05:28 » |
|
is there better way write in C to adjust resister POT from 0~47k to get 0~255 variable count? in repeatability and stable ? /* MPABX1.7; XC8; PIC16F */ #define DLYPOT_Direction TRISAbits.TRISA3 #define DLYPOT PORTAbits.RA3 DLYPOT_count=1;
void Adj_TimeDly(void) { DLYPOT_Direction=0; // state output port DLYPOT=1; // pull-up; charge-up capacitor __delay_us(10); DLYPOT_Direction=1; // input state, pin as input __delay_us(10); while(DLYPOT && DLYPOT_count) // until logiC-0 or timeup255 { _delay(1000); // 1MIPS clocks, delay 1ms DLYPOT_count++; } variable=DLYPOT_count; // variable = 1~255 DLYPOT_count=1; }
|
|
|
Logged
|
|
|
|
FTL
Junior Member
Offline
Posts: 83
Thank You
-Given: 170
-Receive: 33
|
|
« Reply #1 on: April 01, 2013, 06:07:18 06:07 » |
|
Use a PIC with an A to D converter and use it to read the voltage on the pot.
|
|
|
Logged
|
|
|
|
waicon
Active Member
Offline
Posts: 134
Thank You
-Given: 57
-Receive: 79
I'm learning to Dream...not Relaxing...zzZ
|
|
« Reply #2 on: April 01, 2013, 06:32:10 06:32 » |
|
no ADC available for this 16F chip
|
|
|
Logged
|
|
|
|
flo0319
Junior Member
Offline
Posts: 81
Thank You
-Given: 7
-Receive: 17
|
|
« Reply #3 on: April 01, 2013, 09:14:38 09:14 » |
|
you try to do an AD converter using a capacitor and a digital pin ? Controlling the current you can obtain a timing for charge your capacitor (discharge in your case) , but your circuit is a very unstable, you have a lot of noise factors (temp, magnetic field, current variation, pin input and output thresholds )
which PIC use you?
you need to use an AD converter with a digital filter (because you are interested in repeatability, this filter is just an average between a big number of samples, 256 - 1024 )
|
|
|
Logged
|
|
|
|
waicon
Active Member
Offline
Posts: 134
Thank You
-Given: 57
-Receive: 79
I'm learning to Dream...not Relaxing...zzZ
|
|
« Reply #4 on: April 01, 2013, 09:20:18 09:20 » |
|
PIC16F628A not doing ADC, turning POT min->max to get similar variable value 0~255 . as similar in PIC-basic POT command.
|
|
|
Logged
|
|
|
|
flo0319
Junior Member
Offline
Posts: 81
Thank You
-Given: 7
-Receive: 17
|
|
« Reply #5 on: April 01, 2013, 09:52:59 09:52 » |
|
in this PIC you have a comparator module and a voltage reference output, you can combine these modules to charge your capacitor with a certain current through your POT, and you need just to measure the time between start (with empty cap) and finish (when comparator module generate an interrupt). You know the output voltage, capacitor value and time for charge, now you can calculate the current through POT and after this you know which resistance was on it
|
|
|
Logged
|
|
|
|
waicon
Active Member
Offline
Posts: 134
Thank You
-Given: 57
-Receive: 79
I'm learning to Dream...not Relaxing...zzZ
|
|
« Reply #6 on: April 01, 2013, 11:10:55 11:10 » |
|
as comparator pins and a voltage reference pin, 2~3 pins counts just for 1 variable feature, pin utilization not good.
but, it give me a good idea as using one of PORTB pin pullup measure interrupt RBIF (down edge interrupt), but not sure isr orrcur during "START" to discharge or "AFTER" discharge to 0v. any input?
EDIT: weak pullup cannot apply to this discharge, as it impact on discharge curve. so. OPTION_REGbits.nRBPU = 1; /*no pullup*/
|
|
« Last Edit: April 01, 2013, 11:19:21 11:19 by waicon »
|
Logged
|
|
|
|
flo0319
Junior Member
Offline
Posts: 81
Thank You
-Given: 7
-Receive: 17
|
|
« Reply #7 on: April 01, 2013, 11:29:01 11:29 » |
|
I am not sure if I understand you!
you want to determine which is the position for your POT, right? this can be done using a MCU with AD converter (with 8 bits resolution)
using charging capacitance time, you need to use very stable values for power supply, capacitance, resistors and to know very precisely the internal pin circuit, 2 pins from the same IC can be very different in parameters, and with the same code and constant values you can have a different result on they.
|
|
|
Logged
|
|
|
|
waicon
Active Member
Offline
Posts: 134
Thank You
-Given: 57
-Receive: 79
I'm learning to Dream...not Relaxing...zzZ
|
|
« Reply #8 on: April 01, 2013, 11:35:44 11:35 » |
|
position of POT not important, but turning min --> max to get 0~255 more important.
|
|
|
Logged
|
|
|
|
pickit2
Moderator
Hero Member
Offline
Posts: 4667
Thank You
-Given: 834
-Receive: 4322
There is no evidence that I muted SoNsIvRi
|
|
« Reply #9 on: April 01, 2013, 12:30:14 12:30 » |
|
|
|
|
Logged
|
Note: I stoped Muteing bad members OK I now put thier account in sleep mode
|
|
|
robotai
Junior Member
Offline
Posts: 60
Thank You
-Given: 27
-Receive: 23
|
|
« Reply #10 on: April 01, 2013, 12:34:56 12:34 » |
|
What's the target do you want to get 0..255 measurement? To check the discharge time? Will POT be random adjust or just fixed on some value?
If you turn the RA3 to 1. Wait longer until C2 full charged. Then read and count down until RA3 become 0. Then based on the count down value to calculate delay time for each step so later measurement will in range in 0..255. Is that what you want? However, I don't think the the Schmitt Trigger in the IO port can always triggered on the same level.
|
|
|
Logged
|
|
|
|
waicon
Active Member
Offline
Posts: 134
Thank You
-Given: 57
-Receive: 79
I'm learning to Dream...not Relaxing...zzZ
|
|
« Reply #11 on: April 01, 2013, 01:43:29 13:43 » |
|
not the measurement data, is the
human interface: such as turning POT min<-->max
<to get similar digitally>
response variable: such as value 0~255 (equivalent to min<-->max)
similar to PICBASIC: POT command.
i think it is simplest way(may not be accurate-way) to let machine know that you(human) are turning something.
|
|
|
Logged
|
|
|
|
metal
Global Moderator
Hero Member
Offline
Posts: 2420
Thank You
-Given: 862
-Receive: 678
Top Topic Starter
|
|
« Reply #12 on: April 01, 2013, 01:55:02 13:55 » |
|
You need to use a timer, for example, timer1 and an I/O change interrupt, look at the datasheet where port pin characteristics are described to see at what voltage the interrupt occurs so that you correctly calculate the actual time constant related to PIC, not to your RC. Measure the time constant of your RC circuit by reading timer1 value, and divide that on 255. But this is not going to be accurate because resistance and capacitance depend on temperature. This link might help you: http://www.pic_examples.byethost3.com/capacitance_meter.html try to understand what the guy is doing, so that you can modify his idea to suite your needs.
|
|
|
Logged
|
|
|
|
flo0319
Junior Member
Offline
Posts: 81
Thank You
-Given: 7
-Receive: 17
|
|
« Reply #13 on: April 01, 2013, 02:59:59 14:59 » |
|
position of POT not important, but turning min --> max to get 0~255 more important.
min = 0 and max = 255 ? ... this is not a consequence of wiper position ?
|
|
|
Logged
|
|
|
|
waicon
Active Member
Offline
Posts: 134
Thank You
-Given: 57
-Receive: 79
I'm learning to Dream...not Relaxing...zzZ
|
|
« Reply #14 on: April 01, 2013, 03:35:36 15:35 » |
|
yes, metal, u scratch the correct place(time constant) i'm thinking for a while for RBIF. it seem like using interrupt or not , not so much difference. without isr while(DLYPOT && DLYPOT_count) // until logiC-0 or timeup255 // #define DLYPOT PORTAbits.RA3 { _delay(1000); // 4MHz.osc . 1MIPS clocks, 1 nop=1usec , total delay 1ms DLYPOT_count++; } variable=DLYPOT_count; DLYPOT_count=1;
with isr while(DLYPOT && !RBIF) // until logiC-0 or timeup255 // #define DLYPOT PORTAbits.RA3 { _delay(1000); // 4MHz.osc . 1MIPS clocks, 1 nop=1usec , total delay 1ms DLYPOT_count++; } variable=DLYPOT_count; RBIF=0; DLYPOT_count=0;
Posted on: April 02, 2013, 12:08:50 00:08 - Automerged
yes, interrupt may not be at full range of resister, some where at 1/3vcc; 2/3vcc,
|
|
« Last Edit: April 01, 2013, 03:46:33 15:46 by waicon »
|
Logged
|
|
|
|
metal
Global Moderator
Hero Member
Offline
Posts: 2420
Thank You
-Given: 862
-Receive: 678
Top Topic Starter
|
|
« Reply #15 on: April 01, 2013, 03:40:24 15:40 » |
|
which PIC are you using?
|
|
|
Logged
|
|
|
|
waicon
Active Member
Offline
Posts: 134
Thank You
-Given: 57
-Receive: 79
I'm learning to Dream...not Relaxing...zzZ
|
|
« Reply #16 on: April 01, 2013, 03:51:01 15:51 » |
|
PIC16F628A
|
|
|
Logged
|
|
|
|
metal
Global Moderator
Hero Member
Offline
Posts: 2420
Thank You
-Given: 862
-Receive: 678
Top Topic Starter
|
|
« Reply #17 on: April 01, 2013, 04:44:39 16:44 » |
|
hmmm.. you are a C guy, not basic, like me, who don't want to surrender to ready basic functions. I must help you then.. long live C..! This is how it is done, start with small capacitor values, because the way things work might blow the PIC's pin in case you use large capacitor values, better is connect the RC network to the PIC's pin thru 680R resistor, I think you have to start with 10/47/68/100nF till you get rigid results, lloking at your schematic in the first post, you did that, excellent: - drive the pin HIGH for twice the cap (only the cap) time constant. If the cap is 1nF, you have to drive the pin low for at least 1/2.pi.cap_value, result is in seconds. Things might appear slow while you are reading the post, but they are darn fast on PIC! - declare a counter, of long int type, and use NOP instructions to create real delays, say 2 NOPs for 2us. Within a while loop, check the pin status, sth like this: // Global variable, this goes outside main() long int time_elapsed;
// Our function void measure_time_const(){ // No interrupts allowed here, sorry! GIE = 0; // Charge the capacitor TRISA0 = 0; RA0 = 1; __delay_ms(3); // reset the counter here time_elapsed = 0; // Start discharging TRISA0 = 1; // Count... this loop equals 3us~4us // Loop as long as RA0 is HIGH while(RA0){ time_elapsed++; NOP(); NOP(); } // Re-Enable interrupts GIE = 1; }
This is all in my mind, I am not doing anything on real HW, but this is how I would start. Using timer1 alone in this PIC is useless to be honest with you; for is no real way to detect RA0 changing from HIGH to LOW while discharging. RB0 interrupt will not fulfill your needs in this case because it is an edge-based, not a level change-interrupt. Disabling interrupts can serve this function much more than you imagine. The less NOPs you use inside the while loop, the better resolution you get (but you are still limited to the max value long int can hold), this is why I asked you to start with small capacitor values. You might want to increase the variable resistor value to get better results in case 47K was too low for the POT. Keep me updated please. I might have some time tonight to do some experiments and display results on an LCD. Still, how often are you going to call this function, you have to decide about that too, too often and you will kill other interrupts, rarely and you will miss lots of POT movements, so you have to seek balance too. good luck
|
|
« Last Edit: April 01, 2013, 04:51:38 16:51 by metal »
|
Logged
|
|
|
|
flo0319
Junior Member
Offline
Posts: 81
Thank You
-Given: 7
-Receive: 17
|
|
« Reply #18 on: April 01, 2013, 05:32:23 17:32 » |
|
@waicon, you want something stable and with high repeatability ? or just to convert a basic function in a C function ?
Your main functionality is to obtain an input which is direct proportional with the POT position, for this you need to use an AD converter for a good quality(repeatability and stability) or you can use 2 buttons for increment or decrement the same variable , or others methods (a single buttons whit a long press for switching between inc/dec and normal press for inc/dec )
charging time measurement is useful for capacitor touch buttons
|
|
|
Logged
|
|
|
|
Ichan
Hero Member
Offline
Posts: 833
Thank You
-Given: 312
-Receive: 392
|
|
« Reply #19 on: April 01, 2013, 08:53:58 20:53 » |
|
I think R9 resistor better in series with the pot, at the bottom leg - and as the F628 has analog comparator then it can be very useful to detect the discharging time.
-ichan
|
|
« Last Edit: April 01, 2013, 08:56:07 20:56 by Ichan »
|
Logged
|
There is Gray, not only Black or White.
|
|
|
waicon
Active Member
Offline
Posts: 134
Thank You
-Given: 57
-Receive: 79
I'm learning to Dream...not Relaxing...zzZ
|
|
« Reply #20 on: April 02, 2013, 02:28:29 02:28 » |
|
what i have in mind is to get simple; easy; way convert to digitally turn-pot. which is useful for many simple application. here how i measure(theoretically) RC time-constant.
charge: 470x0.0068uf=3.2uS which i put 10us is good enough for full charge-time as 3.2us already at 71% of vcc. and 5v/470 ohm=10.6mA i think PORT can withstand it.
discharge: 47000x0.0068uf=320mS which is max. needed blocking-time for the main() loop. my main loop just a few buttons scan, which is 100ms scan each loop which i think comfortable. i think 47k abit too HI in 0.3second blocking delay.
|
|
|
Logged
|
|
|
|
flo0319
Junior Member
Offline
Posts: 81
Thank You
-Given: 7
-Receive: 17
|
|
« Reply #21 on: April 02, 2013, 09:32:13 09:32 » |
|
OK, but this is not simple, stable or repeatable.
charging and discharging of capacitance is not linear, also pin threshold for "0" and "1" vary to much and your function is very dependent of it. Using comparator module for a fixed threshold of dis/charging, you can increase measuring time precision, and you can use a single comparator like input and a number of pins like output for more POTs, you need just to scan they successively. Also you can use the output voltage reference and comparator module for implementing an AD converter, but you haven't in this mode 8bits resolution . I am not sure if you need a high resolution, turning a pot wiper with human hand I think is enough 32 or 64 (if button is big ) values, 5 or 6 bits.
for high resolution is better if you choose a MCU with AD converter.
|
|
|
Logged
|
|
|
|
waicon
Active Member
Offline
Posts: 134
Thank You
-Given: 57
-Receive: 79
I'm learning to Dream...not Relaxing...zzZ
|
|
« Reply #22 on: April 02, 2013, 11:59:03 11:59 » |
|
you have good point on "increase measuring time precisely" any simple interesting circuit share here
i was thinking using RBIF for precisely timing-flag and without while() timing-blocking, but due to it edge-based trigger it could be triggering between 2/3vcc & 1/3vcc range, edge-interrupt(RBIF) not suitable for this application, far away from good repeatability within same pin.
|
|
|
Logged
|
|
|
|
metal
Global Moderator
Hero Member
Offline
Posts: 2420
Thank You
-Given: 862
-Receive: 678
Top Topic Starter
|
|
« Reply #23 on: April 02, 2013, 12:12:11 12:12 » |
|
You can start with a large capacitor, say 10uF if you want to use RB0 INT. This will give you longer time constant because the interrupt will trigger very soon, using larger cap will give some flexibility. Are you trying on real HW?
|
|
|
Logged
|
|
|
|
waicon
Active Member
Offline
Posts: 134
Thank You
-Given: 57
-Receive: 79
I'm learning to Dream...not Relaxing...zzZ
|
|
« Reply #24 on: April 02, 2013, 01:22:48 13:22 » |
|
my prototype HW coming within 2~3 weeks soon, but from 1st post circuit is working model from other competitor.
min<-->max not necessary exactly 0~255 counts number, its only numbering reference for min/max , such as 10~127 ; 30~200 ...etc. in between min/max having 100 count number is ok.
Posted on: April 02, 2013, 10:14:34 22:14 - Automerged
real HW in attachment
|
|
|
Logged
|
|
|
|
|