MM908E624單片機在汽車電子油門控制系統(tǒng)的應用
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();
}
}
}
}
陀螺儀相關文章:陀螺儀原理
評論