回覆列表
  • 1 # 使用者4912889400776

    /*任意週期和任意佔空比DUTY%的PWM*/

    #define CYCLE 6000L //定義PWM週期(最大值為32767)

    sbit PWM2=P3^7;

    sbit PWM3=P2^1;

    sbit PWM4=P2^2;

    void pwminit()

    { P_SW2 |= 0x80;

    PWMCFG = 0x00; //配置PWM的輸出初始電平為低電平

    PWMCKS = 0x0f; //選擇PWM的時鐘為Fosc/(0+1)

    PWMC = CYCLE; //設定PWM週期,定義PWM週期(最大值為32767)

    PWM2CR = 0x00; //選擇PWM2輸出到P3.7,不使能PWM2中斷

    PWM3CR = 0x00;

    PWM4CR = 0x00;

    PWM2T1 = 0x0001;

    PWM2T2 = 0;

    PWM3T1 = 0x0001;

    PWM3T2 = 0;

    PWM4T1 = 0x0001;

    PWM4T2 = 0;

    PWMCR |= 0x80; //使能PWM模組

    P_SW2 &=~0x80;

    }

    void pwm2( unsigned int DUTY) //PWM2

    {

    if(DUTY==0)

    {

    PWMCR &=~0x01;

    PWM2=0;

    }

    else if (DUTY==100)

    {

    PWMCR &=~0x01;

    PWM2=1;

    }

    else

    {

    P_SW2 |= 0x80; //使能訪問PWM在擴充套件RAM區的特殊功能暫存器XSFR

    PWM2T1 = 0x0001; //設定PWM2第1次反轉的PWM計數

    PWM2T2 = CYCLE * DUTY / 100; //設定PWM2第2次反轉的PWM計數

    P_SW2 &=~0x80; //佔空比為(PWM2T2-PWM2T1)/PWMC

    PWMCR |= 0x01; //使能PWM訊號輸出

    }

    }

    void pwm3(unsigned int DUTY) //PWM3

    {

    if(DUTY==0)

    {

    PWMCR &=~0x02;

    PWM3=0;

    }

    else if(DUTY==100)

    {

    PWMCR &=~0x02;

    PWM3=1;

    }

    else

    {

    P_SW2 |= 0x80;

    PWM3T1 = 0x0001;

    PWM3T2 = CYCLE * DUTY / 100;

    P_SW2 &=~0x80;

    PWMCR |= 0x02;

    }

    }

    void pwm4(unsigned int DUTY) //PWM4

    {

    if(DUTY==0)

    {

    PWMCR &=~0x04;

    PWM4=0;

    }

    else if (DUTY==100)

    {

    PWMCR &=~0x04;

    PWM4=1;

    }

    else

    {

    P_SW2 |= 0x80;

    PWM4T1 = 0x0001;

    PWM4T2 = CYCLE * DUTY / 100;

    P_SW2 &=~0x80;

    PWMCR |= 0x04;

    }

    }

  • 中秋節和大豐收的關聯?
  • 我聽到的歌詞是“啦啦啦啦啦啦啦,快點嫁給他”是什麼歌?男聲唱的?