HCMotor Arduino library for driving DC and stepper motors.

Useful guides, libraries, and example sketches to support our Arduino based products.
Teryo
Posts: 4
Joined: Mon Aug 12, 2019 7:55 pm

Re: HCMotor Arduino library for driving DC and stepper motors.

Post by Teryo » Sun Sep 01, 2019 8:13 am

Hi Andrew,

As a newbie I was digging deep on the “surface” I found Javier Valencia code dealing with timers. I found also some improvements added by Bill Perry for use timer4 on Atmega32u4. I also read data sheet and try to figure out what to add/correct in your program. I might be totally wrong in my thoughts, so can you please revise it and comment if you want or are in the right mood. Here is the code:

// Set timer mode
TCCR4A = (1<<WGM41); // here is the big ... :) Q should it be PWM4A or WGM41 set to 1.
// Set the prescaler
TCCR4B = prescaler;
// Clear timer 4 counter
TCNT4 = 0;
// Set the compare match register
OCR4A = compare;
// Enable timer 4 interrupt on compare match
TIMSK4 = (1<<OCIE4A);

In the Atmega32u4 data sheet p. 156 - 15.8.4 PWM6 Mode, there is some explanation that states that the PWM6 Mode (PWM4A = 1, WGM41 = 1, and WGM40 = x) provide PWM waveform generation option. Here my knowledge and understanding are too “short” to make it as it should be in the program … ;)

andrew
Site Admin
Posts: 1374
Joined: Sun Aug 05, 2012 4:15 pm

Re: HCMotor Arduino library for driving DC and stepper motors.

Post by andrew » Tue Sep 03, 2019 9:00 am

It's good that you are taking a look at the datasheet but for a beginner hardware timers are a relatively advanced feature and so can seem very daunting. Understanding how they work involves understanding several advance concepts that are normally hidden away from you when writing normal Arduino sketches. Depending on what level of understanding you have I would suggest reading up on interrupts, hardware timers in microcontollers and how the they can be used to trigger interrupts. You are already on the right track but I would also suggest that you first experiment with timer interrupts as a separate project before adding the extra complication of trying to modify the HCMotor library. For instance try to figure out how to use a hardware timer to blink an LED as a separate sketch.

For reference the hardware timer used in the HCMotor library is not used to generate a PWM, it is simply used to trigger an interrupt every 100uS (compare match mode) which in turn runs the code found inside the ISR(TIMER2_COMPA_vect) interrupt service routine.
Comments made by this poster do not necessarily reflect the views of Hobby Components Ltd.

Teryo
Posts: 4
Joined: Mon Aug 12, 2019 7:55 pm

Re: HCMotor Arduino library for driving DC and stepper motors.

Post by Teryo » Wed Sep 04, 2019 4:22 am

Hi Andrew,

Thanks for the advices.

I’ll continue to “dig” in order to improve my poor knowledge on advanced concepts like interrupts. Also will try to get into the ISR(TIMER2_COMPA_vect), which the key to the success in our case.

Thanks again and keep burning the candle for the blind... :)
Cheers

Post Reply

Return to “Arduino”