89C52 PWM輸出程序
#include reg52.h> //Keil header file name
#include "use.h"
#include "Ver.h"
//extern void LedOut(unsigned char *pLed);
#define Pmax 55000 //55000 //42666(18.4320)
#define Ki 2.2 //i=2
//----------------------------
sbit PwinPin=P3^5;
//----------------------------
typedef union
{
unsigned int Pwm; //pwm的數(shù)據(jù)
unsigned char PwmUse[2]; //[0]=H;[1]=L
}PWM;
//----------------------------
PWM PwmData;
unsigned int PwmBak;
//----------------------------
unsigned char PwmF=0; //輸出的1,0的標志
/*****************************************
PWM計算,輸出
t0=T50ms(b0/[fs-fo]+Sgn(U)*20%+a1
;T50ms=Pwm50+Ah
;B0fo=>0V;Must Bo In(Fo-Fs)
*******************************************/
void PwmOut(void)
{
float Pwm,i,k,j;
k=Pmax+(Mp1.AH+OminH)*Ki;
i=(PminL+Mp1.AL)*Ki;
//----------------------------
//LedOut(LedBuf); //LED顯示輸出
//----------------------------
j=(LedMenu.B-Mp1.Fo)/(Mp1.Fs-Mp1.Fo);
if(j0) j=0;
if(j>1) j=1;
Pwm=j*k; //Bo/(Fs-Fo)*Pk
j=Pwm*0.8+k*0.2+i; //先計算
if(Mp1.U==1) {Pwm=j;}
else Pwm=Pwm+i;
TR0=0;
PwmBak=(unsigned int)Pwm; //更新數(shù)據(jù)
TR0=1;
}
//-------------------------------
void Init_Pwm(void)
{
PwmData.Pwm=0;
PwmPin=0; //輸出0 (地點平)
// PwmOut();
}
//定時器0 中斷處理 pwmout()
void T0_Do(void) interrupt 1{
TR0=0;
if(PwmF==0) //輸出高電平
{
PwmData.Pwm=-PwmBak;
TH0=PwmData.PwmUse[0];
TL0=PwmData.PwmUse[1];
PwmPin=1; //0 輸出
PwmF=1;
}else{
PwmData.Pwm=-PwmData.Pwm;
TH0=PwmData.PwmUse[0];
TL0=PwmData.PwmUse[1];
PwmPin=0; //1 輸出
PwmF=0;
}
TR0=1;
}
pwm相關(guān)文章:pwm原理
評論