|
///M8
//TIMER0 initialisation - prescale:1024
// WGM: Normal
// desired value: 13mSec
// actual value: 12.917mSec (0.6%)
void timer0_init(void)
{
TCCR0 = 0x00; //stop
TCNT0 = 0xA3; //set count
TCCR0 = 0x05; //start timer
}
#pragma interrupt_handler timer0_ovf_isr:10
void timer0_ovf_isr(void)
{
TCNT0 = 0xA3; //reload counter value
if(Time0_Ovf_ProgramBranch==0)
Com0_Receive_Cout=0;
else
{
Com0_Receive_Complete_Flag=1;
Com0_Receive_CRCcouter=Com0_Receive_Cout;
UCSRB = 0xC8;///close the receiveing interrupt
Time0_Ovf_ProgramBranch=0;
}
}
//UART0 initialisation
// desired baud rate: 9600
// actual: baud rate:9600 (0.0%)
// char size: 8 bit
// parity: Disabled
void uart0_init(void)
{
UCSRB = 0x00; //disable while setting baud rate
UCSRA = 0x00;
UCSRC = 0x86;
UBRRL = 0x2F; //set baud rate lo
UBRRH = 0x00; //set baud rate hi
UCSRB = 0xD8;
}
#pragma interrupt_handler uart0_rx_isr:12
void uart0_rx_isr(void)
{
//uart has received a character in UDR
Com0_Receive_Data_Buffer[Com0_Receive_Cout]=UDR;
if(Com0_Receive_Data_Buffer[0]==Com0_Send_Data_Buffer[0])
{
if(Com0_Receive_Cout<18)
Com0_Receive_Cout++;
Time0_Ovf_ProgramBranch=1;
}
else
{
Com0_Receive_Cout=1;
Time0_Ovf_ProgramBranch=0;
}
TCNT0=0xB1;
}
#pragma interrupt_handler uart0_tx_isr:14
void uart0_tx_isr(void)
{
//character has been transmitted
Com0_Send_Data_Pointer++;
Com0_Send_Data_Bytes--;
if(Com0_Send_Data_Bytes>0)
UDR=*Com0_Send_Data_Pointer;
}
////////////////////////////////////////////////////////
unsigned short getCRC16(unsigned char *ptr,unsigned char len)
{
unsigned char i;
unsigned short crc=0xFFFF;
if(len==0)
len=1;
while(len--)
{
crc^=*ptr;
for(i=0; i<8; i++)
{
if(crc&1)
{
crc>>=1;
crc^=0xA001;
}
else
crc>>=1;
}
ptr++;
}
return(crc);
}
/////////////////////////////////////////////////////////////////////
void Modbus_Function_1(void)
{
unsigned char a, b,c,i;
unsigned int temp;
Com0_Send_Data_Buffer[1]=0x01;
if((Com0_Receive_Data_Buffer[2]==0)&&(Com0_Receive_Data_Buffer[3]<=40))
{
if(Com0_Receive_Data_Buffer[3]+Com0_Receive_Data_Buffer[5]<50)
{
a=Com0_Receive_Data_Buffer[3]>>3;
b=Com0_Receive_Data_Buffer[3]&0x07;
c=Com0_Receive_Data_Buffer[5]+b;
Com0_Send_Data_Buffer[2]=Com0_Receive_Data_Buffer[5]>>3;
if(Com0_Receive_Data_Buffer[5]&0x07)
Com0_Send_Data_Buffer[2]+=1;
Com0_Send_Data_Bytes=Com0_Send_Data_Buffer[2]+3;
if(c&0x07)
c=(c>>3)+1;
else
c=c>>3;
for(i=0;i<c;i++)
{
temp=IO_Battery_Voltage_Alarm[a];
a++;
temp+=(IO_Battery_Voltage_Alarm[a]<<8);
temp>>=b;
Com0_Send_Data_Buffer[i+3]=temp&0xff;
}
Com0_Send_Data_Buffer[i+3]=temp>>8;
Com0_Send_Data_Buffer[i+3]<<=(8-Com0_Receive_Data_Buffer[5]&0x07);
Com0_Send_Data_Buffer[i+3]>>=(8-Com0_Receive_Data_Buffer[5]&0x07);
}
else
{////show error!
Com0_Send_Data_Buffer[1]=0xFF;
Com0_Send_Data_Buffer[2]=2;
Com0_Send_Data_Bytes=5;
Com0_Send_Data_Buffer[3]=0x00;
Com0_Send_Data_Buffer[4]=0x01;
}
}
else
{////show error!
Com0_Send_Data_Buffer[1]=0xFF;
Com0_Send_Data_Buffer[2]=2;
Com0_Send_Data_Bytes=5;
Com0_Send_Data_Buffer[3]=0x00;
Com0_Send_Data_Buffer[4]=0x02;
}
}
///////////////////////////////////////////////////////////////////////
void Modbus_Function_3(void)
{
unsigned char i,a,b;
//WDR();
Com0_Send_Data_Buffer[1]=0x03;
if((Com0_Receive_Data_Buffer[2]==0)&&(Com0_Receive_Data_Buffer[3]<=30))
{
if(Com0_Receive_Data_Buffer[3]+Com0_Receive_Data_Buffer[5]<40)
{
Com0_Send_Data_Buffer[2]=2*Com0_Receive_Data_Buffer[5];
Com0_Send_Data_Bytes=2*Com0_Receive_Data_Buffer[5]+3;
a=Com0_Receive_Data_Buffer[3];
b=Com0_Receive_Data_Buffer[5]*2;
for(i=0;i<b;i++)
{
Com0_Send_Data_Buffer[3+i]=Battery_Voltage_Value[2*a+i];
}
}
else
{////show error!
Com0_Send_Data_Buffer[1]=0xFF;
Com0_Send_Data_Buffer[2]=2;
Com0_Send_Data_Bytes=5;
Com0_Send_Data_Buffer[3]=0x00;
Com0_Send_Data_Buffer[4]=0x03;
}
}
else
{////show error!
Com0_Send_Data_Buffer[1]=0xFF;
Com0_Send_Data_Buffer[2]=2;
Com0_Send_Data_Bytes=5;
Com0_Send_Data_Buffer[3]=0x00;
Com0_Send_Data_Buffer[4]=0x04;
}
}
//////////////////////////////////////////////////////////////
void Modbus_Function_5(void)
{
Com0_Send_Data_Buffer[1]=0x05;
Com0_Send_Data_Buffer[2]=Com0_Receive_Data_Buffer[2];
Com0_Send_Data_Buffer[3]=Com0_Receive_Data_Buffer[3];
Com0_Send_Data_Buffer[4]=Com0_Receive_Data_Buffer[4];
Com0_Send_Data_Buffer[5]=Com0_Receive_Data_Buffer[5];
Com0_Send_Data_Bytes=6;
if((Com0_Receive_Data_Buffer[2]==0)&&(Com0_Receive_Data_Buffer[3]==0))
{
if((Com0_Receive_Data_Buffer[4]==0xFF)&&(Com0_Receive_Data_Buffer[5]==0x00))
{
ALARM=1;
IO_Battery_Voltage_Alarm[0]|=0x01;
}
else
{
ALARM=0;
IO_Battery_Voltage_Alarm[0]&=0xFE;
}
}
else
{
Com0_Send_Data_Buffer[1]=0xFF;
Com0_Send_Data_Buffer[2]=2;
Com0_Send_Data_Bytes=5;
Com0_Send_Data_Buffer[3]=0x00;
Com0_Send_Data_Buffer[4]=0x05;
}
}
/////////////////////////////////////////////////////////////
void Modbus_Function_16(void)
{
Com0_Send_Data_Buffer[1]=0x10;
Com0_Send_Data_Buffer[2]=Com0_Receive_Data_Buffer[2];
Com0_Send_Data_Buffer[3]=Com0_Receive_Data_Buffer[3];
Com0_Send_Data_Buffer[4]=Com0_Receive_Data_Buffer[4];
Com0_Send_Data_Buffer[5]=Com0_Receive_Data_Buffer[5];
Com0_Send_Data_Bytes=6;
if((Com0_Receive_Data_Buffer[2]==0)&&(Com0_Receive_Data_Buffer[3]==20))
{
EEPROMwrite((int)&V_Bat_High_Alarm_H,Com0_Receive_Data_Buffer[7]);
EEPROMwrite((int)&V_Bat_High_Alarm_L,Com0_Receive_Data_Buffer[8]);
Battery_Voltage_Value[40]=Com0_Receive_Data_Buffer[7];
Battery_Voltage_Value[41]=Com0_Receive_Data_Buffer[8];
Battery_Voltage_High_Alarm=Battery_Voltage_Value[41];
Battery_Voltage_High_Alarm|=(int)Battery_Voltage_Value[40] << 8;
}
else if((Com0_Receive_Data_Buffer[2]==0)&&(Com0_Receive_Data_Buffer[3]==21))
{
EEPROMwrite((int)&V_Bat_Low_Alarm_H,Com0_Receive_Data_Buffer[7]);
EEPROMwrite((int)&V_Bat_Low_Alarm_L,Com0_Receive_Data_Buffer[8]);
Battery_Voltage_Value[42]=Com0_Receive_Data_Buffer[7];
Battery_Voltage_Value[43]=Com0_Receive_Data_Buffer[8];
Battery_Voltage_Low_Alarm=Battery_Voltage_Value[43];
Battery_Voltage_Low_Alarm|=(int)Battery_Voltage_Value[42] << 8;
}
else if((Com0_Receive_Data_Buffer[2]==0)&&(Com0_Receive_Data_Buffer[3]==22))
{
EEPROMwrite((int)&Alarm_Bat_Temperature_H,Com0_Receive_Data_Buffer[7]);
EEPROMwrite((int)&Alarm_Bat_Temperature_L,Com0_Receive_Data_Buffer[8]);
Battery_Voltage_Value[44]=Com0_Receive_Data_Buffer[7];
Battery_Voltage_Value[45]=Com0_Receive_Data_Buffer[8];
Battery_Temperature_High_Alarm=Battery_Voltage_Value[45];
Battery_Temperature_High_Alarm|=(int)Battery_Voltage_Value[44] << 8;
}
else if((Com0_Receive_Data_Buffer[2]==0)&&(Com0_Receive_Data_Buffer[3]==23))
{
EEPROMwrite((int)&Address_Bat_check,Com0_Receive_Data_Buffer[8]);
Com0_Send_Data_Buffer[0]=Com0_Receive_Data_Buffer[8];
Battery_Voltage_Value[46]=0;
Battery_Voltage_Value[47]=Com0_Receive_Data_Buffer[8];
}
else if((Com0_Receive_Data_Buffer[2]==0)&&(Com0_Receive_Data_Buffer[3]==24))
{
if((Com0_Receive_Data_Buffer[7]==0)&&(Com0_Receive_Data_Buffer[8]<=19))
{
EEPROMwrite((int)&Num_Bat_Node,Com0_Receive_Data_Buffer[8]);
Battery_Voltage_Value[48]=0;
Battery_Voltage_Value[49]=Com0_Receive_Data_Buffer[8];
}
}
else if((Com0_Receive_Data_Buffer[2]==0)&&(Com0_Receive_Data_Buffer[3]==25))
{
if((Com0_Receive_Data_Buffer[7]==0)&&(Com0_Receive_Data_Buffer[8]<=200))
{
EEPROMwrite((int)&Battery_Voltage_Value_potentiometer01_L,Com0_Receive_Data_Buffer[8]);
Battery_Voltage_Value[50]=0;
Battery_Voltage_Value[51]=Com0_Receive_Data_Buffer[8];
}
}
else if((Com0_Receive_Data_Buffer[2]==0)&&(Com0_Receive_Data_Buffer[3]==26))
{
if((Com0_Receive_Data_Buffer[7]==0)&&(Com0_Receive_Data_Buffer[8]<=200))
{
EEPROMwrite((int)&Battery_Voltage_Value_potentiometer02_L,Com0_Receive_Data_Buffer[8]);
Battery_Voltage_Value[52]=0;
Battery_Voltage_Value[53]=Com0_Receive_Data_Buffer[8];
}
}
else
{
Com0_Send_Data_Buffer[1]=0xFF;
Com0_Send_Data_Buffer[2]=Com0_Receive_Data_Buffer[2];
Com0_Send_Data_Buffer[3]=Com0_Receive_Data_Buffer[3];
Com0_Send_Data_Buffer[4]=0x00;
Com0_Send_Data_Buffer[5]=0x05;
}
}
/////////////////////////////////////////////////////////////
void Com0_Communication(void)
{
unsigned short crcresult;
unsigned char temp[2];
crcresult= getCRC16(Com0_Receive_Data_Buffer,Com0_Receive_CRCcouter-2);
temp[1]=crcresult & 0xff;
temp[0]=(crcresult >> 8) & 0xff;
if((Com0_Receive_Data_Buffer[Com0_Receive_CRCcouter-1]==temp[0])&&(Com0_Receive_Data_Buffer[Com0_Receive_CRCcouter-2]==temp[1]))
{
PORTB^=0x20;
switch(Com0_Receive_Data_Buffer[1])
{
case 0x01:
Modbus_Function_1();
break;
case 0x03:
Modbus_Function_3();
break;
case 0x05:
Modbus_Function_5();
break;
case 0x10:
Modbus_Function_16();
break;
default:
{
Com0_Send_Data_Buffer[1]=0xFF;
Com0_Send_Data_Buffer[2]=2;
Com0_Send_Data_Bytes=5;
Com0_Send_Data_Buffer[3]=0x00;
Com0_Send_Data_Buffer[4]=0x06;
}
}
}
else
{
////show error!
Com0_Send_Data_Buffer[1]=0xFF;
Com0_Send_Data_Buffer[2]=2;
Com0_Send_Data_Bytes=5;
Com0_Send_Data_Buffer[3]=0x00;
Com0_Send_Data_Buffer[4]=0x07;
}
crcresult= getCRC16(Com0_Send_Data_Buffer,Com0_Send_Data_Bytes);
Com0_Send_Data_Buffer[Com0_Send_Data_Bytes]=crcresult & 0xff;
Com0_Send_Data_Buffer[Com0_Send_Data_Bytes+1]=(crcresult >> 8) & 0xff;
Com0_Send_Data_Bytes=Com0_Send_Data_Bytes+2;
Com0_Send_Data_Pointer=Com0_Send_Data_Buffer;
UDR=*Com0_Send_Data_Pointer;
}
////////////////////////////////////////////////////////////////////////// |
阿莫论坛20周年了!感谢大家的支持与爱护!!
曾经有一段真挚的爱情摆在我的面前,我没有珍惜,现在想起来,还好我没有珍惜……
|