[mw_shl_code=c,true]#include "modbus.h"
#include "output.h"
///////////////////////////////////////////////////////////
u32 RS485_Baudrate=9600;//í¨Ñ¶2¨ìØÂê
u8 RS485_Parity=0;//0ÎTD£Ñ飻1ÆæD£Ñ飻2żD£Ñé
u8 RS485_Addr=1;//′ó»úμØÖ·
u16 RS485_Frame_Distance=4;//êy¾YÖ¡×îD¡¼ä¸ô£¨ms),3¬1y′Ëê±¼äÔòèÏÎaêÇÏÂò»Ö¡
u8 RS485_RX_BUFF[64];//½óêÕ»o3åÇø2048×Ö½ú
u16 RS485_RX_CNT=0;//½óêÕ¼ÆêyÆ÷
u8 RS485_FrameFlag=0;//Ö¡½áêø±ê¼Ç
u8 RS485_TX_BUFF[64];//·¢Ëí»o3åÇø
u16 RS485_TX_CNT=0;//·¢Ëí¼ÆêyÆ÷
////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Modbus¼Ä′æÆ÷oíμ¥Ƭ»ú¼Ä′æÆ÷μÄó3éä1ØÏμ
vu32 *Modbus_InputIO[10];//êäèë¿a1ØῼÄ′æÆ÷Ö¸Õë(Õaàïê1óÃμÄêÇλ′ø2ù×÷)
vu32 *Modbus_OutputIO[10];//êä3ö¿a1ØῼÄ′æÆ÷Ö¸Õë(Õaàïê1óÃμÄêÇλ′ø2ù×÷)
u16 *Modbus_HoldReg[10];//±£3Ö¼Ä′æÆ÷Ö¸Õë
u16 testData1=1201,testData2=1002,testData3=2303,testData4=8204;
void Modbus_RegMap(void)
{
//êäèë¿a1ØῼÄ′æÆ÷Ö¸ÕëÖ¸Ïò
Modbus_InputIO[0]=(vu32*)&
Ein(4);//KEY0 //&
Ein(4)£oè¡
E4μÄμØÖ·£¬(vu32*)&
Ein(4)½«
E4μØÖ·Ç¿ÖÆ×a»»Îauw32ààDíμÄμØÖ·£¬Modbus_InputIO[0]=(vu32*)&
Ein(4); ½«×a»»oÃμÄμØÖ·Ëí¸øμØÖ·Ö¸ÕëModbus_InputIO[0]£»
Modbus_InputIO[1]=(vu32*)&
Ein(3);//KEY1 //*Modbus_InputIO[0] è¡3öμØÖ·ÖDμÄÄúèY¡£
Modbus_InputIO[2]=(vu32*)&
Ein(2);//KEY2
Modbus_InputIO[3]=(vu32*)&
Ain(0);//KEY3
//êä3ö¿a1ØῼÄ′æÆ÷Ö¸ÕëÖ¸Ïò
Modbus_OutputIO[0]=(vu32*)&
Bout(5);//LED0
Modbus_OutputIO[1]=(vu32*)&PEout(5);//LED1
//±£3Ö¼Ä′æÆ÷Ö¸ÕëÖ¸Ïò
Modbus_HoldReg[0]=(u16*)&testData1;//2aêÔêy¾Y1
Modbus_HoldReg[1]=(u16*)&testData2;//((u16*)&testData1)+1;//2aêÔêy¾Y1
Modbus_HoldReg[2]=(u16*)&testData3;//(u16*)&testData2;//2aêÔêy¾Y2
Modbus_HoldReg[3]=(u16*)&testData4;//((u16*)&testData2)+1;//2aêÔêy¾Y2
Modbus_HoldReg[4]=(u16*)&testData1;
Modbus_HoldReg[5]=(u16*)&testData2;
Modbus_HoldReg[6]=(u16*)&testData3;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//CRCD£Ñé ×Ô¼ooóÃæìí¼óμÄ
const u8 auchCRCHi[] = {
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40,
0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40,
0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40,
0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,0x80, 0x41, 0x00, 0xC1, 0x81, 0x40} ;
const u8 auchCRCLo[] = {
0x00, 0xC0, 0xC1, 0x01, 0xC3, 0x03, 0x02, 0xC2, 0xC6, 0x06,0x07, 0xC7, 0x05, 0xC5, 0xC4, 0x04, 0xCC, 0x0C, 0x0D, 0xCD,
0x0F, 0xCF, 0xCE, 0x0E, 0x0A, 0xCA, 0xCB, 0x0B, 0xC9, 0x09,0x08, 0xC8, 0xD8, 0x18, 0x19, 0xD9, 0x1B, 0xDB, 0xDA, 0x1A,
0x1E, 0xDE, 0xDF, 0x1F, 0xDD, 0x1D, 0x1C, 0xDC, 0x14, 0xD4,0xD5, 0x15, 0xD7, 0x17, 0x16, 0xD6, 0xD2, 0x12, 0x13, 0xD3,
0x11, 0xD1, 0xD0, 0x10, 0xF0, 0x30, 0x31, 0xF1, 0x33, 0xF3,0xF2, 0x32, 0x36, 0xF6, 0xF7, 0x37, 0xF5, 0x35, 0x34, 0xF4,
0x3C, 0xFC, 0xFD, 0x3D, 0xFF, 0x3F, 0x3E, 0xFE, 0xFA, 0x3A,0x3B, 0xFB, 0x39, 0xF9, 0xF8, 0x38, 0x28, 0xE8, 0xE9, 0x29,
0xEB, 0x2B, 0x2A, 0xEA, 0xEE, 0x2E, 0x2F, 0xEF, 0x2D, 0xED,0xEC, 0x2C, 0xE4, 0x24, 0x25, 0xE5, 0x27, 0xE7, 0xE6, 0x26,
0x22, 0xE2, 0xE3, 0x23, 0xE1, 0x21, 0x20, 0xE0, 0xA0, 0x60,0x61, 0xA1, 0x63, 0xA3, 0xA2, 0x62, 0x66, 0xA6, 0xA7, 0x67,
0xA5, 0x65, 0x64, 0xA4, 0x6C, 0xAC, 0xAD, 0x6D, 0xAF, 0x6F,0x6E, 0xAE, 0xAA, 0x6A, 0x6B, 0xAB, 0x69, 0xA9, 0xA8, 0x68,
0x78, 0xB8, 0xB9, 0x79, 0xBB, 0x7B, 0x7A, 0xBA, 0xBE, 0x7E,0x7F, 0xBF, 0x7D, 0xBD, 0xBC, 0x7C, 0xB4, 0x74, 0x75, 0xB5,
0x77, 0xB7, 0xB6, 0x76, 0x72, 0xB2, 0xB3, 0x73, 0xB1, 0x71,0x70, 0xB0, 0x50, 0x90, 0x91, 0x51, 0x93, 0x53, 0x52, 0x92,
0x96, 0x56, 0x57, 0x97, 0x55, 0x95, 0x94, 0x54, 0x9C, 0x5C,0x5D, 0x9D, 0x5F, 0x9F, 0x9E, 0x5E, 0x5A, 0x9A, 0x9B, 0x5B,
0x99, 0x59, 0x58, 0x98, 0x88, 0x48, 0x49, 0x89, 0x4B, 0x8B,0x8A, 0x4A, 0x4E, 0x8E, 0x8F, 0x4F, 0x8D, 0x4D, 0x4C, 0x8C,
0x44, 0x84, 0x85, 0x45, 0x87, 0x47, 0x46, 0x86, 0x82, 0x42,0x43, 0x83, 0x41, 0x81, 0x80, 0x40} ;
u16 CRC_Compute(u8 *puchMsg, u16 usDataLen)
{
u8 uchCRCHi = 0xFF ;
u8 uchCRCLo = 0xFF ;
u32 uIndex ;
while (usDataLen--)
{
uIndex = uchCRCHi ^ *puchMsg++ ;
uchCRCHi = uchCRCLo ^ auchCRCHi[uIndex] ;
uchCRCLo = auchCRCLo[uIndex] ;
}
return ((uchCRCHi<< 8) | (uchCRCLo)) ;
}//uint16 crc16(uint8 *puchMsg, uint16 usDataLen)
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//3õê¼»ˉUSART2
void RS485_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOD,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_CRC,ENABLE);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_2;//PA2£¨TX£©¸′óÃíÆíìêä3ö
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_SetBits(GPIOA,GPIO_Pin_2);//Ĭèϸßμçƽ
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_3;//PA3£¨RX£©êäèëéÏà-
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING; //DT¸ÄÔ-GPIO_Mode_IPU£¨êäèëéÏà-£©->GPIO_Mode_IN_FLOATING(¸¡¿Õêäèë)/////////////////////////////////////////////
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_1;//PA1£¨RE/DE£©í¨óÃíÆíìêä3ö//////////////////////////////////////////////////////////////////////
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_ResetBits(GPIOA,GPIO_Pin_1);//ĬèϽóêÕ×′ì¬
USART_DeInit(USART2);//¸′λ′®¿ú2
USART_InitStructure.USART_BaudRate=RS485_Baudrate;
USART_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
USART_InitStructure.USART_WordLength=USART_WordLength_8b;
USART_InitStructure.USART_StopBits=USART_StopBits_1;
USART_InitStructure.USART_Mode=USART_Mode_Rx|USART_Mode_Tx;//êÕ·¢Ä£ê½
switch(RS485_Parity)
{
case 0:USART_InitStructure.USART_Parity=USART_Parity_No;break;//ÎTD£Ñé
case 1:USART_InitStructure.USART_Parity=USART_Parity_Odd;break;//ÆæD£Ñé
case 2:USART_InitStructure.USART_Parity=USART_Parity_Even;break;//żD£Ñé
}
USART_Init(USART2,&USART_InitStructure);
USART_ClearITPendingBit(USART2,USART_IT_RXNE);
USART_ITConfig(USART2,USART_IT_RXNE,ENABLE);//ê1Äü′®¿ú2½óêÕÖD¶Ï
NVIC_InitStructure.NVIC_IRQChannel=USART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority=1;
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&NVIC_InitStructure);
USART_Cmd(USART2,ENABLE);//ê1Äü′®¿ú2
RS485_TX_EN=0;//ĬèÏÎa½óêÕÄ£ê½
Timer7_Init();//¶¨ê±Æ÷73õê¼»ˉ£¬óÃóú¼àêó¿ÕÏDê±¼ä
Modbus_RegMap();//Modbus¼Ä′æÆ÷ó3éä
}
//¶¨ê±Æ÷73õê¼»ˉ
void Timer7_Init(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM7, ENABLE); //TIM7ê±Öóê1Äü
//TIM73õê¼»ˉéèÖÃ
TIM_TimeBaseStructure.TIM_Period = RS485_Frame_Distance*10; //éèÖÃÔúÏÂò»¸ö¸üDÂê¼t×°èë»î¶ˉμÄ×Ô¶ˉÖØ×°ÔؼÄ′æÆ÷ÖüÆúμÄÖμ
TIM_TimeBaseStructure.TIM_Prescaler =7200; //éèÖÃóÃà′×÷ÎaTIMxê±ÖóÆμÂê3yêyμÄÔ¤·ÖÆμÖμ éèÖüÆêyÆμÂêÎa10kHz
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //éèÖÃê±Öó·Ö¸î:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIMÏòéϼÆêyÄ£ê½
TIM_TimeBaseInit(TIM7, &TIM_TimeBaseStructure); //¸ù¾YTIM_TimeBaseInitStructÖDÖ¸¶¨μÄ2Îêy3õê¼»ˉTIMxμÄê±¼ä»ùêyμ¥λ
TIM_ITConfig( TIM7, TIM_IT_Update, ENABLE );//TIM7 ÔêDí¸üDÂÖD¶Ï
//TIM7ÖD¶Ï·Ö×éÅäÖÃ
NVIC_InitStructure.NVIC_IRQChannel =TIM7_IRQn; //TIM7ÖD¶Ï
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2; //ÏèÕ¼óÅÏ輶2¼¶
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //′óóÅÏ輶3¼¶
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQí¨μà±»ê1Äü
NVIC_Init(&NVIC_InitStructure); //¸ù¾YNVIC_InitStructÖDÖ¸¶¨μÄ2Îêy3õê¼»ˉíaéèNVIC¼Ä′æÆ÷
}
//////////////////////////////////////////////////////////////////////////////
//·¢Ëín¸ö×Ö½úêy¾Y
//buff:·¢ËíÇøê×μØÖ·
//len£o·¢ËíμÄ×Ö½úêy
void RS485_SendData(u8 *buff,u8 len)
{
RS485_TX_EN=1;//ÇD»»Îa·¢ËíÄ£ê½
while(len--)
{
while(USART_GetFlagStatus(USART2,USART_FLAG_TXE)==RESET);//μè′y·¢ËíÇøÎa¿Õ
USART_SendData(USART2,*(buff++));
}
while(USART_GetFlagStatus(USART2,USART_FLAG_TC)==RESET);//μè′y·¢Ëííê3é
}
/////////////////////////////////////////////////////////////////////////////////////
void USART2_IRQHandler(void)//′®¿ú2ÖD¶Ï·tÎñ3ìDò
{
u8 res;
u8 err;
if(USART_GetITStatus(USART2,USART_IT_RXNE)!=RESET)
{
if(USART_GetFlagStatus(USART2,USART_FLAG_NE|USART_FLAG_FE|USART_FLAG_PE)) err=1;//¼ì2aμ½Ôëòô¡¢Ö¡′íÎó»òD£Ñé′íÎó
else err=0;
OUTPUT1=0;
res=USART_ReceiveData(USART2); //¶á½óêÕμ½μÄ×Ö½ú£¬í¬ê±Ïà1رêÖ¾×Ô¶ˉÇå3y
if((RS485_RX_CNT<2047)&&(err==0))
{
RS485_RX_BUFF[RS485_RX_CNT]=res;
RS485_RX_CNT++;
TIM_ClearITPendingBit(TIM7,TIM_IT_Update);//Çå3y¶¨ê±Æ÷òç3öÖD¶Ï
TIM_SetCounter(TIM7,0);//μ±½óêÕμ½ò»¸öDÂμÄ×Ö½ú£¬½«¶¨ê±Æ÷7¸′λÎa0£¬ÖØD¼Æê±£¨Ïàμ±óúÎ11·£©
TIM_Cmd(TIM7,ENABLE);//¿aê¼¼Æê±
}
}
}
///////////////////////////////////////////////////////////////////////////////////////
//óö¨ê±Æ÷7ÅD¶Ï½óêÕ¿ÕÏDê±¼ä£¬μ±¿ÕÏDê±¼ä′óóúÖ¸¶¨ê±¼ä£¬èÏÎaò»Ö¡½áêø
//¶¨ê±Æ÷7ÖD¶Ï·tÎñ3ìDò
void TIM7_IRQHandler(void)
{
if(TIM_GetITStatus(TIM7,TIM_IT_Update)!=RESET)
{
TIM_ClearITPendingBit(TIM7,TIM_IT_Update);//Çå3yÖD¶Ï±êÖ¾
TIM_Cmd(TIM7,DISABLE);//í£Ö1¶¨ê±Æ÷
RS485_TX_EN=1;//í£Ö1½óêÕ£¬ÇD»»Îa·¢Ëí×′ì¬
RS485_FrameFlag=1;//ÖÃλ֡½áêø±ê¼Ç
}
}
/////////////////////////////////////////////////////////////////////////////////////
//RS485·tÎñ3ìDò£¬óÃóú′|àí½óêÕμ½μÄêy¾Y(ÇëÔúÖ÷oˉêyÖDÑ-»·μ÷óÃ)
u16 startRegAddr;
u16 RegNum;
u16 calCRC;
void RS485_Service(void)
{
u16 recCRC;
if(RS485_FrameFlag==1)
{
if(RS485_RX_BUFF[0]==RS485_Addr)//μØÖ·Õyè·
{
if((RS485_RX_BUFF[1]==01)||(RS485_RX_BUFF[1]==02)||(RS485_RX_BUFF[1]==03)||(RS485_RX_BUFF[1]==05)||(RS485_RX_BUFF[1]==06)||(RS485_RX_BUFF[1]==15)||(RS485_RX_BUFF[1]==16))//1|ÄüÂëÕyè·
{
startRegAddr=(((u16)RS485_RX_BUFF[2])<<8)|RS485_RX_BUFF[3];//»ñ衼Ä′æÆ÷Æeê¼μØÖ·
if(startRegAddr<1000)//¼Ä′æÆ÷μØÖ·Ôú·¶Î§Äú
{
calCRC=CRC_Compute(RS485_RX_BUFF,RS485_RX_CNT-2);//¼ÆËãËù½óêÕêy¾YμÄCRC
recCRC=RS485_RX_BUFF[RS485_RX_CNT-1]|(((u16)RS485_RX_BUFF[RS485_RX_CNT-2])<<8);//½óêÕμ½μÄCRC(μí×Ö½úÔúÇ°£¬¸ß×Ö½úÔúoó)
if(calCRC==recCRC)//CRCD£ÑéÕyè·
{
//LED1=0;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
switch(RS485_RX_BUFF[1])//¸ù¾Y2»í¬μÄ1|ÄüÂë½øDD′|àí
{
case 2://¶áêäèë¿a1Øá¿
{
Modbus_02_Solve();
break;
}
case 1://¶áêä3ö¿a1Øá¿
{
Modbus_01_Solve();
break;
}
case 5://D′μ¥¸öêä3ö¿a1Øá¿
{
Modbus_05_Solve();
break;
}
case 15://D′¶à¸öêä3ö¿a1Øá¿
{
Modbus_15_Solve();
break;
}
case 03: //¶á¶à¸ö¼Ä′æÆ÷
{
Modbus_03_Solve();
break;
}
case 06: //D′μ¥¸ö¼Ä′æÆ÷
{
Modbus_06_Solve();
break;
}
case 16: //D′¶à¸ö¼Ä′æÆ÷
{
Modbus_16_Solve();
break;
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
}
else//CRCD£Ñé′íÎó
{
RS485_TX_BUFF[0]=RS485_RX_BUFF[0];
RS485_TX_BUFF[1]=RS485_RX_BUFF[1]|0x80;
RS485_TX_BUFF[2]=0x04; //òì3£Âë
RS485_SendData(RS485_TX_BUFF,3);
}
}
else//¼Ä′æÆ÷μØÖ·3¬3ö·¶Î§
{
RS485_TX_BUFF[0]=RS485_RX_BUFF[0];
RS485_TX_BUFF[1]=RS485_RX_BUFF[1]|0x80;
RS485_TX_BUFF[2]=0x02; //òì3£Âë
RS485_SendData(RS485_TX_BUFF,3);
}
}
else//1|ÄüÂë′íÎó
{
RS485_TX_BUFF[0]=RS485_RX_BUFF[0];
RS485_TX_BUFF[1]=RS485_RX_BUFF[1]|0x80;
RS485_TX_BUFF[2]=0x01; //òì3£Âë
RS485_SendData(RS485_TX_BUFF,3);
}
}
RS485_FrameFlag=0;//¸′λ֡½áêø±êÖ¾
RS485_RX_CNT=0;//½óêÕ¼ÆêyÆ÷Çåáã
RS485_TX_EN=0;//¿aÆô½óêÕÄ£ê½
}
}
//Modbus1|ÄüÂë02′|àí3ìDò/////////////////////////////////////////////////////3ìDòòÑÑéÖ¤OK -----±ØDëÏèÅäÖÃPE4 PE3 PE2 PA0 3õê¼»ˉ°′¼ü2Å¿éòÔOK KEY_Init();
//¶áêäèë¿a1Øá¿
void Modbus_02_Solve(void)
{
u16 ByteNum;
u16 i;
RegNum= (((u16)RS485_RX_BUFF[4])<<8)|RS485_RX_BUFF[5];//»ñ衼Ä′æÆ÷êyá¿
if((startRegAddr+RegNum)<100)//¼Ä′æÆ÷μØÖ·+êyá¿Ôú·¶Î§Äú
{
RS485_TX_BUFF[0]=RS485_RX_BUFF[0];
RS485_TX_BUFF[1]=RS485_RX_BUFF[1];
ByteNum=RegNum/8;//×Ö½úêy
if(RegNum%8) ByteNum+=1;//èç1ûλêy»1óDóàêy£¬Ôò×Ö½úêy+1
RS485_TX_BUFF[2]=ByteNum;//·μ»Øòa¶áè¡μÄ×Ö½úêy
for(i=0;i<RegNum;i++)
{
if(i%8==0) RS485_TX_BUFF[3+i/8]=0x00;
RS485_TX_BUFF[3+i/8]>>=1;//μíλÏè·¢Ëí
RS485_TX_BUFF[3+i/8]|=((*Modbus_InputIO[startRegAddr+i])<<7)&0x80;
if(i==RegNum-1)//·¢Ëíμ½×îoóò»¸öλáË
{
if(RegNum%8) RS485_TX_BUFF[3+i/8]>>=8-(RegNum%8);//èç1û×îoóò»¸ö×Ö½ú»1óDóàêy£¬Ôòê£óàMSBìî3ä0
}
}
calCRC=CRC_Compute(RS485_TX_BUFF,ByteNum+3);
RS485_TX_BUFF[ByteNum+3]=(calCRC>>8)&0xFF;
RS485_TX_BUFF[ByteNum+4]=(calCRC)&0xFF;
RS485_SendData(RS485_TX_BUFF,ByteNum+5);
}
else//¼Ä′æÆ÷μØÖ·+êyá¿3¬3ö·¶Î§
{
RS485_TX_BUFF[0]=RS485_RX_BUFF[0];
RS485_TX_BUFF[1]=RS485_RX_BUFF[1]|0x80;
RS485_TX_BUFF[2]=0x02; //òì3£Âë
RS485_SendData(RS485_TX_BUFF,3);
}
}
//Modbus1|ÄüÂë01′|àí3ìDò ///////////////////////////////////////////////////////////3ìDòòÑÑéÖ¤OK
//¶áêä3ö¿a1Øá¿
void Modbus_01_Solve(void)
{
u16 ByteNum;
u16 i;
RegNum= (((u16)RS485_RX_BUFF[4])<<8)|RS485_RX_BUFF[5];//»ñ衼Ä′æÆ÷êyá¿
if((startRegAddr+RegNum)<100)//¼Ä′æÆ÷μØÖ·+êyá¿Ôú·¶Î§Äú
{
RS485_TX_BUFF[0]=RS485_RX_BUFF[0];
RS485_TX_BUFF[1]=RS485_RX_BUFF[1];
ByteNum=RegNum/8;//×Ö½úêy
if(RegNum%8) ByteNum+=1;//èç1ûλêy»1óDóàêy£¬Ôò×Ö½úêy+1
RS485_TX_BUFF[2]=ByteNum;//·μ»Øòa¶áè¡μÄ×Ö½úêy
for(i=0;i<RegNum;i++)
{
if(i%8==0) RS485_TX_BUFF[3+i/8]=0x00;
RS485_TX_BUFF[3+i/8]>>=1;//μíλÏè·¢Ëí
RS485_TX_BUFF[3+i/8]|=((*Modbus_OutputIO[startRegAddr+i])<<7)&0x80;
if(i==RegNum-1)//·¢Ëíμ½×îoóò»¸öλáË
{
if(RegNum%8) RS485_TX_BUFF[3+i/8]>>=8-(RegNum%8);//èç1û×îoóò»¸ö×Ö½ú»1óDóàêy£¬Ôòê£óàMSBìî3ä0
}
}
calCRC=CRC_Compute(RS485_TX_BUFF,ByteNum+3);
RS485_TX_BUFF[ByteNum+3]=(calCRC>>8)&0xFF;
RS485_TX_BUFF[ByteNum+4]=(calCRC)&0xFF;
RS485_SendData(RS485_TX_BUFF,ByteNum+5);
}
else//¼Ä′æÆ÷μØÖ·+êyá¿3¬3ö·¶Î§
{
RS485_TX_BUFF[0]=RS485_RX_BUFF[0];
RS485_TX_BUFF[1]=RS485_RX_BUFF[1]|0x80;
RS485_TX_BUFF[2]=0x02; //òì3£Âë
RS485_SendData(RS485_TX_BUFF,3);
}
}
//Modbus1|ÄüÂë05′|àí3ìDò ///////////////////////////////////////////////////////3ìDòòÑÑéÖ¤OK
//D′μ¥¸öêä3ö¿a1Øá¿
void Modbus_05_Solve(void)
{
if(startRegAddr<100)//¼Ä′æÆ÷μØÖ·Ôú·¶Î§Äú
{
if((RS485_RX_BUFF[4]==0xFF)||(RS485_RX_BUFF[5]==0xFF)) *Modbus_OutputIO[startRegAddr]=0x01;
else *Modbus_OutputIO[startRegAddr]=0x00;
RS485_TX_BUFF[0]=RS485_RX_BUFF[0];
RS485_TX_BUFF[1]=RS485_RX_BUFF[1];
RS485_TX_BUFF[2]=RS485_RX_BUFF[2];
RS485_TX_BUFF[3]=RS485_RX_BUFF[3];
RS485_TX_BUFF[4]=RS485_RX_BUFF[4];
RS485_TX_BUFF[5]=RS485_RX_BUFF[5];
calCRC=CRC_Compute(RS485_TX_BUFF,6);
RS485_TX_BUFF[6]=(calCRC>>8)&0xFF;
RS485_TX_BUFF[7]=(calCRC)&0xFF;
RS485_SendData(RS485_TX_BUFF,8);
}
else//¼Ä′æÆ÷μØÖ·3¬3ö·¶Î§
{
RS485_TX_BUFF[0]=RS485_RX_BUFF[0];
RS485_TX_BUFF[1]=RS485_RX_BUFF[1]|0x80;
RS485_TX_BUFF[2]=0x02; //òì3£Âë
RS485_SendData(RS485_TX_BUFF,3);
}
}
//Modbus1|ÄüÂë15′|àí3ìDò //////////////////////////////////////////////////////3ìDòòÑÑéÖ¤OK
//D′¶à¸öêä3ö¿a1Øá¿
void Modbus_15_Solve(void)
{
u16 i;
RegNum=(((u16)RS485_RX_BUFF[4])<<8)|RS485_RX_BUFF[5];//»ñ衼Ä′æÆ÷êyá¿
if((startRegAddr+RegNum)<100)//¼Ä′æÆ÷μØÖ·+êyá¿Ôú·¶Î§Äú
{
for(i=0;i<RegNum;i++)
{
if(RS485_RX_BUFF[7+i/8]&0x01) *Modbus_OutputIO[startRegAddr+i]=0x01;
else *Modbus_OutputIO[startRegAddr+i]=0x00;
RS485_RX_BUFF[7+i/8]>>=1;//′óμíλ¿aê¼
}
RS485_TX_BUFF[0]=RS485_RX_BUFF[0];
RS485_TX_BUFF[1]=RS485_RX_BUFF[1];
RS485_TX_BUFF[2]=RS485_RX_BUFF[2];
RS485_TX_BUFF[3]=RS485_RX_BUFF[3];
RS485_TX_BUFF[4]=RS485_RX_BUFF[4];
RS485_TX_BUFF[5]=RS485_RX_BUFF[5];
calCRC=CRC_Compute(RS485_TX_BUFF,6);
RS485_TX_BUFF[6]=(calCRC>>8)&0xFF;
RS485_TX_BUFF[7]=(calCRC)&0xFF;
RS485_SendData(RS485_TX_BUFF,8);
}
else//¼Ä′æÆ÷μØÖ·+êyá¿3¬3ö·¶Î§
{
RS485_TX_BUFF[0]=RS485_RX_BUFF[0];
RS485_TX_BUFF[1]=RS485_RX_BUFF[1]|0x80;
RS485_TX_BUFF[2]=0x02; //òì3£Âë
RS485_SendData(RS485_TX_BUFF,3);
}
}
一周热门 更多>