找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開始

搜索
查看: 3359|回復(fù): 2
打印 上一主題 下一主題
收起左側(cè)

Aduino之PWM控制實(shí)例 可以進(jìn)行DC Motor的控制速度

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:345500 發(fā)表于 2018-6-26 13:38 | 只看該作者 回帖獎(jiǎng)勵(lì) |倒序?yàn)g覽 |閱讀模式
這個(gè)是用串口輸入數(shù)據(jù)進(jìn)行PWM控制的程序,實(shí)際用來可以進(jìn)行DC Motor的控制速度。

源程序如下:
  1. #include <avr/wdt.h>

  2. int duty = 0;
  3. byte lo , hi;

  4. void setup() {
  5.   // put your setup code here, to run once:
  6.   Serial.begin(115200);
  7.   Serial.setTimeout(500);    //setTimeout
  8.   pinMode(9,OUTPUT);
  9.   digitalWrite(2,LOW);
  10.   TCCR1A = 0;
  11.   TCCR1B = 0;
  12.   
  13.   TCCR1A = _BV(WGM11) | _BV(WGM10) | _BV(COM1A1) | _BV(COM1B1);
  14. //  TCCR1A = _BV(WGM11) | _BV(WGM10);
  15.   TCCR1B = _BV(CS10) | _BV(WGM12);
  16.   OCR1A = 0;
  17.   OCR1B = 1000;
  18.   wdt_enable(WDTO_1S);
  19. }

  20. void loop() {
  21.   // put your main code here, to run repeatedly:
  22.   if(Serial.available()>0)
  23.   {
  24.     lo = Serial.read();       //hi = 0x04, lo =0x00
  25. //    Serial.write(Serial.available());
  26.     if( Serial.readBytesUntil(240,&hi,1) != 0)
  27.     {
  28.       if( hi < 4)
  29.       {
  30.         duty = (hi << 8) + lo ;
  31.         OCR1A = duty;
  32. //      Serial.write(lo);
  33. //      Serial.write(hi);   
  34.       }
  35.       else if(hi == 0x70)
  36.         Serial.write(lo);
  37.     }
  38. //    Serial.write(Serial.available());   
  39.   }
  40.   wdt_reset();
  41. }


  42. void software_Reset()
  43. {
  44.   asm volatile("  jmp 0");
  45. }
復(fù)制代碼

所有資料51hei提供下載:
motorSetTimeout.zip (819 Bytes, 下載次數(shù): 11)



評(píng)分

參與人數(shù) 1黑幣 +50 收起 理由
admin + 50 共享資料的黑幣獎(jiǎng)勵(lì)!

查看全部評(píng)分

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏1 分享淘帖 頂 踩
回復(fù)

使用道具 舉報(bào)

沙發(fā)
ID:258474 發(fā)表于 2018-6-27 12:05 | 只看該作者
請(qǐng)問怎樣接線
回復(fù)

使用道具 舉報(bào)

板凳
ID:345500 發(fā)表于 2018-6-27 13:42 | 只看該作者
就用串口接收PC端發(fā)送Pluse數(shù)據(jù),由Aduino  Pin 9輸出控制Mos管對(duì)DC motor電源進(jìn)行控制
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

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

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

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