Skip to main content
Visitor II
July 2, 2021
Question

STM8S105C6 - How to use and configure clock to control and accelerate an PMDC motor using 3 timers.

  • July 2, 2021
  • 0 replies
  • 741 views

Hello, I need to make this stm8s105c6 to control an 3.0 V PMDC motor using timers and microcontroller's clock. Also, I need to use pwm and this driver: EV - VNH7040AY. Timers:

  • timer 1 -> accelerate to nominal power
  • timer 2 -> nominal power
  • timer 3 -> decelerate to 0

I am totaly new and idk how to declare / write complicated sequences. I created an prototype with arduino and L298N. It is possible to translate it to STVD? Any suggestions? Thanks for attention!

Source code Arduino:

// PWM PROTOTYPE - DC MOTOR CONTROL USING ARDUINO UNO AND L298N MOTOR DRIVER - BUSARU ROBERT - CRISTIAN
 
//Initializing pwm pin
int ena = 10; // pwm pin
int in1 = 2; // motor direction
int in2 = 4; // motor direction
 
//Initializing timers;
int tim1 = 3000; // acceleration time
int pwm1 = 255;
 
 
int tim2 = tim1 + 2000; // nominal power time
int pwm2 = 255;
 
int tim3 = tim2 + 3000; // deceleration time
 
 
unsigned long myTime; // clock
 
 
 
void setup() {
 //Declaring pwm pin as output
 pinMode(ena, OUTPUT);
 pinMode(in1, OUTPUT);
 pinMode(in2, OUTPUT);
 //Declaring Serial Monitor to see timers and what happen there;
 Serial.begin(9600);
}
 
 
void phase1() {
 myTime = millis();
 Serial.println(myTime);
 
 
}
 
 
void loop() {
 
 myTime = millis();
 Serial.println(myTime);
 if (myTime <= tim1) { //TIM 1 -> accelerating time
 // currencyTime = myTime;
 for (int i = 0; i < pwm1; i++) {
 analogWrite(ena, i);
 }
 Serial.print("Accelerare: ");
 } else if (myTime > tim1 && myTime < tim2) { //TIM 2 -> nominal power time
 // currencyTime = myTime;
 analogWrite(ena, pwm2);
 Serial.print("Nominal: ");
 } else if (myTime >= tim3 && myTime <= tim3 + tim2 + tim1) { //TIM3 -> deceleration time
 for (int i = pwm2; i >= 0; i--) {
 analogWrite(ena, i);
 if (i == 0 && myTime > tim1 + tim2 + tim3) {
 analogWrite(ena, 0);
 myTime = 0;
 }
 }
 Serial.print("Decelerare: ");
 }
 
 
 digitalWrite(in1, HIGH);
 digitalWrite(in2, LOW);
 
}

    This topic has been closed for replies.