📄 bldcmmain.c
字号:
//********************************************************///////////////
///**purpose: use PWM to control BLDCM**********/////
///***data: 2008.8.20***********///
///***author: XieZu Su********//////
#include <iom48v.h>
#include <macros.h>
#define speed0 0x20
void port_init(void)
{
PORTB = 0x00; //PB1~PB3输出,对应3个下桥臂的通断
DDRB = 0x0E;
PORTC = 0x07; //PC0,PC1,PC2霍尔位置传感器输入,上拉
DDRC = 0x00;
PORTD = 0x00;
DDRD = 0x68; //PD6 PD5 PD3分别对应上桥臂的通断信号PWM
}
void timer0_init(void)
{
//TCCR0B = 0x00; //stop
// TCNT0 = 0x01; //set count
// TCCR0A = 0xA3; // 快速PWM
// TCCR0B = 0x01; //start timer 选择分频系数
// OCR0A = 0x30;
// OCR0B = 0x30;
TCCR0B = 0x00; //stop
TCNT0 = 0x01; //set count
//TCCR0A = 0xa3; //CTC mode
//TCCR0B = 0x01; //start timer
OCR0A = speed0;
OCR0B = speed0;
}
void timer2_init(void)
{
TCCR2B = 0x00; //stop
ASSR = 0x00; //set async mode
TCNT2 = 0x01; //setup
OCR2A = speed0;
OCR2B = speed0;
}
void Start_PWMAB(void)
{
TCCR0B = 0x00;
TCCR0A = 0xA3; //
TCCR0B = 0x01; //start Timer
}
void Start_PWMA_Close_PWMB(void)
{
TCCR0B = 0x00;
TCCR0A = 0x83; //
TCCR0B = 0x01; //start Timer
}
void Start_PWMB_Close_PWMA(void)
{
TCCR0B = 0x00;
TCCR0A = 0x23; //
TCCR0B = 0x01; //start Timer
}
void Shut_PWMAB(void)
{
TCCR0B = 0x00;
TCCR0A = 0x02;
}
void Start_PWMC(void)
{
TCCR2B = 0x00; //stop
TCCR2A = 0x23;
TCCR2B = 0x01; //start
}
void Shut_PWMC(void)
{
TCCR2B = 0x00; //stop
TCCR2A = 0x03;
}
void phase_one_five(void)
{
PORTB = PORTB&0xF5; //关下桥臂4,6
Shut_PWMC(); //关上桥臂 3
Start_PWMA_Close_PWMB(); //关上桥臂2,开上桥臂1
PORTB = PORTB|0x04; //开下桥臂5
}
void phase_one_six(void)
{
PORTB = PORTB&0xF9; //关下桥臂4,5
Shut_PWMC(); //关上桥臂 3
Start_PWMA_Close_PWMB(); //关上桥臂2,开上桥臂1
PORTB = PORTB|0x08; //开下桥臂6
}
void phase_two_six(void)
{
PORTB = PORTB&0xF9; //关下桥臂4,5
Shut_PWMC(); //关上桥臂 3
Start_PWMB_Close_PWMA(); //关上桥臂1,开上桥臂2
PORTB = PORTB|0x08; //开下桥臂6
}
void phase_two_four(void)
{
PORTB = PORTB&0xF3; //关下桥臂4,6
Shut_PWMC(); //关上桥臂 3
Start_PWMB_Close_PWMA(); //关上桥臂1,开上桥臂2
PORTB = PORTB|0x02; //开下桥臂4
}
void phase_three_four(void)
{
PORTB = PORTB&0xF3; //关下桥臂6,5
Shut_PWMAB(); //关上桥臂 1,2
Start_PWMC(); //开上桥臂PWMC
PORTB = PORTB|0x02; //开下桥臂4
}
void phase_three_five(void)
{
PORTB = PORTB&0xF5; //关下桥臂6,4
Shut_PWMAB(); //关上桥臂 1,2
Start_PWMC(); //开上桥臂PWMC
PORTB = PORTB|0x04; //开下桥臂5
}
void uart0_init(void)
{
UCSR0B = 0x00; //disable while setting baud rate
UCSR0A = 0x00;
UCSR0C = 0x06; //8位数据传送
UBRR0L = 0x33; //set baud rate lo
UBRR0H = 0x00; //set baud rate hi
UCSR0B = 0x18; //
}
void init_devices(void)
{
//stop errant interrupts until set up
CLI(); //disable all interrupts
port_init();
timer0_init();
timer2_init();
uart0_init();
MCUCR = 0x00;
EICRA = 0x00; //extended ext ints
EIMSK = 0x00;
TIMSK0 = 0x00; //timer 0 interrupt sources
TIMSK1 = 0x00; //timer 1 interrupt sources
TIMSK2 = 0x00; //timer 2 interrupt sources
PCMSK0 = 0x00; //pin change mask 0
PCMSK1 = 0x00; //pin change mask 1
PCMSK2 = 0x00; //pin change mask 2
PCICR = 0x00; //pin change enable
PRR = 0x00; //power controller
SEI(); //re-enable interrupts
//all peripherals are now initialized
}
void USART_Transmit(unsigned char data)
{
//等待发送器缓冲为空
while(!(UCSR0A&(1<<UDRE0)))
;
//发送数据
UDR0 = data;
}
unsigned char USART_Receive(void)
{
//等待接收数据
while(!(UCSR0A&(1<<RXC0)))
;
return UDR0;
}
void set_speed(unsigned char speed)
{
OCR0A = speed;
OCR0B = speed;
OCR2B = speed;
}
void main(void)
{
unsigned int Pos_Val,Speed_Val;
init_devices();
//Start_PWMA_Close_PWMB();
// Start_PWMC();
// Start_PWMAB();
/* while(1)
{
Speed_Val = USART_Receive();
set_speed(Speed_Val);
}*/
while(1)
{
if((UCSR0A&0x80)!=0)
{
Speed_Val = UDR0;
OCR0A = Speed_Val;
OCR0B = Speed_Val;
OCR2B = Speed_Val;
}
Pos_Val = PINC&0x07;
/* // 500w轮毂电机
switch(Pos_Val)
{
case 3:
phase_three_five();
break;
case 2:
phase_one_five();
break;
case 6:
phase_one_six();
break;
case 4:
phase_two_six();
break;
case 5:
phase_two_four();
break;
case 1:
phase_three_four();
break;
default:
break;
}
*/
// 2.2kw电机
switch(Pos_Val)
{
case 1:
phase_three_five(); //C+ B-
break;
case 3:
phase_one_five(); //A+ B-
break;
case 2:
phase_one_six(); //A+ C-
break;
case 6:
phase_two_six(); //B+ C-
break;
case 4:
phase_two_four(); //B+ A-
break;
case 5:
phase_three_four(); //C+ B-
break;
default:
break;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -