Stas Mamaischi
Newbie level 6
I am a beginner in programming and microcontrollers but I would like to generate pulses with pic 16f886 to 310-630 rpm in the period 0.2 second. Can help me ? . Thank you forward.
Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
#define _XTAL_FREQ 5000000
// Main function
void main(void)
{
TRISC2 = 0; // Make CCP1 pin as output
CCP1CON = 0x0C; // Configure CCP1 module in PWM mode
PR2 = 0xFF;
T2CON = 0x01; //
SetPWMDutyCycle(0);
T2CON |= 0x04;
}
void PWMDuty(unsigned int DutyCycle)
{
CCPR1L = DutyCycle>>2;
CCP1CON &= 0xCF;
CCP1CON |= (0x30&(DutyCycle<<4));
}
PWMDuty(512); //50% duty cycle
while(1)
{
}
}
In the PIC16f886 microcontroller datasheet there will be a section that covers PWM (the CCP) section. It will tell you have to setup the control registers, set your PWM frequency (what you call RPM), and your duty cycle. It has all the formulas and everything in there. Basically it is just setting some registers and setting your timer scalar correctly.
#define _XTAL_FREQ 5000000
// Main function
void main(void)
{
TRISC2 = 0; // Make CCP1 pin as output
CCP1CON = 0x0C; // Configure CCP1 module in PWM mode
PR2 = 0xFF;
T2CON = 0x01; //
SetPWMDutyCycle(0);
T2CON |= 0x04;
}
void PWMDuty(unsigned int DutyCycle)
{
CCPR1L = DutyCycle>>2;
CCP1CON &= 0xCF;
CCP1CON |= (0x30&(DutyCycle<<4));
}
PWMDuty(512); //50% duty cycle
while(1)
{
}
}