-1
\$\begingroup\$

My project is based on a strict time management and precise event timing

for that I need to vary the PWM frequency and duty cycle in real time with two potentiometres.

I've succeded to vary the duty cycle with one potentiometre using a pic18f2550 but I couldn't neither add another analog input for the second potentiometre nor vary the frequency.

I also may need up to 4 PWM signals which will be identical but delayed between eitch other, for this I know that the 18f family has only two CCP modules, so could you please tell me which pic can do the job.

the code below is for a simple duty cycle variation.

Any help is strongly appreciated

thank you all in advance

#include <xc.h>
#include "PWM_config.h"

#include <pic18f2550.h>

void init_ADC (void)
{
ADCON1=0b00001110; //setting voltage reference and AN0 as analog input 
ADCON0bits.CHS=0b0000; //setting channel 0
ADCON2=0b10011101; /* 16Tosc conversion clock, 6Tad acquisition time, ADC Result Right Justified */
}

int doADC0(void)
{
int result; 
ADCON0bits.ADON=1; //turning on ADC
__delay_us (5); // acquisition delay of 4.6 us 
ADCON0bits.GODONE=1; //start conversion 
while (ADCON0bits.GODONE==1);
result=ADRES*0.0977517; 
return result; 
}


void init_PWM1 (unsigned char period)
{
TRISCbits.RC2=0; 

/*CCP PWM mode*/
CCP1CONbits.CCP1M=0b1100; //CCP1 to PWM mode
CCP1CONbits.DC1B=0b00; 

/*timer2 configuration*/
PR2=(period*312500)-1; //avec Fosc=20MHz
T2CON=0x02; //set prescaler to 16
TMR2ON=1; 
}

void PWM_setDC (unsigned int pourcentage_DC)
{
unsigned int DC=(10.23*pourcentage_DC); 
CCPR1L = DC>>2; // PWM duty cycle - first 8-bits (MSb)
CCP1CON |= ((DC<<4)&0x30); // PWM duty cycle - last 2-bits (LSb) in CCP1CON 5,4 bits
}
void main (void)
{
TRISAbits.RA0=1; 
init_ADC(); 
init_PWM1(0.002); //setting the period to 2ms 
while (1)
{
if (doADC0()<1) {PWM_setDC(0); PORTCbits.RC2=0; } 
else PWM_setDC(doADC0());
}
}
\$\endgroup\$
  • \$\begingroup\$ I have to say the formatting is awful. \$\endgroup\$ – a concerned citizen Apr 20 '18 at 10:56
0
\$\begingroup\$

I would start with a 16f1788 pic, has 4 programmable switch mode controllers and PSMC GUI tool available from Microchip. It saved me hours of programming and debugging. The tool makes pwm configuration very easy.

\$\endgroup\$
  • \$\begingroup\$ The problem is that this configuration is fixed for the hole program. But for my case I need an instant variation of the frequency. \$\endgroup\$ – Gon Freeks Apr 20 '18 at 13:18
  • \$\begingroup\$ what would be the range of frequency you look for ? \$\endgroup\$ – johnger Apr 20 '18 at 13:54
  • \$\begingroup\$ from 10Hz to 100Hz \$\endgroup\$ – Gon Freeks Apr 20 '18 at 15:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.