From: Ivan Stefanov Date: Tue, 8 Dec 2020 15:58:38 +0000 (+0200) Subject: Added stop function to DC Motor Demo X-Git-Url: https://kolegite.com/gitweb/?a=commitdiff_plain;h=f464b00569c00bb01318aa345c71395aacf77be9;p=vmks.git Added stop function to DC Motor Demo Added stop function to DC Motor Demo and COMPB interrupt(just for demonstration) to Timer project. --- diff --git a/Examples/DC_motor_and_servo/DC_motor_and_servo.ino b/Examples/DC_motor_and_servo/DC_motor_and_servo.ino index 70011fc..97fe86b 100644 --- a/Examples/DC_motor_and_servo/DC_motor_and_servo.ino +++ b/Examples/DC_motor_and_servo/DC_motor_and_servo.ino @@ -17,32 +17,34 @@ void turn_right(uint8_t speed); void setup() { - pinMode(A1, OUTPUT); - pinMode(A2, OUTPUT); - pinMode(EN_L, OUTPUT); - digitalWrite(A1, LOW); - digitalWrite(A2, LOW); - digitalWrite(EN_L, HIGH); - pinMode(A3, OUTPUT); - pinMode(A4, OUTPUT); - pinMode(EN_R, OUTPUT); - digitalWrite(A3, LOW); - digitalWrite(A4, LOW); - digitalWrite(EN_R, HIGH); + pinMode(A1, OUTPUT); + pinMode(A2, OUTPUT); + pinMode(EN_L, OUTPUT); + digitalWrite(A1, LOW); + digitalWrite(A2, LOW); + digitalWrite(EN_L, HIGH); - aservo.attach(SERVO_PIN); + pinMode(A3, OUTPUT); + pinMode(A4, OUTPUT); + pinMode(EN_R, OUTPUT); + digitalWrite(A3, LOW); + digitalWrite(A4, LOW); + digitalWrite(EN_R, HIGH); + + aservo.attach(SERVO_PIN); } void loop() { - /* - digitalWrite(A1, HIGH); + /* + digitalWrite(A1, HIGH); analogWrite(A2, 128); digitalWrite(EN_L, HIGH); - - aservo.write(90); - */ - + */ + + //aservo.write(90); + + /* for (uint8_t i = 0; i < 4; i++) { forward(255); @@ -52,36 +54,45 @@ void loop() } digitalWrite(EN_L, LOW); digitalWrite(EN_R, LOW); + */ } void forward(uint8_t speed) { - analogWrite(A1, speed); - digitalWrite(A2, LOW); - analogWrite(A3, speed); - digitalWrite(A4, LOW); + analogWrite(A1, speed); + digitalWrite(A2, LOW); + analogWrite(A3, speed); + digitalWrite(A4, LOW); } void reverse(uint8_t speed) { - analogWrite(A2, speed); - digitalWrite(A1, LOW); - analogWrite(A4, speed); - digitalWrite(A3, LOW); + digitalWrite(A1, LOW); + analogWrite(A2, speed); + digitalWrite(A3, LOW); + analogWrite(A4, speed); } void turn_left(uint8_t speed) { - analogWrite(A2, speed); - digitalWrite(A1, LOW); - analogWrite(A3, speed); - digialWrite(A4, LOW); + digitalWrite(A1, LOW); + analogWrite(A2, speed); + analogWrite(A3, speed); + digitalWrite(A4, LOW); } void turn_right(uint8_t speed) { - analogWrite(A1, speed); - digitalWrite(A2, LOW); - analogWrite(A4, speed); - digitalWrite(A3, LOW); + analogWrite(A1, speed); + digitalWrite(A2, LOW); + digitalWrite(A3, LOW); + analogWrite(A4, speed); +} + +void stop_motors() +{ + digitalWrite(A1, LOW); + digitalWrite(A2, LOW); + digitalWrite(A3, LOW); + digitalWrite(A4, LOW); } diff --git a/Examples/Timer_1_COMPA_and_OVF_10kHz_PWM/Timer_1_COMPA_and_OVF_10kHz_PWM.ino b/Examples/Timer_1_COMPA_and_OVF_10kHz_PWM/Timer_1_COMPA_and_OVF_10kHz_PWM.ino index a592e20..efa2118 100644 --- a/Examples/Timer_1_COMPA_and_OVF_10kHz_PWM/Timer_1_COMPA_and_OVF_10kHz_PWM.ino +++ b/Examples/Timer_1_COMPA_and_OVF_10kHz_PWM/Timer_1_COMPA_and_OVF_10kHz_PWM.ino @@ -5,7 +5,7 @@ void setup() { // Stop reception of interrupts noInterrupts(); //cli(); - // Set PB1 to be an output (Pin9 Arduino UNO) + // Set PB1 to be an output (Pin-9 Arduino UNO) DDRB |= (1 << PB1); // Clear Timer/Counter Control Registers @@ -15,6 +15,7 @@ void setup() { // Set non-inverting mode - Table 15-3 (page 108) TCCR1A |= (1 << COM1A1); + TCCR1A |= (1 << COM1B1); // Set Fast-PWM Mode (Mode 14) - Table 15-5 (page 109) TCCR1A |= (1 << WGM11); @@ -27,9 +28,11 @@ void setup() { // Set PWM frequency/top value - Output PWM 10kHz ICR1 = 199; // Fclk_io / (Fout * Prescaler) - 1 OCR1A = 100; // Output OC1A will be ON for [OCR1A/(ICR1+1)]% of the time -> 100/(199+1) = 50% + OCR1B = 50; // Enable compare match interrupt TIMSK1 |= (1 << OCIE1A); + TIMSK1 |= (1 << OCIE1B); TIMSK1 |= (1 << TOIE1); // Set prescaler to 8 and starts PWM @@ -48,6 +51,11 @@ ISR(TIMER1_COMPA_vect) { digitalWrite(13, LOW); } +//Timer1 Compare Match Interrupt turns OFF pin 13 (LED) +ISR(TIMER1_COMPB_vect) { + //digitalWrite(13, LOW); +} + //Timer1 Overflow Interrupt turns ON pin 13 (LED) ISR(TIMER1_OVF_vect) { digitalWrite(13, HIGH);