Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Generate 40KHz without using pwm

Status
Not open for further replies.

slaamdunk@gmail.com

Junior Member level 1
Joined
Aug 27, 2013
Messages
19
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
130
Are there ways to create a 40KHz square wave with 25% duty cycle without using PWM in pic 12F615, just by toggling the pins to '1' and '0'.
I tried and I am not getting an accurate frequency.
Thank You
 

Which compiler is used???
Try using 19us OFF and 6us ON!!! = 25us or 1/40000 = 0,000025s
If in c, use delay routine...
 
Last edited:

Which compiler is used???
Try using 19us OFF and 6us ON!!!
If in c, use delay routine...

I am using Hi-Tech C. I am not getting an accurate frequency if I use the delay routine.
I theoretically calculated the time taken by each instruction using the clock frequency and I tried inserting delays to get a 25us ( 40KHz). But I am not getting the accurate frequency of 40KHz.

- - - Updated - - -

Which compiler is used???
Try using 19us OFF and 6us ON!!! = 25us or 1/40000 = 0,000025s
If in c, use delay routine...
Code:
#include    <htc.h>
//#include <PIC12F615.h>
#ifndef _XTAL_FREQ
#define _XTAL_FREQ 8000000
#endif


__CONFIG(IOSCFS_8MHZ & FOSC_INTOSCCLK & MCLRE_OFF & WDTE_OFF & PWRTE_OFF & CP_OFF & BOREN_OFF );



void main (){
int count = 96;
ANSEL=0x00;

GPIO=0;


TRISIO5 = 0x00;

while(1){
GPIObits.GP5=1; 
__delay_us(6);
GPIObits.GP5=0; 

__delay_us(19);


}

}

Is this how you were infering, because this is still not giving me an accurate 40KHz, it is somewhere near 36 - 37KHz.
Please let me know if I doing something wrong.
Thank you
 

Please... try this:

Code:
/* Hitech C using PIC12F629 */
#include <htc.h>

#ifndef _XTAL_FREQ
 #define _XTAL_FREQ 4000000
#endif

unsigned int count;

 __CONFIG ( FOSC_INTRCIO & WDTE_OFF & PWRTE_OFF & MCLRE_OFF & BOREN_OFF & CP_OFF & CPD_OFF );
void main()
{
	OSCCAL	= 0x3ff;
	GPIO	= 0x00;
	CMCON	= 0x07;
	TRISIO	= 0b00001000;
	GPIO= 0;
	while(1)
	{
		GPIO= 0xFF;		
		__delay_us(6);
		GPIO= 0;
		__delay_us(18);	
	}
}

I dont have oscilloscope!!! :-(

Here is 45KHz and 25% Duty in multimeter
 
Last edited:

Please... try this:

Code:
/* Hitech C using PIC12F629 */
#include <htc.h>

#ifndef _XTAL_FREQ
 #define _XTAL_FREQ 4000000
#endif

unsigned int count;

 __CONFIG ( FOSC_INTRCIO & WDTE_OFF & PWRTE_OFF & MCLRE_OFF & BOREN_OFF & CP_OFF & CPD_OFF );
void main()
{
	OSCCAL	= 0x3ff;
	GPIO	= 0x00;
	CMCON	= 0x07;
	TRISIO	= 0b00001000;
	GPIO= 0;
	while(1)
	{
		GPIO= 0xFF;		
		__delay_us(6);
		GPIO= 0;
		__delay_us(19);	
	}
}

I dont have oscilloscope!!! :-(

Here is 45KHz and 25% Duty in multimeter

Why do you have an input in TRIS ? ( you have a 1 in that binary number )
 

Because in PIC12F629, The GPIO3 is only Input!!!
I like to use binary number in these registers...

Change to

Code:
		GPIO= 0xFF;		
		__delay_us(6);
		GPIO= 0;
		__delay_us(18);
 
Last edited:

Running @ 4 MHz your machine cycle is 1 µs, so it´s not possible to achieve 6.25 µs (40 KHz @ 25% duty cycle).
If you need "exactly" 6.25 µs on and 18.75 µs off (40KHz @ 25%) you will have to choose among these clock
frequencies.

Xtal Freq........# machine cycles on.......# machine cycles off
MHz
1.280.........................2................................6
1.920.........................3................................9
2.560.........................4...............................12
3.200.........................5...............................15
3.840.........................6...............................18
4.480.........................7...............................21
5.120.........................8...............................24
5.760.........................9...............................27
6.400........................10..............................30
7.040........................11..............................33
7.680........................12..............................36
8.320........................13..............................39
8.960........................14..............................42
9.600........................15..............................45
10.240.......................16.............................48
10.880.......................17.............................51
11.520.......................18.............................54
12.160.......................19.............................57
12.800.......................20.............................60
13.440.......................21.............................63
14.080.......................22.............................66
14.720.......................23.............................69
15.360.......................24.............................72
16.000.......................25.............................75
.
.
.

This type of problem is a good example of those you get better results using assembly,

Below a possibility for a 16 MHz Xtal (thanks to PICLOOPS http://www.biltronix.com/picloops.html)

Code:
#asm

LOOP	movlw 0xff
	movwf GPIO
	movlw	D'7'
	movwf	CounterA
loopon	decfsz	CounterA,1
	goto	loopon
	nop
	movlw 0
	movwf GPIO
	movlw	D'23'
	movwf	CounterA
Loopoff	decfsz	CounterA,1
	goto	Loopoff
	nop		
	goto 	LOOP

#endasm


Because in PIC12F629, The GPIO3 is only Input!!!
I like to use binary number in these registers...

Change to

Code:
		GPIO= 0xFF;		
		__delay_us(6);
		GPIO= 0;
		__delay_us(18);
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top