Code to control output On and Off pulse times with pots

hippy

Technical Support
Staff member
If you intend to use the same Picaxe measure the flywheel velocity and to also control a Brushless DC motor with precision it may be
somewhat tricky due to the processor overhead.
It might be time to specify exactly what the project is and how it is intended to work, what sort of range it has to cover, and how accurately.

Generating fixed frequency pulse trains can be done by tweaking PAUSEUS values but the values are usually hand-tuned by trial and error while observing the outputs on a scope or logic analyser.

One would probably have to come up with some sort of look-up table and means of calculating the values if one wanted variable output frequencies. I am not sure how best that would be done or how good the results would be.

Adding something into the mix to allow that to be adjusted from something else would also be a challenge as that itself will affect the timing and values needed. As Goeytex says; could be tricky on a single PICAXE.

It could still be tricky with two PICAXE's, one indicating what is required, the other actually delivering that. One would need to find a way of getting data into the second and having it used without disrupting what it's doing, it's timing in any way. There are some possibilities but they would work best with byte values, not anything larger.
 

Goeytex

Senior Member
Yes, a complete and detailed description of the project is necessary in order for anyone to provide good advise.

Honestly, I see no way with a single Picaxe to to measure flywheel velocity and to use that data as feedback to precisely regulate the speed of a BLDC motor, assuming that is what the OP intends to do. But I don't like to assume.

The code I have simply uses pulsin to measure flywheel velocity and. If the velocity is too high it signals a fault and kills the ignition on a gasoline engine until the RPM falls back within range. (A rev limiter for racing karts). This would not be suitable for regulating the speed of an electric motor uner varying load conditions. But it could be adapted to turn a BLDC Motor off at a certain (programmable) RPM limit.
 

hippy

Technical Support
Staff member
Honestly, I see no way with a single Picaxe to to measure flywheel velocity and to use that data as feedback to precisely regulate the speed of a BLDC motor
It might be possible to have the PICAXE bit-bang control the motor using PAUSEUS set from a variable, and have an input to the PICAXE which can adjust that variable from some input and do that with zero or minimal disruption to the timing loop. Could be hard to achieve though.

Then a second PICAXE could measure the speed of the motor and compare it to some required or reference value and determine if the motor speed needs increasing or decreasing, tell the motor controller to do that. This second PICAXE wouldn't necessarily need to have consistent timing.

In fact, that's probably how I would go about automatically determining the PAUSEUS values automatically for a desired frequency output without having to hand tune it. In that case though it wouldn't need to be zero impact as it's seeking correct values to use, not caring about disruption when changing timing.

Doing it well would be quite some challenge I would have thought. And it would depend on speed ranges, what responsiveness and accuracy were desired, and one would have to overcome or accept speed oscillations or hunting. It's effectively creating a kind of PID loop on a PICAXE.
 

Visser

Well-known member
Don't worry about that. The main aim is to solve any problems there may be, get things working. There always will be wrong alleys gone down.
I can't see any reason it wouldn't, other then the timing maybe not being entirely accurate.

It would help if you could explain in what way it doesn't work, whether it's just the timing which is out or something else.

If it's just a matter of timing then there will be two things at play here; that "PAUSE 10" may not give an entirely accurate 10ms period, and it and the other commands will also have some execution overhead. So your "High B.5 : Pause 10: Low B.5" time will be longer than 10ms, giving less than 50Hz. There's also a slight overhead in the DO ... LOOP which will make the two periods slightly unbalanced.

Some slight delay between turning off B.5/B.6 before turning the other on can be desirable as this avoids shoot-through if it's a H-Bridge circuit. Shoot-through is where both high and low side switches are active at the same time which shorts the +V to 0V which one doesn't want.

The key to getting the timing right and avoiding shoot-through is in the ordering of commands and tweaking the delay times, best done by PAUSEUS rather than PAUSE -
Code:
#Picaxe 18M2
#No_Data
Do
  Low B.5 : High B.6 : PauseUs 869
  Low B.6 : High B.5 : PauseUs 787
Loop
That seemed quite reasonable when I tried it but could do with more tweaking to get each high and low the same length and the frequency to be exactly 50Hz.

Uisng SETFREQ M32 gives much finer control over the timing as each PAUSEUS unit becomes 1.25us rather than 10us. Though one might then have to put a PAUSEUS between the LOW and HIGH to avoid shoot-through. More rough and ready than at 4MHz -
Code:
#Picaxe 18M2
#No_Data
SetFreq M32
Do
  Low B.5 : High B.6 : PauseUs 8000
  Low B.6 : High B.5 : PauseUs 7800
