📄 maincp.c
字号:
////////////////////////////////
////////////////////////////////////////
//////////////////////////////////////////
#include <pic18.h>
#include "fat16.h"
#include "sl811.h"
#include "buffer.h"
#include "lcd.h"
#include "flash.h"
#include "rs485.h"
#include "ds1302.h"
#include "common.h"
#include "struct.h"
extern SYSTEMTIME CurrentTime;
unsigned char bin_to_ascii(unsigned char car);
//void read_usb(unsigned char Filename[],unsigned long Start, unsigned long Length, unsigned char* buf);
void ascii25_to_bin8(unsigned char read_usb_buf[25],unsigned char *to_buf);
void write_usb(unsigned char Filename[], unsigned long Length);
/////////////////////////////////////////
/////////// 变量定义 ////////////////////
unsigned int count;
unsigned char minute;
unsigned char t_count = 0;
//unsinged char set_ten_minute = 2;
//////////////extern引用变量////////////
extern unsigned char flag_me;
extern unsigned char step;
extern unsigned char rec_count;
extern unsigned char RecBuf[LenOfRec];
extern unsigned char usb_flag;
extern char key_finish;
////////////////////////////////////////////
//中断服务函数,定时器Timer1,Usart口接收
void interrupt isr( void )
{
if(INT0IF==1)
{
INT0IF==0;
INT0IE=0;
}
if( TMR1IF == 1 ) //产生定时中断
{
TMR1ON = 0; //关定时器T1
TMR1IF = 0; //清除TMR1 中断标志
count++; //计数,一次0.1s
if( count > 10 ) //600*0.1S=60s,一分钟
{
count = 0;
minute++;
t_count++;
}
TMR1H = 0xC3; //定时器重新载入初值
TMR1L = 0xB0;
TMR1ON = 1; //开定时器T1
}
if( RCIF == 1 )
{
RecBuf[rec_count] = RCREG;
rec_count++;
RCIF = 0; //清除接收中断标志位,硬件会自动清除,这条语句不起作用
if( OERR == 1 )
{
//接收产生溢出错误,清零CREN可以清除OERR
//Print("error");
CREN = 0;
NOP();
NOP();
NOP();
NOP();
CREN = 1;
}
else
{
//无接收溢出产生
if( RecBuf[0] == 0x55 )
{
//Print("microchip");
//接收到从机数据包的起始位
if( rec_count > 2 )
{
// Print( RecBuf );
if( RecBuf[1] == MASTERADDR )
{
//Print("hi");
//Print( &RecBuf[1] );
//TRISD = 0x00;
//PORTD = 0xee;
flag_me = 1; //置标志位
}
else
{
//TRISD = 0x00;
//PORTD = 0xff;
rec_count = 0;
}
}
}
else
{
//没有接收到数据起始位
rec_count = 0; //接收计数清零
}
}
}
}
//////////////////////////////////////////////////////////
//////////////////////////// 主函数 //////////////////////
//////////////////////////////////////////////////////////
void main( void )
{
unsigned char temp_jump = 0;
unsigned char flash_flag[3];
TRISD = 0xFF;
PORTD = 0;
TRISB = 0X00;
///////////////////////////////////////////////////
//////////////////// 初始化函数 ///////////////////////
Ioinit(); //部分IO口初始化
InitLcd(); //lcd显示初始化
Initial_DS1302(); //时钟芯片初始化
spi_init(); //SPI口Flash初始化
Timer1Init(); //定时器T1初始化
UsartInit(); //Usart口初始化
sl811_init();
//Ioinit(); //部分IO口初始化
read_flash_flag(flash_flag);
// if((flash_flag[0]&0xFF == 0xFF)&&(flash_flag[1]&0xFF == 0xFF)&&(flash_flag[2]&0xFF == 0xFF)&&(flash_flag[3]&0xFF == 0xFF))
if(flash_flag[0]&0xFF > 0x3F)
{ //空FLASH为0xFF;
//初始化标志页,仅第一次上电执行
flash_flag[0] = 0x00;
flash_flag[1] = 0x01;
flash_flag[2] = 0x00;
write_flash_flag(flash_flag);
}
/*******************************************/
//////////////////////// 循环 ////////////////////
while(1)
{
if(key_finish == 1)
{
show_time(); //显示当前时间
ClearLcd();
show_time();
}
Setkey();
keydone(); //进入调整模式
if( usb_flag == 1 )
{
usb_flag = 0;
temp_jump = 1;
ClearLcd(); //清屏,开始写u盘
//写u盘
sl811_start();
pause_ms(100);
GotoXY(1,1);
Print("Read BPB...");
pause_ms(100);
ReadBPB(); //从buffer中读取BPB信息
GotoXY(2,1);
Print("Read BPB OK!!");
pause_ms(100);
GotoXY(1,1);
Print("CHECK File...");
pause_ms(100);
write_usb(filename, 500);
// write_usb(filename, 300);
// usb_flag = 0;
// key_finish = 1;
ClearLcd();
}
if(temp_jump == 1)
{
temp_jump = 0;
key_finish = 1;
}
if(key_finish == 1)
{
show_time(); //显示当前时间
show_time();
}
if( key_finish == 1 ) //&& //(minute == step) )
{
//
if( minute >= step )
//count = 0;
{
write_flash(); //写flash,每接收一个箱子上的温度数据就保存一次
minute = 0;
}
//key_finish = 0;
}
}
}
/**********************************************************************************
*USB设备中断,LCD中“COPY”闪烁,按确定键即开始复制
*通过按键执行数据拷贝
* 1.从FLASH读数据
* 2.将数据写入USB设备
***********************************************************************************/
unsigned char bin_to_ascii(unsigned char car)
{
unsigned char temp;
temp=car+0x30;
if(temp > '9')temp+=7;
return temp;
}
//void read_usb(unsigned char filename[],unsigned long Start, unsigned long Length, unsigned char* buf)
void ascii25_to_bin8(unsigned char read_from_usb[25],unsigned char *to_buf)
{
// unsigned char *read_usb_buf = read_from_usb;
to_buf[0] = (read_from_usb[2]-0x30)*10+(read_from_usb[3]-0x30);
to_buf[1] = (read_from_usb[5]-0x30)*10+(read_from_usb[6]-0x30);
to_buf[2] = (read_from_usb[8]-0x30)*10+(read_from_usb[9]-0x30);
to_buf[3] = (read_from_usb[11]-0x30)*10+(read_from_usb[12]-0x30);
to_buf[4] = (read_from_usb[14]-0x30)*10+(read_from_usb[15]-0x30);
to_buf[5] = read_from_usb[17]-0x30; //箱子号
to_buf[6] = read_from_usb[19]-0x30; //通道号
to_buf[7] = (read_from_usb[21]-0x30)*100+(read_from_usb[22]-0x30)*10+(read_from_usb[23]-0x30);
}
void write_usb(unsigned char filename[], unsigned long Length)
{
unsigned char i,j;
unsigned int k;
unsigned int rpage, rpage_offset;
unsigned char read_usb_buf[25];
unsigned char buff[8];//={8,11,20,12,30,1,2,36};
unsigned char pbuf[8];
unsigned long FileSize;
DIR *fileID=0;
unsigned char a;
unsigned char asc[4];
/*************************************/
unsigned int flash_flag_int_new;
unsigned char flash_flag[3];
unsigned char CircleFlag;
unsigned int count_flag;
// unsigned int page0;
read_flash_flag(flash_flag);
flash_flag_int_new= (unsigned int) flash_flag[0];
flash_flag_int_new<<=8;
flash_flag_int_new|=flash_flag[1];
CircleFlag = flash_flag[2];
//定位文件尾并读取操作最后一条数据存至pbuf,读取信息与FLASH中的时间信息进行比较
if(GetFileID(filename,fileID)==0) //获得和文件名对应的目录项
{ //文件不存在。。。创建新文件
GotoXY(2,1);
Print("Create File...");
pause_ms(100);
CreateFile(filename, 0); //创建一个空文件 void CreateFile(uint8 FileName[11], uint32 Size)
FileSize = 0; //新文件初始大小为0
GotoXY(2,1);
Print("Create File OK!");
GetFileID(filename,fileID);
}
else
{ //文件存在
GotoXY(1,1);
Print("File present!");
pause_ms(100);
FileSize = fileID->FilePosit.Size;
}
if(FileSize==0)
{
if(CircleFlag ==0x55)
rpage = flash_flag_int_new;
else
rpage = 1;
}
else
{
OperateFile(0 , FileSize-25, 25, read_usb_buf,fileID); //Length=25
/* ClearLcd();
GotoXY(1,0);
Print("read_usb_buf");
asc[0]=read_usb_buf[0];
asc[1]=read_usb_buf[1];
asc[2]=read_usb_buf[2];
asc[3]=read_usb_buf[3];
GotoXY(2,0);
Print(asc);
pause_ms(250);
pause_ms(250);
pause_ms(250);
pause_ms(250);
pause_ms(250);
pause_ms(250);
*/
// ascii25_to_bin8(read_usb_buf,pbuf);
/* pbuf[0] = 8;
pbuf[1] = 12;
pbuf[0] = 7;
pbuf[0] = 15;
pbuf[0] = 16;*/
// rpage=find_last_copy(pbuf,flash_flag);
if(rpage == 1000)
{
GotoXY(2,0);
Print("No Need COPY!");
pause_ms(200);
return;
}
GotoXY(2,0);
Print("Start COPY!");
pause_ms(200);
}
ClearLcd();
if(rpage <= flash_flag_int_new)
{
count_flag = flash_flag_int_new - rpage;
}
else
{
count_flag = 834 - rpage + flash_flag_int_new;
}
while( count_flag != 0 )
{
count_flag--;
rpage_offset = 0; //每一页页内地址初始值
AddClus(FileSize,fileID); //增加簇链
SetFileSize(1500); //文件扩大容量, 每次1500字节
for(j=0; j<3; ++j)
{
for(i=0; i<20; ++i) //每页60条数据,分3次读取并发送
{
//txt 显示格式需转换为ASCII码 例:2008-11-06 09:00,1,0,030\n 共25字节
read_one_message(buff, rpage, rpage_offset);//rpage_offet++
rpage_offset += 8; //定位到下一条数据
temp_buffer[i*25]='2';
temp_buffer[i*25+1]='0';
temp_buffer[i*25+2]=bin_to_ascii((buff[0]/10)); //年
temp_buffer[i*25+3]=bin_to_ascii((buff[0]%10));
temp_buffer[i*25+4]='-'; //'-';
temp_buffer[i*25+5]=bin_to_ascii((buff[1]/10)); //月
temp_buffer[i*25+6]=bin_to_ascii((buff[1]%10));
temp_buffer[i*25+7]='-';
temp_buffer[i*25+8]=bin_to_ascii((buff[2]/10)); //日
temp_buffer[i*25+9]=bin_to_ascii((buff[2]%10));
temp_buffer[i*25+10]=' ';
temp_buffer[i*25+11]=bin_to_ascii((buff[3]/10)); //时
temp_buffer[i*25+12]=bin_to_ascii((buff[3]%10));
temp_buffer[i*25+13]=':';
temp_buffer[i*25+14]=bin_to_ascii((buff[4]/10)); //分
temp_buffer[i*25+15]=bin_to_ascii((buff[4]%10));
temp_buffer[i*25+16]=',';
temp_buffer[i*25+17]=bin_to_ascii(buff[5]); //箱子号
temp_buffer[i*25+18]=',';
temp_buffer[i*25+19]=bin_to_ascii(buff[6]); //通道号
temp_buffer[i*25+20]=',';
temp_buffer[i*25+21]=bin_to_ascii((buff[7]/100)); //温度数据
temp_buffer[i*25+22]=bin_to_ascii(((buff[7]%100)/10));
temp_buffer[i*25+23]=bin_to_ascii((buff[7]%10));
temp_buffer[i*25+24]='\n';
}
//操作文件的数据void OperateFile(uint8 Write ,uint8 Name[11], uint32 Start, uint32 Length, void* Data)
OperateFile(1 , FileSize, Length, temp_buffer,fileID);
FileSize+=Length;
GotoXY(2,j);
a=j+'0';
Print(&a);
}
GotoXY(2,10);
a=count_flag+'0';
Print(&a);
pause_ms(100);
rpage++;
if(rpage>834)rpage=1;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -