我这里也分享一个github上面网友分享的ti的TMS320F28069写的整车控制器程序,希望对大家有帮助。
先欣赏一下主程序
//############################################################################//
// Vehicle Control Unit V1 //
// Created by Caner Alp //
// 13.06.2018 //
// //
// The Project is for the electric vehicle car Mediterranean Shark //
// by Team ADA //
// Electric Vehicle Development Center //
// Eastern Mediterranean University //
//############################################################################//
//
// Disable Interrupts
//
DINT;
//
//
//
InitPieCtrl();
//
// Disable CPU interrupts and clear all CPU interrupt flags:
//
IER = 0x0000;
IFR = 0x0000;
//
// Initialize the PIE vector table with pointers to the shell Interrupt
// Service Routines (ISR).
// This will populate the entire table, even if the interrupt
// is not used in this example. This is useful for debug purposes.
// The shell ISR routines are found in f2802x_DefaultIsr.c.
// This function is found in f2802x_PieVect.c.
//
InitPieVectTable();
//
// Interrupts that are used in this example are re-mapped to
// ISR functions found within this file.
EALLOW;
PieVectTable.ECAN1INTA = &ecan1intA_isr;
PieVectTable.TINT0 = &cpu_timer0_isr;
/* * * * * * * * * * PWM * * * * * * * * * */
// This is needed to write to EALLOW protected registers
PieVectTable.EPWM1_INT = &epwm1_isr;
PieVectTable.EPWM3_INT = &epwm3_isr;
PieVectTable.EPWM4_INT = &epwm4_isr;
/* * * * * * * * * *SCI* * * * * * * * * */
// PieVectTable.SCIRXINTA = &sciaRxFifoIsr;
PieVectTable.SCIRXINTB = &scibRxFifoIsr;
#ifdef _FLASH
// Copy time critical code and Flash setup code to RAM
// The RamfuncsLoadStart, RamfuncsLoadSize, and RamfuncsRunStart
// symbols are created by the linker. Refer to the F28069.cmd file.
//
memcpy(&RamfuncsRunStart, &RamfuncsLoadStart,
(Uint32)&RamfuncsLoadSize);
pwmInit();//Initialize PWM Pins
//
// Initialize GPIO, and assign the output to deactiveted position
//
gpioInit();
//
// Initialize PWM, and set 0 not cause any power drain immedately
//
pwmInit();
pwmWrite(0, HORN); // duty cycle is 0
pwmWrite(0, LIGHTS); // duty cycle is 0
pwmWrite(0, WIPER); // duty cycle is 0
//
//Initialize SPI to communicate with MSP430
//
spiInit();
//
// Reset MSP to wake up and clear the device
//
resetMsp();
//
// Initialize CAN-BUS for Motor Control Boards, BMS and SMPS.
//
canBusInit();
// gpioInterruptSetup();
uartInit();
//
// enable interrupts
//
// sendSpeed(0);
//
// Enable TINT0 in the PIE: Group 1 interrupt 7
//
PieCtrlRegs.PIECTRL.bit.ENPIE = 1; // Enable the PIE block
PieCtrlRegs.PIEIER9.bit.INTx6 = 1; // Enable ECAN1INTA in the PIE: Group 1 interrupt 7
PieCtrlRegs.PIEIER1.bit.INTx7 = 1; // Enable TINT0 in the PIE: Group 1 interrupt 6
PieCtrlRegs.PIEIER1.bit.INTx4 = 1; // Enable PIE Group 1 INT4, FOR XINT1 GPIO0, UP BUTTON
// PieCtrlRegs.PIEIER1.bit.INTx5 = 1; // Enable PIE Group 1 INT5, FOR XINT2 GPIO15,DOWN BUTTON
PieCtrlRegs.PIEIER9.bit.INTx3 = 1; // Receive interrupt for SCI-B
// for PWMS
// M_INT9 For CAN-BUS
// M_INT2 for PWM
// M_INT1 for Timer0
IER |= M_INT9 | M_INT2 | M_INT1;
//
//
//
// Enable global Interrupts and higher priority real-time debug events
//
EINT; // Enable Global interrupt INTM
ERTM; // Enable Global realtime interrupt DBGM
//
// Initialize UART for LCD and RF transiciver
//
// delayms(1000000);
// delayms(1000000);
CpuTimer0Regs.TCR.all = 0x4000;
while(1)
{ // The modes will be controlled by the switch
switch(driveMode)
{
case NORMAL_MODE: // Normal Drive Control loop
// When speed up button pressed
if(!GpioDataRegs.GPADAT.bit.GPIO0 && GpioDataRegs.GPADAT.bit.GPIO15)
{
while(!GpioDataRegs.GPADAT.bit.GPIO0 );
// DELAY_US(5000);
// increase the speed
if(setSpeed < maxSpeed) // if it reaches its maximum, assign maximum speed
setSpeed += 1;
// sendSpeed(setSpeed);
}
// When Speed down button pressed
if(!GpioDataRegs.GPADAT.bit.GPIO15 && GpioDataRegs.GPADAT.bit.GPIO0)
{
while(!GpioDataRegs.GPADAT.bit.GPIO15);
// DELAY_US(5000);
// decrease the speed
if(setSpeed > 0)
setSpeed -= 1;
// if it reaches its mininum, assign 0
// sendSpeed(setSpeed);
}
reversePressed();
hornPressed(); // Check Horn Button
lightsPressed(); // Check The ligths Switch
controlFan(maxTemperature); // Check the Temperature, if exceeds then turn FAN on.
controlFlash(maxTemperature); // In case fault, activate FLASH
//
// There is no wiper input specifically
// the extra input is used, GPIO19
//
wiperPressed();
//
// There is no output for br ake,
// The brake ligth must be connectted manually, directly to the switch
// When foot brake is pressed the motors will do the action
// based on the situtation
//
brakeSwitchPressed();
tempTemperature = _IQtoF(maxTemperature);
/* if(tempTemperature > maxTemperatureThreshold)
{
driveMode = TEST_MODE;
setSpeed = 0;
}
*/
break;
case AUTONOMOUS_MODE: // Autonomus Drive Control Loop
break;
case TEST_MODE: // Test Mode Loop, Will test the all components - Alarm Mode
if(directionMotors == FORWARD)
direction = DIRECTION;
else
direction = 0;
// Get the Steering Angle
steeringAngle = getSteeringAngle();
angle = steeringAngle;
if(!GpioDataRegs.GPADAT.bit.GPIO1)
{
// Calculate the outer side angle
// Calculate the angle of the outer tire so Left tire angle will be calculated
if(angle < (steerZero - 5.0)) // Steering is turned to RIGHT
{ // SPEED for LEFT
angle = steerZero - steeringAngle;
angleIQ = _IQ(angle);
angleIQ = calculateTireAngle(angleIQ);
angle = _IQtoF(angleIQ);
calculateDifferential(angle, speed, &innerSpeed, &outerSpeed);
leftSpeed = outerSpeed;
rightSpeed = innerSpeed;
}
else if (angle > (steerZero + 5.0)) // Steering is turned to LEFT
{ // Right tire will be OUTER
// Speed for RIGHT
angle = steeringAngle - steerZero;
angleIQ = _IQ(angle);
angleIQ = calculateTireAngle(angleIQ);