#include <stdio.h>
#include <reg51.h>
#include <main.h>
#include <interrupt.h>
#include <spi.h>
#include <dlt645.h>
#include <memory.h>
#include <rs485.h>
#define uchar unsigned char
#define uint unsigned int
//#define MAX_CMD_LEN 53
#define INIT_FLAG_ADDR 0x10
#define INIT_FLAG_LEN 5
#define DEVICE_ID_ADDR 0x15
#define DEVICE_ID_LEN 6
uchar code init_flag[INIT_FLAG_LEN]={0x19,0x98,0x09,0x30,0x04};
//uchar code table_calibrate_cmd[9]={0xc8,0xc4,0xc0,0xd8,0xd4,0xd0,0xe0,0xe8,0xe4};
uchar code table_bit[8]={1,2,4,8,0x10,0x20,0x40,0x80};
extern uchar count_level;
uchar pointer_buf485,rs485_timeout;
uchar pointer_send,send_len;
uchar oper_len,send_offset,chk_sum,send_i;
uchar device_id[DEVICE_ID_LEN];
uchar count_1s;
uint count_10s;
uchar rs485_buf[MAX_485_LEN];
uchar chksum;
uchar idata spi_buf[MAX_SPI_LEN];
uchar bdata flag_byte1;
sbit flag_key=flag_byte1^0;
sbit flag_data_ok=flag_byte1^1;
sbit flag_send_data=flag_byte1^2;
sbit flag_level=flag_byte1^3;
sbit flag_rs485=flag_byte1^4;
sbit flag_key_detect=flag_byte1^5;
sbit flag_rd_measure_module=flag_byte1^6;
sbit flag_drdy=flag_byte1^7;
typedef union
{
uint dptr;
uchar byte[2];
}un;
sbit LED_A=P2^5;
sbit LED_B=P2^6;
sbit LED_C=P2^7;
sbit Sub_Rst=P1^0;
sbit MOTOR_L=P2^1;
sbit MOTOR_COM=P2^2;
sbit MOTOR_R=P2^3;
extern code struct TRead_Write_Attribute irregular_cmd_table[IRREGULAR_CMD_NUM];
//uchar ij;
//uint kj;
//bit flag_motor_l;
main()
{
Delay_10us(250);
P0=0xff;
P1=0xff;
P2=0x0f;
P3=0xff;
Sub_Rst=0;
Delay_10us(250);
Sub_Rst=1;
for(count_1s=0;count_1s<3;count_1s++)
{
if(!Judge_Device_Init_Flag())
break;
else
Delay_10us(250);
}
if(count_1s==3)
{
device_id[0]=device_id[1]=device_id[2]=device_id[3]=device_id[4]=device_id[5]=0;
spi_buf[0]=init_flag[0];
spi_buf[1]=init_flag[1];
spi_buf[2]=init_flag[2];
spi_buf[3]=init_flag[3];
spi_buf[4]=init_flag[4];
Write_To_Memory(spi_buf,INIT_FLAG_ADDR,INIT_FLAG_LEN);
Write_To_Memory(device_id,DEVICE_ID_ADDR,DEVICE_ID_LEN);
}
pointer_buf485=0;
flag_byte1=0x0;
count_1s=0;
Init_8051();
while(1)
{
flag_byte1|=0x70;
if(flag_rs485)
Rs485_Process();
if(flag_key_detect)
Task_Key_Detect();
if(flag_rd_measure_module)
Read_Measure_Module_Data();
}
}
void Rs485_Process(void)
{
if(pointer_buf485<10)
return;
if(pointer_buf485<rs485_buf[9]+12)
return;
rs485_timeout=0;
pointer_buf485--;
if((rs485_buf[7]!=0x68) || (rs485_buf[pointer_buf485]!=0x16) )
{
pointer_buf485=0;
return;
}
pointer_buf485--;
if(Get_Chksum(rs485_buf,rs485_buf[9]+10)!=rs485_buf[pointer_buf485])
{
pointer_buf485=0;
return;
}
if(Compare_Device_ID())
{
pointer_buf485=0;
return;
}
ES=0; //forbid rs485 interrupt
pointer_buf485=0;
if(rs485_buf[8]==0x01)
Read_Data_From_Spi(); //read data from measure board
else if(rs485_buf[8]==0x04 || rs485_buf[8]==0x1e)
Write_Data_To_Spi(); //write data to measure board
else if(rs485_buf[8]==0x0a)
Write_Device_Addr(); //write device id to EEPROM
else if(rs485_buf[8]==0x1d)
Erect_Device_Connect(); //contact with Personal Computer
}
void Task_Key_Detect(void)
{
uchar i;
if(flag_key) //key is pressed
{
KEY_BIT=1;
if(!KEY_BIT)
return;
else
{
for(i=0;i<3;i++);
if(KEY_BIT)
{
flag_key=0;
P2=0xff;
return;
}
}
}
else
{
if(KEY_BIT)
return;
else
{
for(i=0;i<3;i++);
if(!KEY_BIT)
{
flag_key=1;
// count_2min=12000; //T=1200*10ms=120000ms=120s=2min
P2=0x0f;
/*
for test
*/
/*
rs485_buf[0]=0x68;
rs485_buf[1]=device_id[0];
rs485_buf[2]=device_id[1];
rs485_buf[3]=device_id[2];
rs485_buf[4]=device_id[3];
rs485_buf[5]=device_id[4];
rs485_buf[6]=device_id[5];
rs485_buf[7]=0x68;
rs485_buf[8]=0x1d;
rs485_buf[9]=0x01;
rs485_buf[10]=0x55;
// Get_Send_Frame_Package(1);
Send_Frame_To_485(1,0);
*/
return;
}
}
}
}
void Read_Measure_Module_Data(void)
{
uchar temp;
/* SPI_DRDY=HIGH;
if(count_1s)
return;
flag_data_ok=0;
if(SPI_DRDY || flag_send_data)
return;
count_1s=100;
*/
if((!flag_drdy) || flag_send_data)
return;
flag_drdy=0;
if(Read_From_Measure(0x08,spi_buf,MAX_SPI_LEN)) //128
return;
/*
temp=chksum;
if(Read_From_Measure(0x07,&rs485_buf[MAX_485_LEN-2],1))
return;
if(temp!=chksum)
{
// LED_A=~LED_A;
return;
}
*/
LED_A=~LED_A;
flag_data_ok=1;
count_1s=250;
}
void Read_Data_From_Spi(void)
{
uchar item,cmd_type;
// uchar len,property;
un un0;
un0.byte[0]=rs485_buf[11];
un0.byte[1]=rs485_buf[10];
item=Get_Iregular_Cmd_Atrib(un0.dptr);
if(item==0xff)
{
ES=1;
return;
}
cmd_type=irregular_cmd_table[item].cmd_spi_read;
// send_offset=irregular_cmd_table[item].offset;
// oper_len=irregular_cmd_table[item].oper_len;
if(cmd_type==0x60) //read eeprom
{
flag_data_ok=0;
if(Read_From_Measure(0x60,spi_buf,MAX_SPI_LEN)) //128
{
ES=1;
return;
}
}
else
{
if(!flag_data_ok)
{
ES=1;
return;
}
}
Send_Frame_To_485(2+irregular_cmd_table[item].oper_len,irregular_cmd_table[item].offset);
}
void Write_Data_To_Spi(void)
{
uchar item,phase,cmd_type,property;
un un0;
un0.byte[0]=rs485_buf[11];
un0.byte[1]=rs485_buf[10];
item=Get_Iregular_Cmd_Atrib(un0.dptr);
if(item==0xff)
{
ES=1;
return;
}
phase=rs485_buf[10] & 0x0f; //phase=1,2,3(,4,5,6)
// i=rs485_buf[10] >>4; //i=1,2,3,4,5,6,7,8,9
cmd_type=irregular_cmd_table[item].cmd_spi_write;
property=irregular_cmd_table[item].property;
if((property & 0x01)==0)
{
ES=1;
return;
}
SET_ALL_LED_OFF;
if(phase<4)
SET_LED_ON(phase+4); //p2.5 p2.6 p2.7
else
SET_LED_ON(phase+1);
if(property & 0x02) //case1: calibration
{
if(Write_To_Measure(cmd_type,&spi_buf[0],0))
{
ES=1;
SET_ALL_LED_OFF;
return;
}
count_10s=1000; //1000*10ms=10s
SPI_DRDY=HIGH;
do
{
if(count_10s==0)
{
SET_ALL_LED_OFF;
ES=1;
return;
}
}while(SPI_DRDY); //wait drdy to low
do
{
if(count_10s==0)
{
SET_ALL_LED_OFF;
ES=1;
return;
}
}while(!SPI_DRDY); //wait drdy to high
}
else if(property & 0x04) //case2: soft modify
{
if(property & 0x10) //phase modify
{
if(property & 0x08)
rs485_buf[16]=0x87;
else
rs485_buf[16]=0x01;
}
else
{
rs485_buf[16]=rs485_buf[12];
if(property & 0x08)
rs485_buf[16]|=0x80;
}
for(item=0;item<3;item++)
{
if(Write_To_Measure(cmd_type,&rs485_buf[16],1)==0)
break;
}
if(item!=3)
{
for(phase=0;phase<30;phase++)
for(item=0;item<255;item++);
}
}
else //case3:other
{
item=irregular_cmd_table[item].oper_