modbusASCII模式源代码.docx

上传人:韩长文 文档编号:5015252 上传时间:2020-01-28 格式:DOCX 页数:36 大小:28.88KB
返回 下载 相关 举报
modbusASCII模式源代码.docx_第1页
第1页 / 共36页
modbusASCII模式源代码.docx_第2页
第2页 / 共36页
modbusASCII模式源代码.docx_第3页
第3页 / 共36页
modbusASCII模式源代码.docx_第4页
第4页 / 共36页
modbusASCII模式源代码.docx_第5页
第5页 / 共36页
点击查看更多>>
资源描述

《modbusASCII模式源代码.docx》由会员分享,可在线阅读,更多相关《modbusASCII模式源代码.docx(36页珍藏版)》请在三一文库上搜索。

1、#include main.h#include intrins.h#include nokia_5110.h#include ds18b20.h #include EEPROM.h/*modbus ASSCII 的C51程序 单片机STC89C52通信波特率 9600 8位数据 1位停止位 232通位接口*/uint8sendBuf_12435,receBuf_12435; /发送接收缓冲区uint16 reg_1245; /寄存器uint16 wendu_124; /温度值unsigned int zhengshu_124,xiaoshu_124;char temperature_124=

2、. ;uint8 code table_124= /数码管码 0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e;uint8receTimeOut_124;/接收超时bit f1ms_124=0,f10ms_124=0,f100ms_124=0;/定时标志位uint8 c10ms_124,c100ms_124; /定时计数/*/ /*函数名称: commIntProc() *输入参数: 共 0 个参数; *输出参数: 共 0 个参数; *返回值: *需储存的参数: 共 0 个参数; *功

3、能介绍:串口中断函数 *修改日志: *2010-12-14 Ver. 1.00 开始编写;2010-12-7 完成; 2010-12-7 /* */ /*/ void commIntProc_124() interrupt 4 / 串行中断程序if(TI)TI = 0; if(sendPosi_124 =10)c10ms_124 = 0; /10ms计时器清零 f10ms_124=1; / void Timer0IntProc()/*/ /*函数名称: save_124(void) *输入参数: 共 0 个参数; *输出参数: 共 0 个参数; *返回值: *需储存的参数: 共 0 个参数;

4、*功能介绍:保存掉电寄存器数据 *修改日志: *2010-12-14 Ver. 1.00 开始编写;2010-12-14 完成; 2010-12-14 /* */ /*/ void save_124(void) /保存掉电寄存器数据uint8 tempdata_124;tempdata_124=P1;IapEraseSector_124(0x2000); IapProgramByte_124(0x2000,reg_1240);IapProgramByte_124(0x2001,reg_1241);IapProgramByte_124(0x2002,reg_1242);IapProgramByt

5、e_124(0x2003,reg_1243);IapProgramByte_124(0x2004,reg_1244);IapProgramByte_124(0x2005,tempdata_124);/*/ /*函数名称: read_124(void) *输入参数: 共 0 个参数; *输出参数: 共 0 个参数; *返回值: *需储存的参数: 共 0 个参数; *功能介绍:开机读取掉电时的寄存器数据 *修改日志: *2010-12-14 Ver. 1.00 开始编写;2010-12-14 完成; 2010-12-14 /* */ /*/ void read_124(void) /开机读取掉电时

6、的寄存器数据reg_1240=IapReadByte_124(0x2000); reg_1241=IapReadByte_124(0x2001);reg_1242=IapReadByte_124(0x2002);reg_1243=IapReadByte_124(0x2003);P1=IapReadByte_124(0x2005);/*/ /*函数名称: displaytempr(void) *输入参数: 共 0 个参数; *输出参数: 共 0 个参数; *返回值: *需储存的参数: 共 0 个参数; *功能介绍:读取温度值并且显示出来 *修改日志: *2010-12-14 Ver. 1.00

7、开始编写;2010-12-14 完成; 2010-12-14 /* */ /*/ void displaytempr_124(void)wendu_124=read_T_124();if(wendu_1240) receTimeOut_124-; if(receTimeOut_124=0 & receCount_1240) /判断通讯接收是否超时 b232Send_124 = 0; /将232置为接收状态 receCount_124 = 0; /将接收地址偏移寄存器清零 checkoutError_124 = 0; if(f10ms_124=1)f10ms_124=0;if(reg_1244r

8、eg_1242)|(reg_1244=10) c100ms_124=0; displaytempr_124(); bRunLED_124 = bRunLED_124;void main(void)init_124(); /初始化单片机的寄存器 read_124(); /开机去读上次断电的寄存器和线圈状态receCount_124=0;kaiji_124(); /开机显示汉字 chushixianshi_124(); /显示寄存器值和线圈的状态while(1)checkCommdModbus_124(); /接收上位机命令,然后做出响应处理timeProc_124(); /定时去读温度,保存数据

9、,判断是否报警#include reg52.htypedef unsigned charuint8;typedef unsigned intuint16;typedef unsigned longuint32;sbit Coil0_124 = P10;sbit Coil1_124 = P11;sbit Coil2_124 = P12;sbit Coil3_124 = P13;sbit Coil4_124 = P14;sbit Coil5_124 = P15;sbit Coil6_124 = P16;sbit Coil7_124 = P17;/sbit shuanmaen_124 = P10;s

10、bitbRunLED_124 =P24;/系统运行指示灯sbitb232Send_124= P25;/发送接收状态sbitalarm_124 = P27; /报警位sbit checkoutError_124 =P26; /报错位#define TIMER_HIGHT_1240xf8#define TIMER_LOW_1240xcd extern uint8 code table_124;extern uint8 sendBuf_12435,receBuf_12435;extern uint16 reg_1245;/extern uint8checkoutError;/ =2 偶校验错 /ex

11、tern uint8receTimeOut;/extern uint16dwTickCount;#include modbus.h#include main.h#include nokia_5110.huint8sendCount_124;/发送字节个数uint8receCount_124=0; /接收到的字节个数uint8sendPosi_124; /发送位置uint8 flag=0;/*/ /*函数名称: uh_ASCIIh_124(uint8 uh_124) *输入参数: 共 1 个参数; *输出参数: 共 1 个参数; *返回值: asciih *需储存的参数: 共 1 个参数; *功

12、能介绍: 把一个16进制uh的高四位值取出转换为相应的ASCII码 *修改日志: *2010-12-14 Ver. 1.00 开始编写;2010-12-7 完成; 2010-12-7 /*/ uint8 uh_ASCIIh_124(uint8 uh_124)uint8 temph_124;uint8 asciih_124;temph_124=uh_124/16; if(temph_12410)asciih_124=temph_124+0x30;else asciih_124=temph_124+0x37; return asciih_124;/*/ /*函数名称: uh_ASCIIl_124(

13、uint8 uh_124) *输入参数: 共 1 个参数; *输出参数: 共 1 个参数; *返回值: asciil *需储存的参数: 共 1 个参数; *功能介绍: 把一个16进制uh的低四位值取出转换为相应的ASCII码 *修改日志: *2010-12-14 Ver. 1.00 开始编写;2010-12-7 完成; 2010-12-7 /* */ /*/ uint8 uh_ASCIIl_124(uint8 uh_124)uint8 templ_124;uint8 asciil_124;templ_124=uh_124%16;if(templ_12410)asciil_124=templ_1

14、24+0x30;else asciil_124=templ_124+0x37; return asciil_124;/*/ /*函数名称: ASCII_uh_124(uint8 asciih_124,uint8 asciil_124) *输入参数: 共 2 个参数; *输出参数: 共 1 个参数; *返回值: uh *需储存的参数: 共 1 个参数; *功能介绍: 把两个ASCII码转换为10进制的值 *修改日志: *2010-12-14 Ver. 1.00 开始编写;2010-12-7 完成; 2010-12-7 /* */ /*/ uint8 ASCII_uh_124(uint8 asci

15、ih_124,uint8 asciil_124)uint8 uh_124;if(asciih_124=57)asciih_124=asciih_124-48;else asciih_124=asciih_124-55; if(asciil_1241;tempdat_124=tempdat_124&yu_124(countl_124);if(addrl_124=2)tempdat_124=tempdat_1242;tempdat_124=tempdat_124&yu_124(countl_124);if(addrl_124=3)tempdat_124=tempdat_1243;tempdat_1

16、24=tempdat_124&yu_124(countl_124);if(addrl_124=4)tempdat_124=tempdat_1244;tempdat_124=tempdat_124&yu_124(countl_124);if(addrl_124=5)tempdat_124=tempdat_1245;tempdat_124=tempdat_124&yu_124(countl_124);if(addrl_124=6)tempdat_124=tempdat_1246;tempdat_124=tempdat_124&yu_124(countl_124);if(addrl_124=7)te

17、mpdat_124=tempdat_1247;tempdat_124=tempdat_124&yu_124(countl_124); sendBuf_1247=uh_ASCIIh_124(tempdat_124); sendBuf_1248=uh_ASCIIl_124(tempdat_124);sendBuf_1240 = 0X3A;sendBuf_1241 = 0x30;sendBuf_1242 = 0x31;sendBuf_1243 = 0x30;sendBuf_1244 = 0x31;sendBuf_1245 = 0x30;sendBuf_1246 = 0x31;lrcdat_124 =lrc_124(sendBuf_124,4);sendBuf_1249=uh_ASCIIh_124(lrcdat_124);sendBuf_12410=uh_ASCIIl_124(lrcdat_124);sendBuf_12411= 0x0D;sendBuf_12412= 0x0A;sendCount_124=13;beginSend_124();/*/ /*函数名称: forceSingleCoil_124(void) *输入参数: 共 0 个参数; *输出参数: 共 0 个参数;

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 研究报告 > 商业贸易


经营许可证编号:宁ICP备18001539号-1