Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

how to generate a 2 MHz pulse with 30% duty cycle using PICbasic?

Status
Not open for further replies.

wyiancwc88

New Member
I am using PIC 16F877A with 20MHz crystal. I am wondering can it generate a 2MHz clock pulse with 30-40% duty cycle. Can anyone help me on this?
 
Consider a pseudo frequency generator on a port pin by averaging a bit toggle, with a bit toggle and a goto.

If you had an 18F PIC you could use the internal osc at 8Mhz configured with the 4X PLL and clockout function, then everything would be run in the background. The OSCTUNE register could be used to fine tune to 2 Mhz, and possibly even compensate over a temperature range with a table of values.
 
I am using PIC 16F877A with 20MHz crystal. I am wondering can it generate a 2MHz clock pulse with 30-40% duty cycle. Can anyone help me on this?

I don't know PicBasic but that chip running at 20 MHz used with the Picaxe boot loader can run a
PMW at 20 MHz and 30% duty with pwmout (pin number), 2, 3
40% duty is pwmout (pin number), 2, 4
 
crystal = 20MHz,
machine cycle = 20M/4 = 5M = 0.2us
one instruction need two machine cycle = 0.4us
I am wondering is it possible to get a 2MHz pulse wave?? Since one instruction already took 0.4us (2.5Mhz), I am doubting about it..
Actually I'm new to PIC and I am not really know how it operates, so can you provide me the source code in PICbasic? Thx a lot.
 
Last edited:
Most PIC instructions are single cycle. The problem with the 20Mhz clock is as you mentioned 2 instructions gives 2.5Mhz, extending that to 3 intructions gives 1.6667Mhz, so averaging fools a DMM but not the scope.

The aforementioned Picaxe soft PWM routine is unlikely to work at such a high frequency. Setting up the hardware PWM is doable, but with a 16Mhz crystal to get the right instruction cycle clk. Two clocks will give a 2Mhz frequency, but you have to settle for a 50% duty cycle. I have never messed with such a high frequency before because there is no bit resolution (i.e. duty cycle bandwidth).

Code:
'A GCBasic program for a 2Mhz signal on CCP1
'using Hardware PWM with minimum period
'and duty cycle  KS 3/1/11
 
'Chip model 
#chip 16f877a,16
#config     _WDT_OFF & _BODEN_OFF & _PWRTE_ON & _HS_OSC & _LVP_OFF

#define led PortA.0
dir led out
dir PortC.2 out	'make CCP1 an output


'set up hardware PWM with bare minimum values
PR2 = 1		'TMR2 period value, i.e. 2clks
CCPR1L = 1	'set PWM duty cycle, i.e. 1 clk of 2 clk period
T2CON = b'00000100'	'prescale, postscale is 1:1, TMR2 is on
CCP1CON = b'00001100'	'setup PWM mode, output will be on CCP1

Start:
set led on
wait 1 s
set led off
wait 1 s
goto Start
 
Had a quick revisit to this topic, because I forgot about CCP1X, AND CCP1Y. So actually there is 3 bits of resolution going on at PR2 = 1.
 
Status
Not open for further replies.

Latest threads

Back
Top