PDA

View Full Version : trying to use the HPWM command



Rhatidbwoy
- 5th March 2006, 00:24
I have been reading up on this and finding that everybody has there two cents to this. The code to follow is what I assume to use:

' I am using this program to see if I can get a PWM to work on channel 1
CMCON = 7 ' Turns the Ports to Digital
TRISB = 0
pause 500
loop:
HPWM 1,128,721 'This command is to try and get the PWM to modulate.
goto loop

But the thing to it is that I am not getting anything to work. I would like to use this for something more complex of course and I thought that if I could generate frequencies between 700 and 750 I would be good, but I can not even get one out of that range to work. The pic that I am using is the 16f648a and I have changed it out frequently to get nothing. is there any help out there.

Darrel Taylor
- 5th March 2006, 01:06
Hi Rhatidbwoy,

PBP assumes that the CCP1 port is on PORTC.2, but on the 648A it's on PORTB.3

You can correct for this with ...
DEFINE CCP1_REG PORTB
DEFINE CCP1_BIT 3

However, since you had already set PORTB to all output's, that will not solve the problem.

You didn't mention what Oscillator freq you are using, but if it's more than 10mhz, you won't be able to get that low of frequency out of the CCP. For instance, at 20mhz, 1221hz is the minimum.
<br>

Rhatidbwoy
- 5th March 2006, 22:20
I am using the internal osc. That is 4MHz. The funny thing to the whole is that I thought since I have to put 16f648a in MCStudio that would have changed this little delema that I have with the port, but I guess wrong.

I will have to try that and see if it works. It should because I have seen this setup before.

For the code below, what is the meaning to all of this. I do not get the PR2 = 49 making this 40k for the PWM period. What is the calculation and also this must be based to xtal size.

'register setup (this is for a 16F684, so it may need to be modified slightly for your chip)

PR2 = 49 'PWM Period 40K=49
CCPR1L = 10 'PWM Duty Cycle
T2CON = 4 'Timer2 = ON; Prescale = 1:1
CCP1CON = 0 'PWM module off

Srigopal007
- 6th March 2006, 19:01
All the terminology is very well defined in the manual for each chip. You should look at the appropriate chapters for the CCP module under the PWM section. This should help you configure the CCP modules properly.

Here is an example :
for the PIC16F777 Since there are 3 pwm pins available for programmers to use:

Step1: make sure that the each pins are configured correctly, (INPUT/OUTPUT)

Step2:
Set the PR2 Value: This will depend on the oscillator speed and the desired frequency you want to run your pwm channels your duty cycle is 4x the PR2 value. if you want your pwm to go from 0% to 100% in 255 steps, than your pr2 value must be 64. because 64X4 = 255.

Step3: Make sure that you are storing the value for the duty cycle in the appropriate CCPRxL (x represnts the CCP channel you are using 1, 2, or 3)


If you are still confused there is a equation in the Manual under the CCP section that explains everything in more details of what I just mentioned here. So refer to this please. I basically summarized what steps you need to take. to fulfill this.

if you are stuck, respoond here with a snip of your code and we can work something out for you.

Srig

Rhatidbwoy
- 7th March 2006, 20:25
ok I think that clarifies everything. I was more looking to understand that of the HPWM because that is simple enough and works for hte application that I want to use it for.

I just wanted to learn more on the other method that I was talking about earlier were I did the setup myself and have the chip do what it can do for that higher freq. So in most part I can setup anything that I want to do in base of what xtal I am using for my timing sequence for the micro controller. So for 4MHz the time base I would use is 1/4MHz for that base time right?