Low frequency pwm ouput?

Satchid

Member
Dear All,

I need a Low frequency of between 25 and 100Hz pwm out of a picaxe 28x.
Normaly it can only give 4000Hz and up from the PWM outputs.

1: Is there a way to get this done without external divider components?
2: how can I collect the pwm signal inside the 28x, so that I can calculate the pulses/second in real time and show that on a display.

2a: Or if Nr 1 is not working, can I collect the pwm puls from the pwm output and divide that internaly via my picaxe program, and then output that via another output pin?

I hope someone can help me in this because i am searching the net for 3 days to find a good solution.

Thanks allot,

Willy
 

sghioto

Senior Member
Willy,

Could you do this manually using a "for-next" loop and "pulsout"? The number of loops and the pause duration will set the frequency and duty cycle. Going to require more program memory but it should work. Right?

Steve G.
 

womai

Senior Member
Another option is to go to the Picaxe 28X1 and use a timer interrupt to toggle the PWM pin at regular intervals. I posted some example code doing exactly that in a previous thread:

http://www.picaxeforum.co.uk/showthread.php?t=8541

The advantage is that unlike underclocking it does not slow down the execution speed of the rest of your program.

Wolfgang
 

Satchid

Member
Haven't tried it but a quick go on the calc shows that even with pre-scaler at 1:16 you'll struggle much below 1kHz @ 16MHz xtl.
It seems that even with poke, I will not get a 25 to at least 100hz pwm output. so this is with a 16MH xtl, with a 4Mhz it will then be not below 250HZ i think.

Willy
 
Last edited:

Satchid

Member
You can get quite low frequency outputs if you poke the register for PWM and then
you can also run the chip slower using osccon etc. Or both.

http://www.picaxeforum.co.uk/showthread.php?t=7044

Poking Stuff- Be Careful
http://www.picaxeforum.co.uk/showthread.php?t=9789

Underckocking stuff-
http://www.picaxeforum.co.uk/showthread.php?t=2233

Try Using "SEARCH" there has been a lot done previously.
Do not always think that people are not searching, It is first difficult to find what to search for in cases like this. I am seraching this more then 3 days! and even with the answers given above, (all thou I am grateful for them) is not giving me the real answer.
Then, a commonly asked function like this low pwm output frequentie should be desribed in a faq or so!

Thanks
 

hippy

Ex-Staff (retired)
It should be possible to use TIMER1 and CCP1 to generate low frequency PWM. The CCP can be used to set the CCP output high or low after a desired period and that can be fed into an input for Interrupt generation to turn that into a toggling effect, or by polling an internal flag. I've no idea what the range of PWM would be that way without looking it up, you would need to read the relevant PICmicro datasheet.

Low frequency PWM isn't really something I'd think which Rev-Ed's primary market would be using so any FAQ would likely have to come from the community which does.
 

Satchid

Member
counting

Hi Hippy,
I am grateful for your response.

As I see that this way seems also difficult,
I was thinking of another way to accomplish this, but also that I do not know how to proceed. Maybe your sublime knowledge of picaxe could bring me help.

If I take the pwm output (at pin 13 of a 28x) and hard cable it to an input say; then I count the pulse coming into that input pin and set an output High on pulse count 40 (for 4000/800=50 and 40 is 1/2 cycle of 50Hz) then reset counter and after another 40 pwm cycles put the pin low again. this could solve the problem, and would be very accurate. Only, I do not at all know if this is possible and how. I am very very new at programming and need this soon.

So, how would i proceed?

Thank you,
 

hippy

Ex-Staff (retired)
The problem there is the PICAXE is relatively slow and cannot easily ( if at all ) deal with the 4kHz or higher PWM it can generate, so it's not possible to read the PWM back in, process it and output what you need.

I can appreciate that you are new to programming and may be up against deadlines, however there's often little alternative to learning how things can or have to be done. The more one learns the more clever tricks one can achieve. Some techniques require quite a bit of knowledge to get right and unfortunately it does take time.

The best way to proceed I would say is to look at the PICmicro datasheets and get to understand the techniques for pre-scaling PWM, under-clocking the system speed and the use of the Capture Compare functionality.
 

BCJKiwi

Senior Member
The issue with all the "non PWM" approaches (pulsout, interrupts etc) is that you no longer have the benefit of pwm running in the background.

