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

📄 main.c[2010-03-16-09-04-06].sfb

📁 STM32_fatfs_shell_SDHC.rar
💻 SFB
📖 第 1 页 / 共 3 页
字号:
			}
			printf("\r\n 打开SD 卡成功 ");
			for (;;) {
				res = f_read(&fsrc, buffer, sizeof(buffer), &br);
				if (res || br == 0)
					break; /* error or eof */
				res = f_write(&fdst, buffer, br, &bw);
				if (res || bw < br)
					break; /* error or disk full */
			}
			printf("\r\n 数据成功拷贝到SD卡"); //还需要修改
			/* Close open files */
			f_close(&fsrc);
			f_close(&fdst);
	}

}

void DataFlashToSD(void) {

	if (!GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_8)) {
		delay_ms(50);
		if (!GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_8)) {
			while (!GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_8)) {
				;
			}
			/*
			//LED亮
			GPIO_SetBits(GPIOA, GPIO_Pin_0);
			GPIO_SetBits(GPIOA, GPIO_Pin_1);
			delay_ms(500);
			GPIO_ResetBits(GPIOA, GPIO_Pin_0);
			GPIO_ResetBits(GPIOA, GPIO_Pin_1);
*/
			
			CopyFiles();
		}
	}
}
/*
void SdControl(void) {

	if (!GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_8)) {
		delay_ms(50);
		if (!GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_8)) {
			while (!GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_8)) {
				;
			}

	//		load_config();
			
			CopyFiles();
		}
	}
}
*/


void SdControl(void) {

	if (!GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_5)) {
		delay_ms(50);
		if (!GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_5)) {

			if(sdFlag==0){
				sdFlag=1;
				CopyFiles();
			}
			//点亮绿灯
			GPIO_SetBits(GPIOA, GPIO_Pin_1);
			/*
			//LED亮
			GPIO_SetBits(GPIOA, GPIO_Pin_0);
			GPIO_SetBits(GPIOA, GPIO_Pin_1);
			delay_ms(500);
			GPIO_ResetBits(GPIOA, GPIO_Pin_0);
			GPIO_ResetBits(GPIOA, GPIO_Pin_1);
*/
//			load_config();
			
		}
	}
	else{
		sdFlag=0;
		
		GPIO_ResetBits(GPIOA, GPIO_Pin_1);//关绿灯
	}
}

