controlling ESC with ATmega16

Thread Starter

kripanand

Joined Mar 15, 2012
13
hello guys,
I want to controll ESC which is used for the brushless dc motor I made several programms for the controlling of ESC but I failed. I want to generat PWM pulse for the bldc motor any help really needed
 

jrm

Joined Oct 11, 2011
38
If it is a standard R/C Hobby type ESC then you should just need to output a single 5v pulse every 20ms. The pulse width will range between 1-2ms.
If you are running that chip at 8mhz use the 1024 timer prescale on an 8 bit timer and preload the timer with a value of 100. That will overflow roughly every 20ms. Putting the pulseout in your isr is not the most elegant solution but it is easy.

What compiler are you using? is there a datasheet for the esc?
 

Thread Starter

kripanand

Joined Mar 15, 2012
13
well I am using tower pro ESC of 25 A max. output. I tried by varying freq upto 1 KHZ-8 MHZ in timer still they are not able to move bldc ??? :(
 

hspalm

Joined Feb 17, 2010
201
There are many examples to control hobby/rc servos with an AVR, they use the same pulse width control signal as brushless motor controllers. When a servo is at either ~270 degrees endpoints, your motor will run at either full or no speed.
 

jrm

Joined Oct 11, 2011
38
If I'm reading you correctly, 1khz is way too fast. The esc only needs updates at 50hz. you want leading edge to leading edge at 50hz and a duty cycle of between 5-10% for full range throttle. Hobby servos(and speed controls) operate at geological time compared to what your chip is capable of.
 

Thread Starter

kripanand

Joined Mar 15, 2012
13
I tried it on the 50 hz it still no working any guess what could be the problem its really irritating me now...help me out !!!!
 

jrm

Joined Oct 11, 2011
38
This just dawned on me...
That ESC needs 5 seconds of no change to the pulse stream before it will activate. after 5 seconds you will hear a beep. Are you hearing the beep? Once you hear the beep, the pulse width needs to go to low throttle (1ms pulses ) before it will do anything. This is a safety feature to make sure your $1k helicopter doesn't start at full throttle. It is also programmable so if you power it up with full throttle you will enter programming mode. I'm pretty sure I have the exact same speed controller on one of my helicopters.

You would be better off and a lot less frustrated to use a servo in place of the ESC to get started. The servo requires the same exact pulse stream and you will get the benefit of visual feeback.
 

jrm

Joined Oct 11, 2011
38
I don't know codevision but it looks like you have several problems.

All of your ports are configured as inputs and are in tri state mode. What port/pin are you using for the output on the AVR?

Your Timer mask register is set to 00000000 meaning the timer interrpupt is not enabled.

TCCRO needs some help. If I'm reading it correctly the only thing you have changed is cs02bits which is the 256 prescale. Com01 com00 need to be set according to where and how you want the output. The DDR for the output port needs to be set as an output. The way you have it now there is no output; it is set for normal port operation.

I'm assuming you are using timer0. Timer0 with a prescale of 256 @ 8mhz overflows every 8 milliseconds. Using a compare match of 100 with a top of 255 and that prescale gives you a 3ms duration. Not gonna work.


So break out the data sheet for the chip and look at:
DDRx
TCCRO
TIMSK
TCNTO
OCRO

Also make sure your fuse bit clkdiv8 is not set on your chip or you will be running at 1mhz instead of 8mhz and you will pull all of your hair out.
 

hspalm

Joined Feb 17, 2010
201
PORTB pin 3 is actually configured as output.

But yes, you have several problems. You have no WGM bits set, these registers is what decides what mode your timer is in. In your case, for fast PWM the bits WGM00 and WGM01 should be set to 1.

You still have a PWM frequency of 120Hz which is more than double what the ESC is expecting.

And you should really read those hints posted earlier on initializing an ESC, I was not aware of this!

And, your pulses is varying from 4 to ~7 milliseconds, if my math is correct, which the ESC don't understand, it needs 1-2ms pulses.
 

Thread Starter

kripanand

Joined Mar 15, 2012
13
okkk I got your statement .and thnx to u all for their suggestions.

but tell me one thing if I am wrong about the ESCs. there are 3 pins out from the ESC and 2 pins for the battery for Li-Po battery.these 3 pins which are connected to atmega16 are
PWM Pulse
Vcc and
Gnd pin
I connected the PWM pin to Timer0 which is 8 bit timer at Pin no. 4 and Vcc, Gnd to their pins 10,11.

Do I really need to initialise the port should I ?. because I did the PWM pulses generation in Dc motor controll also and I did nothing like initialising of port in dc motor ...or do I need to initialise the 16 bit timer for ESC....please help .....!!!!!
 

Thread Starter

kripanand

Joined Mar 15, 2012
13
void main ()
{
TCCR0|= (1<<WGM00)| (1<<WGM01) | (1<<COM00)| (1<<CS02)| (1<<CS00);
TCNT0=0x00;
OCR0=0x00;

//Set OC0 PIN as output. It is PB3 on ATmega16
DDRB|= (1<<PB3);

while (1)
{

Delay_ms(20);

OCR0=10;

Delay_ms(20);

OCR0=12;

Delay_ms(20);

OCR0=0;

Delay_ms(20);

}












I made this effort by reading last all for your post prescaling timer0 by 1024 and giving the pulse after every 20 ms. and also use the WGM bit, COM and CS bit ...please tell me the error ...I did not burn this program yet !!!
 

hspalm

Joined Feb 17, 2010
201
The 16bit timer you can just leave ut of the picture, continue with the 8 bit timer 0. I don't have the time to re-calculate your new timer values, but I can see your code is not yet implementing the initialization of the ESC.

I added initialization routine before main-loop. I edited OCR0 values to values I think are more correct. I also upped your delay times in the main loop, did you expect to see any change in speeds with 20 ms delays between changes? Or, did you might think that the pulses need to change values every 50Hz (thus 20ms delays)? In either case, the frequency is fixed by the CS-bits, you only set the duty cycle with OCR0 register. I also removed last delay in loop because you had one on the top also making it delay two times , one right after the other.

Only setting COM00 bit is a reserved setting with fast PWM mode, referring to the datasheet. I set COM01 instead, which clears OC0 at top of counter and sets it at the bottom, this is called non-inverting PWM.



void main ()
{
TCCR0|= (1<<WGM00)| (1<<WGM01) | (1<<COM01)| (1<<CS02)| (1<<CS00);
TCNT0=0x00;
OCR0=0x00;

//Set OC0 PIN as output. It is PB3 on ATmega16
DDRB|= (1<<PB3);

//ESC initializing
OCR0 = 7; //0-speed signal at div1024 according to my calculations
Delay_ms(5000); //wait 5 seconds for beep

while (1)
{

Delay_ms(2000);

OCR0=7;

Delay_ms(2000);

OCR0=14;

Delay_ms(2000);

OCR0=0;

}
 

Thread Starter

kripanand

Joined Mar 15, 2012
13
not again still there is something missing in this code.. please look that code one more time . I am still not getting the movement in ESC.. pleaseeee....help .........:(
 

hspalm

Joined Feb 17, 2010
201
Sorry pal. I'm not saying my code is guaranteed to work, and I can not program this for you. If you have access to an oscilloscope that would be handy, if not it's just a shot in the dark when you simply connect the ESC (btw we can not be sure your wiring is okay either).

Read this page
http://extremeelectronics.co.in/avr...ontrol-by-using-avr-atmega32-microcontroller/

See that code? I can not guarantee it to work, but it's the first hit when googling "servo control avr" (what we told you to do, implicitly).

Now delete the whole of main() function and replace with

Rich (BB code):
void main()
{
   //Configure TIMER1
   TCCR1A|=(1<<COM1A1)|(1<<COM1B1)|(1<<WGM11);        //NON Inverted PWM
   TCCR1B|=(1<<WGM13)|(1<<WGM12)|(1<<CS11)|(1<<CS10); //PRESCALER=64 MODE 14(FAST PWM)

   ICR1=4999;  //fPWM=50Hz (Period = 20ms Standard).

   DDRD|=(1<<PD4)|(1<<PD5);   //PWM Pins as Out

    OCR1A = 250;
    _delay_ms(5000);

   while(1)
   {

      OCR1A=250;   //no speed
      _delay_ms(3000);

      OCR1A=500;  //full speed
      _delay_ms(3000);

   }
}
This must be compiled in AVR Studio, so download that along with winavr package. You also need to switch your crystal for a 16MHz. Your PWM output is now PORT D pin 5
 

Thread Starter

kripanand

Joined Mar 15, 2012
13
o.k. thanx a lot for your effort let me try My best this time :)

I tell you tomorrow ....whether its gonna work...

Hope at last something works....:)
 
Top