//PWM波形輸出實驗// 晶振:4MHz
// CPU:PIC16F877A
本文引用地址:http://m.butianyuan.cn/article/201611/321025.htm#include //調用頭文件
__CONFIG(0x3F31); //芯片配置字
#defineucharunsignedchar
#defineuintunsignedint
uchar a;
void CCP1INIT() ;
voiddelayms(uint z);
voidkeyscan();
void main(void)
{
CCP1INIT();
T2CON=0X04;
do
{
keyscan();
}
while(1);
}
void CCP1INIT()
{
PORTB=0xff;
TRISB=0xff;
RBPU=0;
a=0X7F;
CCPR1L=a;
CCP1CON=0X3C;
INTCON=0X00;
PR2=0XFF;
TRISC=0XFB;
}
//通過按鍵分別調大和調小(通過示波器觀看PWM波形的變化)
voidkeyscan()
{
TRISB=0xff;
if(RB1==0)
{
delayms(10);
if(RB1==0)
{
a-=10;
CCPR1L=a;
}
while(RB1==0);
}
if(RB2==0)
{
delayms(10);
if(RB2==0)
{
a+=10;
CCPR1L=a;
}
while(RB2==0);
}
}
////////////延時子函數//////////////////
void delayms(uint z)
{
uint x,y;
for(x=z;x--;x>0)
for(y=40;y--;y>0);
}
評論