我想在ATMEGA16控制器的帮助下,用L6234驱动芯片驱动BlDC电机。第9页的电机驱动器IC L6234数据表中给出了驱动电机的逻辑。这是link for datasheet。因此,根据数据表,我编写了一个代码来驱动我的马达。以下是我的代码:
#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;
}
}
}但是当我运行这段代码时,我的马达不工作了。那么,谁能告诉我,我的代码中的错误在哪里。
发布于 2016-04-29 06:44:41
代码的格式使得调试变得非常困难。既然你已经在使用宏了,我可以提一些建议让它更容易阅读吗?
您有#define hall3 (PINC & 1<<3),但此值需要为4或0。为什么不把它当做布尔值使用呢?
if(hall3 && etc) // note the double &&现在,这将修复一个错误。1<<3是8而不是4,因此在当前编写的代码中,没有一条if语句会成功。(例如,1是1<<0,而不是1<<1。)
PORTB硬编码的输出也很难破译。我建议使用#定义来使这件事变得更容易。
#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 1https://stackoverflow.com/questions/36907568
复制相似问题