Amit Upadhyay
Amit Upadhyay

Reputation: 31

Unable to Run BLDC motor using L6234 with ATmega16

I want to drive BlDC motor using L6234 Driver IC with help of Atmega 16 Controller. Logics for driving motor are given in the motor driver IC L6234 datasheet on page 9. Here is the link for datasheet. So, according to the datasheet I write a code to drive my motor. Here is my code:-

#define F_CPU 8000000UL
#include<avr/io.h>
#include<avr/interrupt.h>
#include<util/delay.h>

#define hall1 (PINC & 1<<1)  // connect hall sensor1
#define hall2 (PINC & 1<<2)  //connect hall sensor2
#define hall3 (PINC & 1<<3)  //connect hall sensor3



void main()
{
    DDRC=0XF0;
    DDRB=0XFF; //output as In1=PB.0 ,In2=PB.1, In3=PB.2, En0=PB.3 ,En1=PB.4, En3=PB.5
    while(1)
    {
        if((hall3==4)&(hall2==0)&(hall1==1)) // step1
          {
             PORTB=0X19;
          }

        if((hall3==0)&(hall2==0)&(hall1==1)) // step2
          {
             PORTB=0X29;
          }

        if((hall3==0)&(hall2==2)&(hall1==1)) // step3
          {
             PORTB=0X33;
          }

        if((hall3==0)&(hall2==2)&(hall1==0)) // step4
          {
             PORTB=0X1E;
          }

        if((hall3==4)&(hall2==2)&(hall1==0))// step5
          {
             PORTB=0X2E;
          }

        if((hall3==4)&(hall2==0)&(hall1==0))// step6
          {
             PORTB=0X34;
          }
    }
}

But when I run this code, my motor is not working. So, can any one tell me, where is the mistake in my code.

Upvotes: 2

Views: 514

Answers (2)

Spektre
Spektre

Reputation: 51893

old / abandoned question but just looking at this:

#define hall1 (PINC & 1<<1)  // connect hall sensor1

make my almost cry... as it is completely wroooong !!!

first you can not have // comments inside macro that will mess up your code as it will be copied to your code too wherever you use hall1

second GC/GCC is really stupid so you have to properly bracket your stuff try:

#define hall1 (PINC & (1<<1))

also I see that your compound expressions are also wrong:

if((hall3==4)&(hall2==0)&(hall1==1))

should be:

if ((hall3==4)&&(hall2==0)&&(hall1==1))

or even better:

if ((hall3)&&(!hall2)&&(hall1))

Upvotes: 0

UncleO
UncleO

Reputation: 8459

The format of your code makes it really hard to debug. Since you are already using macros, may I make some suggestions to make it easier to read?

You have #define hall3 (PINC & 1<<3), but then this value needs to be 4 or 0. Why not just use it as a boolean?

    if(hall3 && etc) // note the double &&

Right away, this will fix one error. 1<<3 is 8 not 4, so none of the if statements will ever succeed as the code is currently written. (Eg., 1 is 1<<0, not 1<<1.)

The PORTB hard-coded outputs are really hard to decipher as well. I suggest using #defines to make this easier, too.

#define EN1 3
#define EN2 4
#define EN3 5
#define IN1 0
#define IN2 1
#define IN3 2

...
    PORTB = 1<<EN1 | 1<<EN2 | 1<<IN1; // step 1

Upvotes: 0

Related Questions