1、【修改】FreeModbus主机写单个保持寄存器方法,并且测试通过;
2、【修改】FreeModbus主机定时器配置,使其操作更加严谨,保证了定时器的稳定性; 3、【修改】user_mb_app.c中保持寄存器回调函数一处书写错误;
This commit is contained in:
parent
a23f9528fb
commit
5f51ac69ae
|
@ -32,7 +32,7 @@ struct rt_thread thread_ModbusMasterPoll;
|
|||
//******************************************************************
|
||||
void thread_entry_SysMonitor(void* parameter)
|
||||
{
|
||||
extern void vMBMasterReadHoldReg(UCHAR ucSlaveAddress, USHORT usRegAddress, USHORT ucRegValue);
|
||||
extern void vMBMasterWriteHoldReg(UCHAR ucSlaveAddress, USHORT usRegAddress, USHORT ucRegValue);
|
||||
|
||||
while (1)
|
||||
{
|
||||
|
@ -47,7 +47,7 @@ void thread_entry_SysMonitor(void* parameter)
|
|||
rt_thread_delay(DELAY_SYS_RUN_LED);
|
||||
IWDG_Feed(); //ι¹·
|
||||
//Test Modbus Master
|
||||
vMBMasterReadHoldReg(1,1,rt_tick_get()%65535);
|
||||
vMBMasterWriteHoldReg(1,3,(USHORT)rt_tick_get());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -323,14 +323,14 @@ eMBMasterPoll( void )
|
|||
}
|
||||
|
||||
//Test Modbus Master
|
||||
void vMBMasterReadHoldReg(UCHAR ucSlaveAddress, USHORT usRegAddress, USHORT ucRegValue)
|
||||
void vMBMasterWriteHoldReg(UCHAR ucSlaveAddress, USHORT usRegAddress, USHORT ucRegValue)
|
||||
{
|
||||
ucMBMasterSndAddress = ucSlaveAddress;
|
||||
pucMasterPUDSndBuf[MB_PDU_FUNC_OFF] = MB_FUNC_READ_HOLDING_REGISTER;
|
||||
pucMasterPUDSndBuf[MB_PDU_DATA_OFF + 0] = usRegAddress / 256;
|
||||
pucMasterPUDSndBuf[MB_PDU_DATA_OFF + 1] = usRegAddress % 256;
|
||||
pucMasterPUDSndBuf[MB_PDU_DATA_OFF + 3] = ucRegValue / 256;
|
||||
pucMasterPUDSndBuf[MB_PDU_DATA_OFF + 4] = ucRegValue % 256;
|
||||
pucMasterPUDSndBuf[MB_PDU_FUNC_OFF] = MB_FUNC_WRITE_REGISTER;
|
||||
pucMasterPUDSndBuf[MB_PDU_DATA_OFF + 0] = usRegAddress >> 8;
|
||||
pucMasterPUDSndBuf[MB_PDU_DATA_OFF + 1] = usRegAddress;
|
||||
pucMasterPUDSndBuf[MB_PDU_DATA_OFF + 2] = ucRegValue >> 8;
|
||||
pucMasterPUDSndBuf[MB_PDU_DATA_OFF + 3] = ucRegValue ;
|
||||
ucMasterSendPDULength = 5;
|
||||
( void ) xMBMasterPortEventPost( EV_MASTER_FRAME_SENT );
|
||||
}
|
||||
|
|
|
@ -29,6 +29,7 @@
|
|||
#if MB_MASTER_RTU_ENABLED > 0 || MB_MASTER_ASCII_ENABLED
|
||||
/* ----------------------- Variables ----------------------------------------*/
|
||||
static USHORT usT35TimeOut50us;
|
||||
static USHORT usPrescalerValue = 0;
|
||||
|
||||
/* ----------------------- static functions ---------------------------------*/
|
||||
static void prvvTIMERExpiredISR(void);
|
||||
|
@ -36,9 +37,6 @@ static void prvvTIMERExpiredISR(void);
|
|||
/* ----------------------- Start implementation -----------------------------*/
|
||||
BOOL xMBMasterPortTimersInit(USHORT usTimeOut50us)
|
||||
{
|
||||
|
||||
uint16_t PrescalerValue = 0;
|
||||
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
|
||||
NVIC_InitTypeDef NVIC_InitStructure;
|
||||
//====================================时钟初始化===========================
|
||||
//使能定时器2时钟
|
||||
|
@ -49,15 +47,10 @@ BOOL xMBMasterPortTimersInit(USHORT usTimeOut50us)
|
|||
//TIM2的时钟倍频后为72MHz(硬件自动倍频,达到最大)
|
||||
//TIM2的分频系数为3599,时间基频率为72 / (1 + Prescaler) = 20KHz,基准为50us
|
||||
//TIM最大计数值为usTim1Timerout50u
|
||||
usPrescalerValue = (uint16_t) (SystemCoreClock / 20000) - 1;
|
||||
//±£´æT35¶¨Ê±Æ÷¼ÆÊýÖµ
|
||||
usT35TimeOut50us = usTimeOut50us;
|
||||
|
||||
PrescalerValue = (uint16_t) (SystemCoreClock / 20000) - 1;
|
||||
//定时器1初始化
|
||||
usT35TimeOut50us = usTimeOut50us; //保存T35定时器计数值
|
||||
|
||||
TIM_TimeBaseStructure.TIM_Prescaler = PrescalerValue;
|
||||
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
|
||||
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
|
||||
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
|
||||
//预装载使能
|
||||
TIM_ARRPreloadConfig(TIM2, ENABLE);
|
||||
//====================================中断初始化===========================
|
||||
|
@ -79,8 +72,10 @@ BOOL xMBMasterPortTimersInit(USHORT usTimeOut50us)
|
|||
|
||||
void vMBMasterPortTimersT35Enable()
|
||||
{
|
||||
//装载计数值 基准50us
|
||||
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
|
||||
TIM_TimeBaseStructure.TIM_Prescaler = usPrescalerValue;
|
||||
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
|
||||
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
|
||||
TIM_TimeBaseStructure.TIM_Period = (uint16_t) usT35TimeOut50us;
|
||||
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
|
||||
|
||||
|
@ -92,9 +87,11 @@ void vMBMasterPortTimersT35Enable()
|
|||
|
||||
void vMBMasterPortTimersConvertDelayEnable()
|
||||
{
|
||||
//装载计数值 基准50us
|
||||
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
|
||||
TIM_TimeBaseStructure.TIM_Period = (uint16_t)(MB_MASTER_DELAY_MS_CONVERT * 1000 / 50) ;
|
||||
TIM_TimeBaseStructure.TIM_Prescaler = usPrescalerValue;
|
||||
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
|
||||
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
|
||||
TIM_TimeBaseStructure.TIM_Period = (uint16_t)(MB_MASTER_DELAY_MS_CONVERT * 1000 / 50);
|
||||
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
|
||||
|
||||
TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
|
||||
|
@ -105,8 +102,10 @@ void vMBMasterPortTimersConvertDelayEnable()
|
|||
|
||||
void vMBMasterPortTimersRespondTimeoutEnable()
|
||||
{
|
||||
//装载计数值 基准50us
|
||||
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
|
||||
TIM_TimeBaseStructure.TIM_Prescaler = usPrescalerValue;
|
||||
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
|
||||
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
|
||||
TIM_TimeBaseStructure.TIM_Period = (uint16_t)(MB_MASTER_TIMEOUT_MS_RESPOND * 1000 / 50);
|
||||
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
|
||||
|
||||
|
|
|
@ -120,7 +120,7 @@ eMBRegHoldingCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs, eMBRegi
|
|||
}
|
||||
else
|
||||
{
|
||||
pusRegHoldingBuf = usSRegInBuf;
|
||||
pusRegHoldingBuf = usSRegHoldBuf;
|
||||
REG_HOLDING_START = S_REG_INPUT_START;
|
||||
REG_HOLDING_NREGS = S_REG_INPUT_NREGS;
|
||||
usRegHoldStart = usSRegInStart;
|
||||
|
|
File diff suppressed because one or more lines are too long
File diff suppressed because it is too large
Load Diff
6667
RVMDK/JLinkLog.txt
6667
RVMDK/JLinkLog.txt
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue