int main() { InitTimer(); InitComm(); PrintLn("hello, world!"); InitOLED(); TestOLED(); return 0; }
bool CStrainDevice::Init(void *pParam) { bool ok = InitComm(); m_nZero = GetPrivateProfileInt("StrainDevice", "Zero", -100, ConfigFilePath); m_nHighLimit = GetPrivateProfileInt("StrainDevice", "High", 1000, ConfigFilePath); m_nLowLimit = GetPrivateProfileInt("StrainDevice", "Low", 200, ConfigFilePath); if (ok) { ok = RequestAddr(); } return ok; }
/*----------------------------------------------------------------------------- * program start */ int main(void) { MCUSR = 0; wdt_disable(); /* get module address from EEPROM */ sMyAddr = eeprom_read_byte((const uint8_t *)MODUL_ADDRESS); PortInit(); TimerInit(); InitComm(); /* enable global interrupts */ ENABLE_INT; SendStartupMsg(); while (1) { Idle(); ProcessBus(); } return 0; /* never reached */ }
/******************************************************************************************************* * * 函数名称: * * 函数功能: * * 入口参数: * * 出口参数: 无 * * 说明: * *******************************************************************************************************/ void InitApp(void) { InitComm(); }
//------------------------------------------------------------------- //----------------------------Code----------------------------------- //------------------------------------------------------------------- void main(void) { // should only execute main loop once; after this just respond to interrupts InitTimers(); InitPorts(); InitComm(); InitInterrupts(); SSPBUF = BytesOut[i]; while(1) { //----------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------- //-----EUART STUFF------------------------------------------------------------------------- //----------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------- if(CommActive) { if( X_Transition!=0 ) { X_Transition = 0; switch( CurrentX_State ) { case X_Idle_State : { SEND = 1; NextX_State = X_StartDelim_State; NextByteOut = 0x7E; // Starting Edit for Variable Message Size MessageCounter = Encode_Message(CurrentType); X_LengthLSB = MessageCounter + 5; //For framing bytes // Populating the Message Frame X_CMD = TXData[0]; X_RF1 = TXData[1]; X_RF2 = TXData[2]; X_RF3 = TXData[3]; X_RF4 = TXData[4]; X_RF5 = TXData[5]; } break; case X_StartDelim_State : { NextX_State = X_LengthMSB_State; NextByteOut = X_LengthMSB ; } break; case X_LengthMSB_State : { NextX_State = X_LengthLSB_State; NextByteOut = X_LengthLSB; } break; case X_LengthLSB_State : { NextX_State = X_API_State; NextByteOut = X_API; } break; case X_API_State : { NextX_State = X_FID_State; NextByteOut = X_FID; } break; case X_FID_State : { NextX_State = X_AddrMSB_State; NextByteOut = X_AddrMSB; } break; case X_AddrMSB_State : { NextX_State = X_AddrLSB_State; NextByteOut = X_AddrLSB; } break; case X_AddrLSB_State : { NextX_State = X_Options_State; NextByteOut = X_Options; } break; case X_Options_State : { NextX_State = X_CMD_State; // will later need logic here for what transition to do based on length LSB NextByteOut = X_CMD; } break; case X_CMD_State : { MessageCounter--; if(MessageCounter) { NextX_State = X_RF1_State; NextByteOut = X_RF1; } else { NextX_State = X_Checksum_State; NextByteOut = CalculateChecksum(); } } break; case X_RF1_State : { MessageCounter--; if(MessageCounter) { NextX_State = X_RF2_State; NextByteOut = X_RF2; } else { NextX_State = X_Checksum_State; NextByteOut = CalculateChecksum(); } } break; case X_RF2_State : { MessageCounter--; if(MessageCounter) { NextX_State = X_RF3_State; NextByteOut = X_RF3; } else { NextX_State = X_Checksum_State; NextByteOut = CalculateChecksum(); } } break; case X_RF3_State : { MessageCounter--; if(MessageCounter) { NextX_State = X_RF4_State; NextByteOut = X_RF4; } else { NextX_State = X_Checksum_State; NextByteOut = CalculateChecksum(); } } break; case X_RF4_State : { MessageCounter--; if(MessageCounter) { NextX_State = X_RF5_State; NextByteOut = X_RF5; } else { NextX_State = X_Checksum_State; NextByteOut = CalculateChecksum(); } } break; case X_RF5_State : { NextX_State = X_Checksum_State; NextByteOut = CalculateChecksum(); } break; case X_Checksum_State : { SEND = 0; NextX_State = X_Idle_State; NextByteOut = 0x7E; } break; } // end switch case }// end if X_Transition if( R_Transition!=0 ) { R_Transition = 0; switch( CurrentR_State ) { case R_Idle_State : { if (ByteIn == 0x7E) // should always be this! { NextR_State = R_LengthMSB_State; } } break; case R_LengthMSB_State : { if ( ByteIn == 0x00 ) // should always be this! { NextR_State = R_LengthLSB_State; } } break; case R_LengthLSB_State : { NextR_State = R_API_State; R_LengthLSB = ByteIn; } break; case R_API_State : { ///Adding Code to Handle different types of received messages R_API = ByteIn; if (R_API == API_TXSTAT) NextR_State = R_FID_State; else if (R_API == API_RXPACK) { NextR_State = R_AddrMSB_State; LostCommCounter = 0; } else NextR_State = R_Idle_State; } break; case R_AddrMSB_State : { NextR_State = R_AddrLSB_State; R_AddrMSB = ByteIn; } break; case R_AddrLSB_State : { NextR_State =R_SigStr_State; R_AddrLSB = ByteIn; } break; case R_SigStr_State : { NextR_State = R_Options_State; R_SigStr = ByteIn; } break; case R_Options_State : { NextR_State = R_CMD_State; // will later need logic here for what transition to do based on length LSB R_Options = ByteIn; } break; case R_CMD_State: { NextR_State = R_RF1_State; R_CMD = ByteIn; } break; case R_RF1_State : { NextR_State = R_Idle_State; R_RF1 = ByteIn; if (R_CMD == 0x05) { //PAIR_RESP Received if(R_RF1&BIT0HI) { Paired = 1; //Stop Broadcasting and send Direct Messages X_AddrMSB = R_AddrMSB; X_AddrLSB = R_AddrLSB; CurrentType = CTRL; Set_PAIRED; PORTC = PORTC_Copy; LostCommCounter = 0; } else { XBee_State(0); } } if (R_CMD == 0x06) { //STATUS Received Process_Status(R_RF1); } } break; case R_FID_State : { NextR_State = R_STATUS_State; R_FID = ByteIn; } break; case R_STATUS_State : { NextR_State = R_Idle_State; R_STATUS = ByteIn; //Code to Resend Message if no ACK if (R_STATUS) { X_Transition = 1; CurrentX_State = X_Idle_State; NextX_State = CurrentX_State; } } break; } // end switch case }// end if R_Transition if (SEND && TXIF) // for some reason, with TXIE I get 0x7E transmitted twice. { X_Transition = 1; TXREG = NextByteOut; } CurrentX_State = NextX_State; CurrentR_State = NextR_State; } } }; // end main
/*----------------------------------------------------------------------------- * process received bus telegrams */ static void ProcessBus(void) { uint8_t ret; TBusMsgType msgType; uint8_t i; uint8_t *p; bool msgForMe = false; uint8_t flags; uint8_t old_osccal; static uint8_t sOsccal = 0; uint16_t startCnt; uint16_t stopCnt; uint16_t clocks; uint16_t diff; uint16_t seqLen; static int sCount = 0; static uint16_t sMinDiff = 0xffff; static uint8_t sMinOsccal = 0; uint8_t osccal_corr; ret = BusCheck(); if (ret == BUS_MSG_OK) { msgType = spRxBusMsg->type; switch (msgType) { case eBusDevReqReboot: case eBusDevReqInfo: case eBusDevReqSetAddr: case eBusDevReqEepromRead: case eBusDevReqEepromWrite: case eBusDevReqDoClockCalib: if (spRxBusMsg->msg.devBus.receiverAddr == MY_ADDR) { msgForMe = true; } break; default: break; } if (msgForMe == false) { return; } switch (msgType) { case eBusDevReqReboot: /* reset controller with watchdog */ /* set watchdog timeout to shortest value (14 ms) */ cli(); wdt_enable(WDTO_15MS); /* wait for reset */ while (1); break; case eBusDevReqInfo: sTxBusMsg.type = eBusDevRespInfo; sTxBusMsg.senderAddr = MY_ADDR; sTxBusMsg.msg.devBus.receiverAddr = spRxBusMsg->senderAddr; sTxBusMsg.msg.devBus.x.devResp.info.devType = eBusDevTypeSw8Cal; strncpy((char *)(sTxBusMsg.msg.devBus.x.devResp.info.version), version, BUS_DEV_INFO_VERSION_LEN); sTxBusMsg.msg.devBus.x.devResp.info.version[BUS_DEV_INFO_VERSION_LEN - 1] = '\0'; BusSend(&sTxBusMsg); break; case eBusDevReqSetAddr: sTxBusMsg.senderAddr = MY_ADDR; sTxBusMsg.type = eBusDevRespSetAddr; sTxBusMsg.msg.devBus.receiverAddr = spRxBusMsg->senderAddr; p = &(spRxBusMsg->msg.devBus.x.devReq.setAddr.addr); eeprom_write_byte((uint8_t *)MODUL_ADDRESS, *p); BusSend(&sTxBusMsg); break; case eBusDevReqEepromRead: sTxBusMsg.senderAddr = MY_ADDR; sTxBusMsg.type = eBusDevRespEepromRead; sTxBusMsg.msg.devBus.receiverAddr = spRxBusMsg->senderAddr; sTxBusMsg.msg.devBus.x.devResp.readEeprom.data = eeprom_read_byte((const uint8_t *)spRxBusMsg->msg.devBus.x.devReq.readEeprom.addr); BusSend(&sTxBusMsg); break; case eBusDevReqEepromWrite: sTxBusMsg.senderAddr = MY_ADDR; sTxBusMsg.type = eBusDevRespEepromWrite; sTxBusMsg.msg.devBus.receiverAddr = spRxBusMsg->senderAddr; p = &(spRxBusMsg->msg.devBus.x.devReq.writeEeprom.data); eeprom_write_byte((uint8_t *)spRxBusMsg->msg.devBus.x.devReq.readEeprom.addr, *p); BusSend(&sTxBusMsg); break; case eBusDevReqDoClockCalib: if (spRxBusMsg->msg.devBus.x.devReq.doClockCalib.command == eBusDoClockCalibInit) { sCount = 0; sOsccal = 0; sMinDiff = 0xffff; } else if (sCount > MAX_CAL_TEL) { sTxBusMsg.msg.devBus.x.devResp.doClockCalib.state = eBusDoClockCalibStateError; sTxBusMsg.senderAddr = MY_ADDR; sTxBusMsg.type = eBusDevRespDoClockCalib; sTxBusMsg.msg.devBus.receiverAddr = spRxBusMsg->senderAddr; BusSend(&sTxBusMsg); break; } flags = DISABLE_INT; BUS_TRANSCEIVER_POWER_UP; PORTB = 0; /* 8 bytes 0x00: 8 * (1 start bit + 8 data bits) + 7 stop bits */ seqLen = 8 * 1000000 * 9 / 9600 + 7 * 1000000 * 1 / 9600; /* = 8229 us */ old_osccal = OSCCAL; ExitComm(); InitTimer1(); TCCR1B = (1 << ICES1) | TIMER1_PRESCALER; OSCCAL = sOsccal; NOP_10; for (i = 0; i < 8; i++) { startCnt = Synchronize(); stopCnt = ClkMeasure(); clocks = stopCnt - startCnt; if (clocks > seqLen) { diff = clocks - seqLen; } else { diff = seqLen - clocks; } if (diff < sMinDiff) { sMinDiff = diff; sMinOsccal = OSCCAL; } OSCCAL++; NOP_4; } BUS_TRANSCEIVER_POWER_DOWN; InitTimer1(); InitComm(); sOsccal = OSCCAL; OSCCAL = old_osccal; RESTORE_INT(flags); if (sCount < MAX_CAL_TEL) { sTxBusMsg.msg.devBus.x.devResp.doClockCalib.state = eBusDoClockCalibStateContiune; sCount++; } else { sTxBusMsg.msg.devBus.x.devResp.doClockCalib.state = eBusDoClockCalibStateSuccess; /* save the osccal correction value to eeprom */ osccal_corr = eeprom_read_byte((const uint8_t *)OSCCAL_CORR); osccal_corr += sMinOsccal - old_osccal; eeprom_write_byte((uint8_t *)OSCCAL_CORR, osccal_corr); OSCCAL = sMinOsccal; NOP_10; } sTxBusMsg.senderAddr = MY_ADDR; sTxBusMsg.type = eBusDevRespDoClockCalib; sTxBusMsg.msg.devBus.receiverAddr = spRxBusMsg->senderAddr; BusSend(&sTxBusMsg); break; default: break; } } }