@Womai,
If I undertand your code the processes run are foreground tasks not background tasks (as are PWMout etc) so the rest of the code will be interrupted repeatedly and the faster the 'pwm' the less of anything else will get done - is this correct?

Have looked at the 28X1 2008 Microchip Datasheet.
Can anyone advise what addresses to poke on the 28X1;
1. for (OSCON) to underclock
2. for prescaler (T2KPS)

A combination of 250khz clock and 1:16 prescaler would produce a minimum period of 15.6Hz if my sums are correct.
Is this feasible?
Also would this affect both the standard pwmout and the hpwm systems? I ask this because the Microchip data sheet does not specifically indicate T2KPS being involved but does indicate TMR2 (which includes the prescaler).
 
Last edited:

Michael 2727

Senior Member
Satchid I know "search" is not the best tool for the job, sometimes it just plays dumb.
It pays to be a little creative when using it also.

From memory, now don't quote me, I was playing with very low PWM frequencies
a few months back. I thought I was able to get as low as 10Hz, using both the
poking and underclocking method. I could be wrong.

And you will probably have to do a "Hard Reset" for every new download once you
fiddle with clocking speeds etc. (chip power OFF / download initiate / chip power ON )
 

BCJKiwi

Senior Member
@ Michael_2727
Theoretically minimum clock is 31kHz and prescaler 16 so that should give ~2Hz minimum.
next step up is
125kHz which gives ~8Hz then the ~16 I was aiming for in the previous post.

Its a real pity the post scaler is not applicable to PWM as that would have given a possible 256 X reduction without reducing clock speed!

Found the answer to the prescaler and enhanced mode question above - it is used the same as for regular pwm.
 
Last edited:

womai

Senior Member
BCJKiwi,

yes, you are right that my approach does periodically interrupt the "normal" program to do the pin toggling. Although it uses very little execution time, so unless you are looking for several hundreds of toggles per second (which the original post explicitly does NOT), you will barely notice the difference. The Picaxe's timing isn't perfectly predictable anyway, so in 99.99% of possible cases the distinction is more academic. The only downside is that you lose the interrupt capability for other tasks, or at least it becomes more work to combine slow PWM and other interrupt tasks.

Wolfgang
 

BCJKiwi

Senior Member
Thanks.
Of course slowing the Processor down to 31KHz will mean very a slow reponse in the rest of the program anyway.

I'm currently considering two PWMouts for which I want to be able to control (independently) the period and the duty by using 4 ReadADCs.

ie, a ReadADC for period and a ReadADC for duty to adjust one pwmout , and a second ReadADC pair to adjust the second pwmout.

Figured I would need a 14M or 28X1 to achieve this.
on 14M - pwmout 2, and a half bridge on hpwmC/D but only using one half,
or on 28X1 - pwmout and hpwm single.

Have done some basic testing at 31KHz and the system response is very slow. Guess I'll have to do some tests with your code technique.
 
Last edited:

Satchid

Member
Hi Hippy and all,


I was thinking that it then could be possible to use a piaxe 08m to only generate the basic pwm signal, and then use a 28x(1) to divide the signal. or an other option could be then to to use one 08m to provide the basic pwm, then a second 08m to do the division, and then a 28x1 to do the rest of the programming for my job.

Thanks.
 
Last edited:

Satchid

Member
Hallo All, Thank you for all your great responses.

Would the picaxe be capable of counting signal of 1.000Hz if I use the following setup:
Picaxe 08M (not down clocked but only poked) at 1000Hz hwpwm out.
Then Feed this 1000Hz pulse in an other 08m and count input pulses (every time that a pulse of the 1KHz go's 1)

count:
make output pin 1 high
count 10 pulses from input pin 1
make output pin 1 low
count 10 pulses from input pin 1
goto count

Is the speed of a Picaxe 08m Ok, or is it nessessary to over clock this one?
What are the consequences on accurasy?
There would be no other program running on this chip except that I might want to make the amount of pulse adjustable via an adc input.

Or, is it unnecessary to move to a pic to do the job? (This would be pity because I just ordered 10 piaxes.)

Thank you all,
Willy
 

hippy

Ex-Staff (retired)
The PICAXE doesn't offer any means to do 'count N pulses then continue' so that's a dead end, and the 4kHz and faster input stream is too fast for counting any other way. If you could do it this way, you may as well just replace the count with a PAUSE or, better still, PULSOUT's which can give 5uS accurate delay periods at 8MHz.

You don't need to move to a PICmicro to do the job, and the PICmicro is no better at generating slow speed background PWM than the PICAXE is; it's the same hardware and the same control registers which need to poked in both cases. You'd have to under-clock and alter the pre-scaler just as you would on the PICAXE to use the PWM mode.

The best bet is still TIMER plus CCP1 in Compare mode. At 4MHz ( no under-clocking ) that can be used to generate PWM from 0.5Hz upwards ( to 250kHz theoretically ). With under-clocking it can go as low as 0.03Hz ( period = 33 seconds ).

This mode of CCP1 is described in Figure 9-2 of the PICmicro 16F88 datasheet, TIMER1 is described in Figure 7-2 and should be the same or very similar on all PICmicros with TIMER1 and CCP1.

This program generates a 2Hz PWM entirely in the background using interrupts on an 18X. It would need to be modified and have the SFR's and CPR1L and CPR1H registers set to create variable ratePWM. You'd have to do that anyway with a PICmicro ...

Code:
; *****************************************************************************
; *                                                                           *
; *     LED Toggling Every 250mS Using CPP1 Compare Interrupt Flag Interrupt  *
; *                                                                           *
; *****************************************************************************

;        PICAXE-18X
;       .----------.
;       |       I0 |<--.   ___    LED
;       |       O3 |---^--|___|---|>|---.
;       `----------'                   _|_ 0V

;       SFR Addresses

        SYMBOL  T1CON           = $10
        SYMBOL  CCPR1L          = $15
        SYMBOL  CCPR1H          = $16
        SYMBOL  CCP1CON         = $17

;       T1CON

        SYMBOL  T1RUN_BIT       = bit6
        SYMBOL  T1CKPS1_BIT     = bit5
        SYMBOL  T1CKPS0_BIT     = bit4
        SYMBOL  T1OSCEN_BIT     = bit3
        SYMBOL  NOT_T1SYNC_BIT  = bit2
        SYMBOL  T1INSYNC_BIT    = bit2
        SYMBOL  TMR1CS_BIT      = bit1
        SYMBOL  TMR1ON_BIT      = bit0

;       CCP1CON

        SYMBOL  CCP1X_BIT       = bit5
        SYMBOL  CCP1Y_BIT       = bit4
        SYMBOL  CCP1M3_BIT      = bit3
        SYMBOL  CCP1M2_BIT      = bit2
        SYMBOL  CCP1M1_BIT      = bit1
        SYMBOL  CCP1M0_BIT      = bit0

; *****************************************************************************
; *                                                                           *
; *     Main Program Loop                                                     *
; *                                                                           *
; *****************************************************************************

        T1CKPS1_BIT     = 1             ; Divide by 8 Pre-Scaler
        T1CKPS0_BIT     = 1             ; Divide by 8 Pre-Scaler
        T1OSCEN_BIT     = 0             ; External LP Oscillator Not Enabled
        NOT_T1SYNC_BIT  = 0             ; Ignored if Internal Clock
        TMR1CS_BIT      = 0             ; Internal Clock
        TMR1ON_BIT      = 1             ; Timer 1 Enabled

        POKE T1CON,b0                   ; Set T1CON

        CCP1M3_BIT      = 1             ; Set Bit on CCP1 Match
        CCP1M2_BIT      = 0
        CCP1M1_BIT      = 0
        CCP1M0_BIT      = 0

        POKE CCP1CON,b0                 ; Set CCP1CON

        SETINT %00000001,%00000001      ; Interrupt when LED goes on

        DO                              ; Do nothing ...
        LOOP                            ; ... LED Toggling done by interrupts

Interrupt:

        IF pin0 = 1 THEN                ; LED has gone on ...
          PEEK CCP1CON,b0               ; Turn LED off at next CCP1 interrupt
          CCP1M0_BIT = 1
          POKE CCP1CON,b0
          SETINT %00000000,%00000001    ; Interrupt when LED goes off
        ELSE
          PEEK CCP1CON,b0               ; Turn LED on at next CCP1 interrupt
          CCP1M0_BIT = 0
          POKE CCP1CON,b0
          SETINT %00000001,%00000001    ; Interrupt when LED goes on
        END IF
        PEEK CCPR1H,b1                  ; Set next CCP1 Interrupt Value
        PEEK CCPR1L,b0
        w0 = w0 + 31250                 ; CCPR1 = CCPR1 + (250000/8)
        POKE CCPR1H,b1
        POKE CCPR1L,b0
        RETURN

; *****************************************************************************
; *                                                                           *
; *     End of Program                                                        *
; *                                                                           *
; *****************************************************************************
 
Last edited:

Mycroft2152

Senior Member
Hippy,

For some reason the code window is locked and wil not scroll beyond the line

: SFR addresses

I have seen this happen occaisionally but usually a cut and paste into the PE will reveal the entire code, but not this time.

The code actually ends with that line.

Myc
 

hippy

Ex-Staff (retired)
I've included the code as an attachment.

Actually it's slightly different, this has an optimised interrupt routine. Use this one.
 

Attachments

BCJKiwi

Senior Member
Just wondering why you need to count the output of the pwm.

If the pwm is running at a known period and duty, then there are numbers that have been determined in order to set the period and duty.

Can those numbers (with some math to provide the desired format), be used for display?
Appreciate this does not actually read the output but is the output going to be different enough from the control that the difference must be known?

If you are prepared to use a separate 08M then you could use a routine like this to produce slow PWM;

Concept only - UNTESTED
Code:
#PICAXE 08M
setfreq m8
 
SYMBOL Pulse  = 4  'Output 4 Leg 3 "pwm" pin
SYMBOL Duty   = 1  'ADC 1  Leg 6
SYMBOL Duty_Set  = b7
SYMBOL Period  = 2  'ADC 2  Leg 5
SYMBOL Period_Set = b8
 
Main:
ReadADC Duty   , Duty_Set 'Read adc adjustment pot to set duty
ReadADC Period , Period_Set 'read adc adjustment pot to set period
Period_Set = Period_Set - Duty_Set   ' working Period_set must allow for Duty_Set
High  Pulse
pause Duty_Set
Low   Pulse
Pause Period_Set
Goto Main
Notes:-
1. Some allowance would need to be made for the read adc timing (and taken off the Period_Set value)
2. Some math would need to be done on the readadc values to provide the appropriate scale.
3. readadc10 could be used (with word variables) but the response would be a bit slower.
4. Some tests would need to be done on the values to ensure there always was a positive value for Period_Set
5. Pulsout Pulse , Duty_Set could be used instead of the high pulse and following pause Duty_Set to obtain shorter duty (5us units instead of 0.5ms units) but the working Period_Set would still need to use pause (ms units)
 
Last edited:

Satchid

Member
Hippy this I great,

I have 2 qs,
1) Your sample code is written for the 18X, is this also applicable to the 28X?
2) How do I have to see this:
Is it right that I have to wire connect the output O3 with input I0?

PICAXE-18X
.----------.
| I0 |<--. ___ LED
| O3 |---^--|___|---|>|---.
`----------' _|_ 0V

Willy.

Ps: I am learning things mostly with example code, The moment I have something working then I change things in the code to see what the consequences are etc.
 

Satchid

Member
Just wondering why you need to count the output of the pwm.

BCJKiwi, I just need a very accurate 50 Hz square wave output.
Therefore, as the picaxe (or pic) is not able to give me that straight away, I thought that if I have a poked 1 kHz signal from one 08x and feed that into another picaxe, I could just make the 50 Hz if I made an output pin high for 10 pulses, and low for the next 10 pulses. This would make an output pin be 50 Hz. But if I understand correct, this is not possible.

Thank you,
 

hippy

Ex-Staff (retired)
1) Your sample code is written for the 18X, is this also applicable to the 28X?

