how make square wave?

For questions and comments on programming in general. And for any items that don't fit into the forums below.

Moderators: Benj, Mods

Post Reply
hertz89
Posts: 19
Joined: Sat Mar 22, 2014 6:30 pm
Been thanked: 3 times
Contact:

how make square wave?

Post by hertz89 »

hi
i have pic 16f627a with 4 MHZ internal ....what is the best way to make square wave(0v..5v) with the maximum frequency? i am now working with this :
loop...> (output B0 = 1...>output B0=0) . without delay.

what is the output frequency will be with that?

any other idea to make very high speed frequency with pic16f627a only?

thanks

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: how make square wave?

Post by Benj »

Hello,

Hard to pinpoint the rate exactly without referring to the generated assembler file but here is an approx guestimate.

4MHz clock = 1Mhz instruction rate

Output icon may take 3-4 instruction cycles

While icon may take a couple of instruction cycles.

So your probably looking at something like 10 instructions so maybe an output frequency of around 100KHz.

You could probably make a slightly tighter loop using a C code icon to forego all of the Flowcode I/O data direction switching etc this might get up to 300Khz or more.

Code: Select all

trisb = 0xFE;
while(1)
{
portb = 0;
portb = 1;
}
Using assembler in a C icon would be the ultimate way of getting the very fastest possible speed. My count is 3 cycles so approx 333.333KHz.

Code: Select all

trisb = 0xFE;
asm
{
Loop:
incf portb, 1
goto Loop
}

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

Re: how make square wave?

Post by medelec35 »

At 4MHz the best way i can think of is use PWM component (output from CCP1 = RB3)
If you set the Period Overflow & prescaler to 1 which should produce 500KHz waveform.
Using component macros:
Enable PWM.
SetDutyCycle10Bit to a duty of 4:
500KHz Square Wave.png
(58.01 KiB) Downloaded 3412 times
In theory a duty of 4 should produce 100 duty cycle, but on my hardware (16F883 runnning on 4MHz internal osc as not got a 16F627A) I had a duty of 50.2% @ 500KHz
The difference is probably down to the PWM frequency being pushed to the very maximum limit.

Martin
Martin

hertz89
Posts: 19
Joined: Sat Mar 22, 2014 6:30 pm
Been thanked: 3 times
Contact:

Re: how make square wave?

Post by hertz89 »

thanks all....you helped me alot

User avatar
STibor
Posts: 263
Joined: Fri Dec 16, 2011 3:20 pm
Has thanked: 116 times
Been thanked: 113 times
Contact:

Re: how make square wave?

Post by STibor »

Hi,

If you need to square wave:16F627A internal 4MHz clock.
Configuration settings:
oscillator: CLKOUT INTOSC
and other standard setting.
The CLKOUT output (RA6), Internal oscillator / 4 = 1MHz.This frequency can not be changed.

Post Reply