📄 motor.h
字号:
#define Fclk 7.3728 //Fclk I/O in MHz
#define MMotor_ADR 10
#define M1Motor_ADR 14
#define M2Motor_ADR 18
#define Motor_ON 0
#define Motor_OFF 1
#define Motor_R 0
#define Motor_L 1
#define MMotor_PWM PORTD.7
#define MMotor_SW PORTC.1
#define MMotor_RL PORTB.0
#define Motor1_PWM PORTD.5
#define Motor1_SW PORTC.6
#define Motor2_PWM PORTD.4
#define Motor2_SW PORTC.7
float RPM2_max=22;
float RPM2_def;
float RPMA_max=20;
float RPMA_def;
float RPMB_max=20;
float RPMB_def;
int MMotor_Speed;
int Motor1_Speed;
int Motor2_Speed;
//float FreqT2=4000;
float DuC2=50; // Duty Cycle For PWM2 (%) Main Motor
//float Pre2;
float tcnt2=0x4F;
//char tccr2;
float FreqT1=4000;
float DuC1A=50; // Duty Cycle For PWM1A (%) Motor1
float DuC1B=50; // Duty Cycle For PWM1B (%) Motor2
float Pre1;
float tcnt1;
char CO1;
char CO2;
char CO3;
char CO4;
char CO5;
char CO6;
void PWM2_DU(void);
void Timer1_init(void);
void Timer2_init(void);
void MMotor_ON(void);
void Freq_T2(float);
void MMotor_ON(void);
void MMotor_OFF(void);
void M1Motor_ON(void);
void M1Motor_OFF(void);
void M2Motor_ON(void);
void M2Motor_OFF(void);
char Co1;
char Co;
bit Timer1;
bit MMotor_flg;
bit M1Motor_flg;
bit M2Motor_flg;
char stop_flg;
// Timer 1 overflow interrupt service routine
interrupt [TIM1_OVF] void timer1_ovf_isr(void)
{
// Place your code here
TCNT1=tcnt1;
}
// Timer 2 overflow interrupt service routine
interrupt [TIM2_OVF] void timer2_ovf_isr(void)
{
// Place your code here
TCNT2=0x4F;
//PORTC.0=0;
}
// Declare your global variables here
//####################################################
/*
void Freq_T2(float Freq)
{
char i;
char P;
float Fmin2[7]={4,16,31,62,123,489,3907};
float Fmax2[7]={16,31,62,123,489,3907,250000};
for(i=0;i<7;i++)
{
Fmin2[i]*=Fclk;
Fmax2[i]*=Fclk;
if(Freq>=Fmin2[i] && Freq<Fmax2[i]) P=i;
}
if(P==0) Pre2=1024,tccr2=0x7F;
if(P==1) Pre2=256,tccr2=0x7E;
if(P==2) Pre2=128,tccr2=0x7D;
if(P==3) Pre2=64,tccr2=0x7C;
if(P==4) Pre2=32,tccr2=0x7B;
if(P==5) Pre2=8,tccr2=0x7A;
if(P==6) Pre2=1,tccr2=0x79;
tcnt2=256-((Fclk*1000000)/(Pre2*Freq));
TCCR2=tccr2;
TCNT2=tcnt2;
}
*/
//####################################################
//####################################################
void Timer2_init(void)
{
// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: 7372.800 kHz
// Mode: Fast PWM top=FFh
// OC2 output: Inverted PWM
ASSR=0x00;
TCCR2=0x7A;
TCNT2=0x4F;
OCR2=0x00;
}
//####################################################
void Timer1_init(void)
{
// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: 8000.000 kHz
// Mode: Fast PWM top=00FFh
// OC1A output: Inverted
// OC1B output: Inverted
// Noise Canceler: Off
// Input Capture on Falling Edge
TCCR1A=0x01;
TCCR1B=0x09;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
}
//####################################################
// Declare your global variables here
void Freq_T1(float Freq)
{
char i;
char P;
float Fmin[5]={4,16,62,489,3907};
float Fmax[5]={16,62,489,3907,250000};
for(i=0;i<5;i++)
{
Fmin[i]*=Fclk;
Fmax[i]*=Fclk;
if(Freq>=Fmin[i] && Freq<Fmax[i]) P=i;
}
if(P==0) Pre1=1024,TCCR1B=0x0D;
if(P==1) Pre1=256,TCCR1B=0x0C;
if(P==2) Pre1=64,TCCR1B=0x0B;
if(P==3) Pre1=8,TCCR1B=0x0A;
if(P==4) Pre1=1,TCCR1B=0x09;
tcnt1=256-((Fclk*1000000)/(Pre1*Freq));
TCNT1L=tcnt1;
}
//####################################################
void PWM2_DU()
{
if(RPM2_def!=0) RPM2_def+=2;
DuC2=((float)(RPM2_def)/(float)RPM2_max)*100;
if(DuC2<=4)
{
CO2=1;
}
else
{
CO2=0;
}
if(CO2==1){ OCR2=tcnt2+1;}
else{ OCR2=(tcnt2-1)+((DuC2*(256-tcnt2))/100);}
}
//-------------------------------------------------------
void PWM1A_DU()
{
DuC1A=((float)RPMA_def/(float)RPMA_max)*100;
if(DuC1A<=4)
{
CO4=1;
}
else
{
CO4=0;
}
if(CO4==1){ OCR1A=tcnt1+1;}
else {OCR1A=(tcnt1-1)+((DuC1A*(256-tcnt1))/100);}
}
//------------------------------------------------------
void PWM1B_DU()
{
DuC1B=((float)RPMB_def/(float)RPMB_max)*100;
if(DuC1B<=7)
{
CO6=1;
}
else
{
CO6=0;
}
if(CO6==1){ OCR1B=tcnt1+1;}
else {OCR1B=(tcnt1-1)+((DuC1B*(256-tcnt1))/100);}
}
//#####################################################
void MMotor_ON(void)
{
MMotor_flg=1;
TCCR2=0x7A;
TCNT2=0x4F;
RPM2_def=Read_EEProm(10);
PWM2_DU();
}
//###################################################
void MMotor_OFF(void)
{
if(stop_flg==0)MMotor_flg=0;
TCCR2=0x00;
TCNT2=0x4F;
}
//####################################################
void M1Motor_ON(void)
{
M1Motor_flg=1;
if(Timer1==0)
{
Timer1=1;
Timer1_init();
Freq_T1(FreqT1);
}
TCCR1A|=0xC1;
PWM1A_DU();
}
//####################################################
void M1Motor_OFF(void)
{
if(stop_flg==0) M1Motor_flg=0;
TCCR1A&=0x3F;
PORTD.5=1;
}
//####################################################
void M2Motor_ON(void)
{
M2Motor_flg=1;
if(Timer1==0)
{
Timer1=1;
Timer1_init();
Freq_T1(FreqT1);
}
TCCR1A|=0x31;
PWM1B_DU();
}
//####################################################
void M2Motor_OFF(void)
{
if(stop_flg==0) M2Motor_flg=0;
TCCR1A&=0xCF;
PORTD.4=1;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -