这个是用串口输入数据进行PWM控制的程序,实际用来可以进行DC Motor的控制速度。
源程序如下:
- #include <avr/wdt.h>
- int duty = 0;
- byte lo , hi;
-
- void setup() {
- // put your setup code here, to run once:
- Serial.begin(115200);
- Serial.setTimeout(500); //setTimeout
- pinMode(9,OUTPUT);
- digitalWrite(2,LOW);
- TCCR1A = 0;
- TCCR1B = 0;
-
- TCCR1A = _BV(WGM11) | _BV(WGM10) | _BV(COM1A1) | _BV(COM1B1);
- // TCCR1A = _BV(WGM11) | _BV(WGM10);
- TCCR1B = _BV(CS10) | _BV(WGM12);
- OCR1A = 0;
- OCR1B = 1000;
- wdt_enable(WDTO_1S);
- }
- void loop() {
- // put your main code here, to run repeatedly:
- if(Serial.available()>0)
- {
- lo = Serial.read(); //hi = 0x04, lo =0x00
- // Serial.write(Serial.available());
- if( Serial.readBytesUntil(240,&hi,1) != 0)
- {
- if( hi < 4)
- {
- duty = (hi << 8) + lo ;
- OCR1A = duty;
- // Serial.write(lo);
- // Serial.write(hi);
- }
- else if(hi == 0x70)
- Serial.write(lo);
- }
- // Serial.write(Serial.available());
- }
- wdt_reset();
- }
- void software_Reset()
- {
- asm volatile(" jmp 0");
- }
复制代码
所有资料51hei提供下载:
motorSetTimeout.zip
(819 Bytes, 下载次数: 11)
|