📄 main.c
字号:
#include ".\STC89c5x.H"
#include"comm.h"
#define EV1000_STARTADDERSS 0
sbit RTS0=P1^0;
sbit RTS1=P1^1;
sbit RTS2=P1^2;
sbit RTS3=P1^3;
sbit RTS4=P1^4;
sbit RUN=P1^6;
sbit RESET =P1^5;
struct datatype
{
unsigned int frequency;
unsigned char flag_station;
}EV1000_BUF[50];
unsigned char frequency_change=0;
unsigned char i,j;
unsigned char timer2_num=0;
unsigned char send_timer=0;
unsigned char temp;
void time_delay(unsigned int s);
extern unsigned char uc_554a_r_complete;
extern unsigned char uc_554a_r_number;
extern unsigned char uc_554b_r_complete;
extern unsigned char uc_554b_r_number;
extern unsigned char uc_554c_r_complete;
extern unsigned char uc_554c_r_number;
extern unsigned char uc_554d_r_complete;
extern unsigned char uc_554d_r_number;
extern unsigned int read_hmi_buf[50];
extern unsigned char uart_complete;
extern unsigned char timer_flag;
extern unsigned char frame_length;
extern void readhmi_message(void);
extern void writehmi_message(void);
extern void uart_timer_set(void);
extern void init_16c554uart(void);
extern void modify_frequency(unsigned char station, unsigned int frequency);
extern void frame_16c554asend(unsigned char t);
extern void frame_16c554bsend(unsigned char t);
extern void frame_16c554csend(unsigned char t);
extern void frame_16c554dsend(unsigned char t);
#define WDT_VALUE 0xf2 // 142.2ms
unsigned char data cy_startup _at_ 0x7f;
void main(void)
{
if(cy_startup!=0xaa)//冷启动
{
cy_startup=0xaa;
AUXR=0x01; //禁止ALE输出
RESET=FALSE; //16c554复位初始为低
RTS0=FALSE; // 默认串口全为接收状态
RTS1=FALSE;
RTS2=FALSE;
RTS3=FALSE;
RTS4=FALSE;
RUN=OFF;
time_delay(500); //time delay
uart_timer_set();
init_16c554uart();
for(i=0;i<50;i++) //初试设定频率为0,变频器站地址为0-49
{
EV1000_BUF[i].frequency=0;
EV1000_BUF[i].flag_station=i+EV1000_STARTADDERSS;
}
RUN=!RUN;
time_delay(500); //time delay
RUN=!RUN;
ES=ENABLE;
EX0=ENABLE;
EX1=ENABLE;
ET0=ENABLE;
ET2=ENABLE;
EA=ENABLE;
}
for(;;)
{
WDT_CONTR=WDT_VALUE; //watch dog
if(timer_flag!=0) //每隔500MS跟屏通信一次
{
RTS0=TRUE;
readhmi_message(); //read hmi data
RTS0=FALSE;
timer_flag=0;
}
if(uart_complete==UART_COMPLETE) //从屏接受到数据
{
for(i=0;i<1;i++)
{
if(read_hmi_buf[i]!=EV1000_BUF[i].frequency)
{
EV1000_BUF[i].frequency=read_hmi_buf[i]; //频率改变存储
EV1000_BUF[i].flag_station=(EV1000_BUF[i].flag_station|0x80); //
}
if(EV1000_BUF[i].flag_station>127) //频率改变
{frequency_change=1; //置位频率改变标志
temp=i;
}
}
uart_complete=0;
TR0=1;
ES=ENABLE;
}
if(frequency_change==1)
{
if(EV1000_BUF[temp].flag_station>127)//频率改变发送到相应站点
{
EV1000_BUF[temp].flag_station=(EV1000_BUF[temp].flag_station&0x7f);//清除改变标志位
modify_frequency(EV1000_BUF[temp].flag_station,EV1000_BUF[temp].frequency);
if((EV1000_BUF[temp].flag_station>=EV1000_STARTADDERSS)&&(EV1000_BUF[temp].flag_station<=(EV1000_STARTADDERSS+13)))
//前14个站接到串口1上,这样不用每个串口多发数据,节约时间
frame_16c554asend(18);//向串口1发送数据
if((EV1000_BUF[temp].flag_station>(EV1000_STARTADDERSS+13))&&(EV1000_BUF[temp].flag_station<=(EV1000_STARTADDERSS+27)))
frame_16c554bsend(18);//15~27号在串口2上
if((EV1000_BUF[temp].flag_station>(EV1000_STARTADDERSS+27))&&(EV1000_BUF[temp].flag_station<=(EV1000_STARTADDERSS+41)))
frame_16c554csend(18);//28~41在串口3上
if((EV1000_BUF[temp].flag_station>(EV1000_STARTADDERSS+41))&&(EV1000_BUF[temp].flag_station<=(EV1000_STARTADDERSS+54)))
frame_16c554dsend(18); //42~54在串口4
RCAP2H=RCAP2H_VALUE; //定时器2工作
RCAP2L=RCAP2L_VALUE;
TR2=1;
}
frequency_change=0;
}
if((uc_554a_r_complete==UART_COMPLETE)||(uc_554b_r_complete==UART_COMPLETE)||(uc_554c_r_complete==UART_COMPLETE)||(uc_554d_r_complete==UART_COMPLETE))//接受到正确数据帧
{
uc_554a_r_complete=0;
uc_554a_r_number=0;
uc_554b_r_complete=0;
uc_554b_r_number=0;
uc_554c_r_complete=0;
uc_554c_r_number=0;
uc_554d_r_complete=0;
uc_554d_r_number=0;
TR2=0;
timer2_num=0;
}
if(timer2_num>=5) //数据发送100MS没有回再次发送
{
timer2_num=0;
TR2=0;
if((EV1000_BUF[temp].flag_station>=EV1000_STARTADDERSS)&&(EV1000_BUF[temp].flag_station<=(EV1000_STARTADDERSS+13)))
frame_16c554asend(18);//向串口1发送数据
if((EV1000_BUF[temp].flag_station>(EV1000_STARTADDERSS+13))&&(EV1000_BUF[temp].flag_station<=(EV1000_STARTADDERSS+27)))
frame_16c554bsend(18);//15~27号在串口2上
if((EV1000_BUF[temp].flag_station>(EV1000_STARTADDERSS+27))&&(EV1000_BUF[temp].flag_station<=(EV1000_STARTADDERSS+41)))
frame_16c554csend(18);//28~41在串口3上
if((EV1000_BUF[temp].flag_station>(EV1000_STARTADDERSS+41))&&(EV1000_BUF[temp].flag_station<=(EV1000_STARTADDERSS+54)))
frame_16c554dsend(18); //42~54在串口4
RCAP2H=RCAP2H_VALUE;
RCAP2L=RCAP2L_VALUE;
TR2=1;
send_timer++;
if(send_timer==4) //发送四次,若通信不正确 放弃
{
send_timer=0;
TR2=0;
timer2_num=0;
//发送HMI通信错误标志
}
}
}
}
/**************************************************************************************************
Name: void time_delay(unsigned int)
Description: time delay, 11.0592M 1 ≌ 2mS
Calls: none
**************************************************************************************************/
void time_delay(unsigned int s)
{
unsigned int p;
for(p=s;p;p--)
{
for(i=166;i;i--);
}
}
void timer2_int(void) interrupt 5 using 0 //WAIT FOR REPLAY TIME timer2_num*20ms
{
TF2=FALSE;
RCAP2H=RCAP2H_VALUE;
RCAP2L=RCAP2L_VALUE;
timer2_num++;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -