MM908E624单片机在汽车电子油门控制系统的应用
1. A/D 采样部分,位置设置传感器的模拟输出到MM908E624 的PTB3/AD3 口,同时芯片的PTB4/AD4 接收油门位移反馈。超声波测距电路,由NE555 驱动传感器40S 发射超声波,40R 接收反射的超声波信号并将微弱的电压信号经集成运放LF357 信号放大输出0~5V 电压。输出的模拟量到PTB5/AD5 口。速度模拟采样在PTB7/AD7 口。
2. 主控芯片部分,MM908E624 对外部传感电路的信号进行运算和分析,判断是否HS1,HS2端口输出,从而驱动油门马达,并将报警信号输出通过PTA3,PTA4。同时通过LIN 总线与主控制器连接及时反馈信息。
3. RESET 部分,通过该复位键L1,及时唤醒芯片进行油门马达反转刹车缓冲。
4. 电源部分,超声波电路部分供电采用12V,芯片采用LIN 总线供电,马达部分采用12V 电源供电。
3.软件结构框图
3.1 软件总体框图: 3.2 程序流程图:
3.3 程序:
void main(void) {
l_bool l_NoBusActivity;
UINT8 scpsc_buf,srsr_buf;
// ----- Config registers (write once)
#if defined(MON08)
rCONFIG2 = bEXTCLKEN|bSSBPUENB|bESCIBDSRC|bTMBCLKSEL; // allow ext.
clock, disable default SS pull ups, ESCI clock = bus clock, TBM clock /128
while(rICGCR!= (bCS|bECGON|bECGS)) {
rICGCR = (bCS|bECGON|bECGS); // turn to
external clock source for debugging
}
#else
rCONFIG2 = bSSBPUENB|bESCIBDSRC|bTMBCLKSEL; // disable
default SS pull ups, ESCI clock = bus clock, TBM clock /128
#endif
rCONFIG1 = bLVI5OR3|bSTOP|bCOPD; // 5V operation,
allow stop, disable Watchdog
rICGTR = 128; // trim to ideal:
4.9152MHz
rICGMR = 64; // 4.9152MHz
internal Bus
srsr_buf=rSRSR
SPI_Init(); // Initialize
SPI
SPI_Enable();
ADC_Init();
(void) l_sys_init();
l_ifc_init_sci0();
(void) l_ifc_connect_sci0();
SPIout = bwMODE2|bwMODE1; // normal mode,
slewrate 20kbit, HSx off
SPIin = SPI_ReadWrite(SPIout);
// PTD0 connected to PWMIn -> static to 5V
rDDRD |= bDDRD0;
rPORTD |= bPTD0;
TimerBInit();
lastDir = eSTOP;
PositionInit();
// init Timer Base Module
TimerInit();
TimerValue[TIMER1IDX] = TIMERBASEINTERVALLMS; // 1,66ms
TimerStart(TIMER1);
cli(); // enable
interrupt
for(;;) { // Main Loop
PositionUpdate();
// ----- No LIN Bus activity -----
// l_ifc_ioctl_sci08(l_op_getidle, l_NoBusActivity);
// if(l_NoBusActivity) State = eAsleep;
rADSCR = !bADCH4|!bADCH3|!bADCH2|bADCH1|bADCH0; // select channel
and start conversion
while(!(rADSCRbCOCO));
fADC_Value1 = rADRL;
rADSCR = !bADCH4|!bADCH3|bADCH2|!bADCH1|!bADCH0; start conversion
while(!(rADSCRbCOCO));
fADC_Value2 = rADRL;
if((fADC_Value1 - fADC_Value2)>5)
{
SPIout=bwHS1|!bwHS2;}
else if((fADC_Value2 - fADC_Value1)>5)
{SPIout=!bwHS1|bwHS2;}
else{
SPIout=bwHS1|bwHS2;}
rADSCR = !bADCH4|!bADCH3|bADCH2|!bADCH1|bADCH0;
while(!(rADSCRbCOCO)); // wait till Conversion Complete
fADC_Value3 = rADRL;
rADSCR = !bADCH4|!bADCH3|bADCH2|bADCH1|bADCH0; // select
channel and start conversion
while(!(rADSCRbCOCO)); // wait till Conversion Complete
fADC_Value4 = rADRL;
if((fADC_Value3 - fADC_Value4)>35)
{
rDDRA = bDDRA3;
rPORTA = bPTA3;}
else{ rDDRA = bDDRA4;
rPORTA = bPTA4;}
if(l_u8_tst_SysCmd()){
if(SysMasterRequest_Sleep()) { // first byte = 0
//State = eAsleep; // goto Sleep
Signal received
EnterLowPowerMode(); // call
LIN_GotoSleep()
}else if(l_u8_rx_SysNodeID()==NodeId){ // for
this node ?
if(SysMasterRequest_DataTx()) {
l_u8_tx_SysCmd(cmdDataTx);
l_u8_tx_SysNodeID(NodeId);
l_u8_tx_SysAdr(l_u8_rx_SysAdr());
if((l_u8_rx_SysAdr()0xC0)==0x00) { // ADC Reg.
l_u8_tx_SysData0(ADC_Value((l_u8_rx_SysAdr()0x1F)));
}else if((l_u8_rx_SysAdr()0xC0)==0x40) { // SPI-Reg.
SPIin = SPI_ReadWrite(SPIout);
l_u8_tx_SysData0(SPIin);
}else if(l_u8_rx_SysAdr()==0x97) { // MCU-Reg. 0x0017
l_u8_tx_SysData0(scpsc_buf);
}else if((l_u8_rx_SysAdr()0xC0)==0x80){ // MCU-Reg. 0x0000-0x003F
l_u8_tx_SysData0(IOBYTE(l_u8_rx_SysAdr()0x3F));
}else if((l_u8_rx_SysAdr()0xC0)==0xC0){ // MCU-Reg. 0xFFFE-0xFFFF
l_u8_tx_SysData0(srsr_buf);
}else{ // SRSR (0xFE01)
//tbd
}
LinMsgStatus[2] = LIN_MSG_UPDATED;
}else if(SysMasterRequest_DataRx()) {
l_u8_tx_SysCmd(cmdDataRx);
l_u8_tx_SysNodeID(NodeId);
l_u8_tx_SysAdr(l_u8_rx_SysAdr());
l_u8_tx_SysData0(l_u8_rx_SysData0());
SPIout = l_u8_rx_SysData0();
SPIin = SPI_ReadWrite(SPIout);
LinMsgStatus[2] = LIN_MSG_UPDATED;
}else if(SysMasterRequest_Reset()) { // reset target
}else if(SysMasterRequest_ChNodeID()) { // change Node ID
newnode = l_u8_rx_SysData0();
Flash_ErasePage(0xC000);
(void)Flash_ProgRow(0xC000,newnode);
l_u8_tx_SysCmd(cmdChNodeID);
l_u8_tx_SysNodeID(NodeId);
LinMsgStatus[2] = LIN_MSG_UPDATED;
}else if(SysMasterRequest_ChICGTR()) { // change Node ID
newnode = NodeId;
newtrim = l_u8_rx_SysData0();
Flash_ErasePage(0xC000);
(void)Flash_ProgRow(0xC000,newnode);
l_u8_tx_SysCmd(cmdChICGTR);
l_u8_tx_SysNodeID(NodeId);
l_u8_tx_SysData0(NodeIcgtr);
rICGTR = NodeIcgtr;
LinMsgStatus[2] = LIN_MSG_UPDATED;
}
}
l_u8_clr_SysCmd(); // clear Flag
}
if (LinMsgStatus[2] == LIN_MSG_UPDATED){
scpsc_buf=rSCPSC;
}
// new command received ?
if(l_u8_tst_Command()) {
SPIout = l_u8_rx_SPIData();
TimerBSetPWMduty(l_u8_rx_PWMDuty()); // change duty
cycle
l_u8_clr_Command();
}
// if only one hall sensor is used need to specify direction !!
if(SPIoutbwHS1) {
lastDir = eCW;
}else if(SPIoutbwHS2) {
lastDir = eCCW;
}
SPIin = SPI_ReadWrite(SPIout);
// send debug information
l_u16_tx_Position(PositionValue());
l_u8_tx_Current(ADC_Value(ADC_CHANNEL_PTB1));
l_u8_tx_PTA0(IOBYTE(0));
l_u8_tx_SPIData(SPIin);
l_u8_tx_ADC5(ADC_Value(ADC_CHANNEL_PTB5));
l_u8_tx_ADC7(ADC_Value(ADC_CHANNEL_PTB7));
l_u8_tx_ADC3(ADC_Value(ADC_CHANNEL_PTB3));
l_u8_tx_ADC4(ADC_Value(ADC_CHANNEL_PTB4));
// Timer
if(TimerElapsed(TIMER1)) {
TimerValue[TIMER1IDX] = TIMERBASEINTERVALLMS; // 1,66ms
TimerStart(TIMER1);
PositionUpdate();
}
}
}
}
// --------------------------------------------------------
// --------------------------------------------------------
// --------------------------------------------------------
void Reset(void) {
asm JMP $2000; // forces a reset
Illegal Address Reset
}
// --------------------------------------------------------
void EnterLowPowerMode(void) { // Initialize
Sleep/Stop Mode
(void)l_ifc_disconnect_sci08(); // disconnect
SCI (port E)
sei();
TimerDisable();
TimerBDisable();
ADC_PowerOff();
// ---------------------
// all unused pins to output
// all outputs to output
// all inputs to input
//
// EY16 / KOBI interface
//
// PTA5/SPSCK (EY16 -> KOBI) PULL DOWN
// PTA6/SSB (EY16 -> KOBI) PULL UP
// PTB1/OUT (EY16 - KOBI)
// PTC0/MISO (EY16 - KOBI)
// PTC1/MOSI (EY16 -> KOBI) PULL DOWN
// PTD0/PWMIN (EY16 -> KOBI) PULL ?
// PTE0/TXD (EY16 -> KOBI) PULL UP
// PTE1/RXD (EY16 - KOBI)
rDDRA = bDDRA6|bDDRA5|bDDRA4|bDDRA3|bDDRA2|bDDRA1|bDDRA0; // all
out
rDDRB = bDDRB7|bDDRB6|bDDRB5|bDDRB4|bDDRB3|bDDRB2|!bDDRB1|bDDRB0; // B1 in
rDDRC = bMCLKEN|bDDRC4|bDDRC3|bDDRC2|bDDRC1|!bDDRC0; // C0 in
rDDRD = bDDRD1|bDDRD0; // all out
rDDRE = !bDDRE1|bDDRE0; // E1 in
rPORTA = !bPTA5|bPTA6; // A5 down, A6 up
rPORTB = 0;
rPORTC = !bPTC1; // C1 down
rPORTD = !bPTD0; // D0 down
rPORTE = bPTE0; // E0 up
/*
(void)SPI_ReadWrite(bwLINSL2|bwLINSL1|bwMODE1);// Stop Command (MCU supplied)
(void)SPI_ReadWrite(bwLINSL2|bwLINSL1|bwMODE1);// needs to be send twice*/
(void)SPI_ReadWrite(bwLINSL2|bwLINSL1); // Sleep Command (MCU off)
(void)SPI_ReadWrite(bwLINSL2|bwLINSL1); // needs to be send twice
SPI_Disable(); // Disable SPI
// Sleep=1; // Sleep
stop(); // enter ULPM
}
// --------------------------------------------------------
// Interrupt Service Routines
// --------------------------------------------------------
#pragma TRAP_PROC
void isrESCIReceive(void) { // SCI Rx
interrupt handler
l_ifc_rx_sci08();
}
// --------------------------------------------------------
#pragma TRAP_PROC
void isrESCIError(void) { // SCI Error
interrupt handler
l_ifc_rx_sci08();
}
// -------------------------------------------------------
l_irqmask l_sys_irq_disable(void){ //Disable
interrupts
sei();
return 0;
}
// -------------------------------------------------------
void l_sys_irq_restore(l_irqmask previous){ //Restore
interrupts
cli();
}
// -------------------------------------------------------
#pragma TRAP_PROC
void ISR_DUMMY(void){ //Dummy ISR
// while(1);
}
// -------------------------------------------------------
#pragma TRAP_PROC
void isrKeyBoard(void) {
swi();
}
// -------------------------------------------------------
// -------------------------------------------------------
#pragma TRAP_PROC
void isrHWIRQ(void) {
UINT8 res;
//swi();
nop();
SPIout = bwMODE2|bwMODE1;
res = SPI_ReadWrite(SPIout);
/*
if(resbrINTSRC) {
}
*/
TimerEnable();
TimerBEnable();
}
4.功能展示
- 基于PC机与单片机的电子油门检测系统设计(11-16)
- DSP和PBL3717A构成的步进电机的控制系统(04-01)
- 基于DSP的新型弹载控制计算机(04-05)
- 基于TMS320VC5509A的语音识别与控制系统(06-17)
- 基于DSP的智能刹车控制系统研究(07-29)
- 基于DSP的逆变电源控制系统设计(08-11)