//说明:完整的PIC16F917编写的c语言程序,通过编译,内容包含有ad变换,pwm输出,数码显示(通过串口移位显示),串行口通讯等
#include<pic.h>
#define uchar unsigned char
#define uint unsigned int
__CONFIG(0x20e4);
#define choose1 RC4
#define choose2 RC3
#define power_high RA6
#define power_low RA4
#define relay_w RE2
#define relay_v RB5
#define relay_u RA0
#define data1 RD0
#define clk1 RA7
#define bell RD3
#define power_supply RD6
#define pha RB2
#define phb RB3
#define phc RB4
#define spa RB0
#define spb RB1
#define up RC1
#define down RC2
#define up1 RD2
#define down1 RC0
#define safe RD7
#define start RD5
#define speeder RD4
#define KDF RD1
#define test_num1 1
#define test_num2 2
#define test_num3 3
#define test_num4 4
#define test_num5 5
#define test_num6 6
#define test_num7 7
#define test_num8 8
#define test_num9 9
#define test_num10 10
#define test_num11 11
#define test_num12 12
#define test_num13 13
#define test_num14 14
#define test_num15 15
#define test_num16 16
#define test_num17 17
#define test_num18 18
#define test_num19 19
#define test_num20 20
#define test_num21 21
#define test_num22 22
#define test_num23 23
#define test_num24 24
#define test_num25 25
#define test_num26 26
#define test_num27 27
#define test_num28 28
#define hold 29
#define stop 30
#define setout 31
#define test_err 32
#define power 33
#define TEST_5045 34
#define TEST_5046 35
#define TEST_5055B 36
#define power1 37
#define setout1 38
#define setout2 39
#define disp_refresh 100
const uchar table[]={0xfc,0x60,0xda,0xf2,0x66,0xb6,0xbe,0xe0,0xfe,0xf6,0xee,0xfe,0x9c,0xfc,0x9e,0x8e,2,0x10,0x80,4};
const uchar table1[]={0xfd,0x61,0xdb,0xf3,0x67,0xb7,0xbf,0xe1,0xff,0xf7,0xef,0xff,0x9d,0xfd,0x9f,0x8f,0x6f,0x0d};
uchar keyvalue=1,keystate=0,keypull=0,status=setout,rcdata1=0x55,rcdata2=0x55,rcdata3=0x55,status2=0;
uchar err_status=0,disp_run=0,ad_test=0,test15_tmp1=0,test15_tmp2=0,test15_tmp3=0,test15_tmp4=0,disp_mod=0,disp_hold=0;
uint rcdata,disp_time1=0,ad_data1,ad_data2;
volatile unsigned int keytime=0,bell_time=0,test_time=0,err_time=0,disp_time=0,tx_time=0;
void disp(uchar num1,uchar num2,uchar num3); // 76us
void disp10(uchar j); // 314us
void keyscan(void);
void key1(void);
void init(void);
void delaye(uint x);
void outpwm(uchar x);
void addisp(uint a);
void addisp_8(uchar a);
void disp20(uchar i,uchar j);
void disp16(uint j);
void err_disp(uchar j);
void disp21(uchar i,uchar j);
uint ad(uchar x);
//3次加和一个除法 87us
void main()
{
uchar tx_conut=0;
init();
while(1)
{
if(status==setout1)
{
if(disp_time>disp_refresh)
{
disp(table[18],table[18],table[18]);
disp_time=0;
}
power_supply=0;power_high=0;power_low=0;relay_u=0;relay_v=0;relay_w=0;
spa=1;spb=1;pha=1;phb=1;phc=0;up=0;down=0;safe=0;up1=0;down1=0;
}
else if(status==setout2)
{
if(disp_time>disp_refresh)
{
disp(table[19],table[19],table[19]);
disp_time=0;
}
power_supply=0;power_high=0;power_low=0;relay_u=0;relay_v=0;relay_w=0;
spa=1;spb=1;pha=1;phb=1;phc=0;up=0;down=0;safe=0;up1=0;down1=0;
}
else if(status==setout)
{
if(disp_time>disp_refresh)
{
disp(table[16],table[16],table[16]);
disp_time=0;
}
power_supply=0;power_high=0;power_low=0;relay_u=0;relay_v=0;relay_w=0;
spa=1;spb=1;pha=1;phb=1;phc=0;up=0;down=0;safe=0;up1=0;down1=0;
// TRISC7=0;
// RC7=1;
// speeder=1;
if(keypull==1)
{
bell=0;
bell_time=0;
keypull=0;
status=power1;
test_time=0;
}
}
else if(status==power1)
{
if(status2==TEST_5055B)
{
TRISC7=1;
TXSTA=0x26;
RCSTA=0x90;
SPBRG=103; //4.8k波特率
power_supply=1;power_high=0;power_low=0;relay_u=0;relay_v=0;relay_w=0;
spa=1;spb=1;pha=1;phb=1;phc=0;up=0;down=0;safe=0;up1=0;down1=0;
speeder=1;
if(tx_time>50&&TRMT==1)
{
if(tx_conut==0)
{
TXREG=0xfe;
tx_time=0;
tx_conut=1;
}
else if(tx_conut==1)
{
TXREG=0xfd;
tx_time=0;
tx_conut=2;
}
else if(tx_conut==2)
{
TXREG=0xfc;
tx_time=0;
tx_conut=3;
}
else if(tx_conut==3)
{
TXREG=0xfb;
tx_time=0;
tx_conut=4;
}
else if(tx_conut==4)
{
TXREG=0xfa;
tx_time=0;
tx_conut=0;
}
}
if(rcdata1==0xaa)status=power;
if(test_time>1500)status=setout2;
}
else if(status2==TEST_5046||status2==TEST_5045)status=power;
}
else if(status==power)
{
power_supply=1;power_high=0;power_low=0;relay_u=0;relay_v=0;relay_w=0;
spa=1;spb=1;pha=1;phb=1;phc=0;up=0;down=0;safe=0;up1=0;down1=0;
if(disp_time>disp_refresh)
{
disp(table[17],table[17],table[17]);
disp_time=0;
}
if(status2==TEST_5046||status2==TEST_5045)
{
TRISC7=0;
RC7=1;
speeder=1;
}
if(keypull==1)
{
bell=0;
bell_time=0;
keypull=0;
status=test_num1;
TRISC7=1;
TXSTA=0x26;
RCSTA=0x90;
SPBRG=103; //4.8k波特率
test_time=0;
disp_run=1;
disp_time1=0;
ad_test=1;
ADCON0=0x85; //通道1
ADCON1=0x50;
}
}
else if(status==test_num1)
{
spa=1;spb=1;pha=1;phb=1;phc=1;up=1;down=1;safe=1;up1=0;down1=0;
power_supply=1;power_high=0;power_low=0;relay_u=0;relay_v=0;relay_w=0;speeder=1;
if(test_time>100&&test_time<1000)
{
if(rcdata1==0&&rcdata2==0&&rcdata3==0)
{
status=test_num2;
test_time=0;
}
}
else if(status==test_num1&&test_time>1000)
{
status=test_err;
err_status=1;
err_time=0;
disp_hold=1;
}
}
else if(status==test_num2)
{
spa=0;spb=0;pha=0;phb=0;phc=0;up=0;down=0;safe=0;up1=0;down1=0;
power_supply=1;power_high=0;power_low=0;relay_u=0;relay_v=0;relay_w=0;speeder=1;
if(test_time>100&&test_time<1000)
{
if(rcdata1==0xff&&rcdata2==0xff&&rcdata3==0xff)
{
status=test_num3;
test_time=0;
}
}
else if(status==test_num2&&test_time>1000)
{
status=test_err;
err_status=2;
err_time=0;
disp_hold=1;
}
}
else if(status==test_num3)
{
if(status2==TEST_5046)
{
spa=1;spb=1;pha=1;phb=0;phc=0;up=1;down=1;safe=1;up1=1;down1=1;
power_supply=1;power_high=0;power_low=0;relay_u=0;relay_v=0;relay_w=0;speeder=1;
if(test_time>100&&test_time<1000)
{
if(rcdata1==0&&rcdata2==0&&rcdata3==0)
{
status=test_num4;
test_time=0;
}
}
else if(status==test_num3&&test_time>1000)
{
status=test_err;
err_status=3;
err_time=0;
disp_hold=1;
}
}
else if(status2==TEST_5055B||status2==TEST_5045)
{
outpwm(200);
test_time=0;
status=test_num5;
}
}
else if(status==test_num4)
{
spa=0;spb=0;pha=0;phb=0;phc=0;up=0;down=0;safe=0;up1=0;down1=0;
power_supply=1;power_high=0;power_low=0;relay_u=0;relay_v=0;relay_w=0;speeder=1;
if(test_time>100&&test_time<1000)
{
if(rcdata1==0xff&&rcdata2==0xff&&rcdata3==0xff)
{
status=test_num5;
outpwm(200);
test_time=0;
}
}
else if(status==test_num4&&test_time>1000)
{
status=test_err;
err_status=4;
err_time=0;
disp_hold=1;
}
}
else if(status==test_num5)
{
speeder=0;power_supply=1;power_high=0;power_low=0;relay_u=0;relay_v=0;relay_w=0;
spa=1;spb=0;pha=0;phb=0;phc=0;up=0;down=0;safe=0;up1=0;down1=0;
if(test_time>100&&test_time<1000)
{
if(rcdata1>172&&rcdata1<192&&rcdata2>172&&rcdata2<192&&rcdata3>172&&rcdata3<192)
{
status=test_num6;
test_time=0;
outpwm(148);
}
}
else if(status==test_num5&&test_time>1000)
{
status=test_err;
err_status=5;
err_time=0;
disp_hold=1;
}
}
else if(status==test_num6)
{
speeder=0;power_supply=1;power_high=0;power_low=0;relay_u=0;relay_v=0;relay_w=0;
spa=1;spb=0;pha=0;phb=0;phc=0;up=0;down=0;safe=0;up1=0;down1=0;
if(test_time>100&&test_time<1000)
{
if(rcdata1>101&&rcdata1<125&&rcdata2>101&&rcdata2<125&&rcdata3>101&&rcdata3<125)
{
status=test_num7;
test_time=0;
outpwm(130);
}
}
else if(status==test_num6&&test_time>1000)
{
status=test_err;
err_status=6;
err_time=0;
disp_hold=1;
}
}
else if(status==test_num7)
{
speeder=0;power_supply=1;power_high=0;power_low=0;relay_u=0;relay_v=0;relay_w=0;
spa=1;spb=0;pha=0;phb=0;phc=0;up=0;down=0;safe=0;up1=0;down1=0;
if(test_time>100&&test_time<1000)
{
if(rcdata1>70&&rcdata1<102&&rcdata2>70&&rcdata2<102&&rcdata3>70&&rcdata3<102)
{
status=test_num8;
test_time=0;
outpwm(60);
}
}
else if(status==test_num7&&test_time>1000)
{
status=test_err;
err_status=7;
err_time=0;
disp_hold=1;
}
}
else if(status==test_num8)
{
speeder=0;power_supply=1;power_high=0;power_low=0;relay_u=0;relay_v=0;relay_w=0;
spa=1;spb=0;pha=0;phb=0;phc=0;up=0;down=0;safe=0;up1=0;down1=0;
if(test_time>100&&test_time<1000)
{
if(rcdata1>14&&rcdata1<35&&rcdata2>14&&rcdata2<35&&rcdata3>14&&rcdata3<35)
{
status=test_num9;
test_time=0;
}
}
else if(status==test_num8&&test_time>1000)
{
status=test_err;
err_status=8;
err_time=0;
disp_hold=1;
}
}
else if(status==test_num9) //2.4---2.53
{
if(test_time<200)
{
power_supply=1;power_high=1;power_low=1;relay_u=0;relay_v=0;relay_w=0;speeder=0;
spa=0;spb=0;pha=0;phb=0;phc=0;up=0;down=0;safe=0;up1=0;down1=0;
}
if(test_time>=200&&test_time<500)
{
power_supply=1;power_high=1;power_low=1;relay_u=1;relay_v=0;relay_w=0;speeder=0;
spa=0;spb=0;pha=0;phb=0;phc=0;up=0;down=0;safe=0;up1=0;down1=0;
}
if(test_time>=500&&test_time<1500)
{
power_supply=1;power_high=1;power_low=1;relay_u=1;relay_v=0;relay_w=0;speeder=0;
spa=0;spb=0;pha=0;phb=0;phc=0;up=0;down=0;safe=0;up1=0;down1=0;
if(test_time>=500&&test_time<1100)
{
if(ad_data1>500&&ad_data1<510&&ad_data2>500&&ad_data2<510)
{
status=test_num10;
test_time=0;
}
}
if(test_time>=1100)
{
status=test_err;
err_status=9;
err_time=0;
ADON=0;
ad_test=0;
disp_mod=1;
}
}
}
else if(status==test_num10)
{
if(test_time<200)
{
power_supply=1;power_high=1;power_low=1;relay_u=0;relay_v=0;relay_w=0;
spa=0;spb=0;pha=0;phb=0;phc=0;up=0;down=0;safe=0;up1=0;down1=0;
}
if(test_time>=200&&test_time<500)
{
power_supply=1;power_high=1;power_low=1;relay_u=0;relay_v=1;relay_w=0;
spa=0;spb=0;pha=0;phb=0;phc=0;up=0;down=0;safe=0;up1=0;down1=0;
}
if(test_time>=500&&test_time<1500)
{
power_supply=1;power_high=1;power_low=1;relay_u=0;relay_v=1;relay_w=0;
spa=0;spb=0;pha=0;phb=0;phc=0;up=0;down=0;safe=0;up1=0;down1=0;
if(test_time>=500&&test_time<1100)
{
if(ad_data1>500&&ad_data1<510&&ad_data2>500&&ad_data2<510)
{
status=test_num11;
test_time=0;
}
}
if(test_time>=1100)
{
status=test_err;
err_status=10;
err_time=0;
ADON=0;
ad_test=0;
disp_mod=1;
}
}
}
else if(status==test_num11)
{
if(test_time<200)
{
power_supply=1;power_high=1;power_low=1;relay_u=0;relay_v=0;relay_w=0;
spa=0;spb=0;pha=0;phb=0;phc=0;up=0;down=0;safe=0;up1=0;down1=0;
}
if(test_time>=200&&test_time<500)
{
power_supply=1;power_high=1;power_low=1;relay_u=0;relay_v=0;relay_w=1;
spa=0;spb=0;pha=0;phb=0;phc=0;up=0;down=0;safe=0;up1=0;down1=0;
}
if(test_time>=500&&test_time<1500)
{
power_supply=1;power_high=1;power_low=1;relay_u=0;relay_v=0;relay_w=1;
spa=0;spb=0;pha=0;phb=0;phc=0;up=0;down=0;safe=0;up1=0;down1=0;
if(test_time>=500&&test_time<1100)
{
if(ad_data1>500&&ad_data1<510&&ad_data2>500&&ad_data2<510)
{
status=test_num12;
test_time=0;
ADCON0=0x89; //通道2
}
}
if(test_time>=1100)
{
status=test_err;
err_status=11;
err_time=0;
ADON=0;
ad_test=0;
disp_mod=1;
}
}
}
else if(status==test_num12) //+u,-V
{
if(test_time<1000)
{
power_supply=1;power_high=0;power_low=0;relay_u=0;relay_v=0;relay_w=0;speeder=0;
spa=0;spb=0;pha=0;phb=0;phc=0;up=0;down=0;safe=0;up1=0;down1=0;
}
else if(test_time>=1000&&test_time<1100)
{
power_supply=1;power_high=0;power_low=0;relay_u=0;relay_v=1;relay_w=0;speeder=0;
phc=0;up=0;down=0;safe=0;up1=0;down1=0;
if(test_time<=1005&&test_time>1000){spa=0;spb=1;pha=1;phb=0;}
else if(test_time<=1015&&test_time>1005){spa=0;spb=1;pha=1;phb=1;}
else if(test_time<=1020&&test_time>1015){spa=0;spb=1;pha=1;phb=0;}
ad_data1=0;
}
else if(test_time>=1100&&test_time<2600) //2.36 483
{
if(test_time>1100&&test_time<2200)
{
spa=1;spb=1;pha=1;phb=0;
power_supply=1;power_high=1;power_low=0;relay_u=0;relay_v=1;relay_w=0;speeder=0;
phc=0;up=0;down=0;safe=0;up1=0;down1=0;
if(test_time>1400&&ad_data1>473&&ad_data1<493)
{
status=test_num13;
test_time=0;
}
}
else if(test_time>2200)
{
status=test_err;
err_status=12;
err_time=0;
// SPEN=0;
// CREN=0;
// TRISC7=0;
// RC7=1;
ADON=0;
ad_test=0;
disp_mod=1;
}
}
}
else if(status==test_num13) //+U,-v
{
if(test_time<1000)
{
power_supply=1;power_high=0;power_low=0;relay_u=0;relay_v=0;relay_w=0;speeder=0;
spa=0;spb=0;pha=0;phb=0;phc=0;up=0;down=0;safe=0;up1=0;down1=0;
}
else if(test_time>=1000&&test_time<1100)
{
power_supply=1;power_high=0;power_low=0;relay_u=1;relay_v=0;relay_w=0;speeder=0;
phc=0;up=0;down=0;safe=0;up1=0;down1=0;
if(test_time<=1005&&test_time>1000){spa=0;spb=1;pha=1;phb=0;}
else if(test_time<=1015&&test_time>1005){spa=0;spb=1;pha=1;phb=1;}
else if(test_time<=1020&&test_time>1015){spa=0;spb=1;pha=1;phb=0;}
ad_data1=0;
}
else if(test_time>=1100&&test_time<2600) //2.66 544
{
if(test_time>1100&&test_time<2200)
{
spa=1;spb=1;pha=1;phb=0;
power_supply=1;power_high=0;power_low=1;relay_u=1;relay_v=0;relay_w=0;speeder=0;
phc=0;up=0;down=0;safe=0;up1=0;down1=0;
if(test_time>1400&&ad_data1>534&&ad_data1<554)
{
status=test_num14;
test_time=0;
}
}
else if(test_time>2200)
{
status=test_err;
err_status=13;
err_time=0;
// SPEN=0;
// CREN=0;
// TRISC7=0;
// RC7=1;
ADON=0;
ad_test=0;
disp_mod=1;
}
}
}
else if(status==test_num14) //+v,-U
{
if(test_time<1000)
{
power_supply=1;power_high=0;power_low=0;relay_u=0;relay_v=0;relay_w=0;speeder=0;
spa=0;spb=0;pha=0;phb=0;phc=0;up=0;down=0;safe=0;up1=0;down1=0;
}
else if(test_time>=1000&&test_time<1100)
{
power_supply=1;power_high=0;power_low=0;relay_u=1;relay_v=0;relay_w=0;speeder=0;
phc=0;up=0;down=0;safe=0;up1=0;down1=0;
ad_data1=0;
if(test_time<=1005&&test_time>1000){spa=0;spb=1;pha=1;phb=0;}
else if(test_time<=1015&&test_time>1005){spa=0;spb=1;pha=1;phb=1;}
else if(test_time<=1020&&test_time>1015){spa=0;spb=1;pha=1;phb=0;}
}
else if(test_time>=1100&&test_time<2600) //2.36 483
{
if(test_time<=2200&&test_time>1100)
{
spa=1;spb=1;pha=0;phb=1;
power_supply=1;power_high=1;power_low=0;relay_u=1;relay_v=0;relay_w=0;speeder=0;
phc=0;up=0;down=0;safe=0;up1=0;down1=0;
if(test_time>1400&&ad_data1>473&&ad_data1<493)
{
status=test_num15;
test_time=0;
}
}
else if(test_time>2200)
{
status=test_err;
err_status=14;
err_time=0;
// SPEN=0;
// CREN=0;
// TRISC7=0;
// RC7=1;
ADON=0;
ad_test=0;
disp_mod=1;
}
}
}
else if(status==test_num15) //+V,-u
{
if(test_time<1000)
{
power_supply=1;power_high=0;power_low=0;relay_u=0;relay_v=0;relay_w=0;speeder=0;
spa=0;spb=0;pha=0;phb=0;phc=0;up=0;down=0;safe=0;up1=0;down1=0;
}
else if(test_time>=1000&&test_time<1100)
{
power_supply=1;power_high=0;power_low=0;relay_u=0;relay_v=1;relay_w=0;speeder=0;
phc=0;up=0;down=0;safe=0;up1=0;down1=0;
ad_data1=0;
if(test_time<=1005&&test_time>1000){spa=0;spb=1;pha=1;phb=0;}
else if(test_time<=1015&&test_time>1005){spa=0;spb=1;pha=1;phb=1;}
else if(test_time<=1020&&test_time>1015){spa=0;spb=1;pha=1;phb=0;}
}
else if(test_time>=1100&&test_time<2600) //2.66 544
{
if(test_time>1100&&test_time<2200)
{
spa=1;spb=1;pha=0;phb=1;
power_supply=1;power_high=0;power_low=1;relay_u=0;relay_v=1;relay_w=0;speeder=0;
phc=0;up=0;down=0;safe=0;up1=0;down1=0;
if(test_time>1400&&ad_data1>534&&ad_data1<554)
{
status=test_num16;
test_time=0;
}
}
else if(test_time>2200)
{
status=test_err;
err_status=15;
err_time=0;
// SPEN=0;
// CREN=0;
/// TRISC7=0;
// RC7=1;
ADON=0;
ad_test=0;
disp_mod=1;
}
}
}
else if(status==test_num16) //-W,+v
{
if(test_time<1000)
{
power_supply=1;power_high=0;power_low=0;relay_u=0;relay_v=0;relay_w=0;speeder=0;
spa=0;spb=0;pha=0;phb=0;phc=0;up=0;down=0;safe=0;up1=0;down1=0;
}
else if(test_time>=1000&&test_time<1100)
{
power_supply=1;power_high=0;power_low=0;relay_u=0;relay_v=0;relay_w=1;speeder=0;
phc=0;up=0;down=0;safe=0;up1=0;down1=0;
ad_data1=0;
if(test_time<=1005&&test_time>1000){spa=0;spb=1;pha=1;phb=0;}
else if(test_time<=1015&&test_time>1005){spa=0;spb=1;pha=1;phb=1;}
else if(test_time<=1020&&test_time>1015){spa=0;spb=1;pha=1;phb=0;}
}
else if(test_time>=1100&&test_time<2600) //2.36 483
{
if(test_time>1100&&test_time<2200)
{
spa=1;spb=0;pha=1;phb=1;
power_supply=1;power_high=1;power_low=0;relay_u=0;relay_v=0;relay_w=1;speeder=0;
phc=0;up=0;down=0;safe=0;up1=0;down1=0;
if(test_time>1400&&ad_data1>473&&ad_data1<493)
{
status=test_num17;
test_time=0;
//addata=0;
}
}
else if(test_time>2200)
{
status=test_err;
err_status=16;
err_time=0;
// SPEN=0;
// CREN=0;
// TRISC7=0;
// RC7=1;
ADON=0;
ad_test=0;
disp_mod=1;
}
}
}
else if(status==test_num17) //+W-v
{
if(test_time<1000)
{
power_supply=1;power_high=0;power_low=0;relay_u=0;relay_v=0;relay_w=0;speeder=0;
spa=0;spb=0;pha=0;phb=0;phc=0;up=0;down=0;safe=0;up1=0;down1=0;
}
else if(test_time>=1000&&test_time<1100)
{
power_supply=1;power_high=0;power_low=0;relay_u=0;relay_v=0;relay_w=1;speeder=0;
phc=0;up=0;down=0;safe=0;up1=0;down1=0;
if(test_time<=1005&&test_time>1000){spa=0;spb=1;pha=1;phb=0;}
else if(test_time<=1015&&test_time>1005){spa=0;spb=1;pha=1;phb=1;}
else if(test_time<=1020&&test_time>1015){spa=0;spb=1;pha=1;phb=0;}
ad_data1=0;
}
else if(test_time>=1100&&test_time<2600) //2.66 2.51 544
{
if(test_time<=2200&&test_time>1100)
{
spa=1;spb=1;pha=0;phb=0;
power_supply=1;power_high=0;power_low=1;relay_u=0;relay_v=0;relay_w=1;speeder=0;
phc=0;up=0;down=0;safe=0;up1=0;down1=0;
if(test_time>1400&&ad_data1>534&&ad_data1<554)
{
status=test_num18;
test_time=0;
}
}
else if(test_time>2200)
{
status=test_err;
err_status=17;
err_time=0;
// SPEN=0;
// CREN=0;
// TRISC7=0;
// RC7=1;
ADON=0;
ad_test=0;
disp_mod=1;
}
}
}
else if(status==test_num18) //+v,-U
{
if(test_time<1000)
{
power_supply=1;power_high=0;power_low=0;relay_u=0;relay_v=0;relay_w=0;speeder=0;
spa=0;spb=0;pha=0;phb=0;phc=0;up=0;down=0;safe=0;up1=0;down1=0;
}
else if(test_time>=1000&&test_time<1100)
{
power_supply=1;power_high=0;power_low=0;relay_u=1;relay_v=0;relay_w=0;speeder=0;
phc=0;up=0;down=0;safe=0;up1=0;down1=0;
if(test_time<=1005&&test_time>1000){spa=0;spb=1;pha=1;phb=0;}
else if(test_time<=1015&&test_time>1005){spa=0;spb=1;pha=1;phb=1;}
else if(test_time<=1020&&test_time>1015){spa=0;spb=1;pha=1;phb=0;}
ad_data1=0;
}
else if(test_time>=1100&&test_time<2600) //2.36 120
{
if(test_time<=2200&&test_time>1100)
{
spa=1;spb=1;pha=0;phb=1;
power_supply=1;power_high=1;power_low=0;relay_u=1;relay_v=0;relay_w=0;speeder=0;
phc=0;up=0;down=0;safe=0;up1=0;down1=0;
if(test_time>1400&&rcdata1>115&&rcdata1<125)
{
status=test_num19;
test_time=0;
}
}
else if(test_time>2200)
{
status=test_err;
err_status=18;
err_time=0;
SPEN=0;
CREN=0;
// TRISC7=0;
// RC7=1;
// ADON=0;
// ad_test=0;
}
}
}
else if(status==test_num19) //+U,-v
{
if(test_time<1000)
{
power_supply=1;power_high=0;power_low=0;relay_u=0;relay_v=0;relay_w=0;speeder=0;
spa=0;spb=0;pha=0;phb=0;phc=0;up=0;down=0;safe=0;up1=0;down1=0;
}
else if(test_time>=1000&&test_time<1100)
{
power_supply=1;power_high=0;power_low=0;relay_u=1;relay_v=0;relay_w=0;speeder=0;
phc=0;up=0;down=0;safe=0;up1=0;down1=0;
if(test_time<=1005&&test_time>1000){spa=0;spb=1;pha=1;phb=0;}
else if(test_time<=1015&&test_time>1005){spa=0;spb=1;pha=1;phb=1;}
else if(test_time<=1020&&test_time>1015){spa=0;spb=1;pha=1;phb=0;}
ad_data1=0;
}
else if(test_time>=1100&&test_time<2600) //2.66 135
{
if(test_time>1100&&test_time<2200)
{
spa=1;spb=1;pha=1;phb=0;
power_supply=1;power_high=0;power_low=1;relay_u=1;relay_v=0;relay_w=0;speeder=0;
phc=0;up=0;down=0;safe=0;up1=0;down1=0;
if(test_time>1400&&rcdata1>130&&rcdata1<140)
{
status=test_num20;
test_time=0;
// addata=0;
}
}
else if(test_time>2200)
{
status=test_err;
err_status=19;
err_time=0;
SPEN=0;
CREN=0;
// TRISC7=0;
// RC7=1;
// ADON=0;
// ad_test=0;
}
}
}
else if(status==test_num20) //-W,+v
{
if(test_time<1000)
{
power_supply=1;power_high=0;power_low=0;relay_u=0;relay_v=0;relay_w=0;speeder=0;
spa=0;spb=0;pha=0;phb=0;phc=0;up=0;down=0;safe=0;up1=0;down1=0;
}
else if(test_time>=1000&&test_time<1100)
{
power_supply=1;power_high=0;power_low=0;relay_u=0;relay_v=0;relay_w=1;speeder=0;
phc=0;up=0;down=0;safe=0;up1=0;down1=0;
if(test_time<=1005&&test_time>1000){spa=0;spb=1;pha=1;phb=0;}
else if(test_time<=1015&&test_time>1005){spa=0;spb=1;pha=1;phb=1;}
else if(test_time<=1020&&test_time>1015){spa=0;spb=1;pha=1;phb=0;}
ad_data1=0;
}
else if(test_time>=1100&&test_time<2600) //2.36 483
{
if(test_time>1100&&test_time<2200)
{
spa=1;spb=0;pha=1;phb=1;
power_supply=1;power_high=1;power_low=0;relay_u=0;relay_v=0;relay_w=1;speeder=0;
phc=0;up=0;down=0;safe=0;up1=0;down1=0;
if(test_time>1400&&rcdata1>115&&rcdata1<125)
{
status=test_num21;
test_time=0;
//addata=0;
}
}
else if(test_time>2200)
{
status=test_err;
err_status=20;
err_time=0;
SPEN=0;
CREN=0;
// TRISC7=0;
// RC7=1;
// ADON=0;
// ad_test=0;
}
}
}
else if(status==test_num21) //+W-v
{
if(test_time<1000)
{
power_supply=1;power_high=0;power_low=0;relay_u=0;relay_v=0;relay_w=0;speeder=0;
spa=0;spb=0;pha=0;phb=0;phc=0;up=0;down=0;safe=0;up1=0;down1=0;
}
else if(test_time>=1000&&test_time<1100)
{
power_supply=1;power_high=0;power_low=0;relay_u=0;relay_v=0;relay_w=1;speeder=0;
phc=0;up=0;down=0;safe=0;up1=0;down1=0;
if(test_time<=1005&&test_time>1000){spa=0;spb=1;pha=1;phb=0;}
else if(test_time<=1015&&test_time>1005){spa=0;spb=1;pha=1;phb=1;}
else if(test_time<=1020&&test_time>1015){spa=0;spb=1;pha=1;phb=0;}
ad_data1=0;
}
else if(test_time>=1100&&test_time<2600) //2.66 2.51 544
{
if(test_time<=2200&&test_time>1100)
{
spa=1;spb=1;pha=0;phb=0;
power_supply=1;power_high=0;power_low=1;relay_u=0;relay_v=0;relay_w=1;speeder=0;
phc=0;up=0;down=0;safe=0;up1=0;down1=0;
if(test_time>1400&&rcdata1>130&&rcdata1<140)
{
status=test_num22;
test_time=0;
ADCON0=0x99; //AN6 剪线
}
}
else if(test_time>2200)
{
status=test_err;
err_status=21;
err_time=0;
SPEN=0;
CREN=0;
// TRISC7=0;
// RC7=1;
// ADON=0;
// ad_test=0;
}
}
}
else if(status==test_num22)
{
if(status2==TEST_5055B)
{
if(test_time<=500)
{
power_supply=1;power_high=0;power_low=0;relay_u=0;relay_v=0;relay_w=0;speeder=0;
spa=0;spb=0;pha=0;phb=0;phc=0;up=0;down=0;safe=0;up1=0;down1=0;
}
else if(test_time>900&&test_time<=1500)
{
power_supply=1;power_high=0;power_low=0;relay_u=0;relay_v=0;relay_w=0;speeder=0;
spa=0;spb=1;pha=1;phb=0;phc=0;up=0;down=0;safe=0;up1=0;down1=0; //1001
if(test_time>700&&ad_data1>490&&ad_data1<550)
{
status=test_num23;
test_time=0;
ADCON0=0x91; //AN4 扫线
}
}
else if(test_time>1500)
{
status=test_err;
err_status=22;
err_time=0;
ADON=0;
ad_test=0;
disp_mod=1;
}
}
else if(status2==TEST_5046||status2==TEST_5045)status=test_num26;
}
else if(status==test_num23)
{
if(test_time<=500)
{
power_supply=1;power_high=0;power_low=0;relay_u=0;relay_v=0;relay_w=0;speeder=0;
spa=0;spb=0;pha=0;phb=0;phc=0;up=0;down=0;safe=0;up1=0;down1=0;
}
else if(test_time>900&&test_time<=1500)
{
power_supply=1;power_high=0;power_low=0;relay_u=0;relay_v=0;relay_w=0;speeder=0;
spa=0;spb=1;pha=0;phb=0;phc=0;up=0;down=0;safe=0;up1=0;down1=0; //1011
if(test_time>700&&ad_data1>490&&ad_data1<550)
{
status=test_num24;
test_time=0;
ADCON0=0x8d; //AN3 抬压脚
}
}
else if(test_time>1500)
{
status=test_err;
err_status=23;
err_time=0;
ADON=0;
ad_test=0;
disp_mod=1;
}
}
else if(status==test_num24)
{
if(test_time<=500)
{
power_supply=1;power_high=0;power_low=0;relay_u=0;relay_v=0;relay_w=0;speeder=0;
spa=0;spb=0;pha=0;phb=0;phc=0;up=0;down=0;safe=0;up1=0;down1=0;
}
else if(test_time>900&&test_time<=1500)
{
power_supply=1;power_high=0;power_low=0;relay_u=0;relay_v=0;relay_w=0;speeder=0;
spa=0;spb=1;pha=1;phb=0;phc=0;up=0;down=0;safe=0;up1=0;down1=0; //1001
if(test_time>700&&ad_data1>490&&ad_data1<550)
{
status=test_num25;
test_time=0;
ADCON0=0x95; //AN5 倒缝
}
}
else if(test_time>1500)
{
status=test_err;
err_status=24;
err_time=0;
ADON=0;
ad_test=0;
disp_mod=1;
}
}
else if(status==test_num25)
{
if(test_time<=500)
{
power_supply=1;power_high=0;power_low=0;relay_u=0;relay_v=0;relay_w=0;speeder=0;
spa=0;spb=0;pha=0;phb=0;phc=0;up=0;down=0;safe=0;up1=0;down1=0;
}
else if(test_time>900&&test_time<=1500)
{
power_supply=1;power_high=0;power_low=0;relay_u=0;relay_v=0;relay_w=0;speeder=0;
spa=0;spb=0;pha=0;phb=0;phc=0;up=0;down=0;safe=0;up1=0;down1=0;
KDF=1;
if(test_time>700&&ad_data1>490&&ad_data1<550) //532
{
status=test_num26;
test_time=0;
}
}
else if(test_time>1500)
{
status=test_err;
err_status=25;
err_time=0;
ADON=0;
ad_test=0;
disp_mod=1;
}
}
else if(status==test_num26)
{
speeder=0;power_supply=1;power_high=0;power_low=0;relay_u=0;relay_v=0;relay_w=0;speeder=0;KDF=0;
spa=1;spb=0;pha=1;phb=0;
if(test_time>100&&test_time<=1000)
{
rcdata=rcdata1+rcdata2+rcdata3;
if(rcdata>240&&rcdata<282)
{
status=test_num27;
test_time=0;
}
}
else if(test_time>1000)
{
status=test_err;
err_status=26;
err_time=0;
disp_hold=1;
}
}
else if(status==test_num27)
{
speeder=0;power_supply=1;power_high=0;power_low=0;relay_u=0;relay_v=0;relay_w=0;speeder=0;KDF=0;
spa=1;spb=0;pha=0;phb=1;
if(test_time>200&&test_time<=400)
{
rcdata=rcdata1+rcdata2+rcdata3;
if(rcdata>220&&rcdata<250)
{
status=test_num28;
test_time=0;
}
}
else if(test_time>400)
{
status=test_err;
err_status=27;
err_time=0;
SPEN=0;
CREN=0;
// TRISC7=0;
// RC7=1;
// ADON=0;
// ad_test=0;
}
}
else if(status==test_num28)
{
if(status2==TEST_5046)
{
disp_run=0;power_supply=1;power_high=0;power_low=0;relay_u=0;relay_v=0;relay_w=0;speeder=0;KDF=0;
spa=0;spb=0;pha=0;phb=1;
test15_tmp3=rcdata1;
test15_tmp4=rcdata2;
if(test_time>100)
{
if((test15_tmp3&0x20)==0)test15_tmp1=test15_tmp3;
else test15_tmp2=test15_tmp3;
if((test15_tmp4&0x20)==0)test15_tmp1=test15_tmp4;
else test15_tmp2=test15_tmp4;
disp20(test15_tmp1,test15_tmp2);
test_time=0;
}
}
else if(status2==TEST_5045)
{
disp_run=0;power_supply=1;power_high=0;power_low=0;relay_u=0;relay_v=0;relay_w=0;speeder=0;KDF=0;
spa=0;spb=0;pha=0;phb=1;
test15_tmp3=rcdata1;
test15_tmp4=rcdata2;
if(test_time>100)
{
if((test15_tmp3&0x20)==0)test15_tmp1=test15_tmp3;
else test15_tmp2=test15_tmp3;
if((test15_tmp4&0x20)==0)test15_tmp1=test15_tmp4;
else test15_tmp2=test15_tmp4;
disp21(test15_tmp1,test15_tmp2);
test_time=0;
}
}
else if(status2==TEST_5055B)status=setout1;
}
else if(status==test_err)
{
disp_run=0;
// power_supply=0;power_high=0;power_low=0;relay_u=0;relay_v=0;relay_w=0;speeder=0;KDF=0;speeder=0;
if(disp_hold==1)
{
if(err_status==26)
{power_supply=1;power_high=0;power_low=0;relay_u=0;relay_v=0;relay_w=0;speeder=0;KDF=0;speeder=0;}
else
{power_supply=1;power_high=0;power_low=0;relay_u=0;relay_v=0;relay_w=0;speeder=0;KDF=0;speeder=1;}
}
if(disp_hold==0)
{power_supply=0;power_high=0;power_low=0;relay_u=0;relay_v=0;relay_w=0;speeder=0;KDF=0;speeder=0;}
if(err_time<=1000)
{
if(disp_time>disp_refresh)
{
err_disp(err_status);
disp_time=0;
}
}
else if(err_time<=2000&&err_time>1000)
{
if(disp_time>disp_refresh)
{
if(disp_mod==1)
{
addisp(ad_data1);
disp_time=0;
}
else
{
disp10(rcdata1);
disp_time=0;
}
}
}
else if(err_time>2000)err_time=0;
}
}
}
void init()
{
/*PORTA */
/*RA0 relay_w 0 */
/*RA1 vol 1 */
/*RA2 +5V 1 */
/*RA3 TYJ 1 */
/*RA4 power_low 0 */
/*RA5 SX 1 */
/*RA6 power_high 0 */
/*RA7 CLK1 0 */
TRISA=0x2e; //0010 1110
ANSEL=0x7e; //模拟输入
PORTA=0;
/*PORTB */
/*RB0 SPA */
/*RB1 SPB */
/*RB2 PHA */
/*RB3 PHB */
/*RB4 PHC */
/*RB5 ralay_v */
/*RB6 CLK */
/*RB7 DATA */
TRISB=0xc0; //1100 0000,rb0--rb5输出,rb6、rb7输入
PORTB=0;
/*PORTC */
/*RC0 DOWN1 */
/*RC1 UP */
/*RC2 DOWN */
/*RC3 choose2 */
/*RC4 choose1 */
/*RC5 PWM */
/*RC6 TXD */
/*RC7 RXD */
TRISC=0x58; //0101 1000
PORTC=0x80; //1000 0000
/*PORTD*/
/*RD0 DATA1 */
/*RD1 KFD */
/*RD2 up1 */
/*RD3 bell */
/*RD4 speeder */
/*RD5 start */
/*RD6 power_supply */
/*RD7 safe */
TRISD=0x20; //0010 0000
PORTD=0x18; //0001 1000
/*PORTE*/
/*RE0 DF */
/*RE1 JX */
/*RE2 relay_u */
/*RE3 VPP */
TRISE=0xb; //0000 1011,全输入
RE2=0;
OSCCON=0xfe; //时钟选择8mhz,内部振荡器
VLCDEN=0;
OPTION=0xc3;
LCDEN=0;
CMCON0=7;
CCP2CON=0;
ADCON1=0x50;
TMR0=7;
PEIE=1;
T0IE=1;
GIE=1;
if(choose1==0&&choose2==1)status2=TEST_5045;
if(choose1==1&&choose2==1)status2=TEST_5046;
if(choose1==1&&choose2==0)status2=TEST_5055B;
}
void keyscan()
{
if(!start&&keyvalue)
{
keyvalue=0;
keytime=0;
}
if(start&&!keyvalue)
{
keyvalue=1;
keytime=0;
}
}
void key1()
{
if(keystate==0)
{
if(keyvalue==0&&keytime>10)
{
keystate=1;
keypull=1;
keytime=0;
}
}
else if(keystate==1)
{
if(keyvalue==1&&keytime>10)
{
keystate=0;
keytime=0;
}
}
else keystate=0;
}
void interrupt tm0()
{
uint int_i;
if(bell_time>50)bell=1;
if(T0IF)
{
keyscan();
key1();
keytime++;
bell_time++;
err_time++;
test_time++;
disp_time++;
disp_time1++;
tx_time++;
if(ad_test==1)
{
if(GODONE==0)
{
// ad_data3=ad_data2;
ad_data2=ad_data1;
int_i=ADRESH;
ad_data1=ADRESL|int_i<<8;
GODONE=1;
}
}
if(RCIF==1)
{
rcdata3=rcdata2;
rcdata2=rcdata1;
rcdata1=RCREG;
}
if(disp_run==1)
{
if(disp_time1>110)disp_time=0;
if(disp_time1==100)
{
disp10(status);
disp_time1=0;
}
}
T0IF=0;
TMR0=7;
}
}
void disp(uchar num1,uchar num2,uchar num3)
{
uchar i;
uchar num_tmp1,num_tmp2;
// cs1=0; //关闭显示
// clr=0; //清除74ls164数据
// clr=1;
clk1=0; //上升沿移位
num_tmp1=num1;
for(i=8;i>0;i--)
{
num_tmp2=num_tmp1;
num_tmp2=num_tmp2&0x01;
if(num_tmp2==1)
data1=1;
else
data1=0;
clk1=1;
clk1=0;
num_tmp1=num_tmp1>>1;
}
num_tmp1=num2;
for(i=8;i>0;i--)
{
num_tmp2=num_tmp1;
num_tmp2=num_tmp2&0x01;
if(num_tmp2==1)
data1=1;
else
data1=0;
clk1=1;
clk1=0;
num_tmp1=num_tmp1>>1;
}
num_tmp1=num3;
for(i=8;i>0;i--)
{
num_tmp2=num_tmp1;
num_tmp2=num_tmp2&0x01;
if(num_tmp2==1)
data1=1;
else
data1=0;
clk1=1;
clk1=0;
num_tmp1=num_tmp1>>1;
}
// cs1=1; //打开显示
}
void outpwm(uchar x)
{
PR2=0xff;
T2CON=0x07; //使能timer2,配置timer2为16分频
CCP1CON=0x3f;
CCPR1L=x;
}
uint ad(uchar x)
{
uint i,j;
x=x<<2;
ADCON1=0x50;
ADCON0=0X81|x; //100 000 01,中间三位选择通道
GODONE=1;
while(GODONE);
i=ADRESH;
j=ADRESL|i<<8;
return j;
}
void addisp(uint a)
{
uint x,y,z;
long unsigned int i;
float x_tmp;
x_tmp=a/1023.0;
i=x_tmp*500;
x=i/100;
y=i%100/10;
z=i%10;
disp(table1[x],table[y],table[z]);
}
void disp10(uchar j)
{
uchar x,y,z,d;
d=j;
x=d/100;
y=d%100/10;
z=d%10;
disp(table[x],table[y],table[z]);
}
void disp20(uchar i,uchar j)
{
uchar tmp1=i,tmp2=j,tmp3=j;
tmp1=tmp1&0x1f;
tmp2=tmp2&3;
tmp1=tmp1|(tmp2<<5);
tmp1=tmp1<<1;
tmp3=tmp3&0x1c;
tmp3=tmp3<<3;
disp(0,tmp1,tmp3);
}
void disp21(uchar i,uchar j)
{
uchar tmp1=i,tmp2=j,tmp3=j;
tmp1=tmp1&0x0f;
tmp2=tmp2&7;
tmp2=tmp2<<4;
tmp1=tmp1|tmp2;
tmp1=tmp1<<1;
tmp3=tmp3&0x18;
tmp3=tmp3<<3;
disp(0,tmp1,tmp3);
}
void err_disp(uchar j)
{
if(j<10)
{
disp(0xce,table[0],table[j]);
disp_time=0;
}
else if(j>=10&&j<20)
{
disp(0xce,table[1],table[j-10]);
disp_time=0;
}
else if(j>=20&&j<30)
{
disp(0xce,table[2],table[j-20]);
disp_time=0;
}
}
/*void disp16(uint j)
{
uchar x,y,z;
uint d;
d=j;
x=d/256;
y=d%256/16;
z=d%16;
disp(table[x],table[y],table[z]);
}*/
/*void addisp_8(uchar a)
{
uint x,y,z;
long unsigned int i;
float x_tmp;
x_tmp=a/255.0;
i=x_tmp*500;
x=i/100;
y=i%100/10;
z=i%10;
disp(table1[x],table[y],table[z]);
}*/