📄 main.c[2010-03-16-09-04-06].sfb
字号:
/* Start ADC1 Software Conversion */
ADC_SoftwareStartConvCmd(ADC1, ENABLE);
}
void USART_Configuration() {
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
/* Configure USARTx_Tx as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Configure USARTx_Rx as input floating */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
USART_InitStructure.USART_BaudRate = 115200;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl
= USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USART1, &USART_InitStructure);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
USART_Cmd(USART1, ENABLE);
}
void GPIO_Configuration(void) {
GPIO_InitTypeDef GPIO_InitStructure;
/* Configure IO connected to LD1, LD2, LD3 and LD4 leds *********************/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* user button ******************************/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* user button ******************************/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOB, &GPIO_InitStructure);
/* PULSE */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
////////////////////////////////////////////////////////////////////////////////
// RTC时钟初始化!
////////////////////////////////////////////////////////////////////////////////
/*******************************************************************************
* Function Name : RTC_Configuration
* Description : 来重新配置RTC和BKP,仅在检测到后备寄存器数据丢失时使用
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void RTC_Configuration(void) {
//启用PWR和BKP的时钟(from APB1)
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);
//后备域解锁
PWR_BackupAccessCmd( ENABLE);
//备份寄存器模块复位
BKP_DeInit();
//外部32.768K其哟偶那个
RCC_LSEConfig( RCC_LSE_ON);
//等待稳定
while (RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET)
;
//RTC时钟源配置成LSE(外部32.768K)
RCC_RTCCLKConfig( RCC_RTCCLKSource_LSE);
//RTC开启
RCC_RTCCLKCmd(ENABLE);
//开启后需要等待APB1时钟与RTC时钟同步,才能读写寄存器
RTC_WaitForSynchro();
//读写寄存器前,要确定上一个操作已经结束
RTC_WaitForLastTask();
//设置RTC分频器,使RTC时钟为1Hz
//RTC period = RTCCLK/RTC_PR = (32.768 KHz)/(32767+1)
RTC_SetPrescaler(32767);
//等待寄存器写入完成
RTC_WaitForLastTask();
//使能秒中断
RTC_ITConfig(RTC_IT_SEC, ENABLE);
//等待写入完成
RTC_WaitForLastTask();
return;
}
void RTC_Config(void) {
//我们在BKP的后备寄存器1中,存了一个特殊字符0xA5A5
//第一次上电或后备电源掉电后,该寄存器数据丢失,
//表明RTC数据丢失,需要重新配置
if (BKP_ReadBackupRegister(BKP_DR1) != 0xA5A5) {
printf("\r\n\n RTC not yet configured....");
//重新配置RTC
printf("\r\n请输入时间,如: !t2010 10 10 10 10 10!");
RTC_Configuration();
//死循环等待输入首次时间
while(timeFirstSet!=1)
{
ProcessProtocol(SERI0);
}
// Time_Adjust();
//配置完成后,向后备寄存器中写特殊字符0xA5A5
BKP_WriteBackupRegister(BKP_DR1, 0xA5A5);
} else {
//若后备寄存器没有掉电,则无需重新配置RTC
//这里我们可以利用RCC_GetFlagStatus()函数查看本次复位类型
if (RCC_GetFlagStatus(RCC_FLAG_PORRST) != RESET) {
//这是上电复位
} else if (RCC_GetFlagStatus(RCC_FLAG_PINRST) != RESET) {
//这是外部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;
}
//检测SD卡 config文件
void CheckSD(void) {
u8 data[100] = 0, i = 0, temp[6] = 0;
f_chdrive(1);
res = f_open(&fsrc, "config.txt", FA_OPEN_EXISTING | FA_READ);
res = f_read(&fsrc, data, 100, &br);
while (i < 6) {
password[i] = data[i];
i++;
} //复制到 password里面
f_chdrive(0);
res = f_open(&fdst, "config.txt", FA_OPEN_EXISTING | FA_READ);
if (res) //文件不存在 创建一个
{
res = f_open(&fdst, "config.txt", FA_CREATE_ALWAYS | FA_WRITE);
res = f_puts(password, &fdst);
}
res = f_open(&fdst, "config.txt", FA_OPEN_EXISTING | FA_READ);
res = f_read(&fdst, temp, 6, &br);
i = 0;
passwordflag = 0;
while (password[i] == temp[i]) {
i++;
passwordflag = 1;
}
}
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:/machine/");
strcat(sdfn, machine);
strcat(sdfn, "/");
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;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -