⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.c

📁 butterflylogger_src_20060822 for atmel avr
💻 C
📖 第 1 页 / 共 3 页
字号:
		// in binary format (lots Faster)		DumpFlash(DUMP_BIN);		enter=1;			}	return ST_OPTIONS_UPLOAD_FUNC;}/*******************************************************************************   Function name : ResetFlash**   Returns :       None**   Parameters :    char input**   Purpose :       Reset current flash page to zero.******************************************************************************/         //__flash char TEXT_RESET[]                     = "Press enter to Reset"  ;char ResetFlash(char input){	static char enter = 1;		if(enter == 1)	{		enter = 0;		LCD_puts_f(PSTR("Press enter to Reset"),1);			}	else if (input == KEY_PREV)	{		enter = 1;		return ST_OPTIONS_RESET;	}	else if (input == KEY_ENTER)    	{		gRolloverFlash = 0;		gDataPage = 0;		gDataPosition=0;		return ST_AVRBF;	}		return ST_OPTIONS_RESET_FUNC;}/*******************************************************************************   Function name : OSCCAL_calibration**   Returns :       None**   Parameters :    None**   Purpose :       Calibrate the internal OSCCAL byte, using the external *                   32,768 kHz crystal as reference******************************************************************************/void OSCCAL_calibration(void){    unsigned char calibrate = FALSE;    int temp;    unsigned char tempL;	    CLKPR = (1<<CLKPCE);        // set Clock Prescaler Change Enable								// set prescaler = 8, Inter RC 8Mhz / 8 = 1Mhz    CLKPR = (1<<CLKPS1) | (1<<CLKPS0);        TIMSK2 = 0;             //disable OCIE2A and TOIE2	    ASSR = (1<<AS2);        //select asynchronous operation of timer2 (32,768kHz)        OCR2A = 200;            // set timer2 compare value 	    TIMSK0 = 0;             // delete any interrupt sources	    TCCR1B = (1<<CS10);     // start timer1 with no prescaling    TCCR2A = (1<<CS20);     // start timer2 with no prescaling	    while((ASSR & 0x01) | (ASSR & 0x04));       //wait for TCN2UB and TCR2UB to be cleared	    Delay(1000);    // wait for external crystal to stabilise        while(!calibrate)    {        cli(); // mt __disable_interrupt();  // disable global interrupt                TIFR1 = 0xFF;   // delete TIFR1 flags        TIFR2 = 0xFF;   // delete TIFR2 flags                TCNT1H = 0;     // clear timer1 counter        TCNT1L = 0;        TCNT2 = 0;      // clear timer2 counter		        while ( !(TIFR2 && (1<<OCF2A)) );   // wait for timer2 compareflag		        TCCR1B = 0; // stop timer1		        sei(); // __enable_interrupt();  // enable global interrupt		        if ( (TIFR1 && (1<<TOV1)) )        {            temp = 0xFFFF;      // if timer1 overflows, set the temp to 0xFFFF        }        else        {   // read out the timer1 counter value            tempL = TCNT1L;            temp = TCNT1H;            temp = (temp << 8);            temp += tempL;        }		        if (temp > OSC_MAX)        {            OSCCAL--;   // the internRC oscillator runs to fast, decrease the OSCCAL        }        else if (temp < OSC_MIN)        {            OSCCAL++;   // the internRC oscillator runs too slow, increase the OSCCAL        }        else            calibrate = TRUE;   // the interRC is correct		        TCCR1B = (1<<CS10); // start timer1    }}/*******************************************************************************   Function name : DumpFlash**   Returns :       None**   Parameters :    None**   Purpose :       Dump the flash contents to the usart :)******************************************************************************/void DumpFlash(char dumpall)     { 	unsigned int page=0;	int totalPages=0;		gUploading=TRUE;			if (dumpall || gRolloverFlash)		totalPages=TOTALPAGESINFLASH;	else		totalPages=gDataPage;		for (page=0;page<totalPages;page++){ // 0 to last logged page            			DF_Print_page(page,RECORDSPERPAGE,dumpall);	}    		if (!dumpall){// we need to Print the last partial page 		DF_Print_page(0,gDataPosition,0);	}		USART_Tx(4); // end with EOT	USART_Tx(4); // end with EOT	USART_Tx(4); // end with EOT				 //USART_Tx(26); // and SUB signals 		DF_CS_inactive;		while (!(UCSRA & (1<<TXC))); // wait for last frame to empty 		gUploading = FALSE;		}/*******************************************************************************   Function name : DF_write_int**   Returns :       None**   Parameters :    int data**   Purpose :       write a 16 bit value to the dataflash******************************************************************************/void DF_write_int(int data){	unsigned char tmpH,tmpL;		tmpH=data >> 8;	tmpL=data & 0x00ff;	DF_SPI_RW(tmpH);	DF_SPI_RW(tmpL);}/*******************************************************************************   Function name : DF_read_int**   Returns :       int data**   Parameters :    None**   Purpose :       read a 16 bit int from the dataflash******************************************************************************/int DF_read_int(void){	unsigned char tmpH,tmpL;	unsigned int tmpInt;		tmpH = DF_SPI_RW(0x00);	tmpL = DF_SPI_RW(0x00);	tmpInt = ((tmpH<<8)+tmpL);		return tmpInt;}/*******************************************************************************   Function name : DF_Print_page**   Returns :       None**   Parameters :    char page no of flash to read, char number of records to print*					char mode to print (ASCII or Binary)**   Purpose :       output 1 page of flash to the usart or buffer if partial page******************************************************************************/void DF_Print_page(char page, char recordstoprint, char mode){	unsigned char pos=0;	unsigned char record; 	unsigned char tmp[3];	unsigned int  tmpInt; 		DF_CS_inactive;	DF_CS_active;  		if (recordstoprint == RECORDSPERPAGE){//read from flash		Cont_Flash_Read_Enable (page,0);// 0 == start of page  	}else{//assume we are reading the last page from the buffer		DF_SPI_RW(Buf1Read);					//buffer 1 read op-code		for (pos=0;pos<4;pos++)			 DF_SPI_RW(0x00);			//don't cares and zero address	}		for (record=0;record<recordstoprint;record++){		if (mode==DUMP_BIN){			for (pos=0;pos<RECORDSIZE;pos++) 				USART_Tx(DF_SPI_RW(0x00));		}else{						// START OF NEW RECORD			#if EN_LOG_DATE			// read and print the date			tmp[1] = DF_SPI_RW(0x00);			tmp[0] = DF_SPI_RW(0x00);			PrintDate(tmp[0],tmp[1]);			USART_Tx(' ');#endif			#if EN_LOG_CLOCK			// read and print the time			for (pos=0;pos<3;pos++)				tmp[pos] = DF_SPI_RW(0x00);			PrintClock(tmp[2],tmp[1],tmp[0]);			USART_Tx(' ');#endif			//Dump params as RAW ADC counts 10Bit #s			for (pos=0;pos<(NOOFPARAMS-NONSTDPARAMS);pos++){     				tmpInt=DF_read_int();				ADC2RAW(TRUE,tmpInt);	//convert and log				USART_Tx(' ');			} 						// Non-standard parmas with custom output formatting			// go here			//#if EN_LOG_BATTERY			tmpInt=DF_read_int();			ADC2Volt(TRUE,tmpInt);	//convert and log			USART_Tx(' ');#endif			#if EN_LOG_SHT			// Dump temp and humidity as raw HEX bytes			for (tmp[0]=0;tmp[0]<2;tmp[0]++){				for (pos=0;pos<2;pos++)					USART_Tx_Hex(DF_SPI_RW(0x00));				USART_Tx(' ');			}#endif			#if EN_LOG_SPEED			// speed can have value greater than 1023 so we use			// special printing function for a 5 digit number			tmpInt=DF_read_int();			PrintSpeed(tmpInt);#endif			#if EN_LOG_TEMP			//last entry is a temperature			tmpInt=DF_read_int();			ADC2Temp(TRUE,tmpInt);#endif			USART_EOL(gEOL);		}	} }/*******************************************************************************   Function name : doLogging**   Returns :       None**   Parameters :    None**   Purpose :       read all the ADC Channels and save to flash*					this takes ~10ms for adc samples.*****************************************************************************/void doLogging(void){	unsigned int	log_data=0;	char	cbdisable;	char	adcv;#if EN_LOG_DIR	char	i; #endif		// exit if flash is full	if (!gEnableRollover && gRolloverFlash) return;		// we should never do this if we are in view mode, so....	cbdisable = Timer0_RemoveCallbackFunction(ADC_periphery); //disable lcd updates for a while	adcv = ADMUX; 		//log to buffer	DF_CS_inactive;	DF_CS_active;  		Buffer_Write_Enable (1,gDataPosition*RECORDSIZE);	#if EN_LOG_DATE	LogDate(); 				// send the Date to flash	#endif	#if EN_LOG_CLOCK	LogClock();				// send the Clock to flash	#endif		// log Light #if EN_LOG_LIGHT	ADC_init(LIGHT_SENSOR);				// set up adc	log_data=1023-ADC_read();			// read adc	DF_write_int(log_data);#endif		// Log direction XYZ Accerlerometer	// (ADC4..6)#if EN_LOG_DIR	for (i=0;i<DIRECTION_ADCS;i++){		ADC_init(DIR_SENSOR+i);			// set up adc		log_data=ADC_read();			// read adc		DF_write_int(log_data);	}#endif		// Log Battery Voltage#if EN_LOG_BATTERY	ADC_init(VOLTAGE_SENSOR);			// set up adc	log_data=ADC_read();				// read adc	DF_write_int(log_data);#endif		// Log Humidity and external Temp#if EN_LOG_SHT	ShtReset();	log_data=ShtMeasure(SHT_TEMPERATURE);	DF_write_int(log_data);	log_data=ShtMeasure(SHT_HUMIDITY);	DF_write_int(log_data);#endif		//log wind speed (rpm)#if EN_LOG_SPEED	DF_write_int(gSpeedTotal);	gSpeedTotal=0;						// clear this if manual logging is being used#endif		//log temp#if	EN_LOG_TEMP	ADC_init(TEMPERATURE_SENSOR);		// set up adc	log_data=ADC_read();				// read adc	DF_write_int(log_data);#endif		gDataPosition++;		//transfer to DataFlash if the page is full	if (gDataPosition==RECORDSPERPAGE){		Buffer_To_Page (1,gDataPage);             		gDataPosition=0;		gDataPage++;				//Check for Flash Rollover		if(gDataPage==TOTALPAGESINFLASH){			gRolloverFlash++; // dump entire flash from now on.			gDataPage=0; // reset to page 0;		}				// read the next page into buffer 1		//Page_To_Buffer (gDataPage, 1); 			}		DF_CS_inactive;		if (cbdisable){		ADMUX=adcv;		Timer0_RegisterCallbackFunction(ADC_periphery); 				}		gLogNow=FALSE;				// clear flag	}	

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -