📄 main.c
字号:
//****************************************************************************
// @Module Project Settings
// @Filename MAIN.C
// @Project svm.dav
//----------------------------------------------------------------------------
// @Controller Infineon XC167CI-16F40
//
// @Compiler Keil
//
// @Codegenerator 2.1
//
// @Description This file contains the project initialization function.
//
//----------------------------------------------------------------------------
// @Date 19.12.2003 11:31:17
//
//****************************************************************************
// USER CODE BEGIN (MAIN_General,1)
// USER CODE END
//****************************************************************************
// @Project Includes
//****************************************************************************
#include "MAIN.H"
// USER CODE BEGIN (MAIN_General,2)
// USER CODE END
//****************************************************************************
// @Macros
//****************************************************************************
// USER CODE BEGIN (MAIN_General,3)
// USER CODE END
//****************************************************************************
// @Defines
//****************************************************************************
// USER CODE BEGIN (MAIN_General,4)
// USER CODE END
//****************************************************************************
// @Typedefs
//****************************************************************************
// USER CODE BEGIN (MAIN_General,5)
// USER CODE END
//****************************************************************************
// @Imported Global Variables
//****************************************************************************
// USER CODE BEGIN (MAIN_General,6)
// USER CODE END
//****************************************************************************
// @Global Variables
//****************************************************************************
// USER CODE BEGIN (MAIN_General,7)
char menu[]=
"\r\n\n\n\n"
"a...Start PWM\r\n"
"b...Stop PWM\r\n"
"d...Change rotating Direction \r\n"
"e...Enable Driver\r\n"
"r...Rotate IM\r\n"
" \r\n";
unsigned char comm_asc0_i = 0;
unsigned char asc0_rece_buf[5];
unsigned char comm_asc0_receCount = 0;
bit on_flag=ON;
bit rot_flag=OFF;
bit dir_flag=ON;
uchar select=0;
int speed=0;
volatile unsigned int idata adc_result[3];
volatile unsigned int idata adc_dclink_res[2];
// USER CODE END
extern unsigned int ENC_OFFSET;
//****************************************************************************
// @External Prototypes
//****************************************************************************
// USER CODE BEGIN (MAIN_General,8)
// USER CODE END
//****************************************************************************
// @Prototypes Of Local Functions
//****************************************************************************
// USER CODE BEGIN (MAIN_General,9)
// USER CODE END
//****************************************************************************
// @Function void MAIN_vInit(void)
//
//----------------------------------------------------------------------------
// @Description This function initializes the microcontroller.
//
//----------------------------------------------------------------------------
// @Returnvalue None
//
//----------------------------------------------------------------------------
// @Parameters None
//
//----------------------------------------------------------------------------
// @Date 19.12.2003
//
//****************************************************************************
// USER CODE BEGIN (Init,1)
// USER CODE END
void MAIN_vInit(void)
{
// USER CODE BEGIN (Init,2)
// USER CODE END
/// -----------------------------------------------------------------------
/// Configuration of the System Clock:
/// -----------------------------------------------------------------------
/// - VCO clock used, input clock is connected
/// - input frequency is 8 MHz
/// - VCO output frequency 100 .. 150 MHz
/// - system clock is 40 MHz
MAIN_vUnlockProtecReg(); // unlock write security
PLLCON = 0x7D12; // load PLL control register
//// -----------------------------------------------------------------------
//// Begin of Important Settings for the Start-Up File
//// -----------------------------------------------------------------------
/// All following settings must be set in the start-up file. You can use
/// DAvE's project file (*.dpt) to include this register values into your
/// compiler EDE.
/// ---------------------------------------------------------------------
/// Initialization of the CPUCON1 Register:
/// ---------------------------------------------------------------------
/// - space between two vectors is 2 words
/// - DISWDT executable until End of Init
/// - segmentation is enabled
/// - switch context is not interruptible
/// - branch prediction is enabled
/// - zero cycle jump function is enabled
//// this register must be set in the start-up file
//// CPUCON1 = 0x0003
/// ---------------------------------------------------------------------
/// Initialization of the VECSEG Register:
/// ---------------------------------------------------------------------
/// - start from internal program memory
//// this register must be set in the start-up file
//// VECSEG = 0x00C0
/// ---------------------------------------------------------------------
/// Initialization of the SYSCON0 Register:
/// ---------------------------------------------------------------------
//// this register must be set in the start-up file
//// SYSCON0 = 0x0000
/// ---------------------------------------------------------------------
/// Initialization of the SYSCON1 Register:
/// ---------------------------------------------------------------------
/// clock prescaler for system is fpll / 1
//// this register must be set in the start-up file
//// SYSCON1 = 0x0000
/// ---------------------------------------------------------------------
/// Initialization of the SYSCON3 Register:
/// ---------------------------------------------------------------------
//// this register must be set in the start-up file
//// SYSCON3 = 0x0000
/// ---------------------------------------------------------------------
/// Initialization of EBC
/// ---------------------------------------------------------------------
//// this register must be set in the start-up file
//// EBCMOD1 = 0x005F
/// EBC is disabled
//// -----------------------------------------------------------------------
//// End of Important Settings for the Start-Up File
//// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
// Initialization of the Peripherals:
// -----------------------------------------------------------------------
// initializes the Parallel Ports
IO_vInit();
// initializes the Asynchronous/Synchronous Serial Interface (ASC0)
ASC0_vInit();
// initializes the General Purpose Timer Unit (GPT1)
GPT1_vInit();
// initializes the Capture / Compare Unit 1 (CAPCOM1)
CC1_vInit();
// initializes the Analog / Digital Converter (ADC)
ADC_vInit();
// initializes the Capture / Compare Unit 6 (CCU6)
CCU6_vInit();
// initializes the Interrupt Controller (INT)
INT_vInit();
// USER CODE BEGIN (Init,3)
// USER CODE END
// globally enable interrupts
PSW_IEN = 1;
} // End of function MAIN_vInit
//****************************************************************************
// @Function void MAIN_vUnlockProtecReg(void)
//
//----------------------------------------------------------------------------
// @Description This function makes it possible to write one protected
// register. After calling of this function and write on the
// protected register is the security level set to low
// protected mode.
//
//----------------------------------------------------------------------------
// @Returnvalue None
//
//----------------------------------------------------------------------------
// @Parameters None
//
//----------------------------------------------------------------------------
// @Date 19.12.2003
//
//****************************************************************************
// USER CODE BEGIN (UnlockProtecReg,1)
// USER CODE END
void MAIN_vUnlockProtecReg(void)
{
ubyte ubPASSWORD;
if((SCUSLS & 0x1800) == 0x0800) // if low protected mode
{
ubPASSWORD = SCUSLS & 0x00FF;
ubPASSWORD = ~ubPASSWORD;
SCUSLC = 0x8E00 | ubPASSWORD; // command 4
} // end if low protected mode
if((SCUSLS & 0x1800) == 0x1800) // if write protected mode
{
SCUSLC = 0xAAAA; // command 0
SCUSLC = 0x5554; // command 1
ubPASSWORD = SCUSLS & 0x00FF;
ubPASSWORD = ~ubPASSWORD;
SCUSLC = 0x9600 | ubPASSWORD; // command 2
SCUSLC = 0x0800; // command 3; new PASSWOR is 0x00
ubPASSWORD = SCUSLS & 0x00FF;
ubPASSWORD = ~ubPASSWORD;
SCUSLC = 0x8E00 | ubPASSWORD; // command 4
} // end if write protected mode
} // End of function MAIN_vUnlockProtecReg
//****************************************************************************
// @Function void main(void)
//
//----------------------------------------------------------------------------
// @Description This is the main function.
//
//----------------------------------------------------------------------------
// @Returnvalue None
//
//----------------------------------------------------------------------------
// @Parameters None
//
//----------------------------------------------------------------------------
// @Date 19.12.2003
//
//****************************************************************************
void ChangeDirection(void)
{
dir_flag = !dir_flag;
}
void SetSpeed(int new_speed)
{
speed = new_speed<<3; // new_speed * 8
}
void StartPwm(void)
{
CCU6_TCTR4 = 0x0002; //starten von T12 der CAPCOM 6
}
void StopPwm(void)
{
CCU6_TCTR4 = 0x0001; //stoppen von T12 der CAPCOM 6
CCU6_CMPMODIF = 0x0700; //reset output line
}
void EnableDriver(void)
{
if (on_flag==OFF)
{
P9=0x0010; // RESET PORT 9.4,5 OUTPUT LATCH
on_flag=ON;
}
else
{
P9=0x0020; // SET PORT 9.4,5 OUTPUT LATCH
on_flag=OFF;
}
}
void Comm_Asc0_Send(unsigned char buff)
{
ASC0_TBUF = buff;
while(!ASC0_TIC_IR){;}
ASC0_TIC_IR = 0;
}
// USER CODE BEGIN (Main,1)
uchar input (void)
{
uchar in = 0;
//do
//{
while (!ASC0_RIC_IR) _nop_();
ASC0_RIC_IR=0;
in= (char)ASC0_RBUF;
//}while (in != 'a' && in != 'b' && in != 'd' && in != 'e' && in != '0' && in != '1' && in != '2' && in != '3'&& in != '4' && in != '5' && in != '6'&& in != '7' && in != '8' && in != '9'&& in != '+'&& in != '-');
return in;
// 接收测试软件(串口0)的数据
/*unsigned char i;
if(ASC0_RIC_IR)
{
ASC0_RIC_IR = 0;
comm_asc0_i = 0;
asc0_rece_buf[comm_asc0_receCount++] = (unsigned char)ASC0_RBUF;
}
if((GPT12E_T2IC_IR) && (comm_asc0_receCount>0))
{
GPT12E_T2IC_IR = 0;
comm_asc0_i++;
}
if(comm_asc0_i>1)
{
comm_asc0_i = 0;
switch(asc0_rece_buf[0])
{
case 0x0a:
{
StartPwm();
break;
}
case 0x0b:
{
StopPwm();
break;
}
case 0x0e:
{
EnableDriver(); break;
}
case 0x0d:
{
ChangeDirection(); break;
}
case 0x0f:
{
if(asc0_rece_buf[1]<70) SetSpeed(asc0_rece_buf[1]);
else SetSpeed(70);
break;
}
case 0x00:
{
ENC_OFFSET = (unsigned int)asc0_rece_buf[1]<<8 + asc0_rece_buf[2];
break;
}
}
Comm_Asc0_Send(asc0_rece_buf,3);
for(i=0;i<comm_asc0_receCount;i++)
{
asc0_rece_buf[i] = 0x00;
}
comm_asc0_receCount = 0;
}*/
}
// USER CODE END
extern int test_position;
void main(void)
{
// USER CODE BEGIN (Main,2)
Configure_PLL();
Configure_SYSCON3();
// USER CODE END
MAIN_vInit();
// USER CODE BEGIN (Main,4)
CCU6_TCTR4 = 0x0002; // Start Timer 12 of CAPCOM 6
InitFOC();
while (1)
{
P7_P6 = 1;
select=input();
//input();
switch (select)
{
case 0x0a: StartPwm(); break;
case 0x0b: StopPwm(); break;
case 0x0c: EnableDriver(); break;
case 0x0d: ChangeDirection(); break;
case 0x00: SetSpeed(0); break;
case 0x01: SetSpeed(10); break;
case 0x02: SetSpeed(20); break;
case 0x03: SetSpeed(30); break;
case 0x04: SetSpeed(40); break;
case 0x05: SetSpeed(50); break;
case 0x06: SetSpeed(60); break;
case 0x07: SetSpeed(70); break;
case 0x08: SetSpeed(100); break;
case 0x09: SetSpeed(150); break;
case 0x99: SetSpeed(200); break;
case 0xaa: SetSpeed(300); break;
case 0x0f: Comm_Asc0_Send((uchar)(GPT12E_T3>>8)); Comm_Asc0_Send((uchar)GPT12E_T3); Comm_Asc0_Send((uchar)(test_position>>8)); Comm_Asc0_Send((uchar)test_position); break;
default: ENC_OFFSET = (uint)(select&0xf0)<<(select&0x0f);
//case 'f': SetSpeed(set_speed); break;
//case '+': ENC_OFFSET=ENC_OFFSET+50;
//case '-': ENC_OFFSET=ENC_OFFSET-50;
}
Comm_Asc0_Send(select);
P7_P6 = 0;
}
// USER CODE END
} // End of function main
// USER CODE BEGIN (MAIN_General,10)
// USER CODE END
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -