Modbus

T64ByteQueue.h

#ifndef T64ByteQueue_h
#define T64ByteQueue_h
typedef struct
{
	unsigned long long t;//t64
	unsigned char byte;
}T64ByteItem;
#define DefineT64ByteQueue(fifoName,areaSize) \
typedef struct \
{\
    unsigned int head;\
    unsigned int tail;\
    T64ByteItem bytes[areaSize];\
}fifoName##Type;\
static volatile fifoName##Type fifoName;\
void fifoName##_reset(void)\
{\
    fifoName.head = fifoName.tail = 0;\
}\
void fifoName##_push(unsigned char byte,unsigned long long t)\
{\
		volatile T64ByteItem t64Byte;\
    unsigned int  pos = (fifoName.head+1)%areaSize;\
    if(pos!=fifoName.tail)\
    {/*非满状态*/\
				t64Byte.t=t;\
				t64Byte.byte=byte;\
        fifoName.bytes[fifoName.head] = t64Byte;\
        fifoName.head = pos;\
    }\
}\
volatile T64ByteItem fifoName##_pop(int *state)\
{\
	volatile T64ByteItem rtn;\
	if(fifoName.tail!=fifoName.head)\
	{/*非空状态*/\
		rtn = fifoName.bytes[fifoName.tail];\
		fifoName.tail = (fifoName.tail+1)%areaSize;\
		*state=1;\
	}\
	else\
	{\
		*state=0;\
	}\
	return rtn;\
}\
unsigned int fifoName##_bytesLen(void)\
{\
    return (fifoName.head+areaSize-fifoName.tail)%areaSize;\
}\
unsigned int fifoName##_size(void)\
{\
    return areaSize;\
}
#define ExportT64ByteQueue_byName(fifoName) \
extern void fifoName##_reset(void);\
extern void fifoName##_push(unsigned char byte,unsigned long long t);\
extern volatile T64ByteItem fifoName##_pop(int *state);\
extern unsigned short fifoName##_bytesLen(void);\
extern unsigned short fifoName##_size(void);
#endif

Modbus.h

#ifndef Modbus_h
#define Modbus_h
typedef struct
{
	struct
	{
		unsigned char id;
	}attribute;
	struct
	{
		unsigned char rxBytes[256];
		unsigned int rxCount;
		unsigned int rxLen;
		unsigned char txBytes[256];
		unsigned int txCount;
		unsigned int txLen;
	}msg;
	struct
	{
		unsigned int (*msgProcess)(void *cThis,unsigned char *rxBytes,unsigned int rxLen,unsigned char *txBytes);//返回发送的长度
		void (*triggerInterruptSend)(void *cThis);
	}event;
	unsigned long long (*GetUS)();
	int(*printf)(const char* format, ...);
}Modubs;
#include"T64ByteQueue.h"
extern void ModbusRun(Modubs *modbus,volatile T64ByteItem (*pop)(int *state));
#endif

Modbus.c

#include"Modbus.h"
void ModbusRun(Modubs *modbus,volatile T64ByteItem (*pop)(int *state))
{
	int state;
	static T64ByteItem oldTemp={0,0};
	T64ByteItem temp={0,0};
	if(pop)
	{
		temp=pop(&state);
		if(state)
		{//有接收的数据
			if((temp.t-oldTemp.t)>3*1000)//2个字节之间相差了3ms
			{
				modbus->msg.rxLen=modbus->msg.rxCount;
				if(modbus->msg.rxLen)
				{
					unsigned int txLen;
					modbus->printf("outTime1");
					if(modbus->event.msgProcess)
					{
						txLen=modbus->event.msgProcess(modbus,modbus->msg.rxBytes,modbus->msg.rxLen,modbus->msg.txBytes);
						modbus->msg.rxCount=0;
						modbus->msg.rxLen=0;
						if(modbus->msg.txLen==0)
						{
							modbus->msg.txLen=txLen;
							if(modbus->event.triggerInterruptSend)
							{
								modbus->event.triggerInterruptSend(modbus);
							}
						}
						else
						{//发送忙
						}
					}
					else
					{//无msgProcess
					}
				}
				else
				{//无需要处理的数据
				}
			}
			if(modbus->msg.rxCount<sizeof(modbus->msg.rxBytes))
			{//存入接收的数据
				modbus->msg.rxBytes[modbus->msg.rxCount++]=temp.byte;
				oldTemp=temp;
			}
		}
		else
		{//无接收的数据
			if((modbus->GetUS()-oldTemp.t)>3*1000)//当前时间与最后接收的1个字节相差了3毫秒
			{
				modbus->msg.rxLen=modbus->msg.rxCount;
				if(modbus->msg.rxLen)
				{
					unsigned int txLen;
					modbus->printf("outTime2");
					if(modbus->event.msgProcess)
					{
						txLen=modbus->event.msgProcess(modbus,modbus->msg.rxBytes,modbus->msg.rxLen,modbus->msg.txBytes);
						modbus->msg.rxCount=0;
						modbus->msg.rxLen=0;
						if(modbus->msg.txLen==0)
						{
							modbus->msg.txLen=txLen;
							if(modbus->event.triggerInterruptSend)
							{
								modbus->event.triggerInterruptSend(modbus);
							}
						}
						else
						{//发送忙
						}
					}
					else
					{//无msgProcess
					}
				}
			}
		}
	}
}

modbus_msgProcess.h

#ifndef modbus_msgProcess_h
#define modbus_msgProcess_h
extern unsigned int modbus_msgProcess(void *cThis,unsigned char *rxBytes,unsigned int rxLen,unsigned char *txBytes);
#endif

modbus_msgProcess.c

#include"modbus_msgProcess.h"
#include"Modbus.h"
static unsigned short modbus_crc16(unsigned char *ptr, int len);
typedef enum
{//ME是modbus error的缩写
	ME_FunctionCode=0x01,	//功能码错误
	ME_DataAddr=0x02,			//数据地址错误
	ME_DataValue=0x03,		//数据值错误
	ME_DeviceFault=0x04 	//设备故障
}ModbusErrorCode;
static int ReturnError(void *cThis,unsigned char* txBytes,ModbusErrorCode code)
{
	Modubs *obj=cThis;
	unsigned short int crc16;
	int i=0;
	txBytes[i++]=obj->attribute.id;
	txBytes[i++]=0x84;
	txBytes[i++]=code;//非法错误码
	//添加CRC16
	crc16=modbus_crc16(txBytes,i);
	txBytes[i++]=crc16&0xFF;
	txBytes[i++]=crc16>>8;
	return i;
}
static int FunctionCode_0x01(void *cThis,unsigned char *rxBytes,unsigned int rxLen,unsigned char *txBytes)
{//0x01读线圈/离散量的输出状态
	Modubs *obj=cThis;
	unsigned short int addr;//寄存器地址
	unsigned short int len;//寄存器数目
	int i,j;
	unsigned short int *p;
	unsigned short int crc16;
	addr=rxBytes[0]<<8|rxBytes[1];//寄存器地址
	len=rxBytes[2]<<8|rxBytes[3]; //寄存器数目
	{
		i=0;
		txBytes[i++]=obj->attribute.id;		//响应的ID
		txBytes[i++]=0x01;								//响应的功能码
		txBytes[i++]=(len/8)+(len%8?1:0);	//数据字节数
		for(j=0;j<txBytes[2];j++)
		{//填充响应的数据
			txBytes[i++]=0x55;
		}
		crc16=modbus_crc16(txBytes,i);
		txBytes[i++]=crc16&0xFF;
		txBytes[i++]=crc16>>8;
	}
	return i;
}
static int FunctionCode_0x02(void *cThis,unsigned char *rxBytes,unsigned int rxLen,unsigned char *txBytes)
{//0x02读离散输入状态
	Modubs *obj=cThis;
	unsigned short int addr;//寄存器地址
	unsigned short int len;//寄存器数目
	int i,j;
	unsigned short int *p;
	unsigned short int crc16;
	addr=rxBytes[0]<<8|rxBytes[1];//寄存器地址
	len=rxBytes[2]<<8|rxBytes[3]; //寄存器数目
	{
		i=0;
		txBytes[i++]=obj->attribute.id;	//响应的ID
		txBytes[i++]=0x02;							//响应的功能码
		txBytes[i++]=(len/8)+(len%8?1:0);							//数据字节数
		for(j=0;j<txBytes[2];j++)
		{//填充响应的数据
			txBytes[i++]=0xAA;
		}
		crc16=modbus_crc16(txBytes,i);
		txBytes[i++]=crc16&0xFF;
		txBytes[i++]=crc16>>8;
	}
	return i;
}

