📄 usart.c
字号:
}//Pvoid cmd_pwrsave(char shift){#ifdef USART_CMD_PWRSAVE if(shift) // toggle auto poweer save gAutoPowerSave= !gAutoPowerSave; // query loggin status USART_Tx_Bool(gAutoPowerSave);#endif}//Rvoid cmd_reset(char shift){#ifdef USART_CMD_RESET if(shift){// reset command character gRolloverFlash = 0; gDataPage = 0; gDataPosition=0; }#endif}//Svoid cmd_sleep(char shift){#ifdef USART_CMD_SLEEP if(shift){// go back to sleep mode gUART=FALSE; gPowerSave = TRUE; }#endif}//Tvoid cmd_temp(char shift){#ifdef USART_CMD_TEMP int t; //display current temperature ADC_init(TEMPERATURE_SENSOR);// set up adc t = ADC_read(); // read adc ADC2Temp(TRUE,t); // print ADC#endif}//Uvoid cmd_intervalP(char shift){#ifdef USART_CMD_INTERVAL char interval; if(shift){//Set the power save time out if (USART_getValue(&interval,0,90)) gPowerSaveTimeout = interval; } //display the interval USART_Tx_Byte(gPowerSaveTimeout);#endif}//Vvoid cmd_battery(char shift){#ifdef USART_CMD_BATTERY int t; //case 'v': ADC_init(VOLTAGE_SENSOR);// set up adc t = ADC_read(); // read adc ADC2Volt(TRUE,t); // print ADC #endif}//Wvoid cmd_windspeed(char shift){#ifdef USART_CMD_WINDSPEED if (shift) USART_Tx_Byte(gSpeedTotal); // last wind speed USART_Tx_Byte(glastSpeed);#endif}//Zvoid cmd_status(char shift){#ifdef USART_CMD_STATUS char d[5],i; // digits for displaying space avail in flash unsigned int spaceLeft; unsigned long Temp; // status command // this command sends software version number back to user. // Primary use is to let user know system is functioning. USART_sendmsg(MT_AVRBF); //MT_AVRBF is defined in "menu.h" USART_Tx(' '); USART_sendmsg(COMPILEDATE); USART_Tx('v'); USART_Tx(SWHIGH + '0'); //SWHIGH/LOW are defined in "main.h" USART_Tx(SWLOW + '0'); USART_Tx(SWREV + 'a' - 1 ); USART_Tx(' '); if (gRolloverFlash) USART_sendmsg(PSTR("Full")); else{ //calculate space left in flash spaceLeft = TOTALPAGESINFLASH - gDataPage-1; spaceLeft *= RECORDSPERPAGE; spaceLeft += RECORDSPERPAGE- gDataPosition; // convert to BCD and print Temp = int2BCD5(spaceLeft); d[0] = ((char)Temp & 0x0F) + '0'; for (i=1;i<5;i++){ d[(int)i] = ((char)(Temp >>= 4)& 0x0F) + '0'; } for (i=4;i<5;i--){ USART_Tx(d[(int)i]); } USART_sendmsg(PSTR(" avail")); } #endif}// command jump table for usart commandsconst CMD_CALLBACK_FUNC USART_cmd[] PROGMEM ={ cmd_alarm, cmd_dumpB, cmd_clock,cmd_dump, cmd_eol, cmd_flash, cmd_intervalS, cmd_default, cmd_interval, cmd_default, cmd_default, cmd_light, cmd_default, cmd_lognow, cmd_logstatus, cmd_pwrsave, cmd_default, cmd_reset, cmd_sleep, cmd_temp, cmd_intervalP, cmd_battery, cmd_windspeed, cmd_default, cmd_default, cmd_status};/****************************************************************************** Function Name : SIG_USART_RECV** Returns : None** Parameters : None** Purpose : Interrupt service routine for Data received from UART* Receives one byte of data from the USART. Determines* command char received and execute code as required.******************************************************************************/SIGNAL(SIG_USART_RECV){ char userCommand; char cmd_index; char shift; CMD_CALLBACK_FUNC cmd; gPowerSaveTimer = 0; // reset the auto sleep timer gUART = TRUE; //stay out of power save mode userCommand = USART_Rx(); // get command character from UART receive register shift=FALSE; //is it a number for ADC channel if (userCommand>='0' && userCommand<='7'){ USART_sendADC(userCommand-'0',FALSE); }else{ // is it an ucase letter? if (userCommand < 'a'){ shift=TRUE; userCommand += 32; //convert to lcase } //is it a command letter a-z if (userCommand >= 'a' && userCommand <= 'z'){ cmd_index = userCommand-'a'; cmd = (CMD_CALLBACK_FUNC) pgm_read_word(USART_cmd+cmd_index); cmd(shift); } } USART_EOL(gEOL); }/******************************************************************************* Function name : USART_sendmsg** Returns : nothing** Parameters : pointer to program memory string** Purpose : Sends a string out on the UART******************************************************************************/void USART_sendmsg (const char *s){ char c; while ((c=pgm_read_byte(s++)) ){ // get next char USART_Tx(c); //send char }} /******************************************************************************* Function name : USART_sendADC** Returns : nothing** Parameters : char channel to read, char invert reading** Purpose : Read from an ADC and send it out via the UART******************************************************************************/void USART_sendADC(char channel,char invert){ int logdata; ADC_init(channel); // set up adc logdata=ADC_read(); // read adc if (invert){ logdata = 1023-logdata; } ADC2RAW(TRUE,logdata);}/******************************************************************************* Function name : USART_changeInterval** Returns : None** Parameters : None** Purpose : change the logging interval via USART******************************************************************************/void USART_changeInterval(void){ char temp; char interval; USART_sendmsg(PSTR("h/m/s/t:")); temp = USART_Rx(); if (temp <= 'a') temp += 32; // convert to lcase switch(temp){ case 's': gLogTimeUnit=SECOND; break; case 'm': gLogTimeUnit=MINUTE; break; case 't': gLogTimeUnit=TICK; break; default: gLogTimeUnit=HOUR; } USART_sendmsg(PSTR("Interval:")); if (!USART_getValue(&interval,0,90)) interval = 90; gLogTime = interval;}/******************************************************************************* Function name : USART_setTheClock** Returns : nothing** Parameters : nothing** Purpose : Takes input from user via Uart and sets the system time.******************************************************************************/void USART_setTheClock(char target){ char theDay; char theMonth; char theYear; char theHour; char theMinutes; char theSeconds; errorCount = 0; //set error counter to zero USART_Tx('Y'); while ((errorCount < USART_ERRORMAX) && !USART_getValue(&theYear,0,99)); //Go get the Year value USART_Tx('M'); while ((errorCount < USART_ERRORMAX) && !USART_getValue(&theMonth,1,12)); //Go get the month value USART_Tx('D'); while ((errorCount < USART_ERRORMAX) && !USART_getValue(&theDay,1,31)); //Go get the day value USART_Tx('H'); while ((errorCount < USART_ERRORMAX) && !USART_getValue(&theHour,0,23)); //Go get the hour value USART_Tx('M'); while ((errorCount < USART_ERRORMAX) && !USART_getValue(&theMinutes,0,59)); //Go get the minute value USART_Tx('S'); while ((errorCount < USART_ERRORMAX) && !USART_getValue(&theSeconds,0,59)); //Go get the second value if (errorCount >= USART_ERRORMAX){ //if the error counter is USART_ERRORMAX then stop trying to set the clock. errorCount = 0; //set error counter to zero USART_sendmsg(UART_ERROR_MSG2); return; } if (target==SETCLOCK){ gDAY = (uint8_t)theDay; //set the global day variable gMONTH = (uint8_t)theMonth; //set the global month variable gYEAR = (uint8_t)theYear; //set the global year variable gSECOND = (uint8_t)theSeconds; //set the global seconds variable gMINUTE = (uint8_t)theMinutes; //set the global minutes variable gHOUR = (uint8_t)theHour; //set the global hour variable. gTICK = 0; //reset thg global tick variable }else if(target==SETALARM){ ALARM_set(theYear,theMonth,theDay,theHour,theMinutes,theSeconds); }}/******************************************************************************* Function name : USART_getValue** Returns : int - 0 for unsuccessful / 1 for successful** Parameters : int ** Purpose : Takes an input from user via Uart and does basic error check******************************************************************************/char USART_getValue(char *value, char lowerbound, char upperbound){ char retval; //function return value char temp; retval = 1; //set retval to 1 as default (successful) *value = 0; //initialise the parameter // print out the limits for the input USART_Tx_Byte(lowerbound); USART_Tx('-'); USART_Tx_Byte(upperbound); USART_Tx(' '); temp = USART_Rx(); //get first character sent *value = ((char)(temp - '0')) * 10; //convert char from Ascii to Hex and multiply by 10 temp = USART_Rx(); //get second character sent *value += ((char)(temp - '0')); //convert char from Ascii to Hex and add to yearValue if ((*value < lowerbound) | (*value > upperbound)){ //check the value provided by user is valid //if not then increment error counter return 0 errorCount++; //increment error counter retval = 0; //set retval to 0 signifying unsuccessful operation USART_sendmsg(UART_ERROR_MSG); USART_EOL(gEOL); return retval; } return retval;}// EXTENDED_USART /////////////////////////////////////////////////////////////#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -