PIC单片机PWM模块应用实验程序
扫描二维码
随时随地手机看文章
#include
//------------------------------------------------------------
//使用PIC16F887芯片,输出50kHz占空比50%的方波
//------------------------------------------------------------
//系统配置
//__CONFIG(HS&PWRTEN&BOREN&PROTECT&WDTDIS);
__CONFIG(INTIO&WDTDIS&PWRTEN&MCLRDIS&UNPROTECT&BORDIS&LVPDIS&IESODIS&FCMDIS);
//-------------中断服务程序------------------------------------
voidinterruptisr()
{
if(TMR2IE==1&&TMR2IF==1)
{TMR2IF=0;
}
}
voidSpwmInit()
{
OSCCON=0x7f;//usinginner8Mosc
TRISC1=1;
TRISC2=1;
//TRISD=0;
PR2=0x27;
CCP1CON=0x0f|0x00;
CCPR1L=0x14;
TMR2IF=0;
T2CON=0;
GIE=0;
TMR2IE=1;
PEIE=1;
TMR2ON=1;
}
main()
{SpwmInit();
TRISC1=0;
TRISC2=0;
while(1)
{;}
}