diff --git a/app_code/main.c b/app_code/main.c index 8e537f9..b68d3c4 100644 --- a/app_code/main.c +++ b/app_code/main.c @@ -1,13 +1,24 @@ #include "STC8.H" #include + + +typedef enum +{ + OUT_INIT=0, + OUT_RUNNING, + OUT_AUTOSTOP, + OUT_ERR, + + OUTSTATE_NUM, +}OUTSTATE_t; + /*************** 宏定义 **************/ #define FOSC 11059200UL // MCU工作频率为11.0592MHz #define BRT (65536 - FOSC / 115200 / 4) // 串口通信波特率为115200 #define IAP_ADDRESS 0x0000 -#define LED_ON P00 = 0 #define LED_OFF P00 = 1 #define WT_30M 0x80 @@ -80,6 +91,9 @@ void Parameter_Save(void); unsigned int Get_ADC12bitResult(unsigned char channel); // channel = 0~14 unsigned int ADC_Average(unsigned int *buff, unsigned int num, unsigned int threshold); +//new +void OutPutCtrl(void); + /***************变量定义**************/ bit busy = 0; bit SendEN = 0; @@ -162,6 +176,7 @@ unsigned char xdata Shut_Min = 0; unsigned char xdata Shut_Sec = 0; unsigned int xdata Shut_MS = 0; + int OffsetVoltage = 0; // 补偿电压 float xdata IA; // 实际输出电流 @@ -170,6 +185,9 @@ float xdata AdjVoltage = 0; // 调节电压 float xdata CompensationValue = 0; // 补偿系数 float xdata OutGain = 0; // 输出增益调节 +//new +OUTSTATE_t xdata OutPutState; +unsigned int xdata InitTimeCnt; /*************** 主函数 **************/ void main() { @@ -246,6 +264,7 @@ void main() PWM_TO_DAC(5000, (unsigned int)(CalibrationVoltage * OutGain) + (unsigned int)IA); // 供电电压为:5000MV,负载端输出电压为:CalibrationVoltage,单片机口输出电压为:SetOutVoltage/3 AcqInitCnt = 0; + InitTimeCnt = 0; while (1) { DataParsing(); // 串口数据解析 @@ -264,32 +283,56 @@ void main() } } - AutoAction(); // 运行自动程序 + //AutoAction(); // 运行自动程序 + OutPutCtrl(); } } +void OutPutCtrl(void) +{ + switch (OutPutState) + { + case OUT_INIT: + PWM_TO_DAC(5000,2000); + InitTimeCnt++; + if (InitTimeCnt > 1000) + { + InitTimeCnt = 0; + OutPutState = OUT_RUNNING; + } + + break; + case OUT_RUNNING: + break; + case OUT_AUTOSTOP: + break; + case OUT_ERR: + break; + default: + break; + } +} + +void turnOFFOutput(void) +{ + PWM_ON_EN = 1; + Shut_Hour = 0; + Shut_Min = 0; + Shut_Sec = 0; + Shut_MS = 0; + // PWM_ON = 1; + PWM_TO_DAC(5000, 0); + AD_Refresh = 0; +} + void AutoAction(void) { if (PWM_ON_EN == 0) // PWM输出使能 - + { if (AcqVolatage >= Limitvoltage) // 采集电压大于1500mV,或电流小于0.5mA,关闭输出 { - if (AD_Refresh) - - LED_ON; - // LED_OFF; - { - PWM_ON_EN = 1; - Shut_Hour = 0; - Shut_Min = 0; - Shut_Sec = 0; - Shut_MS = 0; - // PWM_ON = 1; - PWM_TO_DAC(5000, 0); - AD_Refresh = 0; - } + turnOFFOutput(); } - else { { @@ -1075,27 +1118,8 @@ void AutoAction(void) AD_Refresh = 0; } } - - // 指示灯 - // if((AcqVolatage>500)&&(IA>0.05))//采集电压大于100MV - // { - - // LED_OFF; - // } - // else - // { - // LED_ON; - // } - if (AcqVolatage > 500) // 万博专用 - { - - LED_ON; - } - else - { - LED_OFF; - } } + } OutCurrent = (unsigned int)(IA * 100); // 放大10倍后的电流(触摸屏显示用) } void Delay10ms() //@11.0592MHz