📄 application.c
字号:
#include <p18cxxx.h>
#define __APPLICATION_DEF
#include "application.h"
#define __BLDC_DATA_DEF
#include "BLDC_DATA.h"
/************************************************************************************************/
union
{
unsigned int w;
struct
{
unsigned char lo;
unsigned char hi;
}b;
}PDC_temp;
unsigned int CycleCount;
unsigned char FaultA_Count;
unsigned char PWM_CycleCount;
unsigned char DebounceCounter;
unsigned int Counter;
unsigned char PosTable[8] = { POS_D0,POS_D1,POS_D2,POS_D3,\
POS_D4,POS_D5,POS_D6,POS_D7};
/************************************************************************************************/
void Motor_Turn_Fwd(void)
{
DirMask = MOTOR_TURN_FWD;
}
void Motor_Turn_Rev(void)
{
DirMask = MOTOR_TURN_REV;
}
void Motor_Stop(void)
{
}
/************************************************************************************************/
void Update_Application(void)
{
if (1 == CALC_PWM)
{
Update_PWM();
CALC_PWM = 0;
}
// KeyCheck();
// Process_Key_Pressed();
if (0 == ADCON0bits.GO_DONE)
{
Update_ADC();
ADCON0bits.GO_DONE = 1;
}
}
/************************************************************************************************/
void Update_ADC(void) // ADADAD ;ADC interrupt
{
MotorCurrent.w = ADRES; // Sample A = Iu
SpeedRef.w = ADRES; // Sample B = speed ref
PIR1bits.ADIF = 0; // ADIF flag is cleared for next interrupt
CALC_PWM = 1;
}
/************************************************************************************************/
void Update_Sequence(void)
{
unsigned char cpSeq;
cpSeq = ((PORTA & 0x1C) >> 2) ^ (DirMask & 0x07); // hall sensor inputs
OVDCOND = PosTable[cpSeq];
OVDCONS = 0;
}
/************************************************************************************************/
void Update_PWM(void)
{
if (SpeedRef.b.hi > 0x20)
{
if (SpeedRef.b.hi < 0xC0)
{
}
else
{
SpeedRef.b.hi = 0xFD;
}
//PWM = [(MotorVoltage/DCbus voltage)*(PTPER*4)]*[SpeedRef/255] *16
//16 is the multiplication factor
PDC_temp.w = (unsigned int)(SpeedRef.b.hi * kMAIN_PWM_CONSTANT * 16L);
PWMCON1bits.UDIS = 1; //Disable the PWM buffer update
PDC0H = PDC1H = PDC2H = PDC3H = PDC_temp.b.hi;
PDC0L = PDC1L = PDC2L = PDC3L = PDC_temp.b.lo;
PWMCON1bits.UDIS = 0; //Disable the PWM buffer update
}
else
{
Reset_DutyCycle();
}
}
/************************************************************************************************/
void Reset_DutyCycle(void)
{
PDC0L = 0; // reset PWM DC registers
PDC0H = 0;
PDC1L = 0;
PDC1H = 0;
PDC2L = 0;
PDC2H = 0;
PDC3L = 0;
PDC3H = 0;
Update_Sequence();
}
/************************************************************************************************/
void Init_Application(void)
{
SpeedRef.w = 0;
Flags.b = 0;
Flags1.b = 0;
//-----------------------------------------------------------------
//init PORTC
//PORTC<0> : LED1, Output
//PORTC<1> : FLTA, Input
//PORTC<2> : LED2, Output
//PORTC<3> : LED3, Output
//PORTC<4> : Switch S2, Input
//PORTC<5> : Switch S3, Input
//PORTC<6> : TX, Output
//PORTC<7> : RX, Input
TRISC = 0xB2;
Init_ADC();
Init_HallInp();
Init_PWM();
}
/************************************************************************************************/
void Init_ADC(void)
{
ADCON0 = 0x11;
ADCON1 = 0x10;
ADCON2 = 0x32;
ADCON3 = 0x40;
ADCHS = 0x00;
ANSEL0 = 0x03;
// ANSEL1 = 0x00;
ADCON0bits.GO_DONE = 1;
// PIE1bits.ADIE = 1; // AD Converter Interrupt enable
}
/************************************************************************************************/
void Init_HallInp(void)
{
TRISA |= 0x0E; // init Hall @ IC1/IC2/IC3
T5CON = 0x19;
CAP1CON = 0x08; // Cap1/2/3-capture every input state change
CAP2CON = 0x08;
CAP3CON = 0x48;
DFLTCON = 0x00;
QEICON= 0x00; //Disable QEI
//;init interrupts
PIE3bits.IC1IE = 1; // Cap1 interrupt
PIE3bits.IC2QEIE = 1; // Cap2 interrupt
PIE3bits.IC3DRIE = 1; // Cap3 interrupt
}
/************************************************************************************************/
void Init_PWM(void)
{
// Task:
//Initialize PCPWM to drive a BLDC motor
//PWM0 to PWM5 are used for driving this motor(PORTB 0 to 5)
//Hints:
//-----------------------------------------------------------------------------------------
//Page numbers on datasheet
// Registers PTCON0(page #186), PTCON1(page #186), PTPERH:PTPERL(page #210),
// PWMCON0(page #187), PWMCON1(page #188), DTCON(page #200),
// OVDCOND(page #203), OVDCONS(page #204), FLTCONFIG(page #208)
//-----------------------------------------------------------------------------------------
// You have to write the following:
//---------------------
//A) Enable PWM in edge aligned or cener aligned mode
//B) Calculate and load PTPERH:PTPERL to get PWM freq = 16KHz(Fosc = 5MHz X 4PLL)
//C) Enable PWM0 to PWM5 in independent mode
//D) Using OVDCOND and OVDCONS registers, set outputs controlled by POUT bits
// and initialize all POUT bits to inactive state(low)
//E) Set FaultA in cycle-by-cycle mode and Enable 揊ault condition on break point
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -