c8051f005單片機(jī)PWM測試程序
c8051f005單片機(jī)PWM測試程序
//PWM輸出腳P0.3
//-----------------------------------------------------------------------------
//Includes
//-----------------------------------------------------------------------------
#include
#include
#include
//-----------------------------------------------------------------------------
//FunctionPROTOTYPES
//-----------------------------------------------------------------------------
voidPORT_Init(void);
voidPCA_Init(void);
voidPCA_ISR(void);
voidSYSCLK_Init(void);
//-----------------------------------------------------------------------------
//MAINRoutine
//-----------------------------------------------------------------------------
voidmain(void)
{
unsignedlongb;
WDTCN=0xde;//dISAblewatchdogtimer
WDTCN=0xad;
SYSCLK_Init();
PORT_Init();/*初始化I/O口*/
PCA_Init();/*PCA初始化成8位脈寬調(diào)置方式*/
EIE1=0x08;
EA=1;
PCA0CPH0=0x20;
while(1)
{}
}
//-----------------------------------------------------------------------------
//InitializationSubroutines
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
//PORT_Init
//-----------------------------------------------------------------------------
voidPORT_Init(void)
{
XBR0=0x11;//XBAR0:InitialResetValue
XBR1=0x00;//XBAR1:InitialResetValue
XBR2=0x40;//XBAR2:InitialResetValue
//Portconfiguration(1=PushPullOutput)
PRT0CF=0x08;//OutputconfigurationforP0
PRT1CF=0x00;//OutputconfigurationforP1
PRT2CF=0x00;//OutputconfigurationforP2
PRT3CF=0x00;//OutputconfigurationforP3
}
//-----------------------------------------------------------------------------
//SYSCLK_Init
//-----------------------------------------------------------------------------
voidSYSCLK_Init(void)
{
unsignedinti;//delaynmscounter
OSCXCN=0x67;//startexternaloscillatorwith
for(i=0;i<256;i++);//XTLVLDblankinginterval(>1ms)
while(!(OSCXCN&0x80));//Waitforcrystalosc.tosettle
OSCICN=0x88;//selectexternaloscillatorasSYSCLK
}
//-----------------------------------------------------------------------------
//PCA_Init
//-----------------------------------------------------------------------------
voidPCA_Init(void)
{
//PCA0CPM0=0x42;//PCACapture/CompareRegister0
PCA0CN=0x40;
PCA0CPM1=0x42;
PCA0CPL0=0x00;
PCA0CPH0=0x00;
//PCA0MD&=0x40;
PCA0MD=0x01;
}
//-----------------------------------------------------------------------------
//PCA_ISR
//-----------------------------------------------------------------------------
voidPCA_ISR(void)interrupt9
{unsignedchara;
bitPWM_PAC;
CF=0;
if(PWM_PAC==0)//占空比由0>>0xfe
{
PCA0CPH1=a;
a++;
if(a==0xfe){PWM_PAC=1;}
}
else//占空比由0xfe>>0
{
PCA0CPH1=a;
a--;
if(a==0){PWM_PAC=0;}
}
}