资源简介
modbus主机源代码,经测试,非常好用,也非常容易移植到STM32、51、AVR等单片机。
代码片段和文件信息
#include “stm32f10x.h“
#include “bsp_usart.h“
#include “bsp_key.h“
#include “bsp_joystick.h“
#include “modbusmaster.h“
#include “crc16.h“
/*******************************************全局变量仅限本文件使用****************************************/
unsigned char Master_RxBuf[MASTER_RX_BUF_SIZE]={0};//主机接收缓冲区
unsigned short Master_RxCnt = 0; //接收数据计数器
unsigned char Master_RxFlag = 0;
unsigned char g_ReplyTime = 0;
unsigned char g_MasterSendData = 0;
unsigned char g_MasterWriteRequest = 0;
static unsigned short ReadReg[8];
static unsigned short WriteReg[6];
void Master_Timing(unsigned int time_tick)
{
if(g_ReplyTime > 0)g_ReplyTime ++;
if((time_tick % 25) == 0)g_MasterSendData = 1;
}
//定时器7初始化
void Timer7_Init(unsigned int ulBaudRate)
{
TIM_TimebaseInitTypeDef TIM_TimebaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
unsigned short PrescalerValue = (uint16_t) (SystemCoreClock / 20000L) - 1;
unsigned short usTimerT35_50us=0; //数据帧最小间隔(ms)超过此时间则认为是下一帧
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM7 ENABLE); //TIM7时钟使能
if( ulBaudRate > 19200 )
{
usTimerT35_50us = 35; /* 1800us. */
}
else
{
/* The timer reload value for a character is given by:
*
* ChTimeValue = Ticks_per_1s / ( Baudrate / 11 )
* = 11 * Ticks_per_1s / Baudrate
* = 220000 / Baudrate
* The reload for t3.5 is 1.5 times this value and similary
* for t3.5.
*/
usTimerT35_50us = ( 7UL * 220000UL ) / ( 2UL * ulBaudRate );
}
//TIM7初始化设置RS485_frame_Distance*10
TIM_TimebaseStructure.TIM_Period = usTimerT35_50us; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值
TIM_TimebaseStructure.TIM_Prescaler =PrescalerValue; //设置用来作为TIMx时钟频率除数的预分频值 设置计数频率为10kHz
TIM_TimebaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //设置时钟分割:TDTS = Tck_tim
TIM_TimebaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式
TIM_TimebaseInit(TIM7 &TIM_TimebaseStructure); //根据TIM_TimebaseInitStruct中指定的参数初始化TIMx的时间基数单位
TIM_ClearFlag(TIM7 TIM_FLAG_Update);
TIM_ClearITPendingBit(TIM7 TIM_IT_Update);
TIM_SetCounter(TIM70x0000);
TIM_ITConfig( TIM7TIM_IT_UpdateDISABLE);//TIM7 不允许更新中断
TIM_Cmd(TIM7 ENABLE);
//TIM7中断分组配置
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4); //配置NVIC中断优先级编组
NVIC_InitStructure.NVIC_IRQChannel =TIM7_IRQn; //TIM7中断
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //先占优先级0级
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //从优先级0级
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道被使能
NVIC_Init(&NVIC_InitStructure); //根据NVIC_InitStruct中指定的参数初始化外设NVIC寄存器
}
void Master_Init(unsigned long ulBaudRate)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOC|
RCC_APB2Periph_GPIOE|
RCC_APB2Periph_AFIOENABLE);
RCC_LSEConfig(RCC_LSE_OFF); //关闭外部低速外部时钟信号功能后,PC13
GPIO_InitStructu
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 3782 2017-03-01 23:07 Modbus\crc16.h
文件 10767 2018-05-23 14:48 Modbus\ModbusMaster.c
文件 1150 2018-05-08 16:55 Modbus\ModbusMaster.h
目录 0 2018-05-23 14:48 Modbus
----------- --------- ---------- ----- ----
15699 4
- 上一篇:使用硬连线控制器的CPU设计
- 下一篇:九章算法百度云.txt
评论
共有 条评论