標(biāo)題: Aduino之PWM控制實(shí)例 可以進(jìn)行DC Motor的控制速度 [打印本頁(yè)]

作者: liucheng1973    時(shí)間: 2018-6-26 13:38
標(biāo)題: Aduino之PWM控制實(shí)例 可以進(jìn)行DC Motor的控制速度
這個(gè)是用串口輸入數(shù)據(jù)進(jìn)行PWM控制的程序,實(shí)際用來(lái)可以進(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)




作者: lm18007726198    時(shí)間: 2018-6-27 12:05
請(qǐng)問(wèn)怎樣接線
作者: liucheng1973    時(shí)間: 2018-6-27 13:42
就用串口接收PC端發(fā)送Pluse數(shù)據(jù),由Aduino  Pin 9輸出控制Mos管對(duì)DC motor電源進(jìn)行控制




歡迎光臨 (http://www.torrancerestoration.com/bbs/) Powered by Discuz! X3.1