PDA

View Full Version : Generate a 120Khz signal using the pic16F877a



texas5
- 10th September 2008, 12:32
i try to generate a 120khz signal using pin 17(rc2/ccp1) of pic16f877a.i use 8Mhz osc.the code as shown below(search from internet):

void main()
{
TRISC = 0 ;

/*
* PWM registers configuration
* Fosc = 8000000 Hz
* Fpwm = 117647.06 Hz (Requested : 120000 Hz)
* Resolution is 6 bits
* Prescaler is 1
* Ensure that your PWM pin is configured as digital output
* see more details on http://www.micro-examples.com/
* this source code is provided 'as is',
* use it at your own risks
*/
PR2 = 0b00010000 ;
T2CON = 0b00000100 ;
CCPR1L = 0b00001000 ;
CCP1CON = 0b00011100 ;
}

are those coding will work.can someone help me briefly translate it into basic pro language cause i use microcode studio basic pro to compile.hope mr skimask would help me once again.

skimask
- 10th September 2008, 13:46
are those coding will work.can someone help me briefly translate it into basic pro language cause i use microcode studio basic pro to compile.hope mr skimask would help me once again.
I don't see a lot of helping going on...
Why don't you try coding it yourself...then when it doesn't work right, I'll jump in and help you fix it.

texas5
- 10th September 2008, 16:26
this is my coding:

DEFINE OSC 8
DEFINE CCP1_REG PORTC 'Hpwm 1 pin port
DEFINE CCP1_BIT 2 'Hpwm 1 pin bit


HPWM 1,127,120000 ' Send a 50% duty cycle PWM signal at 120kHz

the problem is i can't compile.it says "120000 numeric overflow, value truncated"

rhino
- 10th September 2008, 16:48
From the manual



5.29. HPWM
HPWM Channel,Dutycycle,Frequency

....Not all frequencies are available at all oscillator settings. The highest
frequency at any oscillator speed is 32767Hz.

skimask
- 10th September 2008, 16:50
Manual? What's that?

That being said...
PIC16F877A datasheet, DS39582B, Table 8-3, Page 68, says you can do 156.3Khz with a 7 bit resolution...if capable of manually setting the registers 'by hand'...

And I'm not 100% sure, but I think with PBPL, you can actually set those frequencies to a much higher value, as long as you're using PBPL and an 18F series PIC. I know I've got a LONG variable driving my HPWM on my 18F4685...can't remember if it goes over 32767 though...

rhino
- 10th September 2008, 20:35
So in other words, you won't be able to use the HPWM command for that high of a frequency. But as Skimask points out, you can achieve that frequency by setting up the PWM registers manually. As mentioned, section 8.3 of the PIC16F87XA datasheet will explain this. If you get stuck, ask.... I've never set these up before, but I'm pretty confident someone here can guide you.

rmteo
- 10th September 2008, 22:18
With an 8MHz clock on a PIC16, the closest you can get is 125kHz (PR2=7) or 111kHz (PR2=8). Also, the resolution will be less than 4 bits (0-8 or just a little more than 3 bit resolution).

skimask
- 10th September 2008, 23:02
With an 8MHz clock on a PIC16, the closest you can get is 125kHz (PR2=7) or 111kHz (PR2=8). Also, the resolution will be less than 4 bits (0-8 or just a little more than 3 bit resolution).

With a nonstandard 9.6Mhz crystal, you can get right on 120Khz.

texas5
- 10th September 2008, 23:18
i've come out with manually pwm generator coding.this is my code:

DEFINE OSC 8
TRISC.2=0
PWM_STATUS VAR PORTC.2

MAIN:
HIGH PWM_STATUS
PAUSEUS 4
LOW PWM_STATUS
PAUSEUS 4
GOTO MAIN

END

total time per cycle=4u+4u=8us...so...f=125khz...i hope this will work.i will tested it today...

skimask
- 10th September 2008, 23:29
DEFINE OSC 8
TRISC.2=0
PWM_STATUS VAR PORTC.2
MAIN:
HIGH PWM_STATUS
PAUSEUS 4
LOW PWM_STATUS
PAUSEUS 4
GOTO MAIN
END
total time per cycle=4u+4u=8us...so...f=125khz...i hope this will work.i will tested it today...

Except that it'll take at least one cycle to set the pin high (probably 2), one cycle to set the pin low (probably 2), 2 cycles to execute the GOTO MAIN...
And if you'd read your PBP manual, you'd notice that pauseus has a minimum delay of 12us vs. the 4us you want.
Therefore, you're code will probably output a 50% duty cycle square wave at about 66.6Khz...give or take...

And do you want actual PWM or just a 120Khz pulse? 2 totally different things you know...

texas5
- 10th September 2008, 23:48
just 120khz pulse only one time use.actually,i want to inject the pulse when the zero-crossing interrupt occur to couple with the 50hz ac power line.the zero-crossing interrupt has be discuss before in this forum.

skimask
- 11th September 2008, 02:53
Looks like you're well on your way over here http://forum.microchip.com/tm.aspx?m=364475

texas5
- 11th September 2008, 21:59
oh no.my x-10 receiver circuit cannot detect 100khz-120khz pwm frequency.i've tested today and you know what, it need a 1Mhz pwm frequency.how can i generate it.oh nooo.