找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 8184|回復(fù): 1
收起左側(cè)

51單片機(jī)小車制作 STEP2.3 直流電機(jī)PWM按鍵調(diào)速

[復(fù)制鏈接]
ID:65608 發(fā)表于 2014-9-12 01:48 | 顯示全部樓層 |閱讀模式
本帖最后由 daliu 于 2014-9-12 01:49 編輯


上面的如果播放不了,可以去這個(gè)地址看http://www.tudou.com/programs/view/Yk844_HjfJ0/
思路什么的,貼吧有http://tieba.baidu.com/p/2807615491?pn=3



#include

sbit PWM_OUT1P=P0^5;
sbit PWM_OUT2P=P0^6;
sbit PWM_OUT3P=P0^7;
sbit key1=P0^0;
sbit key2=P0^1;
bit PWM_OUTDAT1;
bit PWM_OUTDAT2;
bit PWM_OUTDAT3;
bit delaybit1=0; //延時(shí)開關(guān)
bit delaybit2=0;

unsigned int PWM_t1=0;
unsigned int PWM_t2=0;
unsigned int PWM_t3=0;
unsigned int delaytime_res1=0;//延時(shí)長度計(jì)數(shù)
unsigned int delaytime_res2=0;
unsigned int keybit=0; //按鍵計(jì)數(shù)

PWM_OUT1(unsigned int Cycle1,unsigned int PWM_on1);
PWM_OUT2(unsigned int Cycle2,unsigned int PWM_on2);
PWM_OUT3(unsigned int Cycle3,unsigned int PWM_on3);


void main()
{
TMOD=0x01;
TH0=(65536-100)/256;
TL0=(65536-100)%6;
IE=0x82;
TR0=1;
key1=1;
key2=1;
keybit=0;

while(1)
{
if(key1==0)
{
delaybit1=1;
if(delaytime_res1==2)
{
if(key1==0)
{
if(keybit<7)
keybit++;
}
}
}
  else
  {
  delaytime_res1=0;
delaybit1=0;
  }

if(key2==0)
{
delaybit2=1;
if(delaytime_res2==2)
{
if(key2==0)
{
if(keybit>0)
keybit--;
}
}
}
else
{
delaybit2=0;
delaytime_res2=0;
}

switch(keybit)
{

case 1:
PWM_OUT1P=PWM_OUT1(1000,900);
PWM_OUT2P=PWM_OUT2(1000,900);
break;
case 2:
PWM_OUT1P=PWM_OUT1(1000,700);
PWM_OUT2P=PWM_OUT2(1000,700);
break;
case 3:
PWM_OUT1P=PWM_OUT1(1000,500);
PWM_OUT2P=PWM_OUT2(1000,500);
break;
case 4:
PWM_OUT1P=PWM_OUT1(1000,300);
PWM_OUT2P=PWM_OUT2(1000,300);
break;
case 5:
PWM_OUT1P=PWM_OUT1(1000,100);
PWM_OUT2P=PWM_OUT2(1000,100);
break;
case 6:
PWM_OUT1P=PWM_OUT1(1000,0);
PWM_OUT2P=PWM_OUT2(1000,0);
break;
}

  }
}

void Time_r() interrupt 1
{
TH0=(65536-100)/256;
TL0=(65536-100)%6;
PWM_t1++;
PWM_t2++;
PWM_t3++;
if(delaybit1==1)
{
delaytime_res1++;
}
if(delaybit2==1)
{
delaytime_res2++;
}
}

PWM_OUT1(unsigned int Cycle1,unsigned int PWM_on1)
{
if(PWM_t1==PWM_on1)
PWM_OUTDAT1=0;
else if(PWM_t1==Cycle1)
{
PWM_t1=0;
if(PWM_on1!=0)
PWM_OUTDAT1=1;
}

return PWM_OUTDAT1;
}

PWM_OUT2(unsigned int Cycle2,unsigned int PWM_on2)
{
if(PWM_t2==PWM_on2)
PWM_OUTDAT2=0;
else if(PWM_t2==Cycle2)
{
PWM_t2=0;
if(PWM_on2!=0)
PWM_OUTDAT2=1;
}
return PWM_OUTDAT2;
}


回復(fù)

使用道具 舉報(bào)

ID:74784 發(fā)表于 2015-3-19 11:14 | 顯示全部樓層
單片機(jī)與電機(jī)之間應(yīng)該加入驅(qū)動(dòng)電路
回復(fù)

使用道具 舉報(bào)

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表