- #include <cms.h>
- #include "io.h"
- #include "function.h"
-
- bit FLAG_1MS;
-
-
- /*******************发送通讯变量*****************************/
- unsigned char Tx_Step;
- unsigned char Start_Count;
- bit Send_One_Bit_Over;
- unsigned char Tx_Low_Count,Tx_High_Count,Send_Count;
- bit Start_Send;
- unsigned char Tx_Data1;
- /*******************发送通讯变量*****************************/
-
-
- /****************接收板通讯变量**********************/
- bit Rx_Read_Once,Rx_Read_Now,Rx_Change,Rx_Dat_Bit,Rx_Dat_Bit1,Rx_Flag;
- unsigned char Rx_Step,Rx_Start_Count,Rx_Start_Count1,RX_Dat_Low_Count,RX_Dat_High_Count,RX_Count;
- unsigned char RX_Dat1,RX_Dat;
- /*************************************************************/
-
-
- /**************************/
- bit FLAG_ZERO_CHECK;
- bit FLAG_LAST_ZERO_STATE;
- bit FLAG_ZERO_CHANGE;
- bit SCR_OPEN_ONCE;
-
- bit FLAG_QIAN_HEAT_ON;
- bit FLAG_HOU_HEAT_ON;
- bit FLAG_ZUO_HEAT_ON;
- bit FLAG_YOU_HEAT_ON;
- bit FLAG_BAONUAN_HEAT_ON;
-
-
- unsigned char OPEN_SCR_DELAY;
- unsigned char SCR_OPEN_ONCE_DELAY;
-
- unsigned char QIAN_OPEN_GEAR; //前可控硅开的档位
- unsigned char HOU_OPEN_GEAR; //后可控硅开的档位
- unsigned char ZUO_OPEN_GEAR; //左可控硅开的档位
- unsigned char YOU_OPEN_GEAR; //右可控硅开的档位
- unsigned char BAONUAN_OPEN_GEAR;
-
-
- unsigned char QIAN_OPEN_GEAR1; //前可控硅开的档位
- unsigned char HOU_OPEN_GEAR1; //后可控硅开的档位
- unsigned char ZUO_OPEN_GEAR1; //左可控硅开的档位
- unsigned char YOU_OPEN_GEAR1; //右可控硅开的档位
- unsigned char BAONUAN_OPEN_GEAR1;
-
-
- bit FLAG_QIAN_OPEN_SCR;
- unsigned char QIAN_OPEN_SCR_DELAY,QIAN_OPEN_2CI_COUNT,QIAN_REC_TIME,QIAN_OPEN_SCR_ZERO_DELAY;
-
- bit FLAG_HOU_OPEN_SCR;
- unsigned char HOU_OPEN_SCR_DELAY,HOU_OPEN_2CI_COUNT,HOU_REC_TIME,HOU_OPEN_SCR_ZERO_DELAY;
-
- bit FLAG_ZUO_OPEN_SCR;
- unsigned char ZUO_OPEN_SCR_DELAY,ZUO_OPEN_2CI_COUNT,ZUO_REC_TIME,ZUO_OPEN_SCR_ZERO_DELAY;
-
- bit FLAG_YOU_OPEN_SCR;
- unsigned char YOU_OPEN_SCR_DELAY,YOU_OPEN_2CI_COUNT,YOU_REC_TIME,YOU_OPEN_SCR_ZERO_DELAY;
-
- bit FLAG_BAONUAN_OPEN_SCR;
- unsigned char BAONUAN_OPEN_SCR_DELAY,BAONUAN_OPEN_2CI_COUNT,BAONUAN_REC_TIME,BAONUAN_OPEN_SCR_ZERO_DELAY;
-
-
-
- bit FLAG_QIAN_ZERO_COME;
- bit FLAG_HOU_ZERO_COME;
- bit FLAG_ZUO_ZERO_COME;
- bit FLAG_YOU_ZERO_COME;
-
- bit FLAG_BAONUAN_ZERO_COME;
-
-
- /**************************/
-
-
-
- bit FLAG_QIAN_SCR_ON,FLAG_HOU_SCR_ON,FLAG_ZUO_SCR_ON,FLAG_YOU_SCR_ON,FLAG_BAONUAN_SCR_ON;
-
- unsigned char QIAN_SCR_ON_1MS_DELAY,HOU_SCR_ON_1MS_DELAY,ZUO_SCR_ON_1MS_DELAY,YOU_SCR_ON_1MS_DELAY,BAONUAN_SCR_ON_1MS_DELAY;
-
- void IO_INIT()
- {
-
- TRISC0=0;
- TRISC1=0;
-
- TRISB0=0;
- TRISB1=0;
- TRISB2=0;
- TRISB3=0;
- TRISB4=0;
-
- TRISA4=0;
- TRISA3=0;
-
- TRISA0=0;
- TRISA1=0;
- TRISA2=0;
- TRISA5=1;
-
-
-
-
-
- }
-
-
- void TIMER_INIT()
- {
- PIE2 = 0;
- PIE1 = 0B00000010;
- PR2 = 250; //8M下将TMR2设置为125us中断
- T2CON = 4; //使能定时器2
- INTCON = 0XC0; //使能中断
- }
-
- void DEAL_RX_DATA()
- {
-
-
- if(Rx_Flag)
- {
- Rx_Flag=0;
- if(RX_Dat&0x01)
- {
-
- } else
- {
-
- }
-
-
-
-
- if(RX_Dat&0x02)
- {
-
- } else
- {
-
- }
-
-
-
- }
-
- FLAG_QIAN_HEAT_ON=1;
- FLAG_HOU_HEAT_ON=1;
- FLAG_ZUO_HEAT_ON=1;
- FLAG_YOU_HEAT_ON=1;
- FLAG_BAONUAN_HEAT_ON=1;
-
- QIAN_OPEN_GEAR=10; //2的倍数
- HOU_OPEN_GEAR=10;
- ZUO_OPEN_GEAR=10;
- YOU_OPEN_GEAR=10;
- BAONUAN_OPEN_GEAR=10;
-
- COOKING_SCR_IO=1;
- }
-
-
-
-
-
-
-
-
-
-
-
- void interrupt time0(void)
- {
-
- static unsigned char _1MS_DELAY;
- if(TMR2IF)
- {
- TMR2IF = 0;
-
-
-
- _1MS_DELAY++;
- if(_1MS_DELAY>=8)
- {
- _1MS_DELAY=0;
- FLAG_1MS=1;
- }
-
-
-
- if(FLAG_QIAN_SCR_ON)
- {
- QIAN_SCR_IO=0;
- QIAN_SCR_ON_1MS_DELAY++;
- if(QIAN_SCR_ON_1MS_DELAY>=12)
- {
- QIAN_SCR_ON_1MS_DELAY=0;
- QIAN_SCR_IO=1;
- FLAG_QIAN_SCR_ON=0;
- }
- } else
- {
- QIAN_SCR_ON_1MS_DELAY=0;
- QIAN_SCR_IO=1;
- }
-
-
- if(FLAG_HOU_SCR_ON)
- {
- HOU_SCR_IO=0;
- HOU_SCR_ON_1MS_DELAY++;
- if(HOU_SCR_ON_1MS_DELAY>=12)
- {
- HOU_SCR_ON_1MS_DELAY=0;
- HOU_SCR_IO=1;
- FLAG_HOU_SCR_ON=0;
- }
- } else
- {
- HOU_SCR_ON_1MS_DELAY=0;
- HOU_SCR_IO=1;
- }
-
-
- if(FLAG_ZUO_SCR_ON)
- {
- ZUO_SCR_IO=0;
- ZUO_SCR_ON_1MS_DELAY++;
- if(ZUO_SCR_ON_1MS_DELAY>=12)
- {
- ZUO_SCR_ON_1MS_DELAY=0;
- ZUO_SCR_IO=1;
- FLAG_ZUO_SCR_ON=0;
- }
- } else
- {
- ZUO_SCR_ON_1MS_DELAY=0;
- ZUO_SCR_IO=1;
- }
-
- if(FLAG_YOU_SCR_ON)
- {
- YOU_SCR_IO=0;
- YOU_SCR_ON_1MS_DELAY++;
- if(YOU_SCR_ON_1MS_DELAY>=12)
- {
- YOU_SCR_ON_1MS_DELAY=0;
- YOU_SCR_IO=1;
- FLAG_YOU_SCR_ON=0;
- }
- } else
- {
- YOU_SCR_ON_1MS_DELAY=0;
- YOU_SCR_IO=1;
- }
-
- if(FLAG_BAONUAN_SCR_ON)
- {
- BAONUAN_SCR_IO=0;
- BAONUAN_SCR_ON_1MS_DELAY++;
- if(BAONUAN_SCR_ON_1MS_DELAY>=12)
- {
- BAONUAN_SCR_ON_1MS_DELAY=0;
- BAONUAN_SCR_IO=1;
- FLAG_BAONUAN_SCR_ON=0;
- }
- } else
- {
- BAONUAN_SCR_ON_1MS_DELAY=0;
- BAONUAN_SCR_IO=1;
- }
-
-
-
- /**********************读过零信号******************************/
- if(!FLAG_ZERO_CHECK)
- {
- FLAG_ZERO_CHECK=1;
- FLAG_LAST_ZERO_STATE=(unsigned char)ZERO_IO;
- }
- if(FLAG_ZERO_CHECK)
- {
- if(FLAG_LAST_ZERO_STATE!=ZERO_IO) //过零来了
- {
- FLAG_ZERO_CHECK=0;
- FLAG_QIAN_ZERO_COME=1;
- FLAG_HOU_ZERO_COME=1;
- FLAG_ZUO_ZERO_COME=1;
- FLAG_YOU_ZERO_COME=1;
- FLAG_BAONUAN_ZERO_COME=1;
- }
- }
- /**********************读过零信号******************************/
-
-
-
-
- /**********************前可控硅驱动*********************/
-
-
-
-
- if(FLAG_QIAN_HEAT_ON) //工作标志位
- {
-
- if(FLAG_QIAN_ZERO_COME)
- {
- FLAG_QIAN_ZERO_COME=0;
- QIAN_OPEN_SCR_ZERO_DELAY=0;
-
- QIAN_OPEN_GEAR1=QIAN_OPEN_GEAR;
- if(QIAN_OPEN_GEAR1<=2) QIAN_OPEN_GEAR1=2;
- if(QIAN_OPEN_GEAR1>=10) QIAN_OPEN_GEAR1=10;
-
- if(QIAN_OPEN_GEAR1==10)
- {
- FLAG_QIAN_OPEN_SCR=1;
- } else
- {
- QIAN_OPEN_2CI_COUNT++;
-
- if(QIAN_OPEN_2CI_COUNT<=QIAN_OPEN_GEAR1)
- {
- FLAG_QIAN_OPEN_SCR=1;
- }
- if(QIAN_OPEN_2CI_COUNT==1)
- {
- QIAN_REC_TIME=0;
- } else
- {
- QIAN_REC_TIME=2;
- }
-
- if(QIAN_OPEN_2CI_COUNT>=10) QIAN_OPEN_2CI_COUNT=0; //最大开10个周期
-
- }
-
- }
-
- if(FLAG_QIAN_OPEN_SCR)
- {
-
- QIAN_OPEN_SCR_ZERO_DELAY++;
- if(QIAN_OPEN_SCR_ZERO_DELAY>=77-QIAN_REC_TIME) //78
- {
- QIAN_OPEN_SCR_ZERO_DELAY=0;
- FLAG_QIAN_SCR_ON=1;
- FLAG_QIAN_OPEN_SCR=0;
-
- }
- }
- } else
- {
- QIAN_OPEN_SCR_ZERO_DELAY=0;
- QIAN_REC_TIME=0;
- FLAG_QIAN_OPEN_SCR=0;
- FLAG_QIAN_ZERO_COME=0;
- FLAG_QIAN_SCR_ON=0;
- }
- /**********************可控硅驱动END******************************/
-
-
- if(FLAG_HOU_HEAT_ON) //工作标志位
- {
- if(FLAG_HOU_ZERO_COME)
- {
- FLAG_HOU_ZERO_COME=0;
- HOU_OPEN_SCR_ZERO_DELAY=0;
-
- HOU_OPEN_GEAR1=HOU_OPEN_GEAR;
- if(HOU_OPEN_GEAR1<=2) HOU_OPEN_GEAR1=2;
- if(HOU_OPEN_GEAR1>=10) HOU_OPEN_GEAR1=10;
-
- if(HOU_OPEN_GEAR1==10)
- {
- FLAG_HOU_OPEN_SCR=1;
- } else
- {
- HOU_OPEN_2CI_COUNT++;
-
- if(HOU_OPEN_2CI_COUNT<=HOU_OPEN_GEAR1)
- {
- FLAG_HOU_OPEN_SCR=1;
- }
- if(HOU_OPEN_2CI_COUNT==1)
- {
- HOU_REC_TIME=0;
- } else
- {
- HOU_REC_TIME=2;
- }
-
- if(HOU_OPEN_2CI_COUNT>=10) HOU_OPEN_2CI_COUNT=0; //最大开10个周期
-
- }
-
- }
-
- if(FLAG_HOU_OPEN_SCR)
- {
-
- HOU_OPEN_SCR_ZERO_DELAY++;
- if(HOU_OPEN_SCR_ZERO_DELAY>=77-HOU_REC_TIME) //78
- {
- HOU_OPEN_SCR_ZERO_DELAY=0;
- FLAG_HOU_SCR_ON=1;
- FLAG_HOU_OPEN_SCR=0;
-
- }
- }
-
-
-
- } else
- {
- HOU_OPEN_SCR_ZERO_DELAY=0;
- HOU_REC_TIME=0;
- FLAG_HOU_OPEN_SCR=0;
- FLAG_HOU_ZERO_COME=0;
- FLAG_HOU_SCR_ON=0;
- }
-
-
- if(FLAG_ZUO_HEAT_ON) //工作标志位
- {
- if(FLAG_ZUO_ZERO_COME)
- {
- FLAG_ZUO_ZERO_COME=0;
- ZUO_OPEN_SCR_ZERO_DELAY=0;
-
- ZUO_OPEN_GEAR1=ZUO_OPEN_GEAR;
- if(ZUO_OPEN_GEAR1<=2) ZUO_OPEN_GEAR1=2;
- if(ZUO_OPEN_GEAR1>=10) ZUO_OPEN_GEAR1=10;
-
- if(ZUO_OPEN_GEAR1==10)
- {
- FLAG_ZUO_OPEN_SCR=1;
- } else
- {
- ZUO_OPEN_2CI_COUNT++;
-
- if(ZUO_OPEN_2CI_COUNT<=ZUO_OPEN_GEAR1)
- {
- FLAG_ZUO_OPEN_SCR=1;
- }
- if(ZUO_OPEN_2CI_COUNT==1)
- {
- ZUO_REC_TIME=0;
- } else
- {
- ZUO_REC_TIME=2;
- }
-
- if(ZUO_OPEN_2CI_COUNT>=10) ZUO_OPEN_2CI_COUNT=0; //最大开10个周期
-
- }
-
- }
-
- if(FLAG_ZUO_OPEN_SCR)
- {
-
- ZUO_OPEN_SCR_ZERO_DELAY++;
- if(ZUO_OPEN_SCR_ZERO_DELAY>=77-ZUO_REC_TIME) //78
- {
- ZUO_OPEN_SCR_ZERO_DELAY=0;
- FLAG_ZUO_SCR_ON=1;
- FLAG_ZUO_OPEN_SCR=0;
-
- }
- }
-
-
-
- } else
- {
- ZUO_OPEN_SCR_ZERO_DELAY=0;
- ZUO_REC_TIME=0;
- FLAG_ZUO_OPEN_SCR=0;
- FLAG_ZUO_ZERO_COME=0;
- FLAG_ZUO_SCR_ON=0;
- }
-
-
- if(FLAG_YOU_HEAT_ON) //工作标志位
- {
- if(FLAG_YOU_ZERO_COME)
- {
- FLAG_YOU_ZERO_COME=0;
- YOU_OPEN_SCR_ZERO_DELAY=0;
-
- YOU_OPEN_GEAR1=YOU_OPEN_GEAR;
- if(YOU_OPEN_GEAR1<=2) YOU_OPEN_GEAR1=2;
- if(YOU_OPEN_GEAR1>=10) YOU_OPEN_GEAR1=10;
-
- if(YOU_OPEN_GEAR1==10)
- {
- FLAG_YOU_OPEN_SCR=1;
- } else
- {
- YOU_OPEN_2CI_COUNT++;
-
- if(YOU_OPEN_2CI_COUNT<=YOU_OPEN_GEAR1)
- {
- FLAG_YOU_OPEN_SCR=1;
- }
- if(YOU_OPEN_2CI_COUNT==1)
- {
- YOU_REC_TIME=0;
- } else
- {
- YOU_REC_TIME=2;
- }
-
- if(YOU_OPEN_2CI_COUNT>=10) YOU_OPEN_2CI_COUNT=0; //最大开10个周期
-
- }
-
- }
-
- if(FLAG_YOU_OPEN_SCR)
- {
-
- YOU_OPEN_SCR_ZERO_DELAY++;
- if(YOU_OPEN_SCR_ZERO_DELAY>=77-YOU_REC_TIME) //78
- {
- YOU_OPEN_SCR_ZERO_DELAY=0;
- FLAG_YOU_SCR_ON=1;
- FLAG_YOU_OPEN_SCR=0;
-
- }
- }
-
-
-
- } else
- {
- YOU_OPEN_SCR_ZERO_DELAY=0;
- YOU_REC_TIME=0;
- FLAG_YOU_OPEN_SCR=0;
- FLAG_YOU_ZERO_COME=0;
- FLAG_YOU_SCR_ON=0;
- }
-
-
- if(FLAG_BAONUAN_HEAT_ON) //工作标志位
- {
- if(FLAG_BAONUAN_ZERO_COME)
- {
- FLAG_BAONUAN_ZERO_COME=0;
- BAONUAN_OPEN_SCR_ZERO_DELAY=0;
-
- BAONUAN_OPEN_GEAR1=BAONUAN_OPEN_GEAR;
- if(BAONUAN_OPEN_GEAR1<=2) BAONUAN_OPEN_GEAR1=2;
- if(BAONUAN_OPEN_GEAR1>=10) BAONUAN_OPEN_GEAR1=10;
-
- if(BAONUAN_OPEN_GEAR1==10)
- {
- FLAG_BAONUAN_OPEN_SCR=1;
- } else
- {
- BAONUAN_OPEN_2CI_COUNT++;
-
- if(BAONUAN_OPEN_2CI_COUNT<=BAONUAN_OPEN_GEAR1)
- {
- FLAG_BAONUAN_OPEN_SCR=1;
- }
- if(BAONUAN_OPEN_2CI_COUNT==1)
- {
- BAONUAN_REC_TIME=0;
- } else
- {
- BAONUAN_REC_TIME=2;
- }
-
- if(BAONUAN_OPEN_2CI_COUNT>=10) BAONUAN_OPEN_2CI_COUNT=0; //最大开10个周期
-
- }
-
- }
-
- if(FLAG_BAONUAN_OPEN_SCR)
- {
-
- BAONUAN_OPEN_SCR_ZERO_DELAY++;
- if(BAONUAN_OPEN_SCR_ZERO_DELAY>=77-BAONUAN_REC_TIME) //78
- {
- BAONUAN_OPEN_SCR_ZERO_DELAY=0;
- FLAG_BAONUAN_SCR_ON=1;
- FLAG_BAONUAN_OPEN_SCR=0;
-
- }
- }
-
-
-
- } else
- {
- BAONUAN_OPEN_SCR_ZERO_DELAY=0;
- BAONUAN_REC_TIME=0;
- FLAG_BAONUAN_OPEN_SCR=0;
- FLAG_BAONUAN_ZERO_COME=0;
- FLAG_BAONUAN_SCR_ON=0;
- }
-
-
-
-
-
-
- if (Rx_Read_Once==0) //读一次现在的IO口状态
- {
- Rx_Read_Once=1;
- Rx_Read_Now=RX_TX_IO;
- }
-
-
- if (Rx_Read_Now!=RX_TX_IO) //IO口电平发生了变化
- {
- Rx_Read_Once=0;
- Rx_Change=1;
- }
-
-
- switch (Rx_Step)
- {
- case 0:
- if (RX_TX_IO==0) {
- Rx_Step=1;
- }
- Rx_Start_Count=0;
- break;
- case 1:
- if (RX_TX_IO==0)
- {
- Rx_Start_Count++;
- }
- if (RX_TX_IO==1)
- {
- if (Rx_Start_Count>=21&&Rx_Start_Count<=38) //9ms 9000/125=72
- {
- Rx_Start_Count=0;
- Rx_Step=2;
- } else
- {
- Rx_Start_Count=0;
- Rx_Step=0;
- }
- }
- break;
- case 2:
- if (RX_TX_IO==1)
- {
- Rx_Start_Count1++;
- }
- if (RX_TX_IO==0)
- {
- if (Rx_Start_Count1>=30&&Rx_Start_Count1<=40) //4.5ms 4500/125=36
- {
- Rx_Step=3;
- //UART0_SendByte(Rx_Start_Count1);
- Rx_Start_Count1=0;
- Rx_Change=0;
- Rx_Read_Once=0;
- } else
- {
- Rx_Start_Count1=0;
- Rx_Step=0;
- }
- }
- break;
- case 3:
- if (Rx_Dat_Bit==0) //数一下低电平的时间
- {
- RX_Dat_Low_Count++;
- if (Rx_Change==1) //IO口由低变成高了
- {
- Rx_Change=0;
- Rx_Read_Once=0;
- Rx_Dat_Bit=1;
- }
- }
- if (Rx_Dat_Bit==1) //数完低电平 数高电平
- {
- RX_Dat_High_Count++;
- if (Rx_Change==1) //IO口由高变成低
- {
- Rx_Change=0;
- Rx_Read_Once=0;
- Rx_Dat_Bit=0;
- Rx_Dat_Bit1=1;
- }
- }
- if (Rx_Dat_Bit1)
- {
- Rx_Dat_Bit1=0;
- RX_Dat1=RX_Dat1<<1;
- if (RX_Dat_Low_Count>=3&&RX_Dat_High_Count>=11) //收到拉低0.5MS 拉高1.5
- {
- RX_Dat=(RX_Dat1 |= 0x01); //收到1
- }
- if (RX_Dat_Low_Count>=3&&RX_Dat_High_Count<=6&&RX_Dat_High_Count>=3) //收到拉低0.5MS 拉高0.5MS
- {
- RX_Dat=RX_Dat1; //收到0
- //0
- }
- RX_Dat_Low_Count=0;
- RX_Dat_High_Count=0;
- RX_Count++;
- if (RX_Count>=8) //收8位数据
- {
- RX_Count=0;
- RX_Dat=RX_Dat1;
- Rx_Step=0;
- Rx_Flag=1;
- RX_Dat1=0;
-
-
- // FLAG_START_RX=0;
- }
- }
- break;
- default:
- break;
- }
-
- }
- else
- {
- PIR1 = 0;
- PIR2 = 0;
- }
-
-
-
- }