/**
* @file protocol.c
*
* @brief functions about protocol
*
* $Id: protocol.c 2012-5-17 13:22:35 wdx $
*
* @author humtech Corporation: http://www.humtech.com
* @author Support email: [email protected]
*/
/*
* Copyright (c) 2010, humtech Corporation All rights reserved.
*
* Licensed under humtech's Limited License Agreement --> LICENSE.txt
*/
/* === INCLUDES ============================================================ */
#include <stdint.h>
#include <ioavr.h>
#include <string.h>
#include <ina90.h>
#include "protocol.h"
#include "uart.h"
#include "crc.h"
#include "io.h"
#include "app.h"
#include "timer.h"
/* === TYPES =============================================================== */
/* === MACROS ============================================================== */
#define LOCAL_ADDR (0x01)
#define MAX_HOLD_REG (0x02)
#define DEV_TYPE (0x02)
#define DEV_NO (0x01)
#define SENSOR_NO (0x00)
#define SNSOR_HOLD_REG_NO (0x00)
#define HOLD_START_ADDR (49900)
/*define modbus command*/
#define READ_COIL_STAT (0x01)
#define READ_HOLD_REG (0x03)
#define FORC_SNGLE_COIL (0x05)
/* === GLOBALS ============================================================= */
static uint8_t pro_len;
static uint8_t pro_rx_buf[RX_BUF_LEN];
static uint8_t pro_tx_buf[TX_BUF_LEN];
static uint16_t hold_reg_val[MAX_HOLD_REG];
/* === PROTOTYPES ========================================================== */
uint8_t chk_rcv_dat(void);
void set_single_coil_stat(uint16_t reg_addr,uint16_t dat);
void read_coil_stat(uint16_t addr,uint16_t no);
void read_hold_reg(uint16_t reg_addr,uint16_t reg_no);
/* === IMPLEMENTATION ====================================================== */
/*set hold register initial value*/
void init_protocol(void)
{
hold_reg_val[0] = (DEV_TYPE << 8) | DEV_NO;//0X0201
hold_reg_val[1] = (SNSOR_HOLD_REG_NO << 8) | SENSOR_NO;//0x0000
}
/**
* @brief the handler of protocol
*
* @para none
*
* @return none
*/
void protocol_handler(void)
{
uint8_t chk_status;
uint8_t cmd;
uint16_t reg_addr,dat;
uint16_t reg_no;
if(uart_rcv_flag == TRUE)
{
/*reset uart recive flag*/
uart_rcv_flag = FALSE;
/*save uart receive data length*/
memcpy(pro_rx_buf,rx_buf,uart_rx_cnt);
pro_len = uart_rx_cnt;
uart_rx_cnt = 0x00;
/*check receive data valid*/
chk_status = chk_rcv_dat();
if(chk_status == TRUE)
{
led_rx_cnt = LED_TIM_OV;
LED_RX_ON();
cmd = pro_rx_buf[1];
switch(cmd)
{
case READ_COIL_STAT:
{
reg_addr = (pro_rx_buf[2]<<8) | pro_rx_buf[3];
reg_no = (pro_rx_buf[4]<<8) | pro_rx_buf[5];
read_coil_stat(reg_addr,reg_no);
break;
}
case READ_HOLD_REG:
{
reg_addr = (pro_rx_buf[2]<<8) | pro_rx_buf[3];
reg_no = (pro_rx_buf[4]<<8) | pro_rx_buf[5];
read_hold_reg(reg_addr,reg_no);
break;
}
case FORC_SNGLE_COIL:
{
reg_addr = (pro_rx_buf[2]<<8) | pro_rx_buf[3];
dat = (pro_rx_buf[4]<<8) | pro_rx_buf[5];
set_single_coil_stat(reg_addr,dat);
break;
}
}
}
}
}
/**
* @brief check receiv data valid
*
* @para none
*
* @return none
*/
uint8_t chk_rcv_dat(void)
{
uint16_t crc16_val;
if(pro_len == 0x00)
{
return FALSE;
}
if(pro_rx_buf[0] != LOCAL_ADDR)
{
return FALSE;
}
crc16_val = crc16(pro_rx_buf,pro_len-2);
if((pro_rx_buf[pro_len-2] != ((crc16_val>>8)&0x00FF) |
(pro_rx_buf[pro_len-1] != (crc16_val&0x00FF)))
)
{
return FALSE;
}
return TRUE;
}
/**
* @brief read coil state
*
* @para reg_addr the register address of coil
* no no. of reading coil
* @return none
*/
void read_coil_stat(uint16_t reg_addr,uint16_t reg_no)
{
uint16_t crc16_val;
uint8_t index = 0;
/*check register address valid*/
if(reg_no > MAX_COIL)
{
return;
}
pro_tx_buf[index++] = LOCAL_ADDR; /*first byte: local address */
pro_tx_buf[index++] = READ_COIL_STAT; /*second byte: command */
pro_tx_buf[index++] = 1; /*third byte: data feild length */
pro_tx_buf[index++] = coil_sta[0];
crc16_val = crc16(pro_tx_buf,index); /*calculate crc value */
pro_tx_buf[index++] = (crc16_val>>8)&0X00FF;
pro_tx_buf[index++] = crc16_val & 0x00FF;
uart_send_dat(pro_tx_buf,index); /*send data by uart */
}
/**
* @brief read holding reg
*
* @para reg_addr the register address of coil
* no no. of reading coil
* @return none
*/
void read_hold_reg(uint16_t reg_addr,uint16_t reg_no)
{
uint16_t crc16_val;
uint8_t index = 0;
uint8_t start_addr;
uint8_t i,len;
/*check register address valid*/
if(reg_no > MAX_HOLD_REG)
{
return;
}
if(reg_addr < HOLD_START_ADDR)
{
return;
}
/*build tx data buf*/
pro_tx_buf[index++] = LOCAL_ADDR; /*first byte: local address */
pro_tx_buf[index++] = READ_HOLD_REG; /*second byte: command */
pro_tx_buf[index++] = 0; /*third byte: data feild length */
len = index;
start_addr = reg_addr - HOLD_START_ADDR;
for(i = 0; i < reg_no;i++)
{
pro_tx_buf[index++] = hold_reg_val[start_addr]>>8;
pro_tx_buf[index++] = hold_reg_val[start_addr++] & 0x00FF;
}
len = index - len;
pro_tx_buf[2] = len;
crc16_val = crc16(pro_tx_buf,index); /*calculate crc value */
pro_tx_buf[index++] = crc16_val>>8;
pro_tx_buf[index++] = crc16_val & 0xFF;
uart_send_dat(pro_tx_buf,index); /*send data by uart */
}
/**
* @brief set single coil state
*
* @para reg_addr the register address of coil
* dat state of set coil
* @return none
*/
void set_single_coil_stat(uint16_t reg_addr,uint16_t dat)
{
//check register address valid
if(reg_addr > MAX_COIL)
{
return;
}
if(dat == 0xFF00)
{
FAN_OPEN();
coil_sta[0] = 0x01;
}
else
{
FAN_CLOSE();
coil_sta[0] = 0x00;
}
uart_send_dat(pro_rx_buf,pro_len);
}
没有合适的资源?快使用搜索试试~ 我知道了~
io_ctrl.rar_ATMEGA88
共52个文件
r90:23个
h:6个
c:6个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 47 浏览量
2022-09-24
18:51:43
上传
评论
收藏 146KB RAR 举报
温馨提示
ATMEGA88单片机下的IO控制程序,内有MODUBS协议
资源推荐
资源详情
资源评论
收起资源包目录
io_ctrl.rar (52个子文件)
io_ctrl
inc
protocol.h 871B
uart.h 987B
crc.h 802B
io.h 2KB
timer.h 871B
app.h 834B
src
main.c 1KB
crc.c 5KB
uart.c 4KB
protocol.c 7KB
io.c 1KB
timer.c 2KB
prj
Release
List
Exe
Obj
io_ctrl.pbd 554B
io_ctrl.dep 11KB
io_ctrl.ewd 51KB
io_ctrl_d90.aps 8KB
io_ctrl.ewp 52KB
io_ctrl.eww 161B
settings
io_ctrl.cspy.bat 2KB
io_ctrl.wsdt 6KB
io_ctrl.dni 327B
io_ctrl.dbgdt 63B
io_ctrl_d90.aws 842B
Debug
List
io_ctrl_d90.aps 8KB
Exe
io_ctrl.d90 46KB
io_ctrl.hex 10KB
abc.d90 46KB
io_ctrl_d90.aws 985B
Obj
main.r90 8KB
mbfunccoils.r90 25KB
mbfuncother.r90 18KB
excoils.r90 11KB
io.r90 7KB
mbutils.r90 15KB
mbascii.r90 4KB
mb.r90 60KB
porttimer.r90 18KB
portevent.r90 5KB
mbfuncdisc.r90 12KB
mbrtu.r90 49KB
io_ctrl.pbd 542B
crc.r90 8KB
mbfuncholding.r90 27KB
mbfuncdiag.r90 1KB
timer.r90 15KB
portserial.r90 18KB
protocol.r90 49KB
int0.r90 7KB
mbtcp.r90 3KB
uart.r90 26KB
mbcrc.r90 7KB
mbfuncinput.r90 11KB
共 52 条
- 1
资源评论
四散
- 粉丝: 52
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功