/*************************************************************************
Title: Yet another (small) modbus implementation for the avr.
Author: Max Brueggemann
Hardware: any AVR with hardware UART, tested on Atmega 88/168 at 20Mhz
License: GNU General Public License
DESCRIPTION:
Refer to the header file yaMBSiavr.h.
USAGE:
Refer to the header file yaMBSiavr.h.
LICENSE:
Copyright (C) 2016 Max Brueggemann
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
*************************************************************************/
#include <avr/io.h>
#include "yaMBSiavr.h"
#include <avr/interrupt.h>
volatile unsigned char BusState = 0;
volatile uint16_t modbusTimer = 0;
volatile unsigned char rxbuffer[MaxFrameIndex+1];
volatile uint16_t DataPos = 0;
volatile unsigned char PacketTopIndex = 7;
volatile unsigned char modBusStaMaStates = 0;
uint8_t modbusGetBusState(void) {
return BusState;
}
#if ADDRESS_MODE == SINGLE_ADR
volatile unsigned char Address = 0x00;
uint8_t modbusGetAddress(void) {
return Address;
}
void modbusSetAddress(unsigned char newadr) {
Address = newadr;
}
#endif
#if PHYSICAL_TYPE == 485
void transceiver_txen(void) {
//TRANSCEIVER_ENABLE_PORT|=(1<<TRANSCEIVER_ENABLE_PIN);
PORTD |= 0x10;
}
void transceiver_rxen(void) {
//TRANSCEIVER_ENABLE_PORT&=~(1<<TRANSCEIVER_ENABLE_PIN);
PORTD &= 0xEF;
}
#endif
/* A fairly simple Modbus compliant 16 Bit CRC algorithm.
* Returns 1 if the crc check is positive, returns 0 if it fails.
* Appends
*/
uint16_t crc16(volatile uint8_t *ptrToArray,uint8_t inputSize) //A standard CRC algorithm
{
uint16_t out=0xffff;
uint16_t carry;
unsigned char n;
inputSize++;
for (int l=0; l<inputSize; l++) {
out ^= ptrToArray[l];
for (n = 0; n < 8; n++) {
carry = out & 1;
out >>= 1;
if (carry) out ^= 0xA001;
}
}
//out=0x1234;
if ((ptrToArray[inputSize]==out%256) && (ptrToArray[inputSize+1]==out/256)) //check
{
return 1;
} else {
ptrToArray[inputSize]=out%256; //append Lo
ptrToArray[inputSize+1]=out/256; //append Hi
return 0;
}
}
/* @brief: copies a single or multiple words from one array of bytes to another array of bytes
* amount must not be bigger than 255...
*
*/
void listRegisterCopy(volatile uint8_t *source, volatile uint8_t *target, uint8_t amount) {
for (uint8_t c=0; c<amount; c++)
{
*(target+c)=*(source+c);
}
}
/* @brief: copies a single bit from one char to another char (or arrays thereof)
*
*
*/
void listBitCopy(volatile uint8_t *source, uint16_t sourceNr,volatile uint8_t *target, uint16_t targetNr) {
if(*(source+(sourceNr/8))&(1<<(sourceNr-((sourceNr/8)*8))))
{
*(target+(targetNr/8))|=(1<<(targetNr-((targetNr/8)*8)));
} else *(target+(targetNr/8))&=~(1<<(targetNr-((targetNr/8)*8)));
}
void modbusReset(void) {
BusState=(1<<TimerActive); //stop receiving (error)
modbusTimer=0;
}
void modbusTickTimer(void) {
if (BusState&(1<<TimerActive)) {
modbusTimer++;
if (BusState&(1<<Receiving)) //we are in receiving mode
{
if ((modbusTimer==modbusInterCharTimeout)) {
BusState|=(1<<GapDetected);
} else if ((modbusTimer==modbusInterFrameDelayReceiveEnd)) { //end of message
#if ADDRESS_MODE == MULTIPLE_ADR
if (crc16(rxbuffer,DataPos-3)) { //perform crc check only. This is for multiple/all address mode.
BusState=(1<<ReceiveCompleted);
} else modbusReset();
#endif
#if ADDRESS_MODE == SINGLE_ADR
if (rxbuffer[0]==Address && crc16(rxbuffer,DataPos-3)) { //is the message for us? => perform crc check
BusState=(1<<ReceiveCompleted);
} else modbusReset();
#endif
}
} else if (modbusTimer==modbusInterFrameDelayReceiveStart) BusState|=(1<<BusTimedOut);
}
}
ISR(UART_RECEIVE_INTERRUPT) {
unsigned char data;
data = UART_DATA;
modbusTimer=0; //reset timer
if (!(BusState & (1<<ReceiveCompleted)) && !(BusState & (1<<TransmitRequested)) && !(BusState & (1<<Transmitting)) && (BusState & (1<<Receiving)) && !(BusState & (1<<BusTimedOut))) { // //
if (DataPos>MaxFrameIndex) modbusReset();
else {
rxbuffer[DataPos]=data;
DataPos++; //TODO: maybe prevent this from exceeding 255?
}
} else if (!(BusState & (1<<ReceiveCompleted)) && !(BusState & (1<<TransmitRequested)) && !(BusState & (1<<Transmitting)) && !(BusState & (1<<Receiving)) && (BusState & (1<<BusTimedOut))) { //) {
rxbuffer[0]=data;
BusState=((1<<Receiving)|(1<<TimerActive));
DataPos=1;
}
PORTC |= (1 << 4);
}
ISR(UART_TRANSMIT_INTERRUPT) {
BusState&=~(1<<TransmitRequested);
BusState|=(1<<Transmitting);
UART_DATA=rxbuffer[DataPos];
DataPos++;
if (DataPos==(PacketTopIndex+1)) {
UART_CONTROL&=~(1<<UART_UDRIE);
}
PORTC &= ~(1 << 4);
}
ISR(UART_TRANSMIT_COMPLETE_INTERRUPT) { //Sobald Stopbit übertragen wurde RS485-Transceiver wieder auf empfangen umstellen
#if PHYSICAL_TYPE == 485
transceiver_rxen();
#endif
modbusReset();
}
void modbusInit(void)
{
// UBRRH = (unsigned char)(Baud>>8);
// UBRRL = (unsigned char)Baud;
UBRRL = 12;
UART_CONTROL = (1<<RXCIE)|(1<<RXEN)|(1<<TXEN); // USART receiver and transmitter and receive complete interrupt
UART_STATUS |= (0<<U2X); //double speed mode.
UCSRC = (1<<URSEL)|(3<<UCSZ0); //Frame Size //for other
//UCSRC = (3<<UCSZ0); //Frame Size //for mega128
UART_CONTROL|=(1<<TXCIE); //Transmit Complete Interrupt an
#if PHYSICAL_TYPE == 485
//TRANSCEIVER_ENABLE_PORT_DDR|=(1<<TRANSCEIVER_ENABLE_PIN);
DDRD |= 0x10;
transceiver_rxen();
#endif
BusState=(1<<TimerActive);
}
/* @brief: Sends a response.
*
* Arguments: - packtop: Position of the last byte containing data.
* modbusSendException is a good usage example.
*/
void modbusSendMessage(unsigned char packtop) {
PacketTopIndex=packtop+2; //diff: +2
crc16(rxbuffer,packtop); //diff: alt: -1, neu +1
BusState|=(1<<TransmitRequested);
DataPos=0;
#if PHYSICAL_TYPE == 485
transceiver_txen();
#endif
UART_CONTROL|=(1<<UART_UDRIE);
BusState&=~(1<<ReceiveCompleted);
}
void modbusSendException(unsigned char exceptionCode) {
rxbuffer[1]|=(1<<7); //setting MSB of the function code (the exception flag)
rxbuffer[2]=exceptionCode; //Exceptioncode. Also the last byte containing data
modbusSendMessage(2);
}
uint16_t modbusRequestedAmount(void) {
return (rxbuffer[5]|(rxbuffer[4]<<8));
}
uint16_t modbusRequestedAddress(void) {
return (rxbuffer[3]|(rxbuffer[2]<<8));
}
/* @brief: copies a single or multiple bytes from one array of bytes to an array of 16-bit-words
*
*
*/
void intToModbusRegister(volatile uint16_t *inreg, volatile uint8_t *outreg, uint8_t amount) {
for (uint8_t c=0; c<amount; c++)
{
*(outreg+c*2) = (uint8_t)(*(inreg+c) >> 8);
*(outreg+1+c*2) = (uint8_t)(*(inreg+c));
}
}
/* @brief: copies a single or multiple 16-bit-words from one array of integers to an array of bytes
*
*
*/
void modbusRegisterToInt(volatile uint8_t *inreg, volatile uint16_t *outreg, uint8_t amount) {
for (uint8_t c=0; c<amount; c++)
{
*(outreg+c) = (*(inreg+c*2) << 8) + *(inreg+1+c*2);
}
}
/* @brief: Handles single/multiple register reading and single/multiple register writing.
*
* Arguments: - ptrToInArray: pointer to the user's data array containing registers
* - startAddress: address of the first register in the supplied array
* - size: input array size in the requested format (16bit-registers)
*
*/
uint8_t mod
没有合适的资源?快使用搜索试试~ 我知道了~
modbus-TC.zip_avr modbus_gettingqdd_modbus_zip
![preview](https://csdnimg.cn/release/downloadcmsfe/public/img/white-bg.ca8570fa.png)
共107个文件
hsc:52个
blb:8个
xml:7个
![preview-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/scale.ab9e0183.png)
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 156 浏览量
2022-09-22
14:33:03
上传
评论
收藏 547KB ZIP 举报
温馨提示
avr implement modbus
资源推荐
资源详情
资源评论
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
收起资源包目录
![package](https://csdnimg.cn/release/downloadcmsfe/public/img/package.f3fc750b.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/BMP.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
共 107 条
- 1
- 2
资源评论
![avatar-default](https://csdnimg.cn/release/downloadcmsfe/public/img/lazyLogo2.1882d7f4.png)
![avatar](https://profile-avatar.csdnimg.cn/5f02f331e1ea4222a10b21da48ddddbe_weixin_42651748.jpg!1)
JonSco
- 粉丝: 77
- 资源: 1万+
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助
![voice](https://csdnimg.cn/release/downloadcmsfe/public/img/voice.245cc511.png)
![center-task](https://csdnimg.cn/release/downloadcmsfe/public/img/center-task.c2eda91a.png)
最新资源
- 冬天就快来了,n 只鼠鼠偷了一大堆奶酪放到老鼠洞里,约定第二天平分 这些鼠鼠很崇拜鼠王杰瑞,所以都想给它留一些奶酪 可没过一会
- meidaj安装包11111111
- 游戏-Java-扫雷小游戏
- 二分查找教程.docx
- 2023-04-06-项目笔记 - 第一百七十阶段 - 4.4.2.168全局变量的作用域-168 -2024.06.20
- Vl171 demo.zip
- 毕设项目:城市公交查询系统的设计与实现(ASP.NET+源代码+论文)
- 有关各种常用语言的框架用法
- 一个简单的协同过滤推荐系统的Python示例,使用了一个假设的电影-用户评分矩阵
- 编程项目实战:基于asp.net架构的美容院管理系统(含论文+需求分析+开题报告+源代码)
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback-tip](https://img-home.csdnimg.cn/images/20220527035111.png)
安全验证
文档复制为VIP权益,开通VIP直接复制
![dialog-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/green-success.6a4acb44.png)