Fixed frequency timed output

An area to discuss 8-bit PIC specific problems and examples

Moderator: Benj

siliconchip
Posts: 392
Joined: Wed Jan 05, 2011 11:24 am
Has thanked: 101 times
Been thanked: 24 times
Contact:

Fixed frequency timed output

Post by siliconchip »

Hi all, is it possible to generate a fixed 60Hz square wave for a timed period from a pic and if so how should i start to approach this, thanks in advance

Bob

stefan.erni
Valued Contributor
Valued Contributor
Posts: 654
Joined: Fri Aug 19, 2016 2:09 pm
Location: switzerland
Has thanked: 182 times
Been thanked: 179 times
Contact:

Re: Fixed frequency timed output

Post by stefan.erni »

Hi Bob

There is a very praktikel tool in flowcode to generate a signal

https://www.flowcode.co.uk/wiki/index.p ... ator_(DSP)

or if you need a signal on a pin you can use an interrupt for this.

If you explain more we, can help you more....

regards
Stefan

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Fixed frequency timed output

Post by medelec35 »

Hi Bob,
Take a look at this post.
Martin

siliconchip
Posts: 392
Joined: Wed Jan 05, 2011 11:24 am
Has thanked: 101 times
Been thanked: 24 times
Contact:

Re: Fixed frequency timed output

Post by siliconchip »

Thanks stefan and martin this gives me something to get started on
Cheers
Bob

siliconchip
Posts: 392
Joined: Wed Jan 05, 2011 11:24 am
Has thanked: 101 times
Been thanked: 24 times
Contact:

Re: Fixed frequency timed output

Post by siliconchip »

Hi ive added my code without testing on hardware at this point im hopeful ive got the correct calculations for 60Hz, also is it acceptable the way i start the toggle then disable the Interrupt after 20 seconds or is there a better way

Cheers
Bob
Attachments
60Hztimer.fcfx
(11.26 KiB) Downloaded 173 times

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Fixed frequency timed output

Post by medelec35 »

Hi Bob,
Nothing wrong with your flowchart from what I can see.
I have attached an alternative flowchart which is the way I would do it.
The timer interrupt fires way too quickly during simulation only.
For that reason, I have used the IsSim component, which is used for calling the interrupt macro during simulation.
Also is using the delay component they are not as accurate as timer interrupts and the delay will not allow you to run anything else until it has finished.
For that reason I have used the interrupt for the delay.
Attachments
60Hztimer Alternative.fcfx
(13.18 KiB) Downloaded 180 times
Martin

siliconchip
Posts: 392
Joined: Wed Jan 05, 2011 11:24 am
Has thanked: 101 times
Been thanked: 24 times
Contact:

Re: Fixed frequency timed output

Post by siliconchip »

Hi Martin thanks as always, I've been messing around also to try and display the output frequency but I'm running into walls everywhere is it even possible ??

Bob

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Fixed frequency timed output

Post by medelec35 »

Hi Bob you're welcome.
Unfortunately, the built in scope and data recorder is not accurate for simulations.
The only way I can think of is use a third part simulator that loads hex files or for 8 bit pics use - BL0011 - 8-Bit PIC Programmer.
As you can use ICD.
That will make the built-in scope and data recorder be 100% accurate.
Also you can see the variable values of hardware.
As the physical pin is toggling, you can use test equipment like a scope or analyser to determine frequency.
You could also place microcontroller on a solderless breadboard and program via ICSP.
Martin

siliconchip
Posts: 392
Joined: Wed Jan 05, 2011 11:24 am
Has thanked: 101 times
Been thanked: 24 times
Contact:

Re: Fixed frequency timed output

Post by siliconchip »

Hi Martin is it possible to show the frequency on an lcd ..?

Bob

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Fixed frequency timed output

Post by medelec35 »

Do you mean just show 60 Hz on the display for the 20 second duration and 0 Hz when not generating a 60 Hz frequency?
Martin

siliconchip
Posts: 392
Joined: Wed Jan 05, 2011 11:24 am
Has thanked: 101 times
Been thanked: 24 times
Contact:

Re: Fixed frequency timed output

Post by siliconchip »

Hi Martin yep spot on when it runs for 20 secs show it's output on the lcd I did try but didn't understand how to convert the toggle

Bob

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Fixed frequency timed output

Post by medelec35 »

This should do that.
Attachments
60Hztimer Alternative 2.fcfx
(15.59 KiB) Downloaded 160 times
Martin

siliconchip
Posts: 392
Joined: Wed Jan 05, 2011 11:24 am
Has thanked: 101 times
Been thanked: 24 times
Contact:

Re: Fixed frequency timed output

Post by siliconchip »

Martin thanks I can't check it at the moment but I'm sure as always it's awesome I will provide feedback tomorrow now where's that bloody thanks button 😀😀, once again top bloke thank you

Bob

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Fixed frequency timed output

Post by medelec35 »

Haha, Thanks Bob
You are welcome.
Lets hope it's what you are after.
Don't forget to test speed on your hardware as a first step, buy doing a 1 second flash test.
I hope the thanks is brought back soon!
Martin

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Fixed frequency timed output

Post by medelec35 »

Hi Bob,
Sorry false of habit using V8.
IsSim was introduced in V8.
I have posted a V7 version with the Sim detection events added, so will work the same.
When running simulation the interrupt will be ignored and the Call Timer2 macro will be accessed.
The opposite will be true when running on hardware.
Attachments
60Hztimer Alternative 3.fcfx
(17.49 KiB) Downloaded 127 times
Martin