void Update(void) {
	static u8 i = 0;
	u32 unixTime=0;
	
	if (i == UPDATESECONDS) {
		i = 0;
		//换行符号
		pulse_count = TIM_GetCounter(TIM1);
		AD_value = filter();
		AD_value = (AD_value * 330) / 4096;
		time_now = Time_GetCalendarTime();
		timedata[0] = time_now.tm_hour / 10 + '0';
		timedata[1] = time_now.tm_hour % 10 + '0';
		timedata[2] = ':';
		timedata[3] = time_now.tm_min / 10 + '0';
		timedata[4] = time_now.tm_min % 10 + '0';
		timedata[5] = ':';
		timedata[6] = time_now.tm_sec / 10 + '0';
		timedata[7] = time_now.tm_sec % 10 + '0';
		timedata[8] = 0x20; //空格
		timedata[9] = AD_value / 100 + '0';
		timedata[10] = (AD_value / 10) % 10 + '0';
		timedata[11] = AD_value % 10 + '0';
		timedata[12] = 0x20; //空格
		timedata[13] = pulse_count / 100 + '0';
		timedata[14] = (pulse_count / 10) % 10 + '0';
		timedata[15] = pulse_count % 10 + '0';
		timedata[16] = 0x0D;
		timedata[17] = 0x0A;

		//清空定时器
		printf("\r\n 脉冲值 : %d ",pulse_count);
		TIM_SetCounter(TIM1, 0);

		currentFileName[0] = time_now.tm_year / 1000 + '0';
		currentFileName[1] = time_now.tm_year % 1000 / 100 + '0';
		currentFileName[2] = time_now.tm_year % 100 / 10 + '0';
		//currentFileName[3] = time_now.tm_year % 10 + '1';
		currentFileName[3] = time_now.tm_year % 10 + '0';
		currentFileName[4] = (time_now.tm_mon+1) / 10 + '0';
		currentFileName[5] = (time_now.tm_mon+1) % 10 + '0';
		currentFileName[6] = time_now.tm_mday / 10 + '0';
		currentFileName[7] = time_now.tm_mday % 10 + '0';
		currentFileName[8] = '.';
		currentFileName[9] = 't';
		currentFileName[10] = 'x';
		currentFileName[11] = 't';
		currentFileName[12] = '\0';

		f_chdrive(0);
		printf("\r\n开始存储数据");
		printf("\r\n currentFileName %s",currentFileName);
		res = f_open(&fdst, currentFileName, FA_OPEN_ALWAYS | FA_WRITE);
		if (res) {
			printf("\r\n 1打开DataFlash 失败 ");
			return;
		}
		printf("\r\n 1打开DataFlash 成功 ");
		res = f_lseek(&fdst, fdst.fsize);
	//	res = f_write(&fdst, timedata, 9, &bw);
	   	unixTime=RTC_GetCounter();
//	res = f_puts(" aa:", &fdst);
	   res = f_write(&fdst, &unixTime, 4, &bw);
//	   res = f_puts(" bb:", &fdst);
//	   f_printf(&fdst, "%ld", unixTime); 

res = f_write(&fdst, &AD_value_mul4, 2, &bw);
res = f_write(&fdst, &pulse_count, 2, &bw);
	   f_putc(0x0D,&fdst);
		f_putc(0x0A,&fdst);	   
	   	 printf("\r\n存入的数据是  %s",timedata);   
	/*  
	   f_putc(0x20,&fdst);
	   printf("\r\n unixTime %ld",unixTime);
	   res = f_write(&fdst, &AD_value_text, 2, &bw);
	   f_putc(0x20,&fdst);
	   res = f_write(&fdst, &pulse_count, 2, &bw);
	   f_putc(0x0D,&fdst);
		f_putc(0x0A,&fdst);
		*/
//		res = f_puts(timedata, &fdst);

		/*
		if (res)
			printf("\r\n数据成功存入dataflash");
		else
			printf("\r\n存储数据失败");
	*/

		//res = f_write(&fdst, currentFileName, 11, &bw);

		f_close(&fdst);
	}
	i++;
 
	//删除超过日期较早的文件。
	RemoveFile();
	/*	
	 f_chdrive(0);
	 printf("\r\n开始存储数据");
	 res = f_open(&fdst, "test.txt", FA_OPEN_EXISTING | FA_WRITE);
	 res =f_lseek(&fdst,fdst.fsize);
	 //res = f_write(&fdst, timedata, 14, &bw);
	 res =f_puts(timedata,&fdst);
	 if(res) printf("\r\n数据成功存入dataflash");
	 else printf("\r\n存储数据失败");
	 printf("\r\n存入的数据是  %s",timedata);
	 
	 f_close(&fdst);
	 }
	 i++;
	 */


}


void DataFlashInit(void) {
	FATFS * fs;
	DWORD clust;
	//检测到dataflash有文件系统
	if (f_getfree("0:", &clust, &fs) == FR_OK) {
		// Get free space
		printf("\r\n%lu KB 总空间.\n"
			"还有%lu KB 可用.\n", (DWORD)(fs->max_clust - 2) * fs->csize / 2, clust
				* fs->csize / 2);
	} else {
		printf("\r\n dataflash不存在文件系统,开始格式化dataflash\n\n");
		res = f_mkfs(0, 1, 0);
		if (res)
			printf("\r\n ddataflash 格式化失败  \n\n");
		printf("\r\n dataflash 格式化成功  \n\n");
		f_chdrive(0); //打开SD卡	  0代表 SD卡,,1代表dataflash
		//        res = f_open(&fsrc, "test.txt", FA_CREATE_ALWAYS ); 
		//        if(res)  printf("\r\n 创建测试文件成功 ");
	}
	f_close(&fsrc);
}

