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.

Altering PWM Period and Duty Cycle from ADC results - PIC16F684

Status
Not open for further replies.

Sicknote

New Member
Hi,
As part of a uni project I need to program a pic to vary the PWM period and duty cycle in conjunction with 2 potentiometers.

I have set up both ADC channels and have checked they're both functioning properly.
The PWM is set up and running.
One of the pots works fine with the ADC to provide a new value for CCPR1L and thus change the duty cycle however I am not sure how to program it to change the period, once the PWM is running.

Once the result of the 2nd ADC has been saved in a register, can this then be moved, at any point, in to the PR2 register or are there certain conditions that must be set before PR2 can be given a new value?

Also, how do I get the mark:space ratio to remain unchanged so that the 'ON' time increases proportionally with the Period time and maintaining the current duty cycle?
This is the bit thats really bugging me, I know the equations but I can't see how I would write this in code without getting unusable values.... Ie decimals and values greater than 255!

I've read the data sheet front to back and still can't get it.

The equation basically says that the duty cycle = CCPR1L / PR2 (I'm only using 8 bits of resolution so I'm not using the 2 bits of CCP1CON)

Lets say CCPR1L = 127 & PR2 = 255 then the duty cycle comes out at 0.5 this value can't be saved to a register and the figures for the duty cycle get even more obscure when you have values like 200 & 230 respectively, the duty cycle is 0.8695!!!!

Any help would be greatly appreciated.

Thanks
 
What you actually want to do is changing the frequency, keeping the duty cycle constant, right?

If so, it seems that you have to repeat every time, the initial calculation for frequency and duty cycle and set the registers on the fly. Rule of three I guess...

Long time I do not work with the 16F. Rule of 3, I guess...

Please comment.
 
Last edited:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top