In concept but there may be changes needed to reflect the different hardware of the PICAXE/PICmicro. I'm afraid I don't know what those changes would have to be.

2) Is it right that I have to wire connect the output O3 with input I0?

PWM comes out on a different pin so using CCP1 that should be PORTC Output 2 ( Leg 12 ). Note that there's potential for confusion between PICAXE commands PWMOUT 1 and PWMOUT 2 which use CCP2 and CCP1 respectively.

You'll need to check the Microchip 16F874A datasheet to verify exactly what connections there needs to be. You may need to use "LET DIRSC=" to enable the CCP1 output.

The code's written to use Input 0 but can use other pins but the interrupt handling code would need to change to reflect that.
 

hippy

Ex-Staff (retired)
BCJKiwi, I just need a very accurate 50 Hz square wave output.
Your initial spec asked for "a Low frequency of between 25 and 100Hz" - I'm not sure how you'll generate anything over 50Hz from 50Hz by counting or dividing. Perhaps we need some clarification of exactly what the requirements are before everyone gets confused ?

If considering a multi-PICAXE sytsem it should be possible to use an 08/08M as a PWM generator which can have its PWM frequency increased and decreased or set to a particular value within the ranges required.
 

Satchid

Member
Hippy,
I wrote:
"I thought that if I have a poked 1 kHz signal from one 08x and feed that into another picaxe, I could just make the 50 Hz if I made an output pin high for 10 pulses (from the 1kHz input signal), and low for the next 10 pulses. This would make an output pin be 50 Hz."

If I arrive at 50Hz THE WAY I DESCRIBE between the quotes above, then I would have an accurate 50Hz, and this is my primary goal.
But, if then, as an extra, the amount of pulses (10 that is for 50Hz) change to something else (like 20 for 25Hz and 5 for 100 and 1 for 500), then I am able to get easily between 25 and 100Hz
Also with a little extra program and hardware, I could make the frequency variable via an adc port and a potentiometer.

But, You're right, this was not completely clear, I hope it is now. I am not native English and therefore the explanations are not always so accurate. I know what I want to do, but with only a remote almost forgotten knowledge of programing accumulated with the language problem, it is really not easy to convey this to other people. To make this message as correct as possible took me the best part of an hour.

So, I hope you are not angry to me,

Thank you all for willing to help me and all the others.

Willy
 
Last edited:

hippy

Ex-Staff (retired)
So, I hope you are not angry to me

Not at all, I just got a bit confused about where were heading.

1kHz has a period of 1ms so it should be possible for a PICAXE to count that and do as you suggest but you could be getting close to its capabilities there, and adding a read of the pot to select what to count could go beyond it. It would be possible to reduce the frequency of the input which would help a lot.
 

Satchid

Member
settimer for 28X1

Thank you Hippy,

I fell on the settimer command wile reading through the manual for the 28X1:

This is part of the explanation:
External Counter
In external counter mode an internal counter register (not accessible to the end
user) is incremented on every positive going edge detected on input 0. This pulse
counting occurs in the background, so the PICAXE program can perform other
tasks at the same time as it is counting. When the internal
counter register overflows from 65535 to 0, the special &#8216;timer&#8217; variable is
automatically incremented.
Therefore to increment the timer variable on every 10 external pulses set the
preload value to 65536 - 10 = 65526. After ten pulses the counter register will
overflow and hence increment the &#8216;timer&#8217; variable. To increment the &#8216;timer&#8217;
variable on every external pulse simply set the preload value to 65535.
If the timer word variable overflows (ie from 65535 to 0) the timer overflow flag
(toflag) is set. The toflag is automatically cleared upon the settimer command,
but can also be cleared manually in software via &#8216;let toflag = 0&#8217;. If desired an
interrupt can be set to detect this overflow by use of the setintflags command

Can this be used fr my aplication?
How would i set to "external counter mode" versus "internal counter mode"? (not said in the manual)
Could I put the 1kHz or even standard 4kHz or ebven better, the 4MHz of the external resonator on this input port0 and proceed from there? (not said in the manual)
Can I make an output pin High and low by means of this command? (not said in the manual)
Willy
 
Last edited:

BCJKiwi

Senior Member
Satchid,
If you are looking for a square wave with equal high and low time then that is 50% duty on PWM.
If you use a separate 08M then slow the chip/pwm by whatever means so you can reach the slowest rate you need. Then adjust for higher speed via pwm period.

If you use any process other than PWM and try do everything in one chip, then the speed of the rest of your code will be affected.

If the rest of the program is not time critical, then the 28X1 has pauseus (pause microseconds) or pulsout (the steps are 10usec) but this won't run in the background - pwm is the only one for that. So you need to use the pause/pauseus/pulsout inside an interrupt. If you think this will work, then use Womai's settimer internal/setintflags technique as it is internal timer driven and does not need to count external pulses.
Note that the 'minor tick' is 64us and the interrupt is checked only between program lines so if other parts of the program have complex expressions, then timing may be affected.
Any process other than pwm which runs in the background will not be as accurate but how accurate does it need to be? Is it equal high/low or consistent period or both that is needed?
 
Last edited:

BCJKiwi

Senior Member
Just run some tests with this code (based on code above) and timed the minimum period at 2.6ms and the minimum duty at 0.8ms

Code:
#PICAXE 08M
setfreq m8
 
SYMBOL Pulse  = 4    'Output 4 Leg 3 "pwm" pin
SYMBOL Duty   = 1    'ADC 1  Leg 6
SYMBOL Duty_Set  = b11
SYMBOL Period  = 2    'ADC 2  Leg 5
SYMBOL Period_Set = b12
SYMBOL Overhead     = 1    'The time taken to read ADC and calculate Period_Set
 
Main:
ReadADC Duty   , Duty_Set   'Read adc adjustment pot to set duty
Duty_Set = Duty_Set / 2
ReadADC Period , Period_Set   'read adc adjustment pot to set period
If Period_Set < Duty_Set Then
 Period_set = 1
Else
 Period_Set = Period_Set - Duty_Set - Overhead Min 0  ' working Period_set must allow for Duty_Set
EndIf
High  Pulse
pause Duty_Set
Low   Pulse
Pause Period_Set
Goto Main
This suggests the overhead is approx 1.8ms
With better pots and a reasonable build (instead of trimpots on a breadboard) slightly better times should be achievable.
This overhead also means you can't get to 100% duty as there will always be the overhead plus the duty.
 
Last edited:

BCJKiwi

Senior Member
Just carried out further tests using prescaler and slow clock with the following code;

Code:
#PICAXE 08M
 
SYMBOL Pulse  = 2    'PWM 2  Leg 5 "pwm" pin
SYMBOL Duty   = 1    'ADC 1  Leg 6
SYMBOL Duty_Set  = b9
SYMBOL Duty_Store = b10
SYMBOL Period  = 4    'ADC 4  Leg 3
SYMBOL Period_Set = b11
SYMBOL Period_Store = b12
POKE $12, $06             'provides a pre-scale of 1:16
POKE $8F, %00000000      '= 31 kHz processor speed
 
Main:
ReadADC Duty   , Duty_Set   'Read adc adjustment pot to set duty
ReadADC Period , Period_Set   'read adc adjustment pot to set period
If Period_Set <> Period_Store OR Duty_Set <> Duty_Store Then
 Duty_Store   = Duty_Set
 Period_Store = Period_Set
 PWMout Pulse,Period_Store,Duty_Store
EndIf
Goto Main
This gives minimum period of around 300us with a minimum duty of around 100us.
Response is quicker than than you can reasonably turn the knob even at a clock of 31kHz.
 

boriz

Senior Member
Will this be a mains powered project? Where do you live?

Here in the UK, 50Hz is the mains line frequency. It would be simple to get 50Hz from a small transformer or even a simple capacitor/resistor affair. The mains frequency is well regulated. Lots of electric clocks work this way.

Just how accurate do you need it? Can’t you just use a 555 timer?
 

Dippy

Moderator
"BCJKiwi, I just need a very accurate 50 Hz square wave output."

Oh dear, it took 24 posts before we got the info that all that was wanted was an accurate 50hz -can you now tell us exactly what you mean by "accurate"? 1%? 0.00001%?

Is it for a bed-side clock? Maybe the project will tell us what degree of accuracy is expected.

There are numerous monostable/astable chips including the 'old reliable' 555 that can provide 50Hz - some are more precise and stable than others.

And please, in future, try and tell us all your actual requirements in the first couple of posts. There are many near-geniuses on this Forum but few clairvoyants and they've gone on holiday.
 
Last edited:

Satchid

Member
Long answer

"BCJKiwi, I just need a very accurate 50 Hz square wave output."

Oh dear, it took 24 posts before we got the info that all that was wanted was an accurate 50hz -can you now tell us exactly what you mean by "accurate"? 1%? 0.00001%?

There are numerous monostable/astable chips including the 'old reliable' 555 that can provide 50Hz - some are more precise and stable than others.

And please, in future, try and tell us all your actual requirements in the first couple of posts. There are many near-geniuses on this Forum but few clairvoyants and they've gone on holiday.
Dear all,

In fact the first question was really the right question. The END RESULT will be a real stable 100% accurate 50/50 duty cycle 50Hz square wave generator. So accurate that I will be comparing it with the atom clock in Frankfurt every day.
But, this generator is part of a "special converter" with internal coils around iron cores. I want to see the influence of different frequency's in this converter. Therefore I wanted the possibility of varying the frequency from 25 to 100Hz.
No, a 555 would not be in time because the capacitors are not time stable. The electronic way to go is a high freq xtal and dividers.

But, I want to do it with an xtal driven picaxe because I want to learn to program these little beauties and also supporting the project by using the Picaxe in my projects, and in the mean time doing something useful. Yes, I know that this project is a little difficult to start with, but on the other hand it is the challenge that I need to really dive into this.
Every answer and every sample code that is given in this thread helped me already, or will help me, in achieving the goal of the end result, and more for a longer term the necessary program skills to do more difficult projects in the future.
Sometimes questions are not, to short answered, even wrong because for different reasons.
It is extremely difficult for teachers to keep on answering so called stupid questions because there is no challenge in them, there is seemingly no award in doing this.
I want here to plead for you teachers to read the questions properly and answer the questions properly. The result will be fewer questions afterwards also. You yourself will feel more satisfied with you work at the end of the day.
I want here to honor the great people like Hippy, technical, BJC, Technical and the countless others that try to answer all the (quote) stupid questions of all of us. These people are truly great teachers and I am very grate full for there efforts.

Lastly, (mainly for Technical I think) I would like to plead for a better command manual. It exists with only a few examples, I would like to see more different examples for the (learning people) difficult commands. This would also lead to fewer simple questions and thus the questions would mature, the teachers would be les bored and therefore the quality of the answers would increase. At the end everybody is happy.

Thank you all,

Willy
 

hippy

Ex-Staff (retired)
The END RESULT will be a real stable 100% accurate 50/50 duty cycle 50Hz square wave generator. So accurate that I will be comparing it with the atom clock in Frankfurt every day.

Few things are 100% accurate, not even atomic clocks. You may get close to matching an atomic clock but not 100%, with a PICAXE or any other microprocessor. You can however get close enough that the accuracy and difference doesn't matter in most real world cases.

I think you are looking for far greater accuracy than people had thought ( including myself ). To get high accuracy you are probably going to need a highly accurate and stable crystal / clock source and will need to divide down to the 25Hz to 100Hz using high-speed hardware. The PICAXE would control that hardware rather than doing the division itself.

The first thing which needs doing in my opinion is defining exactly how accurate the nominal 50Hz output must be; 20 millisecond period, plus or minus how many milli, micro, nano or pico seconds ?

A typical crystal would probably give an accuracy of 20mS +/- 2us, an OCXO 20ms +/- 2ns. Crystal ageing factors would change that over time.
 
Last edited:

Satchid

Member
accuracy

Hippy, Your right of course, I agree, 100% does not exist.

I relay welcome these questions!!!

Here is the accuracy I would like:
In the beginning of the 70ties, I was visiting a power station. In the control room where 2 clock on the wall, one was driven by the power of the grid at 50Hz, and the other was xtal driven (now days it would be radio or GPS clock “controlled by the atom clock in Frankfurt”). At the end of each day at midnight, they would compare the 2 and adjust the alternators manually to make up for the difference and make all the grid driven clocks in the country run on the right time again.
I would like to have the accuracy of the powestation, and compare this with the radio clock, then make up for the difference. If I was saying 100% then I in fact meant the accuracy of the attom clock. I think that a few seconds or so a day would not mater if it can be made up in the clock comparison. How I will do the clock comparison automatic, that is for later but I would like to use 1 clock driven by de converter and a radio clock. I will place a led sensor (the black thing like in a computer mouse) so that the secondhand will cut the led light from the sensors, then see witch on is passing first and how far apart, then let the converter run slower or faster as needed till the clocks are very close again.
By writing this, I see that I can do this procedure even 10 times a day, or adjust if the difference is at set maximum and make it easier on the accuracy of the basic 50Hz generator. So, a couple of seconds a day will not matter anymore on the picaxe generator as long as the comparator is able to adjust it.
After this project I will know more about picaxe programming I hope.