void Time_Adjust(void) {
	struct tm now;
	now.tm_year = -1;
	now.tm_mon = -1;
	now.tm_mday = -1;
	now.tm_hour = -1;
	now.tm_min = -1;
	now.tm_sec = -1;
	printf("\r\n==============时间设置=====================================");
	/////年
	printf("\r\n  输入年份");
	while (now.tm_year > 2037 || now.tm_year < 2000) {
		printf("\r\n  请输入00---37");
		now.tm_year = 2000 + USART_Scanf(37);
	}
	printf("\r\n你输入的是:  %d", now.tm_year);
	/////月/
	printf("\r\n  输入月份");
	while (now.tm_mon > 12 || now.tm_mon < 1) {
		printf("\r\n  请输入1---12");
		now.tm_mon = USART_Scanf(12) - 1;
	}
	printf("\r\n你输入的是:  %d", now.tm_mon + 1);
	/////日 
	printf("\r\n  输入日期");
	while (now.tm_mday > 31 || now.tm_mday < 1) {
		printf("\r\n 请输入1---31");
		now.tm_mday = USART_Scanf(31);
	}
	printf("\r\n你输入的是:  %d", now.tm_mday);

	//////时
	printf("\r\n  输入小时");
	while (now.tm_hour > 23 || now.tm_hour < 0) {
		printf("\r\n 请输入1----23");
		now.tm_hour = USART_Scanf(23);
	}
	printf("\r\n你输入的是:  %d", now.tm_hour);
	////////分
	printf("\r\n  输入分钟");
	while (now.tm_min > 59 || now.tm_min < 0) {
		printf("\r\n请输入1---59");
		now.tm_min = USART_Scanf(59);
	}
	printf("\r\n你输入的是:  %d", now.tm_min);
	////////秒
	printf("\r\n  输入秒");
	while (now.tm_sec > 59 || now.tm_sec < 0) {
		printf("\r\n 请输入1----59");
		now.tm_sec = USART_Scanf(59);

	}
	printf("\r\n你输入的是:  %d", now.tm_sec);
	Time_SetCalendarTime(now); //设置 时间
}

void Time_Display(void) {
	//  current_time = Time_GetUnixTime();
	AD_value = ADC_GetConversionValue(ADC1); //0--4096
	AD_value = (AD_value * 330) / 4096;
	time_now = Time_GetCalendarTime();
	printf("\r\n当前时间:%d-%d-%d    %02d:%02d:%02d", time_now.tm_year,
			time_now.tm_mon + 1, time_now.tm_mday, time_now.tm_hour,
			time_now.tm_min, time_now.tm_sec);
	printf("\r\n当前的电压是 %d:", AD_value);
}

u8 USART_Scanf(u32 value) {
	u32 index = 0;
	u32 tmp[2] = { 0, 0 };

	while (index < 2) {
		/* Loop until RXNE = 1 */
		while (USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET) {
		}
		tmp[index++] = (USART_ReceiveData(USART1));
		if ((tmp[index - 1] < 0x30) || (tmp[index - 1] > 0x39)) {
			printf("\r\nPlease enter valid number between 0 and 9");
			index--;
		}
	}
	/* Calculate the Corresponding value */
	index = (tmp[1] - 0x30) + ((tmp[0] - 0x30) * 10);
	/* Checks */
	if (index > value) {
		printf("\r\nPlease enter valid number between 0 and %d", value);
		return 0xFF;
	}
	return index;
}

//中位值平均滤波法
#define N 5
u16 filter() 
{ 
char count,i,j; 
u16 value_buf[N]; 
u16 sum=0;
u16 temp;
for (count=0;count<N;count++) 
{ 
value_buf[count] = ADC_GetConversionValue(ADC1); 
delay_ms(1);
} 
for (j=0;j<N-1;j++) 
{ 
for (i=0;i<N-j;i++) 
{ 
if ( value_buf[i]>value_buf[i+1] ) 
{ 
temp = value_buf[i]; 
value_buf[i] = value_buf[i+1]; 
value_buf[i+1] = temp; 
} 
} 
} 
for(count=1;count<N-1;count++) 
sum += value_buf[count]; 
return (char)(sum/(N-2)); 
} 

void Time_Show(void) {
	printf("\n\r");

	/* Infinite loop */
	while (1) {
	if(shellEnable==FALSE){
		SdControl();
		ProcessProtocol(SERI0);
		/* If 1s has paased */
		if (TimeDisplay == 1) {
			/* Display current time */
			Time_Display();
			TimeDisplay = 0;
			Update();
		}
	}
		else
		{
		;
		}
	}
}


⌨️ 快捷键说明

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