-
大小: 5KB文件類型: .rar金幣: 2下載: 0 次發(fā)布日期: 2021-05-09
- 語言: 其他
- 標(biāo)簽: modbus主機(jī)??
資源簡介
modbus主機(jī)源代碼,經(jīng)測試,非常好用,也非常容易移植到STM32、51、AVR等單片機(jī)。

代碼片段和文件信息
#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};//主機(jī)接收緩沖區(qū)
unsigned?short?Master_RxCnt?=?0;???????????????????//接收數(shù)據(jù)計(jì)數(shù)器
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;
}
//定時(shí)器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; //數(shù)據(jù)幀最小間隔(ms)超過此時(shí)間則認(rèn)為是下一幀
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM7?ENABLE);?//TIM7時(shí)鐘使能?
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初始化設(shè)置RS485_frame_Distance*10
TIM_TimebaseStructure.TIM_Period?=?usTimerT35_50us;?//設(shè)置在下一個(gè)更新事件裝入活動的自動重裝載寄存器周期的值
TIM_TimebaseStructure.TIM_Prescaler?=PrescalerValue;?//設(shè)置用來作為TIMx時(shí)鐘頻率除數(shù)的預(yù)分頻值?設(shè)置計(jì)數(shù)頻率為10kHz
TIM_TimebaseStructure.TIM_ClockDivision?=?TIM_CKD_DIV1;?//設(shè)置時(shí)鐘分割:TDTS?=?Tck_tim
TIM_TimebaseStructure.TIM_CounterMode?=?TIM_CounterMode_Up;??//TIM向上計(jì)數(shù)模式
TIM_TimebaseInit(TIM7?&TIM_TimebaseStructure);?//根據(jù)TIM_TimebaseInitStruct中指定的參數(shù)初始化TIMx的時(shí)間基數(shù)單位
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中斷優(yōu)先級編組
NVIC_InitStructure.NVIC_IRQChannel?=TIM7_IRQn;??????????????//TIM7中斷
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority?=?0;???//先占優(yōu)先級0級
NVIC_InitStructure.NVIC_IRQChannelSubPriority?=?1;??????????//從優(yōu)先級0級
NVIC_InitStructure.NVIC_IRQChannelCmd?=?ENABLE;?????????????//IRQ通道被使能
NVIC_Init(&NVIC_InitStructure);?????????????????????????????//根據(jù)NVIC_InitStruct中指定的參數(shù)初始化外設(shè)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); //關(guān)閉外部低速外部時(shí)鐘信號功能后,PC13
GPIO_InitStructu
?屬性????????????大小?????日期????時(shí)間???名稱
-----------?---------??----------?-----??----
?????文件???????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設(shè)計(jì)
- 下一篇:九章算法百度云.txt
評論
共有 條評論