So, with automatic adjustment, a minute a day on the picaxe (before comparison) would be the target, and closer if it happens to work out that way.


Thanks Hippy,

Willy.
 

MFB

Senior Member
RC background timer

You might find the following extract, from my post about generating a constant scan rate, may give you some alternative ideas about background timing. This approach only needs a resistor, capacitor and diode and is therefore simpler than using a 555 or 08M.

" If you need to monitor a number of analog inputs and transmit the ASCII data over a serial link, the following 18X code is unable to maintain a constant scan rate. As the loop time will be dependant on the analog input levels, due to higher ADC input voltages generating more ASCII characters per sample. When running the example code at 8MHz, reading three zero inputs results in a scan rate of 70Hz but this drops to only 40Hz when reading three full-scale voltage inputs.

setfreq m8

start:
readadc10 0,w0
readadc10 1,w1
readadc10 2,w2
sertxd (#w0,",",#w1,",",#w2,cr)
goto start


This potential problem can be overcome by triggering each scan from an external clock pulse, via a digital input. However, if you don’t want to add another chip (e.g. 555) I have developed an alternative three 'passive' component solution. A resistor is connected between a PICAXE output and one of its (Schmitt) inputs, with a capacitor between the input and ground. A diode is placed in parallel with the resistor with the anode towards the capacitor.

The following code is revised to discharge the capacitor (via the diode) at the start of each scan. The capacitor then starts to charge, through the resistor, whilst data is being transmitted. At the end of transmission the PICAXE monitors the input and triggers the next scan when the voltage across the capacitor exceeds the 'logic 1' threshold. As an example, a 100K resistor and 0.33uF capacitor will result in a scan rate of about 30Hz, which provide an adequate margin for the worst-case condition (e.g. all analog inputs at full scale).


setfreq m8
high 7

start:
pulsout 7, 200
readadc10 0,w0
readadc10 1,w1
readadc10 2,w2
sertxd (#w0,",",#w1,",",#w2,cr)

tick:
if pin6 = 0 then : goto tick : else : goto start: endif


Tests show that this approach offers timing stability that is at least as good as the 18X internal clock (using a polypropolene capacitor, thin film resistor and BAT 85 diode). To better this you would need to use a 28X1 and crystal, with each scan triggered from the internal timer."
 

hippy

Ex-Staff (retired)
This is starting to make sense to me now if I've got it right ...

You have some time keeping or time indicating device which is ticked along by supplying a 50Hz signal but if it loses or gains time you want to increase or decrease the frequency of that ticking to bring the time back to what it should be.

That is entirely feasible and I think it takes us back to the TIMER1 and CCP1 solution. That generates its interrupts when the PICAXE internal timer reaches a certain value ( held in CPR1H:CPR1L ) and that value can be set to generate a 50Hz signal. By increasing and decreasing the value in CPR1H:CPR1L the frequency can be adjusted.

All that is needed then is to determine what time the local clock shows, what time Frankfurt says it is and to adjust CPR1H:CPR1L up or down to bring them back together in a certain time period when you'll perform the process again.

This is a variation on a PID algorithm but should be fairly easy to implement without having to know much about PID algorithms.
 

Satchid

Member
This is starting to make sense to me now if I've got it right ...

You have some time keeping or time indicating device which is ticked along by supplying a 50Hz signal but if it loses or gains time you want to increase or decrease the frequency of that ticking to bring the time back to what it should be.

That is entirely feasible and I think it takes us back to the TIMER1 and CCP1 solution. That generates its interrupts when the PICAXE internal timer reaches a certain value ( held in CPR1H:CPR1L ) and that value can be set to generate a 50Hz signal. By increasing and decreasing the value in CPR1H:CPR1L the frequency can be adjusted.

All that is needed then is to determine what time the local clock shows, what time Frankfurt says it is and to adjust CPR1H:CPR1L up or down to bring them back together in a certain time period when you'll perform the process again.

This is a variation on a PID algorithm but should be fairly easy to implement without having to know much about PID algorithms.
Yes Hippy, that is explained much shorter as wath i could do and it is correct.

Thank you, I will look into that code more. I should receive the 28x1 and the 08M chips probably tomorrow.

Thank you for your patience,

Willy
 
Top