📄 main.c
字号:
#include ".\COM.h"
#include".\ppi.h"
//串口通讯设置相关参数
#define IP_VALUE 0x10 //串行中断高优先级
#define TMOD_VALUE 0x21 //T0十六位定时器,T1 波特率发生器
#define SCON_VALUE 0xD0 //方式3,11位
#define PCON_VALUE 0x00 //单倍波特率
#define BAUD_VALUE 0xFA //22.1184MHz晶振,波特率设定植为9600
#define T2CON_VALUE 0x00 /*T2 act as 16 bit timer*/
#define TH0_VALUE 0xB8//定时10MS
#define TL0_VALUE 0x00
#define RCAP2H_VALUE 0x6F //定时20ms
#define RCAP2L_VALUE 0xFF
#define UART_COMPLETE 0x80
#define INITIAL 0x00
#define LISTEN_TOKEN 0x01
#define CLAIM_TOKEN 0x02
#define ACTIVE_IDLE 0x04
#define USE_TOKEN 0x08
#define AWAIT_RESPONSE 0x10
#define CHECK_ACCESS_TIME 0x20
#define PASS_TOKEN 0x40
#define CHECK_PASS_TOKEN 0x80
#define AWAIT_STATUS_RESPONSE 0x81
#define SLOT_TIME 10
#define SYNC_TIME 3
#define ACCESS_TIME 10
unsigned char data YZ_parity;
unsigned char s_buffer_tb8[50];
unsigned char r_buffer_rb8[50];
unsigned char YZ_s_buffer[50];
unsigned char YZ_r_buffer[50];
unsigned char YZ_uart_num;
unsigned char YZ_uart_complete;
unsigned char status;
unsigned char YZ_stages;
unsigned char comm_error=0;
unsigned char YZ_i;
unsigned char YZ_address;
unsigned char YZ_idle_timer;
unsigned char YZ_sync_timer;
unsigned char bdata flag=0;
//sbit YZ_setup_flag=flag^0;
sbit YZ_sync_timer_flag=flag^1;
sbit YZ_slot_timer_flag=flag^2;
sbit YZ_no_response_flag=flag^3;
sbit YZ_response_flag=flag^4;
sbit YZ_current_gap_again=flag^5;
sbit YZ_retry_flag=flag^6;
sbit YZ_response_complete_flag=flag^7;
sbit YZ_idle_timer_flag=flag^0;
unsigned char bdata YZ_flag=0;
sbit YZ_token_passed=YZ_flag^0;
sbit YZ_claim_token_flag=YZ_flag^1;
unsigned char YZ_current_gap_address;
unsigned char YZ_slot_timer;
unsigned char YZ_access_timer;
unsigned char YZ_status_bef;
unsigned char YZ_request_timer=0;
unsigned char YZ_pass_token_timer=0;
unsigned char YZ_await_response_time=0;
#define WDT_VALUE 0xf2 // 142.2ms
unsigned char data cy_startup _at_ 0x7f;
list_of_active_station list;
SD1_Message_Formate * YZ_SD1;
SD4_Message_Formate*YZ_SD4;
unsigned char s_buffer[18];
void time_delay(unsigned int s) ;
void uart_timer_set(void) ;
void byte_parity(unsigned char num) ;
void frame_send(unsigned char t);
unsigned char receive_parity_check(unsigned char YZ_r_number);
void send_sd1_frame(unsigned char destination_adderss,unsigned char frame_control);
void send_sd4_frame(unsigned char destination_address);
void init_dymicram() ;
unsigned char *sd2data_write_formate();
extern init_16c554uart();
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();
init_dymicram();
RUN=!RUN;
time_delay(500); //time delay
RUN=!RUN;
EX0=ENABLE;
EX1=ENABLE;
ET0=ENABLE;
ET2=ENABLE;
EA=ENABLE;
status=0;
YZ_stages=0;
YZ_current_gap_address=ADDRESS+1;
}
for(;;)
{ WDT_CONTR=WDT_VALUE; //watch dog
switch ( status)
{
case INITIAL:
list.next_stationaddress=127;// list init
list.previous_stationaddress=0;
list.FDL_status_response=0x10; //master not ready
status=LISTEN_TOKEN;
YZ_idle_timer=6*SLOT_TIME+(2*ADDRESS*SLOT_TIME);
YZ_sync_timer=SYNC_TIME;
YZ_slot_timer=SLOT_TIME;
YZ_request_timer=3;
break;
case LISTEN_TOKEN:
ES=ENABLE;//串口打开
TR0=1;//定时器打开
if(YZ_uart_complete==UART_COMPLETE)
{
TR0=0;
if(YZ_r_buffer[0]==SC)
{YZ_uart_num=0;
YZ_uart_complete=0;
}
if(YZ_r_buffer[0]==SD4)//有令牌传送
{
YZ_idle_timer=6*SLOT_TIME+(2*ADDRESS*SLOT_TIME);
comm_error= receive_parity_check(YZ_uart_num);
if(comm_error==0)
{
YZ_SD4=(SD4_Message_Formate *)YZ_r_buffer;
if((YZ_SD4->Source_Address<ADDRESS)&&(YZ_SD4->Source_Address>list.previous_stationaddress))//LAS
list.previous_stationaddress=YZ_SD4->Source_Address;
if((YZ_SD4->Source_Address>ADDRESS)&&(YZ_SD4->Source_Address<list.next_stationaddress))
list.next_stationaddress=YZ_SD4->Source_Address;
}
YZ_uart_num=0;
YZ_uart_complete=0;
}
if(YZ_r_buffer[0]==SD1)// FDL 状态请求
{
comm_error= receive_parity_check(YZ_uart_num);
if(comm_error==0)
{
YZ_SD1=(SD1_Message_Formate *)YZ_r_buffer;
if(((YZ_SD1->Destination_Adderss+YZ_SD1->Source_Address+YZ_SD1->Frame_Contorl)%256)==YZ_SD1->Frame_check_squence)
{
if(YZ_SD1->Frame_Contorl==0x49) //
{
switch (YZ_stages)
{
case 0:
YZ_address= YZ_SD1->Source_Address;
list.FDL_status_response=0x10; //master not ready
YZ_stages++;
break;
case 1:
if(YZ_address==YZ_SD1->Source_Address)
{
if(YZ_address>ADDRESS)
{list.next_stationaddress=YZ_address;
list.previous_stationaddress=YZ_address; //没有比本站更低的主站
}
else
{
list.previous_stationaddress=YZ_address;
}
}
list.FDL_status_response=0x20;//ready to enter
status=ACTIVE_IDLE;
TR0=0;
YZ_idle_timer=6*SLOT_TIME+(2*ADDRESS*SLOT_TIME);
//YZ_stages=0;
break;
default:
break;
}
send_sd1_frame(YZ_address, list.FDL_status_response);
}
}
}
YZ_uart_num=0;
YZ_uart_complete=0;
}
}
if(YZ_idle_timer_flag==1)
{
YZ_idle_timer_flag=0;
status=CLAIM_TOKEN;
TR0=0;
}
break;
case ACTIVE_IDLE:
TR0=1;
YZ_status_bef=0;
if(YZ_uart_complete==UART_COMPLETE)
{TR0=0;
YZ_idle_timer=6*SLOT_TIME+(2*ADDRESS*SLOT_TIME);
if(YZ_r_buffer[0]==SD4)// 令牌传递
{
comm_error= receive_parity_check(YZ_uart_num);
if(comm_error==0)
{
YZ_SD4=(SD4_Message_Formate *)YZ_r_buffer;
if(YZ_SD4->Destination_Adderss==ADDRESS) //pass token to me
{
if(YZ_SD4->Source_Address==list.previous_stationaddress)
{status=USE_TOKEN;
YZ_status_bef=ACTIVE_IDLE;
}
else if(YZ_SD4->Source_Address!=list.previous_stationaddress)
{
if(YZ_SD4->Source_Address<ADDRESS)
list.previous_stationaddress=YZ_SD4->Source_Address;
else
{list.previous_stationaddress=YZ_SD4->Source_Address;
list.next_stationaddress=YZ_SD4->Source_Address;
}
}
}
else//不是给我的令排
{
YZ_idle_timer=6*SLOT_TIME+(2*ADDRESS*SLOT_TIME);
}
}
}
else
{
TR0=1;
}
YZ_uart_num=0;
YZ_uart_complete=0;
}
if(YZ_idle_timer_flag==1)
{
YZ_idle_timer_flag=0;
status=CLAIM_TOKEN;
TR0=0;
}
break;
case USE_TOKEN:
if(YZ_status_bef==ACTIVE_IDLE)
{
YZ_status_bef=0;
//send_sd1_frame(list.previous_stationaddress, list.FDL_status_response);
TR0=1;
TH0=TH0_VALUE;
TL0=TL0_VALUE;
YZ_access_timer=ACCESS_TIME;//USE TOKEN TIME*10MS
YZ_request_timer=3;//request time
}
else if(YZ_status_bef==CHECK_ACCESS_TIME)
{ unsigned char *buffer;
if(YZ_claim_token_flag==0) //非主动申请令牌
{
if(YZ_request_timer>0)
{
YZ_request_timer--;
if(YZ_request_timer==2)
{YZ_request_timer=3;
buffer=sd2data_write_formate();
for(YZ_i=0;YZ_i<39;YZ_i++)
{
YZ_s_buffer[YZ_i]=*(buffer+YZ_i);
}
for(YZ_i=0;YZ_i<39;YZ_i++)
{
YZ_parity=YZ_s_buffer[YZ_i];
byte_parity(8);
s_buffer_tb8[YZ_i]=YZ_parity;
}
frame_send(39);
status=AWAIT_RESPONSE;
TR0=0;
free(buffer);
}
if(YZ_request_timer==1)
{
YZ_request_timer=3;// status=AWAIT_RESPONSE;
}
}
else //NO REQUEST
{
status=PASS_TOKEN;
YZ_status_bef=USE_TOKEN;
TR0=0;
}
}
else //主动申请令牌
{ YZ_claim_token_flag=0;
status=PASS_TOKEN;
YZ_status_bef=USE_TOKEN;
TR0=0;
}
}
else if(YZ_status_bef==AWAIT_RESPONSE)
{
YZ_status_bef=USE_TOKEN;
status=PASS_TOKEN;
}
else
{TR0=1;
// TH0=TH0_VALUE;
// TL0=TL0_VALUE;
YZ_access_timer=ACCESS_TIME;//USE TOKEN TIME*10MS
}
break;
case CHECK_ACCESS_TIME://10.ms once time
YZ_access_timer--;
if(YZ_access_timer>0)//HAVE TIME SEND REQUEST
{
status=USE_TOKEN;
YZ_status_bef=CHECK_ACCESS_TIME;
}
else
{YZ_access_timer=ACCESS_TIME;
status=PASS_TOKEN;
YZ_status_bef=CHECK_ACCESS_TIME;
}
break;
case AWAIT_RESPONSE:
if(YZ_uart_complete==UART_COMPLETE)
{
comm_error= receive_parity_check(YZ_uart_num);
if(comm_error==0)
{
if(YZ_r_buffer[0]==SC)// request replay
{if(YZ_await_response_time==0)
{
YZ_sync_timer=SYNC_TIME;
TR0=1;
YZ_await_response_time++;
}
}
else if(YZ_r_buffer[0]==SD2)
{
if( YZ_await_response_time==1)
{YZ_await_response_time=0;
YZ_response_complete_flag=1;
status=USE_TOKEN;
YZ_status_bef= AWAIT_RESPONSE;
}
}
else if(YZ_r_buffer[0]==SD1)
{YZ_SD1=( SD1_Message_Formate * )YZ_r_buffer;
if((YZ_SD1->Frame_Contorl==0x02)||(YZ_SD1->Frame_Contorl==0x03))
{YZ_retry_flag=1;
status=PASS_TOKEN;
YZ_status_bef= AWAIT_RESPONSE;
}
}
else
{status=ACTIVE_IDLE;
YZ_status_bef= AWAIT_RESPONSE;
}
}
else
{
status=ACTIVE_IDLE;
}
YZ_uart_num=0;
YZ_uart_complete=0;
}
if(YZ_sync_timer_flag==1)
{YZ_sync_timer_flag=0;
TR0=0;//
send_sd1_frame(0x02, 0x5c);
}
break;
case CLAIM_TOKEN:
TR0=0;
status=PASS_TOKEN;
YZ_status_bef=CLAIM_TOKEN;
YZ_claim_token_flag=1;
break;
case PASS_TOKEN:
if(YZ_status_bef==USE_TOKEN) //normal setup enent
{
TR0=1;
if(YZ_sync_timer_flag==1)
{TR0=0;
YZ_sync_timer_flag=0;
YZ_sync_timer=SYNC_TIME;
if(list.next_stationaddress!=ADDRESS+1) //HAVE GAP
{
send_sd1_frame(YZ_current_gap_address, 0x49);
status=AWAIT_STATUS_RESPONSE;
YZ_status_bef=PASS_TOKEN;
}
}
}
if(YZ_status_bef==CLAIM_TOKEN)
{
YZ_status_bef=PASS_TOKEN;
send_sd4_frame(ADDRESS);
status=USE_TOKEN;
}
if(YZ_status_bef==AWAIT_STATUS_RESPONSE)
{
if(YZ_no_response_flag==1)//STATION IS NOT EXIST
{
YZ_no_response_flag=0;
YZ_current_gap_address++;// UPDATA GAP ADDRESS
if(YZ_current_gap_again==0) //gap management
{
if(list.next_stationaddress!=127)
{
if(YZ_current_gap_address==list.next_stationaddress)
YZ_current_gap_address=ADDRESS+1;
}
else
{
if(YZ_current_gap_address>126)
{
YZ_current_gap_address=0;
YZ_current_gap_again=1;//第二轮
}
}
}
else
{
if(list.previous_stationaddress!=0)
{
if(YZ_current_gap_address==list.previous_stationaddress)
{
YZ_current_gap_address=ADDRESS+1;
YZ_current_gap_again=0;
}
}
else
{
if(YZ_current_gap_address==ADDRESS)
{YZ_current_gap_address=ADDRESS+1;
YZ_current_gap_again=0;
}
}
}
YZ_SD4=( SD4_Message_Formate * )YZ_s_buffer;//
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -