Tenofik:
Code:
#include <Servo.h>
Servo myservo;
// sterowanie silnikiem
int InA1 = 7;
int InB1 = 8;
int PWM1 = 9;
int skret = 0;
int pozycja = 0;
int szybkosc = 0;
void setup()
{
myservo.attach(3);
pinMode(InA1, OUTPUT);
pinMode(InB1, OUTPUT);
pinMode(PWM1, OUTPUT);
analogWriteSAH_Init();
}
void loop()
{
Skrec();
ustawSzybkosc();
}
void Skrec() {
pozycja = (skret+90);
if (pozycja != myservo.read()) {
myservo.write(pozycja);
delay(10);
}
}
void ustawSzybkosc() {
int moc = szybkosc*800/100;
digitalWrite(InA1, LOW);
digitalWrite(InB1, HIGH);
analogWriteSAH(moc);
}
//*******************************************
// THIS PART IS FROM THIS TOPIC http://forum.arduino.cc/index.php?topic=161643.0 :
void analogWriteSAH_Init( void )
{
// Stop the timer while we muck with it
TCCR1B = (0 << ICNC1) | (0 << ICES1) | (0 << WGM13) | (0 << WGM12) | (0 << CS12) | (0 << CS11) | (0 << CS10);
// Set the timer to mode 14…
//
// Mode WGM13 WGM12 WGM11 WGM10 Timer/Counter Mode of Operation TOP Update of OCR1x at TOV1 Flag Set on
// CTC1 PWM11 PWM10
// ---- ----- ----- ----- ----- ------------------------------- ---- ----------------------- -----------
// 14 1 1 1 0 Fast PWM ICR1 BOTTOM TOP
// Set output on Channel A and B to…
//
// COM1z1 COM1z0 Description
// ------ ------ -----------------------------------------------------------
// 1 0 Clear OC1A/OC1B on Compare Match (Set output to low level).
TCCR1A =
(1 << COM1A1) | (0 << COM1A0) | // COM1A1, COM1A0 = 1, 0
(1 << COM1B1) | (0 << COM1B0) |
(1 << WGM11) | (0 << WGM10); // WGM11, WGM10 = 1, 0
// Set TOP to…
//
// fclk_I/O = 16000000
// N = 1
// TOP = 799
//
// fOCnxPWM = fclk_I/O / (N * (1 + TOP))
// fOCnxPWM = 16000000 / (1 * (1 + 799))
// fOCnxPWM = 16000000 / 800
// fOCnxPWM = 20000
ICR1 = 799;
// Ensure the first slope is complete
TCNT1 = 0;
// Ensure Channel A and B start at zero / off
OCR1A = 0;
OCR1B = 0;
// We don’t need no stinkin interrupts
TIMSK1 = (0 << ICIE1) | (0 << OCIE1B) | (0 << OCIE1A) | (0 << TOIE1);
// Ensure the Channel A and B pins are configured for output
DDRB |= (1 << DDB1);
DDRB |= (1 << DDB2);
// Start the timer…
//
// CS12 CS11 CS10 Description
// ---- ---- ---- ------------------------
// 0 0 1 clkI/O/1 (No prescaling)
TCCR1B =
(0 << ICNC1) | (0 << ICES1) |
(1 << WGM13) | (1 << WGM12) | // WGM13, WGM12 = 1, 1
(0 << CS12) | (0 << CS11) | (1 << CS10);
}
void analogWriteSAH( uint16_t value )
{
if ( (value >= 0) && (value < 800) )
{
OCR1A = value;
}
}
Motor works good, using this code, but servo doesn't work at all. I tried diffrent digital PINs for it, but it didn;t help.
If I delete "analogWriteSAH_Init() and change "analogWriteSAH(moc);" to "analogWrite(PWM1, moc);" servo works, of course, but I need about <=20 kHz PWM frequency for my motor.
You’re still trying to use timer 1 and the servo library at the same time. If you want to use Servo, then stay away from PWM on pins 9 and 10 with timer 1. Use one of the other timers and work on different pins. You can’t do PWM on 9 or 10 and use Servo at the same time.