📄 main.c
字号:
/*
The main for uC pic16f688
*/
#include <htc.h>
#include <pic16f688.h>
#include "IntServ.h"
#include "AD.H"
#include "Type.h"
#define PMC(V) if(!V) RC4=1;else RC4=0;////PMC=Z;PMC=0;
#define VP(V) if(V==1) {RA5=1;RC5=0;}\//VP=Z
else if(V==5) {RA5=0;RC5=0;}\//VP=5V
else if(V==10) {RA5=0;RC5=1;}\//VP=10V
else if(!V) {RA5=1;RC5=0;}//VP=0
int VP_I,VP_U,PMC_I,PMC_U;//VP_I,VP_U: CURRENT & VOLTAGE VALUE OF PIN7 SUPPLY FOR RJ485 TERMINAL
//PMC_I,PMC_U: CURRENT & VOLTAGE VALUE OF PIN1&3 RETURN FROM RJ485 TERMINAL
unsigned int Time10ms;//10ms timer1
extern int adc[5];
extern bit bShortCurrent;
//extern bit bSC_first;
//ADCs adc[5]; //5 adc channels
void main(void)
{
char Status=0;
unsigned int n;
// bSC_first=0;
bShortCurrent=0;
Time10ms=0;
VP_I=0;
VP_U=0;
PMC_I=0;
PMC_U=0;
// ini_variable();
ini_chip();
// PMC(0);VP(5); //to test
PMC(Z);VP(Z); //Initial state of pin1,3,7 on rj45 terminal
// PMC(Z);VP(10); //to test
n=30000;
while(n--){;}
n=30000;
while(n--){;}
//wait for 2 seconds, to test
SWDTEN=1; //start WDT
// RA4=0;
Status=S0_0; //State machine starts from state0_0
while(1)
{
/* Arith_AD();
if(VP_I)
{
PMC(Z);VP(Z);
}*/ //to test short current
asm("CLRWDT");//clear WDT each while
// PMC(Z);VP(5);Arith_AD();//VP(10);
/*
if(!pin1 && !pin2)
{
PMC(0);VP(10);//Supply 10V
Status=S0_0;
Stop_Timer1();
}
else if(!pin1 && pin2)
{
PMC(0);VP(5);//Supply 5V
Status=S0_0;
Stop_Timer1();
}
else if(pin1 && !pin2)
{
PMC(0);VP(0);//Supply 0V
Status=S0_0;
Stop_Timer1();
}
else if(pin1 && pin2) //enter auto Detect
{
//switch(Status)
}
*/ //auto detection section: not tested
switch(Status) //auto Detect state machine
{
case S0_0:
Start_Timer1();
Status=S0;
break;
case S0:
PMC(0);VP(Z);Arith_AD();//sample confitions and adc
if(bShortCurrent) //Adjust if short current occur
{
Status=Soff; //Enter status nerver restored
break;
}
if(Time10ms<=100) //S0: WAIT FOR 1S
{
//RA4=1;; ////(!PMC_I && !VP_U)
//VP_U=VP_U-5;
//if(VP_U==5)
if(!PMC_I && !VP_U)
{
Status=S0;
}
else
{
Status=S7;
Stop_Timer1();
}
}
else
{
Status=S1;
Stop_Timer1();
Start_Timer1();
}//jump next status:S1
break;
case S1:
PMC(0);VP(5);Arith_AD();
if(bShortCurrent)
{
Status=Soff;
break;
}
if(Time10ms<=100)
{
if(!VP_I)
{
Status=S1;
}//VP_I=0;VP_U=0;PMC_I=0;PMC_U=0;
else
{
Status=S2;
Stop_Timer1();
}
}
else
{
Stop_Timer1();
Status=S0_0;
}
break;
case S2:
PMC(0);VP(Z);Arith_AD();
if(bShortCurrent)
{
Status=Soff;
break;
}
if(!PMC_I && !VP_U)
{Status=S3;}
else
{Status=S7;}
break;
case S3:
PMC(Z);VP(5);Arith_AD();
if(bShortCurrent)
{
Status=Soff;
break;
}
if(50<=PMC_U && PMC_U<270)//0.25V<=PMC:U<1.35V
{Status=S4;}
else if(PMC_U<50)
{Status=S7;}
else if(PMC_U>=270)
{Status=S5;}
break;
case S4:
PMC(Z);VP(10);Arith_AD();//VP(10);
if(bShortCurrent)
{
Status=Soff;
break;
}
if(PMC_U<270)
{Status=S7;}
else if(PMC_U>=270)
{Status=S6;}
break;
case S5:
PMC(0);VP(5);Arith_AD();
if(bShortCurrent)
{
Status=Soff;
break;
}
if(VP_I || PMC_U>10)
{Status=S5;}
else
{Status=S0_0;}
break;
case S6:
PMC(0);VP(10);Arith_AD();//VP(10);
if(bShortCurrent)
{
Status=Soff;
break;
}
if(VP_I || PMC_U>10)
{Status=S6;}
else
{Status=S0_0;}
break;
case S7:
PMC(0);VP(Z);Arith_AD();//VP(5) FOR ATV31 DEVICE
if(bShortCurrent)
{
Status=Soff;
break;
}
if(!PMC_I && !VP_U)
{Status=S0_0;}
else
{Status=S7;}
break;
default:
{PMC(Z);VP(Z);
// RA4=1;bSC_first=1;bShortCurrent=0;
// n=20000;
// while(n--){;}//delay 20000*2us=40ms before sampling adc
// Status=S1;RA4=0; //test delay time of short current
}
//Soff: stop usb cable when short current occurs external device
}
// Arith_AD(); //sample adc
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -