📄 mywinchbeta.c
字号:
AD_CYC = Alarm_Time;
Prog_Time = (unsigned long)SMU*65536*256 +
(unsigned long)SMV*65536 +
(unsigned long)SMH*256 +
(unsigned long)SML;
Prog_Time=Prog_Time+time.l;
return;
}
void ADtemp(void)
{
TRISD = 0x00;
lcdnum = depth.u;
lcdnum = lcdnum*10/n.i[1];
format(lcdnum, buf, 1);
lcd_goto(2);
lcd_puts(buf);
// Depth Alarm = RF0
lcdnum /= 10;
lcdnum = speed;
lcdnum = lcdnum*2000/n.i[1]; // sample rate: 0.5s.
format(lcdnum, buf, 3);
lcd_goto(18);
lcd_puts(buf);
lcdnum = force;
lcdnum = (lcdnum*n.i[3]*11)/(n.i[4]*9);//weiyang 结构系数
format(lcdnum, buf, 3);
lcd_goto(10);
lcd_puts(buf);
lcdnum = delta;
lcdnum = (lcdnum*n.i[3]*11)/(n.i[4]*9);//weiyang 结构系数
format(lcdnum, buf, 3);
lcd_goto(26);
lcd_puts(buf);
}
void ADShow(void)
{
Delay10KTCYx(25);
f0 = f1;
f1 = force;
delta = f1 - f0;
startADC();
ADtemp();
while(!rdyADC());
force = getADC()+n.i[2];
Recorddepth();
if(PORTAbits.RA5==0) { depth.u=0; }
Nop();
Nop();
}
void Alarm(void)
{
timep.l=time.l;
timep.l=(timep.l+10);
if(LATF!=0) {
do{
ADShow();//
// ADonce();//
Delay10KTCYx(10);
PORTCbits.RC0=1;
Delay10KTCYx(10);
PORTCbits.RC0=0;
Delay10KTCYx(10);
}while(time.l<timep.l);
}
else PORTCbits.RC0=0;
LATF=0;
}
void ADCone(void)
{
TRISD = 0x00;
lcdnum = depth.u;
lcdnum = lcdnum*10/n.i[1];
format(lcdnum, buf, 1);
lcd_goto(2);
lcd_puts(buf);
// Depth Alarm = RF0
lcdnum /= 10;
if(speed>0){
if((lcdnum>n.i[5])&(lcdnum<(n.i[5]+10))) LATFbits.LATF0=1;
else LATFbits.LATF0=0;
}
if(speed<0) {
if((lcdnum>(n.i[6]-10))&(lcdnum<n.i[6])) LATFbits.LATF0=1;
else LATFbits.LATF0=0;
}
lcdnum = speed;
lcdnum = lcdnum*2000/n.i[1]; // sample rate: 0.5s.
format(lcdnum, buf, 3);
lcd_goto(18);
lcd_puts(buf);
// Speed Alarm = RF1
if(lcdnum>n.i[7]) LATFbits.LATF1=1;
else LATFbits.LATF1=0;
lcdnum = force;
lcdnum = (lcdnum*n.i[3]*11)/(n.i[4]*9);//weiyang 结构系数
format(lcdnum, buf, 3);
lcd_goto(10);
lcd_puts(buf);
// Force Alarm = RF2
if(lcdnum>n.i[8]) LATFbits.LATF2=1;
else LATFbits.LATF2=0;
lcdnum = delta;
lcdnum = (lcdnum*n.i[3]*11)/(n.i[4]*9);//weiyang 结构系数
format(lcdnum, buf, 3);
lcd_goto(26);
lcd_puts(buf);
/////报警控制
Alarm();
}
void delay(long int i)
{
long int j;
for(j=0;j<i;j++);
putch(0xbb);
}
void ADNow(void)
{
Delay10KTCYx(25);
f0 = f1;
f1 = force;
delta = f1 - f0;
startADC();
ADCone();
while(!rdyADC());
force = getADC()+n.i[2];
Recorddepth();
if(PORTAbits.RA5==0) { depth.u=0; }
Nop();
Nop();
}
void ShowAlarm(void)
{
char a1,a2;
int j,t;
int c[3];
union eep {
unsigned char c[2];
unsigned int i;
}mc;
lcd_clear();
for(j=5;j<9;j++){
t=j*2;
if(eeget(0) == 0x55 && eeget(1) == 0xaa) {
mc.c[0] = eeget(0x10+t);
mc.c[1] = eeget(0x10+t+1);
}
c[j-5]=mc.i;
}
lcd_goto(0);
lcd_putrs((unsigned rom char *)"最大深度");
lcd_goto(0x10);
lcd_putrs((unsigned rom char *)"最小深度");
lcd_goto(8);
lcd_putrs((unsigned rom char *)"最大速度");
lcd_goto(0x18);
lcd_putrs((unsigned rom char *)"最大张力");
do{
format(c[0], buf, 0);
lcd_goto(4);
lcd_puts(buf);
format(c[1], buf, 0);
lcd_goto(20);
lcd_puts(buf);
format(c[2], buf, 0);
lcd_goto(12);
lcd_puts(buf);
format(c[3], buf, 0);
lcd_goto(28);
lcd_puts(buf);
}while(PORTAbits.RA4==0);
InitLCD();
return;
}
void OtherSample(void)
{
// InitLCD();
int a=0;
lcd_goto(0);
lcd_putrs((unsigned rom char *)"深度 m +");
while(final!=0xff){
do{
do{
ADNow();
if((PORTAbits.RA4==0)&(FristTime==1)) {
ShowAlarm();
lcd_goto(0);
lcd_putrs((unsigned rom char *)"深度 m +");
}
if(depth.u!=tp.u) { Sample(); }//深度不增加不存,10分钟1存
while(TL!=0);
a=a+1;
}while(a<600); //while((time.l%60)!=0);
a=0;
DataSave(); // Data save
tp.u=depth.u;
////GetRealTime();
}while(Prog_Time>time.l);
PSEG = PSEG + 1;
GetProgram();
final=(SMU&SMV&SMH&SML);
}
}
void manytimes(void)
{
int a=0;
lcd_goto(0);
lcd_putrs((unsigned rom char *)"深度 m ?");
// T0CONbits.TMR0ON=0;
do{
ADNow();
if((PORTAbits.RA4==0)&(FristTime==1)) {
ShowAlarm();
lcd_goto(0);
lcd_putrs((unsigned rom char *)"深度 m ?");
}
if(depth.u != tp.u) { Sample(); }
if(PORTAbits.RA5==0){
depth.u=0;
}
while(TL!=0);
a=a+1;
}while(a<30);
// }while((time.l%30)!=0);
// T0CONbits.TMR0ON=1;
OtherSample();
}
void Sample(void)
{
lcd_goto(0);
lcd_putrs((unsigned rom char *)"深度 m ");
while(final!=0xff){
do{
do{
ADNow();
if((PORTAbits.RA4==0)&(FristTime==1)) { ShowAlarm(); }
if((depth.u==tp.u)&(delta<25)&(FristTime==1)) { manytimes(); }//深度不增加不存,1分钟1存
while(TL!=0);
}while((time.l%AD_CYC)!=0);
DataSave(); // Data save
tp.u=depth.u;
////GetRealTime();
}while(Prog_Time>time.l);
PSEG = PSEG + 1;
GetProgram();
FristTime=1;
final=(SMU&SMV&SMH&SML);
}
}
void SampleInit(void)
{
//initial Timer0;
OpenTimer0(TIMER_INT_ON&T0_16BIT&T0_SOURCE_INT&T0_PS_1_8);
//initial RB1;
OpenRB1INT (PORTB_CHANGE_INT_ON & PORTB_PULLUPS_ON & FALLING_EDGE_INT);
depth.u = n.i[0];
depth.u *= n.i[1];
speed = 0;
initADC();
RCONbits.IPEN = 1;
INTCONbits.GIEH = 1;
///从Sample()中调过来的
final=0x00;
PSEG=0;
SamplInit();
tp.u=depth.u;////
}
//------------------------
void ADCMode(void)
{
lcd_clear();
lcd_goto(0x10);
lcd_putrs((unsigned rom char *)"记录模式 >>");
Version();
Init_FLASH_MEM();
MEM_FindFirst();
MEM_LableFirst();
Init_MEM();
RTCInit();
SampleInit();
if(n.i[0]==0) Readrecord();///如果深度预制为0,则读取掉电前的值
Nop();
Nop();
speed = 0;
force = 0;
Delay10KTCYx(100);
InitLCD();
Sample();
}
//////
void Dectime(void)
{
Get_RealTimeClock();
sec=hextodec(real_time.Seconds);
min=hextodec(real_time.Minutes);
hour=hextodec(real_time.Hours);
mday=hextodec(real_time.Date);
mon=hextodec(real_time.Month);
year=hextodec(real_time.Year)+2000;
}
unsigned int sleyear(unsigned int yr)
{
if(yr%4==0||(yr%4==0&&yr%100!=0)) return(366);
else return(365);
}
void DostoUnix(void)
{
unsigned int i;
unsigned long j;
j=0;
///从1970到现在的天数
for(i=2000; i<year; i++){
j=j+sleyear(i);///2000~2005
}
///计算本年到现在天数
for(i=0; i<(mon-1); i++){
j+=moninit[i];
}
j=j+mday;
///若本年为闰年则加1天
if(mon >= 2 && sleyear(year) == 366) j++;
j=j-1;
j=j*86400;
j=j+(hour*3600);
j=j+(min*60);
j=j+sec;
totalsec=j;
time.l=totalsec; ///给计时赋初值
Nop();
Nop();
}
void Version(void)
{
lcd_goto(8);
lcd_putrs((unsigned rom char *)"Ver 2.08b");
}
///////
void read_status(void)
{
unsigned char busy,temp;
LCD_RS=0;
LCD_RW=1;
TRISD = 0xff;
do{
PORTD=0xff;
LCD_EN=1;
busy=PORTD;
LCD_EN=0;
temp=busy&0x80;
}while(temp!=0);
TRISD = 0x00;
}
/////
void wr_command1(unsigned char c)
{
LCD_CS1=1;
LCD_CS2=0;
read_status();
LCD_RS=1;
LCD_RW=0;
PORTD=c;
LCD_EN=1;
LCD_EN=0;
}
/////
void wr_data1(unsigned char c)
{
LCD_CS1=1;
LCD_CS2=0;
read_status();
LCD_RS=1;
LCD_RW=0;
PORTD=c;
LCD_EN=1;
LCD_EN=0;
}
/////
void wr_command2(unsigned char c)
{
LCD_CS1=0;
LCD_CS2=1;
read_status();
LCD_RS=1;
LCD_RW=0;
PORTD=c;
LCD_EN=1;
LCD_EN=0;
}
/////
void wr_data2(unsigned char c)
{
LCD_CS1=0;
LCD_CS2=1;
read_status();
LCD_RS=1;
LCD_RW=0;
PORTD=c;
LCD_EN=1;
LCD_EN=0;
}
//
void clear_prog(void)
{
int i,j;
unsigned char a;
for(i=0;i<8;i++){
// com=(0xb8+i);
a=(0xb8+i);
wr_command1(a);
wr_command2(a);
// com=0x40;
wr_command1(0x40);
wr_command2(0x40);
for(j=0;j<64;j++){
wr_data1(0xff);
wr_data2(0xff);
}
}
}
//
void display_prog(void)
{
unsigned char a;
int i,j,k;
for(i=0;i<8;i++){
com=0xb8+i;
wr_command1(com);
wr_command2(com);
// com=0x40;
wr_command1(0x40);
wr_command2(0x40);
for(j=0;j<64;j++) { wr_data1(j); }
for(k=64;k<128;k++) { wr_data2(k); }
}
}
/////
void Newlcd_init(void)
{
TRISD = 0x00;
LCD_RS = 0;
// com=0xc0;
wr_command1(0xc0);
wr_command2(0xc0);
// com=0x3f;
wr_command1(0x3f);
wr_command2(0x3f);
}
void main(void)
{
TRISC = 0x18;
OpenI2C(MASTER,SLEW_OFF); // Open I2C Bus
SSPADD = 9; // 100kHz clock (3.6864MHz)
Dectime();
DostoUnix();
PMode=1;
if(PORTAbits.RA4==0) PMode=0; //模式键(S2)判断
FristTime=0;
InitPorts();
/// PORTAbits.RA5==1; //深度清0
Init_MEM();
// lcd_init();
Newlcd_init();
// Lcdmain();
////---------------
if(PMode==1){///1是采样
ADCMode();
}
else {
SwapMode = 0;
// lcd_clear();
// lcd_goto(0x10);
// lcd_putrs((unsigned rom char *)"通讯模式 >>");
// Version();
clear_prog();
display_prog();
do{
in=Buf;
out=Buf;
SCIGetPack(); // Get a data packet
SCISendACK(0xa1); // send ACK
switch(pcmd)
{
case 1:
SCISendACK(0xa1);break; // Send acknowledge[SendNull]
case 2:
SCIData();break; // Send a sample[GetSample]
case 4:
SCIErase();break; // EraseBlock
case 8:
SCIFormat();break; // FormatTool<<<<<-------
case 16:
SCIRead();break; // ReadLine
case 32:
SCIWrite(); break; // WriteLine
case 64:
SCIBlock();break; // ReadBlock
case 80:
ShowAllParameter();break; //显示所有设置参数
case 81:
SetParameter();break; //设置参数
case 82:
ReadParameter();break; //读取参数
case 83:
ShowTime();break;
case 84:
InitDate();break; //预制日期(年/月/日)
case 85:
InitTime();break; //预制时间(24时/分/秒)
default:break;
}
}while(SwapMode!=1);
SwapMode = 0;
}
}
//-----End of Program-----
//=============
//----NOTE-----
//=============
//+++++++ Function Map +++++++
//----中断服务程序(2级中断模式)
//----实时时钟
//----存储器代码
//----LCD显示
//----7714代码
//----响应与应答
//----PC接口函数
//----Log Mode
//----初始化
//----模式转换
//----main()
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -