DOC

pwm

By Jane Cooper,2014-09-04 17:09
14 views 0
pwm

#include

sbit MA = P3^0;

    sbit MB = P3^1;

    sbit PWM = P3^7;

bit KeyFree;

    unsigned char PWM_OUT;

    void Delay(unsigned char); //声明延时函数 unsigned char KeyScan(void); void keyProcess(unsigned char);

    main()

    {

     TMOD = 0x01;

     TH0 = - 50000 /256;

     TL0 = - 50000 % 256;

     IE = 0x82;

     TR0 = 1;

     while (1)

     {

     keyProcess(KeyScan());

     }

    }

    void T0_INT () interrupt 1 //定时器函数控制PWM输出 {

     TH0 = -5000 / 256;

     TL0 = -5000 % 256;

     PWM=1; //设置PWM输出

     Delay(PWM_OUT);

     PWM=0;

     Delay(100-PWM_OUT); }

    void Delay(unsigned char x) {

     char i;

     while(x--) for(i = 0;i < 200; i++);

    }

    unsigned char KeyScan(void) {

     // P1|=0x1f;

     if((P1&0x1f)!=0x1f)

     {

     Delay(10); //延时去抖

     }

     return P1&0x1f;

    }

    void keyProcess(unsigned char key) {

    if(key==0x1f) /*******************/

     { /* */

     KeyFree=1; /* 软件松手检测 */

     } /* */

     else /* */

     { /* */

     //if(KeyFree==1) /* */

     // { /*******************/

     KeyFree=0;

    /*******************************************/

     switch (key)

     {

     case 0x1e : {

     MA=1;

     MB=0;

     break;}

     case 0x1d : {

     MA=0;

     MB=1;

     break;}

     case 0x1b : {

     MA=0;

     MB=0;

     PWM_OUT =0;

     break;}

     case 0x17 : { MA=1;

     MB=1; //这里的30;60;90秒有问题

     if(PWM_OUT!=90){

     PWM_OUT+=30;}

     break;}

     case 0x0f : { MA=1;

     MB=1;

     if(PWM_OUT!=30&&PWM_OUT!=0){

     PWM_OUT-=30;}

     break;}

     }

    /*******************************************/

     }

    }

Report this document

For any questions or suggestions please email
cust-service@docsford.com