Loop
[/QUOTE



Good day. I tried the code above. The last one gives problems on the pic when driving the H bridge inverter. The 2 nd last one works good
Will it be possible to do code to introduce dead times like in my picture?
I need that dead times to allow the reverse inductive pulse from the transformer to discharge through the mosfet build in diode before the other 2 mosfets of the H bridge switch. I tried but just dont succeed
picaxe timing.jpg
 
Last edited:

Visser

Well-known member
I seem to get it right using the pwm wizard but would like to use B5 and B6 outputs And this is 61Hz It cannot do PWM at 50Hz
It seems like nobody has an answer for me
I was also wondering if Picaxe can do SPWM signals for a modified sine wave inverter or should I rather try Arduino?
 

AllyCat

Senior Member
Hi,

Probably no answers because you're largely asking for the "impossible". The PWM outputs are generated by the base PIC hardware with the well-known limitations that the pin functions are fixed and the minimum frequency is just above 60 Hz (at SETFREQ M4). But there are a few "work-arounds", depending on your overall requirements. Firstly, the 18M2 is not a good choice with only two PWM named output pins, the 14M2 and 20M2 have 4 PWM outputs and possibly also some HPWMOUTs. There is also the DSM module which can "copy" some internal/input signals to its output, but that won't help if you've already "fixed" which pins you want to use for the PWM outputs. Finally, you could generate a 50 Hz output by using SETFREQ M2 (use the Wizard for 100 Hz, M4), but that will compromise the general processing speed.

As for SPWM, it very much depends what switching frequency you want to use and the duty cycle update rate (which is one of the factors that will determine the "purity" of the sine-wave). There are certainly "challenges" in using a PICaxe (or probably almost any microcontroller): You might be able to update the "sine" value by reading a lookup table about every millisecond, (but not using PWMDUTY, which is too slow) and you may need some clever coding to keep the frequency close to 50 Hz. Of course, most PICaxe chips have at least one Comparator, so in principle you could generate SPWM from the comparator by using a PWM (HF switching) output to generate a triangle wave and the "program" to synthesise a sine wave from a combination of lookup table, analogue filtering and timer/polling/interrupt, etc.. Maybe easier on an Arduino, maybe not......

Cheers, Alan.
 

Visser

Well-known member
Hi,

Probably no answers because you're largely asking for the "impossible". The PWM outputs are generated by the base PIC hardware with the well-known limitations that the pin functions are fixed and the minimum frequency is just above 60 Hz (at SETFREQ M4). But there are a few "work-arounds", depending on your overall requirements. Firstly, the 18M2 is not a good choice with only two PWM named output pins, the 14M2 and 20M2 have 4 PWM outputs and possibly also some HPWMOUTs. There is also the DSM module which can "copy" some internal/input signals to its output, but that won't help if you've already "fixed" which pins you want to use for the PWM outputs. Finally, you could generate a 50 Hz output by using SETFREQ M2 (use the Wizard for 100 Hz, M4), but that will compromise the general processing speed.

As for SPWM, it very much depends what switching frequency you want to use and the duty cycle update rate (which is one of the factors that will determine the "purity" of the sine-wave). There are certainly "challenges" in using a PICaxe (or probably almost any microcontroller): You might be able to update the "sine" value by reading a lookup table about every millisecond, (but not using PWMDUTY, which is too slow) and you may need some clever coding to keep the frequency close to 50 Hz. Of course, most PICaxe chips have at least one Comparator, so in principle you could generate SPWM from the comparator by using a PWM (HF switching) output to generate a triangle wave and the "program" to synthesise a sine wave from a combination of lookup table, analogue filtering and timer/polling/interrupt, etc.. Maybe easier on an Arduino, maybe not......

Cheers, Alan.
Thank you Alan
I will try more and post here
 

Visser

Well-known member
I don't know just what you are trying to do [switch high power MOSFETs?] but I recalled Don Lancaster's work started many years ago https://www.tinaja.com/mssamp1.shtml which might interest you.
Thank you I will check it out. Yes H bridge igbt switching through isolated power supply and mosfet drivers. Square wave inverter. Im investigating to see what extra battery life I can get with ac load recovery back to the batteries
 

Visser

Well-known member
Thank you I will check it out. Yes H bridge igbt switching through isolated power supply and mosfet drivers. Square wave inverter. Im investigating to see what extra battery life I can get with ac load recovery back to the batteries
Oh and Im also studying how to build a pure sine wave inverter. Your link will help a lot
 

Visser

Well-known member
Uisng SETFREQ M32 gives much finer control over the timing as each PAUSEUS unit becomes 1.25us rather than 10us. Though one might then have to put a PAUSEUS between the LOW and HIGH to avoid shoot-through. More rough and ready than at 4MHz -
Code:
#Picaxe 18M2
#No_Data
SetFreq M32
Do
  Low B.5 : High B.6 : PauseUs 8000
  Low B.6 : High B.5 : PauseUs 7800
Loop
Hi Hippy
I am trying to make this last code working as strange things happens and the pic goes nuts when I use it
You said to "put a PAUSEUS between the LOW and HIGH to avoid shoot-through"
Do you mean like this?
Do
Low B.5 : High B.6 : PauseUs 8000
PauseUs ?
Low B.6 : High B.5 : PauseUs 7800
Loop

Regards
Vissie
 

AllyCat

Senior Member
Hi,

I've not looked back through the thread to see what configuration you're using (even if it's shown), but I expect that hippy meant: LOW B.5 : PAUSEUS xx : HIGH B.6 . But I'm not convinced that would help if "shoot through" is a problem. More important might be whether you use LOW B.5 : HIGH B.6 or HIGH B.6 : LOW B.5 etc. specifically at each instance.

To avoid shoot through, I would normally expect to see something like: INPUT B.5 : HIGH B.6 : LOW B.5 to first tri-state one output, then switch the other and then turn on the first. But that's all very low-frequency stuff; for proper PWM / Power conversion, I'd be looking at HPWM which includes a configuarable delay to avoid shoot through currents.

Cheers, Alan.
 

Visser

Well-known member
Hi,

I've not looked back through the thread to see what configuration you're using (even if it's shown), but I expect that hippy meant: LOW B.5 : PAUSEUS xx : HIGH B.6 . But I'm not convinced that would help if "shoot through" is a problem. More important might be whether you use LOW B.5 : HIGH B.6 or HIGH B.6 : LOW B.5 etc. specifically at each instance.

To avoid shoot through, I would normally expect to see something like: INPUT B.5 : HIGH B.6 : LOW B.5 to first tri-state one output, then switch the other and then turn on the first. But that's all very low-frequency stuff; for proper PWM / Power conversion, I'd be looking at HPWM which includes a configuarable delay to avoid shoot through currents.

Cheers, Alan.
Hi Alan
Thank you for your response .
You said to use "something like: INPUT B.5 : HIGH B.6 : LOW B.5"
I am sure how to use that
If I look in the PDFs it state that Input command, Make pin an input
I use an H bridge mosfet switch for a simple square wave inverter at 50 Hz
Regards
Vissie
 

AllyCat

Senior Member
Hi,

It depends how B.5 and B.6 are actually connected to the "H bridge". Also, I don't understand why hippy thought that a (long) delay would be needed to prevent "shoot through" (so we may need to be patient). There will already be a delay of around 400us (less at higher PICaxe clock frequencies) between a HIGH or LOW and a subsequent LOW or HIGH command, so I don't see that another few hundred microseconds would make much difference.

So yes, an INPUT (i.e "tri-state") command is probably not appropriate; but if, for example, B.5 were a "Direction" pin and B.6 an "Enable" pin, then you might use a sequence like : LOW B.6 (disable) : HIGH (or toggle) B.5 : HIGH B.6 (enable).

Cheers, Alan.
 

Visser

Well-known member
Hi,

I've not looked back through the thread to see what configuration you're using (even if it's shown), but I expect that hippy meant: LOW B.5 : PAUSEUS xx : HIGH B.6 . But I'm not convinced that would help if "shoot through" is a problem. More important might be whether you use LOW B.5 : HIGH B.6 or HIGH B.6 : LOW B.5 etc. specifically at each instance.

To avoid shoot through, I would normally expect to see something like: INPUT B.5 : HIGH B.6 : LOW B.5 to first tri-state one output, then switch the other and then turn on the first. But that's all very low-frequency stuff; for proper PWM / Power conversion, I'd be looking at HPWM which includes a configuarable delay to avoid shoot through currents.

Cheers, Alan.
I would like to know how to use the HPWM command but that PDF instruction is a nightmare
 

tmfkam

Senior Member
I never got my head around generating PWM to drive a full bridged output. I tried, but failed.

In the end I built my 240V AC 50Hz to 240V AC 25Hz - 250Hz frequency inverter using a full hardware solution. No processors at all.

Great fun was had in the development stages. A lot of FETs were lost on the electronic battlefield!
 

Visser

Well-known member
Hi
I need to control the speed of a small dc motor with PWM USING A PICAXE 08M2
I know how to use the pwm wizard but have no idea how to make it so that the pulse width varies as voltage over a 12V battery changes
ie: pwmout C.2, 49, 99 ; 20000Hz at 50% @ 4MHz is what the wizard give me for 20Khz 50% duty
But that cannot change as the adc input change that comes from a resistor network that sits parallel over the battery. Or
a 10v zener and resistor So over the resistor the voltage will vary from 0v to 5V if the battery voltage is from 10v to 15v
When the battery voltage is 10V there will be a voltage drop of 0v over the resistor and I want to feed that to a adc input and pwm output must be 20Khz 100% duty(always on)
When the battery voltage is 11V there will be a voltage drop of 1v over the resistor and I want to feed that to a adc input and pwm output must be 20Khz 90% duty
When the battery voltage is 12V there will be a voltage drop of 2v over the resistor and I want to feed that to a adc input and pwm output must be 20Khz 70% duty
Etc: 13v 50% 14v 30% till 15v that must be 5v and 20Khz 10% duty
Any ideas please . These are from wizard but will not vary
pwmout C.2, 49, 19 ; 20000Hz at 10% @ 4MHz at 15V
pwmout C.2, 49, 59 ; 20000Hz at 30% @ 4MHz at 14V
pwmout C.2, 49, 99 ; 20000Hz at 50% @ 4MHz at 13V
pwmout C.2, 49, 139 ; 20000Hz at 70% @ 4MHz at 12V
pwmout C.2, 49, 179 ; 20000Hz at 90% @ 4MHz at 11V
pwmout C.2, 49, 199 ; 20000Hz at 100% @ 4MHz at 10V
The way i would like it to work is for the pulse width to change linear 100% to 1% as the voltage change from 0 to 5v

IMG_6842.JPG
 
Last edited:

AllyCat

Senior Member
Hi,

How have you determined that those are the percentage Duty Cycles that you need? How do you want the motor speed to change over the range of 10 - 15 volts? For constant speed of the motor, I would expect the PWM to fall from 100% to about 66% at 15v. With the values you've quoted, the motor would completely stop at close to 15 volts (certainly before 16 v).

However, to achieve what you've specified, then with a 5 volt PICaxe supply rail; 0v at the ADC input will return a READADC value of 0 , and 5 volts input will read 255. The percentage values you've quoted don't change uniformly (sometimes by 10%, sometimes 20% per volt change) so we can't write a simple mathematical expression. But we can calculate the integer voltages you've quoted by dividing the ADC value by 51, e.g. 255 / 51 = 5 volts. Then you can use a LOOKUP integerADCvalue, (199,179,139,99,59,19), DutyCycle followed by the PWMOUT C.2, 49, DutyCycle .

Cheers, Alan.
 

Visser

Well-known member
Hi,

How have you determined that those are the percentage Duty Cycles that you need? How do you want the motor speed to change over the range of 10 - 15 volts? For constant speed of the motor, I would expect the PWM to fall from 100% to about 66% at 15v. With the values you've quoted, the motor would completely stop at close to 15 volts (certainly before 16 v).

However, to achieve what you've specified, then with a 5 volt PICaxe supply rail; 0v at the ADC input will return a READADC value of 0 , and 5 volts input will read 255. The percentage values you've quoted don't change uniformly (sometimes by 10%, sometimes 20% per volt change) so we can't write a simple mathematical expression. But we can calculate the integer voltages you've quoted by dividing the ADC value by 51, e.g. 255 / 51 = 5 volts. Then you can use a LOOKUP integerADCvalue, (199,179,139,99,59,19), DutyCycle followed by the PWMOUT C.2, 49, DutyCycle .

Cheers, Alan.
Thank you for the insight. I didnt think it through properly. Yes I would like it to be uniformly. When battery reach 12v the puls should be 100% duty (full on). And when battery reach 15v about 10% duty. i will have to work out with a resistor voltage divider how to get 0 to 5V over the battery 12 to 15v range
 

AllyCat

Senior Member
Hi,

First we need to consider the hardware, I assume the PICaxe is supplied from an accurate 5 volt supply (e.g. a 7805 regulator) because that is the reference voltage for the ADC. If it is not accurate then we could use the internal Fixed Voltage Reference (FVR) but that's more complicated.

The zener diode is a "non linear device" (which can make calculations more difficult), so I would replace it by a 3k resistor (i.e. 3 times the lower resistor) to divide the battery voltage by 4. But then use READADC10 which multiplies the regular READADC value by 4, that happens to give a value very similar to using the zener. Thus, the "full scale" voltage is 20 volts (i.e. 5 volts x 4) and the corresponding ADC value is 1024 (or strictly the maximum 10 bit value is 1023).

The mathematical calculation is not too difficult but a graph of the required "transfer characteristic" might be helpful. The horizontal (X) axis is calibrated with the supply voltage and the expected ADC values. (note the scale below 10 volts has been "squashed" to save some space) The vertical (Y) axis shows the required PWM percentages and the corresponding PWMOUT values (at the Right Hand Side):
Code:
     ^
     | Duty Cycle                                   \                             PWM
100% +-----------------------------------------------@............................200
     |                                               :\
 90% |                                               : \
     |                                               :  \
 80% |                                               :   \
     |                                               :    \
 70% |                                               :     \   
     |                                               :      \
 60% |                                               :       \
     |                                               :        \
 50% |...............................................;.........\..................100
     |                                               :          \
 40% |                                               :           \
     |                                               :            \
 30% |                                               :             \
     |                                               :              \
 20% |                                               :               \
     |                                               :                \
 10% |...............................................;.................@...........20
     |                                               :                 :\
  0% +--+--+--+--+---+---+---+---+---+---+-----+-----+-----+-----+-----+-#--+---%---+------>
     0v 1v 2v 3v 4v  5v  6v  7v  8v  9v 10v    11v  12v   13v   14v   15v  16v     20v   Voltage 
     0              256                 512         614               768          1024   ADC10
                                         0 <---------Low Byte-------> 256
The two "required" points are marked with @ symbols, joined by a diagonal straight line and extended to meet the X axis (at the #). The gradient (or slope) of the line is quite convenient at 30% (duty cycle) per volt and normally will be given a negative sign as it slopes "backwards" (i.e. the duty cycle output reduces for an increasing input voltage). It's quite easy to convert between the two scales on each axis; from Duty Cycle % to PWM we simply multiply by 2 (in this example) and from ADC value to volts we divide by 51 (i.e. Volts = ADC * 20 / 1024). Also, the gradient of the line tells us that an increase of 1 volt requires the PWM to reduce by 30% or 60 units of duty cycle.

Therefore, to calculate the duty cycle value we first subtract 614 from the ADC10 value and multiply by the transfer characteristic, i.e. 60 units / 51 (adc / volt). Finally we subtract the calculated duty cycle from 200, because the value reduces from the 12v @ 100% point. Thus the (untested) program calculation could be:
Code:
   READADC10 adcpin , w1
   w2 = w1 - 614 * 60 / 51
   w1 = 200 - w2
   PWMOUT pwmpin , 49 , w1    ; 20 kHz
Cheers, Alan.
 

tmfkam

Senior Member
Hi,

First we need to consider the hardware, I assume the PICaxe is supplied from an accurate 5 volt supply (e.g. a 7805 regulator) because that is the reference voltage for the ADC. If it is not accurate then we could use the internal Fixed Voltage Reference (FVR) but that's more complicated.

The zener diode is a "non linear device" (which can make calculations more difficult), so I would replace it by a 3k resistor (i.e. 3 times the lower resistor) to divide the battery voltage by 4. But then use READADC10 which multiplies the regular READADC value by 4, that happens to give a value very similar to using the zener. Thus, the "full scale" voltage is 20 volts (i.e. 5 volts x 4) and the corresponding ADC value is 1024 (or strictly the maximum 10 bit value is 1023).

The mathematical calculation is not too difficult but a graph of the required "transfer characteristic" might be helpful. The horizontal (X) axis is calibrated with the supply voltage and the expected ADC values. (note the scale below 10 volts has been "squashed" to save some space) The vertical (Y) axis shows the required PWM percentages and the corresponding PWMOUT values (at the Right Hand Side):
Code:
     ^
     | Duty Cycle                                   \                             PWM
100% +-----------------------------------------------@............................200
     |                                               :\
90% |                                               : \
     |                                               :  \
80% |                                               :   \
     |                                               :    \
70% |                                               :     \  
     |                                               :      \
60% |                                               :       \
     |                                               :        \
50% |...............................................;.........\..................100
     |                                               :          \
40% |                                               :           \
     |                                               :            \
30% |                                               :             \
     |                                               :              \
20% |                                               :               \
     |                                               :                \
10% |...............................................;.................@...........20
     |                                               :                 :\
  0% +--+--+--+--+---+---+---+---+---+---+-----+-----+-----+-----+-----+-#--+---%---+------>
     0v 1v 2v 3v 4v  5v  6v  7v  8v  9v 10v    11v  12v   13v   14v   15v  16v     20v   Voltage
     0              256                 512         614               768          1024   ADC10
                                         0 <---------Low Byte-------> 256
The two "required" points are marked with @ symbols, joined by a diagonal straight line and extended to meet the X axis (at the #). The gradient (or slope) of the line is quite convenient at 30% (duty cycle) per volt and normally will be given a negative sign as it slopes "backwards" (i.e. the duty cycle output reduces for an increasing input voltage). It's quite easy to convert between the two scales on each axis; from Duty Cycle % to PWM we simply multiply by 2 (in this example) and from ADC value to volts we divide by 51 (i.e. Volts = ADC * 20 / 1024). Also, the gradient of the line tells us that an increase of 1 volt requires the PWM to reduce by 30% or 60 units of duty cycle.

Therefore, to calculate the duty cycle value we first subtract 614 from the ADC10 value and multiply by the transfer characteristic, i.e. 60 units / 51 (adc / volt). Finally we subtract the calculated duty cycle from 200, because the value reduces from the 12v @ 100% point. Thus the (untested) program calculation could be:
Code:
   READADC10 adcpin , w1
   w2 = w1 - 614 * 60 / 51
   w1 = 200 - w2
   PWMOUT pwmpin , 49 , w1    ; 20 kHz
Cheers, Alan.
Sublime.
 

Visser

Well-known member
Wow! That's a lot of trouble from your side. Thank you
I will try my best to study this and understand it
I'm using a 5V regulator for pic supply
It will be a while before testing it properly but I will be back with result
Thank you
 

Visser

Well-known member
I think I get it
Will it need a do and loop command?
#Picaxe 08M2
#No_Data
Do
READADC10 C.4 , w1
w2 = w1 - 614 * 60 / 51
w1 = 200 - w2
PWMOUT C.2 , 49 , w1 ; 20 kHz
Loop
 

lbenson

Senior Member
You may need some protection against an invalid ADC read, perhaps as little as:
Code:
Do
  READADC10 C.4 , w1
  IF w1 >= 614 then
    w2 = w1 - 614 * 60 / 51
    w1 = 200 - w2
    PWMOUT C.2 , 49 , w1 ; 20 kHz
  endif
Loop
 

AllyCat

Senior Member
Hi,
Will it need a do and loop command?
If the battery voltage is continuously changing and you want the PWM value to "follow" it, then Yes (but otherwise the PWM will keep running with the initial value anyway). Updates are probably not required very frequently, so there could be a long Pause in the loop.

Yes, I did wonder about setting limits to the ADC/PWM value, but as I don't know what is the purpose, I didn't know if it was needed. Probably the simplest way to avoid a calculation "Underflow" (if the ADC value is less than 614) or "Overflow", is to change the one line in the equation to: w2 = w1 MIN 614 - 614 * 60 / 51 MAX 200.

Cheers, Alan.
 

Visser

Well-known member
READADC10 C.4 , w1
w2 = w1 - 614 * 60 / 51
w1 = 200 - w2
PWMOUT C.2 , 49 , w1 ; 20 kHz

I don't get this part. No matter how I calculate it doesn't make sense to me
for 12v on battery there will be 9v over 3k and 3v over 1k resistor. so READADC10 C.4 =12= w1?(READADC10 which multiplies the regular READADC value by 4 )
trying to fit the value of 12 into the next 2 command is dutch to me
 

AllyCat

Senior Member
Hi,

The ADC (10 bits) divides the Reference Voltage (5v) into 1024 "steps", where each step = 5.0 / 1024 = 4.88 mV. Thus with 3.0 volts input, the ADC reading is 3000 / 4.88 = 614. The "normal" (8-bit) READADC full-scale value is 255 (actually 256 -1) and the 10-bit full-scale value is 1023 (i.e. 1024 -1) which is 4 times larger.

Cheer, Alan.
 

Visser

Well-known member
Hi
I'm busy trying to test on a 18M2 test board with isolated mosfet drivers
After setting up the voltage divider with 3k and 1k resistors I connected it to an adjustable supply. The voltages over the 1k that has to go to the adc input measure as expected but the moment I connect it to pixaxe gnd and any of the adc inputs the voltage drops. 3v drops to about 2.8V So I made the voltage divider resistors smaller using 1k5 and 500 ohm to be able to supply more current to the adc but the same still happens. With this divider there should be 6ma available to go to adc. The pdf only states:
The ADC range is the power supply voltage range. The maximum recommended
input impedance is 20k.
I'm not to sure how to solve this. Maybe using even lower value resistors?
 

Visser

Well-known member
well I ignored the above and tested your code. It works beautifully!
#Picaxe 18M2
#No_Data
Do
READADC10 C.1 , W1
w2 = w1 MIN 614 - 614 * 60 / 51 MAX 200
W1 = 200 - W2
PWMOUT B.6 , 49 , w1 ; 20 kHz
loop

I wish I could post a video so you can see. I will post some photos
Under 12V it 100% duty
As soon as it reaches 12V the signal appears on the scope and as voltage goes up the duty cycle goes down till it reach about 15,9v where the pulsing stop and all is 0v

Thank you so much.
 

Attachments

Visser

Well-known member
Hi
If I look at the manual it looks as if I can use READADC10 on picaxe 08M . Is that so or must it be 08M2?
 

AllyCat

Senior Member
Hi,

Yes, if the PICaxe Editor (with a #define 08M at the top) accepts it without reporting a syntax error (which it appears to do) then you should be able to use READADC10 <ADC pin number> , <Wordvariable> with an 08M.

Cheers, Alan.
 

Visser

Well-known member
Hi,

Yes, if the PICaxe Editor (with a #define 08M at the top) accepts it without reporting a syntax error (which it appears to do) then you should be able to use READADC10 <ADC pin number> , <Wordvariable> with an 08M.

Cheers, Alan.
Hi
I build this circuit with a picaxe 8m2 and I'm getting worst results than when I was using the 18M2. Is it possible because of clock speed or something?
Should I rather try an 8m2?
 

AllyCat

Senior Member
Hi,
I build this circuit with a picaxe 8m2 and I'm getting worst results.
Should I rather try an 8m2?
Do you mean 08M in one of those positions? What do you mean by "worse results"? What are you trying to measure? A circuit diagram and/or photo might help.

Generally, most of the PICaxes should give similar results (the older 08M might be less good) so I would be looking for a "Hardware" problem. For example, bad power supply, long wires or bad connections, missing supply decoupling capacitor, or incorrect circuit/connections (the 18M2 has a completely different pinout), etc...

Cheers, Alan.
 

Visser

Well-known member
Hi,

Do you mean 08M in one of those positions? What do you mean by "worse results"? What are you trying to measure? A circuit diagram and/or photo might help.

Generally, most of the PICaxes should give similar results (the older 08M might be less good) so I would be looking for a "Hardware" problem. For example, bad power supply, long wires or bad connections, missing supply decoupling capacitor, or incorrect circuit/connections (the 18M2 has a completely different pinout), etc...

Cheers, Alan.
Yes sorry. 08M . All on a pc board. I don't have Q3 and 4 in yet but plan do do it to get a 12v pulse to the gate
 

Attachments

AllyCat

Senior Member
Hi,

The zener diode, Z1, will make the potential divider very inaccurate. I don't have a graph for the 5 volt (5V1) version, but the attached curves are quite similar and the current at the "reference" voltage in each case is 5 mA (note that the small squares are 10 mA), so it may "leak" away several mA. However, the total current in the divider resistors is less than 2.5 mA, whilst ideally it should be at least 100 times higher than any "leakage" current (for +/-1% accuracy).

The lowest voltage zener diode which has a sharp "knee" is about 7v5, so it would be better to connect a normal diode between the ADC input pin and the supply rail, to "catch" any higher voltage. But also, a potentiometer connected like that could be turned to zero ohms and damage the Zener or the PICaxe. :(


BZX79s.jpg

Cheers, Alan.
 
Top