static int FunctionCode_0x03(void *cThis,unsigned char *rxBytes,unsigned int rxLen,unsigned char *txBytes)
{//0x03读多个保持寄存器
	Modubs *obj=cThis;
	unsigned short int addr;//寄存器地址
	unsigned short int len;//寄存器数目
	int i,j;
	unsigned short int *p;
	unsigned short int crc16;
	addr=rxBytes[0]<<8|rxBytes[1];//寄存器地址
	len=rxBytes[2]<<8|rxBytes[3]; //寄存器数目
	{//响应
		i=0;
		txBytes[i++]=obj->attribute.id;	//响应的ID
		txBytes[i++]=0x03;							//响应的功能码
		txBytes[i++]=len*2;							//响应的字节数
		for(j=0;j<len;j++)
		{//填充响应的数据
			txBytes[i++]=(j>>8);
			txBytes[i++]=(j&0xFF)+2;
		}
		crc16=modbus_crc16(txBytes,i);
		txBytes[i++]=crc16&0xFF;
		txBytes[i++]=crc16>>8;
	}
	return i;
}
static int FunctionCode_0x04(void *cThis,unsigned char *rxBytes,unsigned int rxLen,unsigned char *txBytes)
{//0x04读输入寄存器
	Modubs *obj=cThis;
	unsigned short int addr;//寄存器地址
	unsigned short int len;//寄存器数目
	int i,j;
	unsigned short int *p;
	unsigned short int crc16;
	addr=rxBytes[0]<<8|rxBytes[1];//寄存器地址
	len=rxBytes[2]<<8|rxBytes[3]; //寄存器数目
	{//响应
		i=0;
		txBytes[i++]=obj->attribute.id;	//响应的ID
		txBytes[i++]=0x04;							//响应的功能码
		txBytes[i++]=len*2;							//响应的字节数
		for(j=0;j<len;j++)
		{//填充响应的数据
			txBytes[i++]=(j>>8);
			txBytes[i++]=(j&0xFF)+2;
		}
		crc16=modbus_crc16(txBytes,i);
		txBytes[i++]=crc16&0xFF;
		txBytes[i++]=crc16>>8;
	}
	return i;
}
static int FunctionCode_0x05(void *cThis,unsigned char *rxBytes,unsigned int rxLen,unsigned char *txBytes)
{//0x05写单个线圈
	Modubs *obj=cThis;
	unsigned short int addr;			//寄存器地址
	unsigned short int changeData;//寄存器数目
	int i=0,j,k;
	unsigned short int crc16;
	addr=rxBytes[0]<<8|rxBytes[1];			//寄存器地址
	{
		changeData=rxBytes[2]<<8|rxBytes[3];//变更数据
	}
	{//响应报文
		i=0;
		txBytes[i++]=obj->attribute.id;	//响应的ID
		txBytes[i++]=0x05;							//响应的功能码
		txBytes[i++]=addr>>8;			
		txBytes[i++]=addr&0xFF;	
		{//响应的数据
			txBytes[i++]=changeData>>8;			
			txBytes[i++]=changeData&0xFF;	
		}
		//添加CRC8
		crc16=modbus_crc16(txBytes,i);
		txBytes[i++]=crc16&0xFF;
		txBytes[i++]=crc16>>8;
	}
	return i;
}
static int FunctionCode_0x06(void *cThis,unsigned char *rxBytes,unsigned int rxLen,unsigned char *txBytes)
{//0x06写单个保持寄存器
	Modubs *obj=cThis;
	unsigned short int addr;			//寄存器地址
	unsigned short int changeData;//寄存器数目
	int i=0,j,k;
	unsigned short int crc16;
	addr=rxBytes[0]<<8|rxBytes[1];			//寄存器地址
	{
		changeData=rxBytes[2]<<8|rxBytes[3];//变更数据
	}
	{//响应报文
		i=0;
		txBytes[i++]=obj->attribute.id;	//响应的ID
		txBytes[i++]=0x06;							//响应的功能码
		txBytes[i++]=addr>>8;			
		txBytes[i++]=addr&0xFF;	
		{//响应的数据
			txBytes[i++]=changeData>>8;			
			txBytes[i++]=changeData&0xFF;	
		}
		//添加CRC16
		crc16=modbus_crc16(txBytes,i);
		txBytes[i++]=crc16&0xFF;
		txBytes[i++]=crc16>>8;
	}
	return i;
}
static int FunctionCode_0x0F(void *cThis,unsigned char *rxBytes,unsigned int rxLen,unsigned char *txBytes)
{//0x0F写多个线圈
	Modubs *obj=cThis;
	unsigned short int addr;//寄存器地址
	unsigned short int len;//寄存器数目
	unsigned char bytesNum;//字节数
	int i=0,j,k;
	unsigned short int *p;
	unsigned short int crc16;
	addr=rxBytes[0]<<8|rxBytes[1];//寄存器地址
	len=rxBytes[2]<<8|rxBytes[3];	//寄存器数目
	bytesNum=rxBytes[4];//字节数
	p=(unsigned short int*)&rxBytes[5];
	{
		for(j=0;j<len;j++)
		{
			
		}
	}
	{//响应报文
		i=0;
		txBytes[i++]=obj->attribute.id;	//响应的ID
		txBytes[i++]=0x0F;							//响应的功能码
		txBytes[i++]=addr>>8;
		txBytes[i++]=addr&0xFF;
		txBytes[i++]=len>>8;
		txBytes[i++]=len&0xFF;
		//添加CRC16
		crc16=modbus_crc16(txBytes,i);
		txBytes[i++]=crc16&0xFF;
		txBytes[i++]=crc16>>8;
	}
	return i;
}
static int FunctionCode_0x10(void *cThis,unsigned char *rxBytes,unsigned int rxLen,unsigned char *txBytes)
{//0x10写多个保持寄存器
	Modubs *obj=cThis;
	unsigned short int addr;//寄存器地址
	unsigned short int len;//寄存器数目
	unsigned char bytesNum;//字节数
	int i=0,j,k;
	unsigned short int *p;
	unsigned short int crc16;
	addr=rxBytes[0]<<8|rxBytes[1];//寄存器地址
	len=rxBytes[2]<<8|rxBytes[3];	//寄存器数目
	bytesNum=rxBytes[4];//字节数
	p=(unsigned short int*)&rxBytes[5];
	{
		for(j=0;j<len;j++)
		{
			
		}
	}
	{//响应报文
		i=0;
		txBytes[i++]=obj->attribute.id;	//响应的ID
		txBytes[i++]=0x10;							//响应的功能码
		txBytes[i++]=addr>>8;
		txBytes[i++]=addr&0xFF;
		txBytes[i++]=len>>8;
		txBytes[i++]=len&0xFF;
		//添加CRC16
		crc16=modbus_crc16(txBytes,i);
		txBytes[i++]=crc16&0xFF;
		txBytes[i++]=crc16>>8;
	}
	return i;
}
static int FunctionCode(void *cThis,unsigned char fCode,unsigned char *rxBytes,unsigned int rxLen,unsigned char *txBytes)
{
	Modubs *obj=cThis;
	obj->printf("\r\nFunction Code:0x%.2X",fCode);
	switch(fCode)
	{
		case 0x01:
		{//0x01读线圈/离散量的输出状态
			return FunctionCode_0x01(cThis,rxBytes,rxLen,txBytes);
		}
		case 0x02:
		{//0x02读离散输入状态
			return FunctionCode_0x02(cThis,rxBytes,rxLen,txBytes);
		}
		case 0x03:
		{//0x03读多个保持寄存器
			return FunctionCode_0x03(cThis,rxBytes,rxLen,txBytes);
		}
		case 0x04:
		{//0x04读输入寄存器
			return FunctionCode_0x04(cThis,rxBytes,rxLen,txBytes);
		}
		case 0x05:
		{//0x05写单个线圈
			return FunctionCode_0x05(cThis,rxBytes,rxLen,txBytes);
		}
		case 0x06:
		{//0x06写单个保持寄存器
			return FunctionCode_0x06(cThis,rxBytes,rxLen,txBytes);
		}
		case 0x0F:
		{//0x0F写多个线圈
			return FunctionCode_0x0F(cThis,rxBytes,rxLen,txBytes);
		}
		case 0x10:
		{//0x10写多个保持寄存器
			return FunctionCode_0x10(cThis,rxBytes,rxLen,txBytes);
		}
		default:
		{//错误
			return ReturnError(cThis,txBytes,ME_FunctionCode);
		}
	}
	return 0;
}
static unsigned short modbus_crc16(unsigned char *ptr, int len)
{
    unsigned int i;
    unsigned short crc = 0xFFFF;
    while(len--)
    {
        crc ^= *ptr++;
        for (i = 0; i < 8; ++i)
        {
            if (crc & 1)
                crc = (crc >> 1) ^ 0xA001;
            else
                crc = (crc >> 1);
        }
    }
    
    return crc;
}
unsigned int modbus_msgProcess(void *cThis,unsigned char *rxBytes,unsigned int rxLen,unsigned char *txBytes)
{
	Modubs *obj=cThis;
	if(obj->attribute.id==rxBytes[0])
	{//是自己的ID
		//obj->printf("\r\n rxLen:%d crc:0x%.4X",rxLen,crc16(rxBytes,rxLen));
		if(modbus_crc16(rxBytes,rxLen)==0x00)
		{//crc校验通过
			return FunctionCode(cThis,rxBytes[1]/*功能码*/,&rxBytes[2],rxLen-4,txBytes);
		}
		else
		{//crc校验未通过
			return 0;
		}
	}
	else
	{//不是自己的ID
		return 0;
	}
}

ModbusStart.c

#include"Modbus.h"
#include"XCOSnTh.h"
#include "at32f435_437.h"
#include"modbus_msgProcess.h"
static void triggerInterruptSend(void *cThis)
{
	usart_interrupt_enable(USART2,USART_TDBE_INT, TRUE);
}
Modubs modbus1={0};
static void ModbusInit(AutoCallObj obj,void *cThis)
{
	modbus1.attribute.id=0x01;
	modbus1.GetUS=ShellRegisterFormFind("User","GetCountForUS");
	modbus1.event.msgProcess=modbus_msgProcess;
	modbus1.event.triggerInterruptSend=triggerInterruptSend;
	modbus1.printf=obj->printf;
}
AutoCall_InitDef(1000,V_ModbusInit,ModbusInit,"");
ExportT64ByteQueue_byName(modubs1);
static void ModbusWhile(AutoCallObj obj,void *cThis)
{
	ModbusRun(&modbus1,modubs1_pop);
}
AutoCall_WhileDef(1,V_ModbusWhile,ModbusWhile,"");

Timer_1us.c

#include "at32f435_437.h"
#if(0)
void led_config(void)
{
  gpio_init_type gpio_init_struct;

	/* 使能对应端口的时钟 */
  crm_periph_clock_enable(CRM_GPIOA_PERIPH_CLOCK, TRUE);  
	/* 默认填充配置 */
  gpio_default_para_init(&gpio_init_struct);

	/* 设置 GPIO 驱动能力较大电流推动/吸入能力 */
  gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER; 
	/* 设置 GPIO 输出类型为推挽输出模式 */
  gpio_init_struct.gpio_out_type  = GPIO_OUTPUT_PUSH_PULL;
	/* 设置 GPIO 模式为输出模式 */
  gpio_init_struct.gpio_mode = GPIO_MODE_OUTPUT;
	/* 配置 GPIO 引脚 LED2引脚定义为GPIO_PINS_3 */
  gpio_init_struct.gpio_pins = GPIO_PINS_0;
	/* 设置 GPIO 无上下拉模式 */
  gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
	/* 初始化 GPIO 外设 */
  gpio_init(GPIOA, &gpio_init_struct);
}
#endif
#include"XCOSnTh.h"
void basic_tmr7_config(void)
{
	crm_clocks_freq_type crm_clocks_freq_struct = {0};
	/* 返回片上不同的时钟频率 */
  crm_clocks_freq_get(&crm_clocks_freq_struct);

	/* 使能对应端口的时钟 */
  crm_periph_clock_enable(CRM_TMR7_PERIPH_CLOCK, TRUE);

	/* 初始化 TMR 周期、分频 */
  tmr_base_init(TMR7, 1, (crm_clocks_freq_struct.apb2_freq/1000/1000) - 1);//1MHz
	/* 设置 TMR 计数器计数方向 */
  tmr_cnt_dir_set(TMR7, TMR_COUNT_UP);

	/* 启用 TMR 中断 */
  tmr_interrupt_enable(TMR7, TMR_OVF_INT, TRUE);
  /* 中断优先级分组配置 */
  //nvic_priority_group_config(NVIC_PRIORITY_GROUP_4);
	/* 中断使能及优先级配置 */
  nvic_irq_enable(TMR7_GLOBAL_IRQn, 0, 0);

	/* 启用或禁用 TMR 计数器 */
  tmr_counter_enable(TMR7, TRUE);
}
static volatile unsigned long long uscount=0;
unsigned long long GetCountForUS()
{
	return uscount;
}
RegisterItemDef(User,GetCountForUS, "Cmd serial Rx", GetCountForUS);
void TMR7_GLOBAL_IRQHandler(void)  
{
	
  if(tmr_flag_get(TMR7, TMR_OVF_FLAG) == SET)
  {
		uscount++;
		//GPIOA->odt^=GPIO_PINS_0;
    tmr_flag_clear(TMR7, TMR_OVF_FLAG);
  }
}