siliconchip
Posts: 392
Joined: Wed Jan 05, 2011 11:24 am
Has thanked: 101 times
Been thanked: 24 times
Contact:

Re: Fixed frequency timed output

Post by siliconchip »

Hi martin this would explain the issues i had trying to simulate once i did i noticed that when button was pressed instead of running for 20 seconds it ran for about 0.5 of a second, where as this runs alot longer, should this then in hardware run for approx 20 secs ? I realise hardware testing is the end goal but work is not permitting this yet

Edit just timed this and still runs continuously after 2.5 mins in simulation hence does this relate to your comments above about software and hardware

Bob

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Fixed frequency timed output

Post by medelec35 »

siliconchip wrote:
Sat Oct 24, 2020 12:50 pm
i did i noticed that when button was pressed instead of running for 20 seconds it ran for about 0.5 of a second
Yes that's simulation only.
On hardware, the interrupt will work at the correct frequency, so the delay will be at 20 seconds.
The attached flowchart misses out the interrupt during simulation, so will take care of that.
The delay will still not be same as hardware but will allow you to single step so you can follow whats going on.
Martin

siliconchip
Posts: 392
Joined: Wed Jan 05, 2011 11:24 am
Has thanked: 101 times
Been thanked: 24 times
Contact:

Re: Fixed frequency timed output

Post by siliconchip »

Hi martin
That makes sense with my interupt calcs the ouput should be 60Hz in hardware, ?? Should this 60Hz sway in the real world ie drop down to 58 this wouldnt show on the display would it or is it such that timing remains stable i would be using INTOSC at 16 MHz

Bob

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Fixed frequency timed output

Post by medelec35 »

Yes, the interrupt service routine will be accessed 120 times a second.
As pin A0 changes state every time interrupt is accessed, then the pin frequency will be 60Hz in hardware.
Yes, it will remain stable at 60Hz for the duration of the 20 seconds, even if using internal osc.

The LCD just prints a string that is "0" or "60" then "Hz".
Flowchart does not measure frequency.
Martin

siliconchip
Posts: 392
Joined: Wed Jan 05, 2011 11:24 am
Has thanked: 101 times
Been thanked: 24 times
Contact:

Re: Fixed frequency timed output

Post by siliconchip »

Hi martin thats great as i said i need to test in hardware and ive started to adapt it further but thank you for putting the time in its appreciated and hopefully now the use of interupts rather than delays will be a new avenue for me in future, your a top bloke and Matrix take note lol

Regards bob

siliconchip
Posts: 392
Joined: Wed Jan 05, 2011 11:24 am
Has thanked: 101 times
Been thanked: 24 times
Contact:

Re: Fixed frequency timed output

Post by siliconchip »

hi all
ive started to adapt the above flowcode and im trying to display the actual toggle output of the port and dividing the value by 2, which should be 61Hz but the output changes every second i can briefly show the output but its constantly resetting any ideas how i could capture or sample this, also when running on proteus software im getting stack underflow ???

cheers bob
Attachments
60Hzfreq.fcfx
(18.74 KiB) Downloaded 109 times

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Fixed frequency timed output

Post by medelec35 »

Hi Bob,
Try changing interrupt to:
Change timer interrupt to this.png
Change timer interrupt to this.png (60.02 KiB) Viewed 7077 times
Martin

siliconchip
Posts: 392
Joined: Wed Jan 05, 2011 11:24 am
Has thanked: 101 times
Been thanked: 24 times
Contact:

Re: Fixed frequency timed output

Post by siliconchip »

hi martin
thanks as always for the input, i rearranged as per your pic but no change on display, i then moved freq=timertick/2 from the calc box you showed because this was with TimerTick =0 to the calc box above the decision but i got similar results before, the hertz counts to 60 but after 120 counts resets again

bob
Attachments
60Hzfreq.fcfx
(18.83 KiB) Downloaded 110 times

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Fixed frequency timed output

Post by medelec35 »

The idea of the moving the calculation within the interrupt was so the 60 is shown only when LED is enabled and output is on (and see seconds counting).
Otherwise displays shows as 0.
Just tried it and works every time for me.
Changed your flowchart to replicate the image I have posted.
I can see a solid 60 for 20 seconds
Is that not what you want?
I would also recommend tiding the display up.
E.g have:
Freq Time
60Hz 15sec

After the c you will need to add a space otherwise toy will see with seconds less than 10 the word secc
instead of sec
Attachments
60Hzfreq v2.fcfx
(19.06 KiB) Downloaded 129 times
Martin

siliconchip
Posts: 392
Joined: Wed Jan 05, 2011 11:24 am
Has thanked: 101 times
Been thanked: 24 times
Contact:

Re: Fixed frequency timed output

Post by siliconchip »

hi martin
i copied the reworked timer as per your first comment but it didnt work ?? if i went wrong not sure where it was V2 is working in flowcode but when i simulate in proteus it gets as far as 3 seconds then error message flags

"[PIC16 CORE] PC=0x04C0. Stack overflow executing CALL instruction. [U1]"

"
[PIC16 CORE] PC=0x04D5. Stack underflow executing RETURN instruction. [U1]

[PIC16 CORE] PC=0x070F. Stack underflow executing RETFIE instruction. [U1]


and the program fails to go any further, i realise this is not hardware testing just wondered if proteus has picked something else up, also proteus did not flag any faults until i started to try displaying the toggle frequency ??

ive reopened proteus and tried again its behaving at the moment

bob

Post Reply