標(biāo)題:
STM32F030+SD2405+24C08+SD卡
[打印本頁]
作者:
f32174501
時(shí)間:
2023-7-20 20:51
標(biāo)題:
STM32F030+SD2405+24C08+SD卡
#include "my.h"
#include "24C08.h"
#include "SD2405.h"
#include "led.h"
FRESULT write_TF(FIL fsr,char *namebuf,uint8_t *databuf,uint32_t len,uint16_t lseek,uint8_t location)
{
FRESULT res1;
UINT bw;
res1=f_open(&fsr,namebuf,FA_OPEN_ALWAYS | FA_WRITE);
switch (location)
{
case 0 ://指定位置
res1=f_lseek(&fsr,lseek);
break;
case 1 : //從頭
break;
case 2 : //至尾
res1=f_lseek(&fsr,fsr.fsize);
break;
default:
break;
}
res1 = f_write(&fsr,databuf,len,&bw);
res1 = f_close(&fsr);
return res1;
}
uint32_t FileSize;
FRESULT read_TF(FIL fsr,char *namebuf,uint8_t *databuf,uint32_t len,uint16_t lseek,uint8_t location)
{
uint32_t Datalen;
FRESULT res1;
UINT br;
res1=f_open(&fsr,namebuf,FA_READ);
FileSize=f_size(&fsr);
switch (location)
{
case 0 : //指定位置
res1=f_lseek(&fsr,lseek);
break;
case 1 : //從頭
break;
case 2 : //至尾
res1=f_lseek(&fsr,fsr.fsize);
break;
default:
break;
}
if(len==0)
Datalen=FileSize;
else
Datalen=len;
res1 = f_read(&fsr,databuf,Datalen,&br);
res1 = f_close(&fsr);
return res1;
}
///////////////////////////////////////////////////////////////
//channel=0:串口1,time15定時(shí)時(shí)間
//channel=1:串口2,time16定時(shí)時(shí)間
//channel=2:深度數(shù)據(jù),淺-米
//channel=3:深度數(shù)據(jù),深-米
extern uint32_t Time_PT[2];
extern uint32_t Work_Time[4];
uint16_t usart_readtime(uint8_t channel,uint8_t *buf)
{
uint8_t i;
uint16_t data;
uint32_t sum;
uint16_t temp;
if(channel<2)
{
sum=(buf[4*channel+0]*60*60+buf[4*channel+1]*60+buf[4*channel+2])*1000+buf[4*channel+3]*10;
if(sum>10)
{
Time_PT[channel]=sum/10;
data=10;
}
else
{
data=sum;
}
}
else if(2<=channel && channel<=3)
{
temp=(buf[2*(channel-2)+8]<<8) + buf[2*(channel-2)+9];
data=temp;
}
else if(channel>3)
{
channel=4;
for(i=0;i<2;i++)
{
Work_Time[2*(channel+i-4)]=buf[4*(channel+i-1)]*100+buf[4*(channel+i-1)+1]; //12,13 16,17
Work_Time[2*(channel+i-4)+1]=buf[4*(channel+i-1)+2]*100+buf[4*(channel+i-1)+3];//14,15 18,19
}
data=0;
}
return data;
}
extern int32_t depth_data; //厘米的深度數(shù)據(jù),除以100是米,除以10000是mpa。
extern int16_t depth_temp; //水溫?cái)?shù)據(jù),3位小數(shù)點(diǎn),單位℃。
//出口:depth_temp = 水溫?cái)?shù)據(jù)
// depth_data = 壓力數(shù)據(jù)
void temper_dep(uint8_t *databuf)
{
uint8_t state2;
uint8_t c2;
uint8_t buf[8];
signed long L32;
//2-18的數(shù)字取出并累加
state2=0;
for(c2=2;c2<USART1_RX_MAX-3;c2++)
{
state2 = state2 + (databuf[c2]-48);//0x30
}
//接收的累加和
buf[0]= databuf[USART1_RX_MAX-3]-48;//百位
buf[1]= databuf[USART1_RX_MAX-2]-48;//十位
buf[2]= databuf[USART1_RX_MAX-1]-48;//個(gè)位
c2=(buf[0]*100)+(buf[1]*10)+buf[2];
if(state2==c2)
{
//與接收的ASCII累加和相等
//取出地址
buf[0]= databuf[2]-48;//地址十位
buf[1]= databuf[3]-48;//地址個(gè)位
buf[2]= buf[0]*10+buf[1];
if(buf[2]==DEPTH_TEMP_ADD)//地址=1
{
//取出壓力值和標(biāo)志
state2 = databuf[4]-48;//取出壓力正負(fù)標(biāo)志
buf[0]=databuf[5]-48;//壓力千位
buf[1]=databuf[6]-48;//壓力百位
buf[2]=databuf[7]-48;//壓力十位
buf[3]=databuf[8]-48;//壓力個(gè)位
buf[4]=databuf[9]-48; //小數(shù)1位
buf[5]=databuf[10]-48;//小數(shù)2位
buf[6]=databuf[11]-48;//小數(shù)3位
buf[7]=databuf[12]-48;//小數(shù)4位
L32 = (long)((buf[0]*10000000)+(buf[1]*1000000)+(buf[2]*100000)
+(buf[3]*10000)+(buf[4]*1000)+(buf[5]*100)+(buf[6]*10)+buf[7]);
depth_data=L32; //壓力數(shù)據(jù)
if(state2==1)//壓力正負(fù)標(biāo)志,00=正數(shù),01=負(fù)數(shù)
{
depth_data = ~depth_data+1;//轉(zhuǎn)換為負(fù)數(shù)
}
//取出溫度值和標(biāo)志
state2 = databuf[13]-48;//取出溫度正負(fù)標(biāo)志
buf[0]=databuf[14]-48;//溫度十位
buf[1]=databuf[15]-48;//溫度個(gè)位
buf[2]=databuf[16]-48;//溫度小數(shù)1
buf[3]=databuf[17]-48;//溫度小數(shù)2
buf[4]=databuf[18]-48;//溫度小數(shù)3
L32 = (long)((buf[0]*10000)+(buf[1]*1000)+(buf[2]*100)+(buf[3]*10)+buf[4]);
depth_temp = L32;
if(state2==1)//水溫度正負(fù)標(biāo)志,00=正數(shù),01=負(fù)數(shù)
{
depth_temp = ~depth_temp+1;//轉(zhuǎn)換為負(fù)數(shù)
}
}
}
}
FRESULT storage(FIL fsr,Time_Def *timebuf,uint8_t channel)
{
FRESULT res1;
uint8_t databuf[12];
char filebuf[13];
//RTC_GetRTCTime(timebuf);
RTC_ReadDate(timebuf);
if(channel==0)
{
filebuf[0]=timebuf->Year/16+48;
filebuf[1]=timebuf->Year%16+48;
filebuf[2]=timebuf->Month/16+48;
filebuf[3]=timebuf->Month%16+48;
filebuf[4]=timebuf->Day/16+48;
filebuf[5]=timebuf->Day%16+48;
filebuf[6]=timebuf->Hour/16+48;
filebuf[7]=timebuf->Hour%16+48;
filebuf[8]='.';
filebuf[9]='t';
filebuf[10]='x';
filebuf[11]='t';
filebuf[12]='\0';
databuf[0]=0Xff;
databuf[1]=0Xfa;
databuf[2]=timebuf->Minute;
databuf[3]=timebuf->Second;
databuf[4]=depth_data >> 24;
databuf[5]=depth_data >> 16;
databuf[6]=depth_data>> 8;
databuf[7]=depth_data;
databuf[8]=depth_temp>> 8;
databuf[9]=depth_temp;
databuf[10]=0Xfa;
databuf[11]=0Xff;
res1=write_TF(fsr,filebuf,databuf,12,0,2);
}
if(channel==1)
{
}
return res1;
}
extern uint8_t Wrong_buf[5];
void Modbus_0X10_EEPROM(uint8_t *buf,uint16_t len)
{
uint8_t i;
uint8_t databuf1[20];
uint8_t databuf2[20];
for(i=0;i<6;i++)
databuf2[i]=buf[i];
CrcCheck(databuf2,6);
if(buf[2]==0)
{
for(i=0;i<buf[6];i++)
databuf1[i]=buf[i+7];
EEPROM_Write(buf[3],databuf1,buf[6]);
USART_SendBuffDat(USART4,databuf2,8);
}
else
{
if(buf[5]==3 && buf[6]==6)
{
time_init.Year=buf[7];
time_init.Month=buf[8];
time_init.Day=buf[9];
time_init.Week=0x01;
time_init.Hour=buf[10];
time_init.Minute=buf[11];
time_init.Second=buf[12];
RTC_WriteDate(&time_init);
USART_SendBuffDat(USART4,databuf2,8);
}
else
{
USART_SendBuffDat(USART4,Wrong_buf,sizeof(Wrong_buf));
}
}
}
void Modbus_0X03_EEPROM(uint8_t *buf,uint16_t len)
{
uint8_t i;
uint8_t databuf1[20];
uint8_t databuf2[20];
databuf2[0]=buf[0];
databuf2[1]=buf[1];
databuf2[2]=buf[5]*2;
if(buf[2]==0)
{
EEPROM_Read(buf[3],databuf1,buf[5]*2);
//delay_ms(10);
for(i=0;i<buf[5]*2;i++)
databuf2[3+i]=databuf1[i];
CrcCheck(databuf2,buf[5]*2+3);
USART_SendBuffDat(USART4,databuf2,buf[5]*2+5);
}
else
{
if(buf[5]*2==6)
{
RTC_ReadDate(&sysTime);
databuf1[0]=sysTime.Year;
databuf1[1]=sysTime.Month;
databuf1[2]=sysTime.Day;
databuf1[3]=sysTime.Hour;
databuf1[4]=sysTime.Minute;
databuf1[5]=sysTime.Second;
for(i=0;i<buf[5]*2;i++)
databuf2[3+i]=databuf1[i];
CrcCheck(databuf2,buf[5]*2+3);
USART_SendBuffDat(USART4,databuf2,buf[5]*2+5);
}
else
{
USART_SendBuffDat(USART4,Wrong_buf,sizeof(Wrong_buf));
}
}
}
extern uint8_t BD_SendBuf[UART_BUFF_MAX_SIZE];
extern uint8_t Right_buf[5];
extern uint8_t Wrong_buf[5];
uint8_t BD_Return(char *buf,uint16_t len)
{
uint8_t i;
if(buf[0]=='
僅供參考:
代碼.7z
(664 KB, 下載次數(shù): 15)
2023-7-20 22:15 上傳
點(diǎn)擊文件名下載附件
下載積分: 黑幣 -5
&& buf[59]=='\r' && buf[60]=='\n')//$DWXX,21/12/09,04:59:29,121.532003,E,31.079860,N,24,8,0.00\r\n
{
time_init.Year=(buf[6]-48)*16+(buf[7]-48);
time_init.Month=(buf[9]-48)*16+(buf[10]-48);
time_init.Day=(buf[12]-48)*16+(buf[13]-48);
time_init.Week=0x01;
time_init.Hour=(buf[15]-48)*16+(buf[16]-48);
time_init.Minute=(buf[18]-48)*16+(buf[19]-48);
time_init.Second=(buf[21]-48)*16+(buf[22]-48);
RTC_WriteDate(&time_init);
BD_SendBuf[8]=0x22;
for(i=0;i<len-6;i++)
BD_SendBuf[9+i]=buf[i];
BD_SendBuf[9+len-6]=0x22;
BD_SendBuf[9+len-5]=0x0D;
BD_SendBuf[9+len-4]=0x0A;
delay_ms(50);
USART_SendBuffDat(USART2,BD_SendBuf,len+12-6); //AT+SEND="$DWXX,21/12/09,04:59:29,121.532003,E,31.079860,N,24,8,0.00"\r\n
USART_SendBuffDat(USART4,Right_buf,sizeof(Right_buf));
return 0;
}
else if(!strncmp(buf,"NO EFFECTIVE LOCATION INFORMATION",len))
{
USART_SendBuffDat(USART4,Wrong_buf,sizeof(Wrong_buf));
return 1;
}
else if(!strncmp(buf,"OK",len))
{
USART_SendBuffDat(USART4,Right_buf,sizeof(Right_buf));
return 2;
}
else
{
USART_SendBuffDat(USART4,Wrong_buf,sizeof(Wrong_buf));
return 3;
}
}
extern uint32_t Time_PT[2];
extern uint32_t Time15_tag;
extern uint32_t Time16_tag;
extern uint8_t BD_Work_Flag;
void Sensor_BD_Power(uint32_t *buf)
{
if(Time_PT[0]>(buf[0]+buf[1]))
{
if(Time15_tag>(Time_PT[0]-buf[0]) || Time15_tag<buf[1])
SENSOR_ON;
else
SENSOR_OFF;
}
else
SENSOR_ON;
if(BD_Work_Flag)
if(Time_PT[1]>(buf[2]+buf[3]))
{
if(Time16_tag>(Time_PT[1]-buf[2]) || Time16_tag<buf[3])
BD_ON;
else
BD_OFF;
}
else
BD_ON;
else
BD_OFF;
}
extern uint8_t tebuf[LOAD_LEN_MAX];
void Data_load(FIL fsr,uint8_t *buf,uint32_t len)
{
uint8_t NoData_buf[8]={0xBB,0xAA,0x23,0x04,0x23,0x13,0xDD,0xCC};
uint8_t load_buf[LOAD_LEN_MAX];
FRESULT res1;
char filebuf[13];
uint8_t hour;
uint8_t temp;
uint32_t i,j;
hour=buf[6]/16*10+buf[6]%16;
temp=hour;
for(i=0;i<len-2;i++)
tebuf[i]=buf[i];
for(i=0;i<24-temp;i++)
{
filebuf[0]=buf[3]/16+48;//年
filebuf[1]=buf[3]%16+48;
filebuf[2]=buf[4]/16+48;//月
filebuf[3]=buf[4]%16+48;
filebuf[4]=buf[5]/16+48;//日
filebuf[5]=buf[5]%16+48;
filebuf[6]= hour/10+48;//時(shí)
filebuf[7]= hour%10+48;
filebuf[8]='.';
filebuf[9]='t';
filebuf[10]='x';
filebuf[11]='t';
filebuf[12]='\0';
res1=read_TF(fsr,filebuf,load_buf,0,0,1);
tebuf[2]=FileSize+8;
tebuf[6]=hour/10*16+hour%10;
tebuf[9]=1+i;
tebuf[10]=1;
for(j=0;j<FileSize;j++)
tebuf[11+j]=load_buf[j];
tebuf[FileSize+11]=0xCC;
tebuf[FileSize+12]=0xDD;
if(res1==0)
USART_SendBuffDat(USART4,tebuf,FileSize+13);
else
{
for(j=0;j<4;j++)
NoData_buf[2+j]=buf[3+j];
NoData_buf[5]=hour/10*16+hour%10;
USART_SendBuffDat(USART4,NoData_buf,sizeof(NoData_buf));
}
hour++;
}
}
復(fù)制代碼
僅供參考:
歡迎光臨 (http://www.torrancerestoration.com/bbs/)
Powered by Discuz! X3.1