1、【修改】FreeModbus主机写单个保持寄存器方法,并且测试通过;

2、【修改】FreeModbus主机定时器配置,使其操作更加严谨,保证了定时器的稳定性;
3、【修改】user_mb_app.c中保持寄存器回调函数一处书写错误;
This commit is contained in:
armink 2013-08-30 11:45:33 +08:00
parent a23f9528fb
commit 5f51ac69ae
7 changed files with 7717 additions and 948 deletions

View File

@ -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());
}
}

View File

@ -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 );
}

View File

@ -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时钟
@ -48,16 +46,11 @@ BOOL xMBMasterPortTimersInit(USHORT usTimeOut50us)
//HCLK为72MHzAPB1经过2分频为36MHz
//TIM2的时钟倍频后为72MHz硬件自动倍频,达到最大)
//TIM2的分频系数为3599时间基频率为72 / (1 + Prescaler) = 20KHz,基准为50us
//TIM最大计数值为usTim1Timerout50u
PrescalerValue = (uint16_t) (SystemCoreClock / 20000) - 1;
//定时器1初始化
usT35TimeOut50us = usTimeOut50us; //保存T35定时器计数值
//TIM最大计数值为usTim1Timerout50u
usPrescalerValue = (uint16_t) (SystemCoreClock / 20000) - 1;
//保存T35定时器计数值
usT35TimeOut50us = usTimeOut50us;
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);

View File

@ -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

File diff suppressed because it is too large Load Diff