代码如下:
- #include "STC12C5A60S2.H"
- #include <intrins.H>.// 头文件
- #define uchar unsigned char
- #define uint unsigned int
- uchar sendbuffer[17];// 发送数据
- uchar flag=0;
- uchar re_buffer[32];
- uchar count=0;
- uint ad_data=0;
- double ad_vol=0;
- uint con_mid=0;
- uint gascon=0;
- long sum_o2=0;
- uint average_counter=20;
- uint idata oldtemp[21];
- uchar idata Send_Buff[20]; //module
- uint pre_contemp=0;
- uint O2_con=0;
- uchar idata test[21]; //module
- uint temperature=0;
- uint dat;
- uint testlcd=0;
- uchar c[]={0xb0,0xb1,0xb2,0xb3,0xb4,0xb5,0xb6,0xb7,0xb8,0xb9};
- //MAX1241 模数芯片引脚设置,此电路板我没焊^_^
- //sbit ADC_CS=P0^1;
- //sbit ADC_CLK=P0^0;
- //sbit ADC_DOUT=P0^2;
- sbit ADC_CS=P3^6;
- sbit ADC_CLK=P3^5;
- sbit ADC_DOUT=P3^7;
- 4
- //LCD12864 LCD 屏幕引脚设置
- sbit cs=P2^2;
- sbit sid=P2^1;
- sbit sclk=P2^0;
- sbit DQ=P0^1; //DS18B20 温度引脚设置,依然没有⋯
- #define N 11
- #define N2 20
- void delayms(unsigned char t)
- {
- unsigned char i;
- unsigned char j;
- for(j=t;j;j--)
- for(i=192;i;i--);/*1ms 延时*/
- }
- void delayus(uint t)
- {
- uint i;
- for(i=0;i<t;i++)
- {
- _nop_();
- }
- }
- void delay(float sec)
- {
- unsigned int i;
- unsigned int j;
- j=sec*100;
- while(j--)
- {
- i=1561;
- while(--i);
- }
- }
- void UART1_init() // 串口1 初始化,此串口和电脑通讯
- {
- TMOD=0x20;/设置定时器工作方式2
- TH1=0xfd; // 波特率9600
- 5
- TL1=0xfd;
- TR1=1;
- REN=1;
- SM0=0;
- SM1=1;
- //ES=1;
- }
- void UART2_init()// 串口2 初始化,和模块通讯 Modbus
- {
- S2CON= 0x50; // 方式1,允许接收
- BRT = 0xf4; // 波特率2400
- AUXR = AUXR |0X10; // 允许独立波特率允许
- AUXR1 = AUXR1&0xef; // 将uart2 切换到P1 口
- IE2 = IE2|0X01; // 允许串口2 中断
- }
- void UART1_Send (unsigned char UART_data)//
- {
- //ES=0;
- SBUF = UART_data; // 将接收的数据发送回去
- while(TI!=1); // 检查发送中断标志位
- TI = 0; // 另发送中断标志位为0
- //ES=1;
- }
- void UART2_Send(unsigned char UART_data)// 串口2 发送
- {
- //ES = 0 ;
- S2BUF = UART_data;
- while((S2CON&0x02)!=0x02);
- S2CON &= ~0x02;
- //ES = 1 ;
- }
- void UART1_Send_String (char *str, char len)// 串口1 发送字符串
- {
- unsigned char i;
- for(i=0;i<=len;i++)
- {
- UART1_Send(str[i]);
- }
- }
- 6
- void UART2_Send_String (char *str, char len) // 串口2 发送字符串
- {
- unsigned char i;
- for(i=0;i<=len;i++)
- {
- UART2_Send(str[i]);
- }
- }
- unsigned char Creat_Addr(unsigned char adr, unsigned char position)// 计算模
- 块地址
- {
- unsigned char hich;
- unsigned char loch;
- hich = adr/16;
- loch = adr%16;
- if(hich>9)
- hich+=7;
- if(loch>9)
- loch+=7;
- if(position == 1)
- {
- return hich+0x30;
- }
- else if(position == 0)
- {
- return loch+0x30;
- }
- }
- unsigned char CheckSum(unsigned char *str, unsigned char position, uchar len)/
- 计算校验码
- {
- uchar i;
- unsigned int sum=0;
- uchar hi, lo;
- //uchar len = 12;
- for(i = 1; i <= len; i ++)
- {
- *str ++;
- sum += *str;
- }
- sum = 256-(sum%256);
- 7
- hi = sum/16;
- lo = sum%16;
- if(hi > 9)
- hi += 7;
- if(lo > 9)
- lo += 7;
- hi += 0x30;
- lo += 0x30;
- if(sum == 256)
- hi = lo = 0x30;
- if(position == 1)
- {
- return hi;
- }
- else if(position == 0)
- {
- return lo;
- }
- }
- void data_init()
- {
- sendbuffer[0]=0x5B;
- sendbuffer[1]=0x30;
- sendbuffer[2]=0x30;
- sendbuffer[3]=0x30;
- sendbuffer[4]=0x30;
- sendbuffer[5]=0x30;
- sendbuffer[6]=0x7C;
- sendbuffer[7]=0x30;
- sendbuffer[8]=0x30;
- sendbuffer[9]=0x30;
- sendbuffer[10]=0x30;
- sendbuffer[11]=0x30;//TEM
- sendbuffer[12]=0x30;
- sendbuffer[13]=0x30;
- sendbuffer[14]=0x5D;
- sendbuffer[15]=0x0D;
- sendbuffer[16]=0x0A;
- }
复制代码
以上内容的pdf格式文档下载(内容和本网页上的一模一样,方便大家保存):
51单片机新手入门之Modbus通讯.pdf
(816.34 KB, 下载次数: 62)
|