/* * 设置步骤见datasheet 的page 68 setup for PWM operation */
#include #define uchar unsigned char #define uint unsigned int // CONFIG #pragma config FOSC = HS // 12MHZ ???? #pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT enabled) #pragma config PWRTE = ON // Power-up Timer Enable bit (PWRT disabled) #pragma config BOREN = ON // Brown-out Reset Enable bit (BOR enabled) #pragma config LVP = OFF // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3/PGM pin has PGM function; low-voltage programming enabled) #pragma config CPD = OFF // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off) #pragma config WRT = OFF // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control) #pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off)
void delay1ms(uint DelayTime)
{
uint temp;
for(;DelayTime>0;DelayTime--)
{
for(temp=0;temp<270;temp++)
{;}
}
}
void main(void)
{
//***********************************************************
CCP1M3=1;//Enable CPP1 Work as PWM mode
CCP1M2=1;
CCP1M1=1;
CCP1M0=1;
TRISC2=0;//Enable RC2 work as output
CCPR1L=0x10;//PWM脉宽值,改变PWM的脉宽,只需改变CCPR1L中的值
CCP1X=0;//以及CCP1CON中的CCP1X和CCP1Y这两个位的值
CCP1Y=0;
//***********************************************************
CCP2M3=1;//Enable CPP2 Work as PWM mode
CCP2M2=1;
CCP2M1=1;
CCP2M0=1;
TRISC1=0;//Enable RC1 work as output
CCPR2L=0x20;//PWM脉宽值,改变PWM的脉宽,只需改变CCPR2L中的值
CCP2X=0;//以及CCP2CON中的CCP2X和CCP2Y这两个位的值
CCP2Y=0;
//***********************************************************
TOUTPS3=0;//Enable TMR2--->1:1
TOUTPS2=0;
TOUTPS1=0;
TOUTPS0=0;
T2CKPS1=1;//Enable TMR2--->1:16
T2CKPS0=1;
TMR2ON=1;//Enable TMR2 Working
TMR2=0;
while(1)
{
for(;num <= 1023; num++)
{
PR2=num;//PWM周期,改变PWM的周期,只需改变PR2中的值即可
delay1ms(10);
}
}
}