你用過552,我想問一下我用定時器控制pwm輸出,按鍵脈寬怎么不能實現的
#include "CH552.H"
#include "Debug.H"
#include "Timer.H"
#include "stdio.h"
#include "GPIO.H"
#pragma NOAREGS
//UINT8 FLAG;
//UINT16 Cap[8] = {0};
//typedef unsigned int uint16_t;
sbit LED1 = P3^4;
sbit led2 = P1^3;
sbit key = P1^4;
unsigned int condition=0;//這里高電平占一份
unsigned int cnt; //整個周期
void button();
void main( )
{
CfgFsys( ); //CH554時鐘選擇配置
mDelaymS(5); //修改主頻,建議稍加延時等待主頻穩定
Port3Cfg(1,4);
Port1Cfg(1,3);
Port1Cfg(3,4);
mTimer0Clk12DivFsys(); //T0定時器時鐘設置
mTimer_x_ModInit(0,2); //T0 定時器模式設置
mTimer_x_SetData(0,0x5555); //T0定時器賦值
mTimer0RunCTL(1); //T0定時器啟動
ET0 = 1; //T0定時器中斷開啟
EA = 1;
while(1)
{
if(cnt>=100)
{
LED1=0;
}
if(cnt<condition)
{
LED1=1;
}
else
{
LED1=0;
}
button();
}
}
void Time0Interrupt(void) interrupt INT_NO_TMR0 using 1
{
mTimer_x_SetData(0,0x5555); //T0定時器賦值
cnt++;
}
void button()
{
if(key==0)
{
mDelaymS(5);
if(key==0)
{
condition=condition+30; //調制脈寬
led2=!led2; //led燈反轉
if(condition>=90) //如果脈寬等于總周期了等于0
{
condition=90;
}
while(!key); //等待按鍵松手
}
}
} |