用PIC16F1937的CCP1,CCP4发生两路PWM,时钟源由TIME2供给
以下为示例程序:
#include
#define uint16 unsigned int
__CONFIG(FOSC_INTOSC&WDTE_OFF&PWRTE_OFF&MCLRE_ON&CP_OFF
&BOREN_OFF&CLKOUTEN_OFF&IESO_OFF&FCMEN_OFF);
__CONFIG(WRT_OFF&VCAPEN_OFF&PLLEN_OFF&STVREN_OFF&BORV_19&LVP_OFF);
#define BACK_LED RC1
void delay(uint16 us)
{
while(us–);
}
void interrupt isr()
{
if((TMR2IE==1)&&(TMR2IF==1))
{
TMR2IF=0;
}
}
void Init_PWM1_2(void)//CCP4和CCP1
{
TRISD1=1; //CCP4禁驱动
PR2=0X65;
CCP4CON=0X0C; //CCP4规范PWM输出
CCPR4L=0X15; //CCP4的占空
CCPTMRS0=0X00; //CCP1&4都用TIME2
//PR4=0X65;
//CCP
CCP1CON=0X0C; //CCP1规范PWM输出
CCPR1L=0X32; //CCP1的占空
TRISC2=0; //使能CCP驱动引脚
TRISD1=0;
//TMR2IE=1;
//TMR2IF=0;
//PEIE=1;
T2CON=0X05; //预分频4
//GIE=0;
}
void main()
{
OSCCON=0X70; //8MHZ
delay(50000);
TRISC1=0;
BACK_LED=1;
Init_PWM1_2();
while(1);
}