Thanks in advance!ĮDIT: I'm using VS Code 1.62.0 with Arduino Extension 0.4.7 and C/C++ 1.7.1 I'm embedding the code below, and I'll attach my serial data output. Either one of these I have not been able to find as the serial printouts confirm desired settings.Ĭan anyone please help find where I've gone wrong?, parhaps? Somehow, either the prescale bits in the timer are not being set properly, thus leading to a higher frequency count, or the OCR registers are not being written properly.
However, since my drive is going way too fast during the "error loops", that cannot be the cause. If the interrupt were being interfered with (blocked), then my drive would not speed up and would stay at the initial OCR value. For one, I set and confirm the OCR value at multiple points prior to starting the movement. I had originally thought that the Timer0 interrupts might be taking excessive time and interfering with my speed ramp, but I no longer think that is the case. Directly accessing the OCR values just prior to starting the movement also confirms the correct settings. While some of the values are quite large, I'm using large data types, and the resulting OCR values seem to match up to my manual calculations. Furthermore, I've printed out the OCR array values and confirmed them against manual calculations - no issues found. I've looked through this, and looked up all of the C++ standard promotions, but I can't seem to find anywhere that would cause a problem in the CalculateSpeedRamp() member function. Some sort of overflow or other calculation / data type error. Even with serial printing all of the register values (directly - not using my class structure), I cannot see anything wrong with the settings just before starting the timer/counter.
Perhaps there is something out of order that I'm simply not seeing.
I've written many successful test sketches directly manipulating the timer control registers to get a square wave output, but I've never wrapped them in a class like this. Through the class structure, I'm setting timer control registers in an incorrect order or something of the sort. I've also tried (total shotgun move here) "synchronizing" the prescale for Timers 1,3,4,5 (GTCCR |= 1 ), but I don't think I fully grasp exactly what this does, and it did not resolve the issue.Īfter all of my troubleshooting, I've stayed consistent with two potential root causes (although I can't seem to locate the smoking gun for either): Unfortunately, with all of the serial removed from my sketch, the incorrect behavior remains. While reading through the datasheet to find potential causes to my issue, I noticed that the USART0 interrupts have a higher priority than the Timer 3 interrupts, so I suspected maybe something with the serial dumps in my test were interfering with the timer output compare interrupt. I want to potentially use micros() and/or millis() for this application, so I'm steering clear of messing with Timer 0. I know that the init() function adjusts the timer registers before setup, so I clear the control registers for timers 1 through 5 in setup(). Loops 2 and 3, as well as up to 10 have shown the expected behavior, but I have not gone beyond 10 to look for any deeper patterns. Unfortunately, I am experiencing very odd behavior out of the timer during the first loop of my sketch (as well as the fourth loop) where The timer seems to be at a much higher frequency than expected, thus running the motor at some awful speed and not the currently set "maxSpeed". To test this class structure, I've written a sketch to test run the stepper motor for one revolution (of the subassembly), increase the speed, and repeat. I plan on writing more classes which will inherit CL42T and will have subassembly context member functions (i.e. This class sets the pointers to the appropriate addresses in the constructor, and has member functions for SetupDrive() (set the timer control registers, clear count, etc.), Enable/DisableDrive (set enable pin high/low), SetDirection(char) (set direction pin high or low), run/stop (start/stop timer), and Increment (set target steps, turn on timer interrupt, call RunDrive()). Stepper Speed Ramp Class: contains speed ramp information like max speed, acceleration, deceleration, an array for OCR values, and a couple member functions for setting max speed and calculating the speed ramp (OCR values in the array).ĬL42T Class: this class inherits (publicly) the Stepper Speed Ramp class, and contains timer register pointers, I/O port register pointers, timer masks, and a few other member variables. The essence of what I'm going for is the following: I'm writing a class for a stepper motor driver, and I'm using the arduino timers (Timer 3) to control the step pulses to the motor driver. Hello, everyone! This is my first post, so first off: thank you to everyone around this community for all of the help you've unknowingly provided to me over the years