static void basic_tmr7Init(AutoCallObj obj,void *cThis)
{
	//led_config();
	basic_tmr7_config();
	obj->printf("\r\nsiezof long long:%d Byte",sizeof(long long));
}
AutoCall_InitDef(1,V_basic_tmr7Init,basic_tmr7Init,"");

ModbusSerial.c

#include "at32f435_437.h"
#include"T64ByteQueue.h"
#include"Modbus.h"
extern Modubs modbus1;
DefineT64ByteQueue(modubs1,257);//定义modbus接收的FIFO

static unsigned long long (*GetUS)()=0;
void ModbusSerialCfg(int baud_rate)
{
	gpio_init_type gpio_init_struct;
	
	/* 使能对应端口的时钟 */
  crm_periph_clock_enable(CRM_USART2_PERIPH_CLOCK, TRUE);
	crm_periph_clock_enable(CRM_GPIOA_PERIPH_CLOCK, TRUE);

	/* 默认填充配置 */
  gpio_default_para_init(&gpio_init_struct);
	
	/* 设置 GPIO 驱动能力较大电流推动/吸入能力 */
  gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
	/* 设置 GPIO 输出类型为推挽输出模式 */
  gpio_init_struct.gpio_out_type  = GPIO_OUTPUT_PUSH_PULL;
	/* 设置 GPIO 模式为复用模式 */
  gpio_init_struct.gpio_mode = GPIO_MODE_MUX;
	/* 配置 GPIO 引脚 */
  gpio_init_struct.gpio_pins = GPIO_PINS_2;//Tx PA2
	/* 设置 GPIO 无上下拉模式 */
  gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
	/* 初始化 GPIO 外设 */
  gpio_init(GPIOA, &gpio_init_struct);

  gpio_init_struct.gpio_pins = GPIO_PINS_3;//Rx PA3
  gpio_init(GPIOA, &gpio_init_struct);
	
	/* 配置引脚复用功能 */
	gpio_pin_mux_config(GPIOA, GPIO_PINS_SOURCE2, GPIO_MUX_7);
	gpio_pin_mux_config(GPIOA, GPIO_PINS_SOURCE3, GPIO_MUX_7);
	
	/* 波特率 数据位 停止位配置 */
  usart_init(USART2, baud_rate, USART_DATA_8BITS, USART_STOP_1_BIT);
	
	/* 中断使能及优先级配置 */
  nvic_irq_enable(USART2_IRQn, 0,1);

  /* 发送使能 */
  usart_transmitter_enable(USART2, TRUE);
	/* 接收使能 */
  usart_receiver_enable(USART2, TRUE);
  /* 中断使能 */	
	usart_interrupt_enable(USART2,USART_RDBF_INT, TRUE);
	//usart_interrupt_enable(USART2,USART_TDC_INT, TRUE);
	//usart_interrupt_enable(USART2,USART_TDBE_INT, TRUE);
  /* 串口使能 */
  usart_enable(USART2, TRUE);
}
void USART2_IRQHandler()
{
	uint16_t temp;
	if(usart_flag_get(USART2, USART_RDBF_FLAG) != RESET)
  {
    /* read one byte from the receive data register */
    temp = usart_data_receive(USART2);
		if(GetUS)
		{
			modubs1_push(temp,GetUS());//将接收的数据与接收数据的时间压入到FIFO中
		}
		else
		{
			usart_data_transmit(USART2,temp);
		}
  }
	if(usart_flag_get(USART2, USART_TDBE_FLAG) != RESET)
	{//USART_TDBE_FLAG
		if(modbus1.msg.txCount<modbus1.msg.txLen)
		{
			usart_flag_clear(USART2,USART_TDBE_FLAG);
			usart_data_transmit(USART2,modbus1.msg.txBytes[modbus1.msg.txCount++]);
		}
		else
		{
			modbus1.msg.txCount=0;
			modbus1.msg.txLen=0;
			usart_interrupt_enable(USART2,USART_TDBE_INT, FALSE);//关闭TDBE中断
			usart_flag_clear(USART2,USART_TDBE_FLAG);
		}
	} 
	if(usart_flag_get(USART2, USART_TDC_FLAG) != RESET)
	{
		usart_flag_clear(USART2,USART_TDC_FLAG);
	}
}
#include"XCOSnTh.h"
static void ModbusSerialInit(AutoCallObj obj,void *cThis)
{
	GetUS=ShellRegisterFormFind("User","GetCountForUS");
	ModbusSerialCfg(115200);
}
AutoCall_InitDef(1000,V_ModbusSerialInit,ModbusSerialInit,"");

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值