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

📄 main.c

📁 STM32_fatfs_shell_SDHC.rar
💻 C
📖 第 1 页 / 共 2 页
字号:
			//这是外部RST管脚复位
		}
		printf("\r\n No need to configure RTC....");
		//清除RCC中复位标志
		RCC_ClearFlag();

		//虽然RTC模块不需要重新配置,且掉电后依靠后备电池依然运行
		RCC_RTCCLKCmd( ENABLE);
		//等待RTC时钟与APB1时钟同步
		RTC_WaitForSynchro();

		//使能秒中断
		RTC_ITConfig(RTC_IT_SEC, ENABLE);
		//等待操作完成
		RTC_WaitForLastTask();
	}
	RCC_ClearFlag();

#ifdef RTCClockOutput_Enable
	/* Enable PWR and BKP clocks */
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);

	/* Allow access to BKP Domain */
	PWR_BackupAccessCmd( ENABLE);

	/* Disable the Tamper Pin */
	BKP_TamperPinCmd( DISABLE); /* To output RTCCLK/64 on Tamper pin, the tamper
	 functionality must be disabled */

	/* Enable RTC Clock Output on Tamper Pin */
	BKP_RTCOutputConfig( BKP_RTCOutputSource_CalibClock);
#endif

	return;
}

int fputc(int ch, FILE *f) {
	/* Write a character to the USART */
	USART_SendData(USART1, (u8) ch);

	/* Loop until the end of transmission */
	while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET) {
	}

	return ch;
}

FRESULT scan_files(char* path) {
	FRESULT res;
	FILINFO fno;
	DIR dir;
	int i;
	char *fn;
#if _USE_LFN
	static char lfn[_MAX_LFN * (_DF1S ? 2 : 1) + 1];
	fno.lfname = lfn;
	fno.lfsize = sizeof(lfn);
#endif

	res = f_opendir(&dir, path);
	if (res == FR_OK) {
		i = strlen(path);
		for (;;) {
			res = f_readdir(&dir, &fno);
			if (res != FR_OK || fno.fname[0] == 0)
				break;
			if (fno.fname[0] == '.')
				continue;
#if _USE_LFN
			fn = *fno.lfname ? fno.lfname : fno.fname;
#else
			fn = fno.fname;
#endif
			if (fno.fattrib & AM_DIR) {
				sprintf(&path[i], "/%s", fn);
				res = scan_files(path);
				if (res != FR_OK)
					break;
				path[i] = 0;
			} else {
				printf("%s/%s\n", path, fn);
			}
		}
	}

	return res;
}


FRESULT RemoveFile(void) {
	FRESULT res;
	FILINFO fno;
	DIR dir;
	int i;
	char fileNum = 0;
	char stringTemp[13] = "99999999.txt";
	char *fn;
	char* path = "0:";
#if _USE_LFN
	static char lfn[_MAX_LFN * (_DF1S ? 2 : 1) + 1];
	fno.lfname = lfn;
	fno.lfsize = sizeof(lfn);
#endif

	res = f_opendir(&dir, path);
	if (res == FR_OK) {
		i = strlen(path);
		for (;;) {
			res = f_readdir(&dir, &fno);
			if (res != FR_OK || fno.fname[0] == 0)
				break;
			if (fno.fname[0] == '.')
				continue;
#if _USE_LFN
			fn = *fno.lfname ? fno.lfname : fno.fname;
#else
			fn = fno.fname;
#endif
			if (strcmp(stringTemp, fn) > 0) {
				strcpy(stringTemp, fn);
			}
			strcpy(*(fileNames + fileNum), fn);
			fileNum++;
			if (fileNum == FILEAMOUNT) {
				f_unlink(stringTemp);
				break;
			}
		}
	}

	return res;
}

void CopyFiles(void) {

	FRESULT res;
	FILINFO fno;
	DIR dir;
	int i;
	char fileNum = 0;
	char *fn;
	char* path = "0:";
	char sdfn[40];
	u8 buffer[512];
#if _USE_LFN
	static char lfn[_MAX_LFN * (_DF1S ? 2 : 1) + 1];
	fno.lfname = lfn;
	fno.lfsize = sizeof(lfn);
#endif

	res = f_opendir(&dir, path);
	if (res == FR_OK) {
		i = strlen(path);
		for (;;) {
			res = f_readdir(&dir, &fno);
			if (res != FR_OK || fno.fname[0] == 0)
				break;
			if (fno.fname[0] == '.')
				continue;
#if _USE_LFN
			fn = *fno.lfname ? fno.lfname : fno.fname;
#else
			fn = fno.fname;
#endif
			if (fno.fattrib & AM_DIR) {
				sprintf(&path[i], "/%s", fn);
				res = scan_files(path);
				if (res != FR_OK)
					break;
				path[i] = 0;
			} else {
				//	printf("%s/%s\n", path, fn);
				strcpy(fileNames[fileNum], fn);
				fileNum++;

			}

		}
	}

	for (; fileNum > 0; fileNum--) {

		f_mount(0, &fs[0]);
		f_mount(1, &fs[1]);
		printf("\r\n 开始拷贝数据");
		f_chdrive(0); //打开dataflash
		// res = f_open(&fsrc, currentFileName, FA_OPEN_EXISTING | FA_READ);
		res = f_open(&fsrc, fileNames[fileNum - 1], FA_OPEN_ALWAYS | FA_READ);
		if (res) {
			printf("\r\n 打开DataFlash 失败 ");
			return;
		}
		printf("\r\n 打开DataFlash 成功 ");
		f_chdrive(1); //打开SD卡
		//获取文件路径"1:/machine/machine1/20100208.txt", 

		strcpy(sdfn, "1:/");
		strcat(sdfn, fileNames[fileNum - 1]);

		//	res = f_opendir(&fdst, sdfn);//创建一个文件,如果存在清除数据
		res = f_open(&fdst, sdfn, FA_CREATE_ALWAYS | FA_WRITE);//创建一个文件,如果存在清除数据
		if (res) {
			printf("\r\n 打开SD 卡失败 ");
			return;
		}
		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 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 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) {
	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);
}

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;
			}
		} else {
			;
		}
	}
}

⌨️ 快捷键说明

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