ckshivaram
Advanced Member level 6
- Joined
- Apr 21, 2008
- Messages
- 5,060
- Helped
- 2,150
- Reputation
- 4,306
- Reaction score
- 2,088
- Trophy points
- 1,403
- Location
- villingen (Germany) / Bangalore
- Activity points
- 30,088
Yes........ In the absence of PR2 timer2 is same as normal timers.... i feel you can still improve the code...
Yes........ In the absence of PR2 timer2 is same as normal timers.... i feel you can still improve the code...
Yes, there is no problem with this. In this case you have a fixed frequency determined by PR2 and you can just vary CCPRxL to vary the duty cycle (and prescaler also if needed) as you said. Of course, it depends on your application, eg. if you need a specific frequency, but for general application, this will be fine.
Hope this helps.
Tahmid.
Yes its correct, i did a simple program to vary ADC input and see the PWM duty cycle varying to learn this concept. later to which i interfaced a motor to play with.
The PLL is enabled in HSPLL, XTPLL, ECPLL and
ECPIO Oscillator modes. It is designed to produce a
fixed 96 MHz reference clock from a fixed 4 MHz input.
TOSC = 1/Fosc
e.g.
TOSC = 1/4Mhz (crystal frequency)
or
1/48Mhz (from PLL)
The actual maximum Fosc or system clock is 48MHz, due to the postscaler options of which the 1/2 is the small divisor. In other words 96MHz divided by 2 is 48MHz, the stated maximum Fosc in the devices datasheet.
Glad your power is back up.
TOSC = 1/48 Mhz
and the 4Mhz crystal frequency will not be use when using PLL..
#include<htc.h>
void init_pwm()
{
TRISC = 0; //set PORTC as output
CCP1CON |= 0x0c; //enable PWM mode
T2CONbits.TMR2ON = 1;
//T2CONbits.T2CKPS0 = 1; //prescaler 4
T2CONbits.T2CKPS1 = 1; //prescaler 16
}
void pwm(unsigned char period, unsigned char duty)
{
PR2 = period;
CCPR1L = duty;
CCP1CONbits.DC1B0 |= 0;
CCP1CONbits.DC1B1 |= 0;
}
#include<htc.h>
#include"pwm.h"
#include"adc.h"
void init_pwm();
void pwm(unsigned char period, unsigned char duty);
void main()
{
unsigned char x;
init_pwm();
init_adc();
while(1)
{
x = adc_read(); //i will fixed this part
pwm(255,x);
}
}
#include<htc.h>
void init_adc()
{
/* select channel 0, enable ADC */
ADCON0bits.ADON = 1;
/* Voltage reff is VDD, configure AN0 as analog */
ADCON1bits.VCFG0 = 0;
ADCON1bits.VCFG1 = 0;
ADCON1 |= 0b1110;
/*2 x TAD, FOSC/32 */
ADCON2bits.ACQT0 = 1;
ADCON2bits.ADCS1 = 1;
TRISAbits.TRISA0 = 1; // input
ADCON2bits.ADFM = 0; // A/D result is left justified
}
unsigned int adc_read()
{
unsigned int adc_result;
ADCON0 = 0x00;
ADCON0bits.ADON = 1; //turn on adc
ADCON0bits.GO_DONE = 1; //start converstion
while(GO_DONE); //wait for converstion to finish
ADCON0bits.ADON = 0; //turn off adc
adc_result = (ADRESH<<2) | (ADRESL>>6);
return adc_result;
}
adc_result = (ADRESH<<2) | (ADRESL>>6);
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?