| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 678 人关注过本帖
标题:高手帮帮忙?利用modbus采集数据。帮我看看代码哪边出问题
只看楼主 加入收藏
xuexibianc
Rank: 2
等 级:论坛游民
帖 子:35
专家分:34
注 册:2010-9-8
结帖率:75%
收藏
已结贴  问题点数:20 回复次数:3 
高手帮帮忙?利用modbus采集数据。帮我看看代码哪边出问题
#include "reg51.h"
#ifndef __config_H__
#define __config_H__ 1

/*********************************************/
#define M8    1
#define M16   2
#define M32   3
#define M64   4
#define M128  5
/*********************************************/
#define CPU_TYPE  M16

#define F_CPU 8000000

#if CPU_TYPE == M128
#include <iom128v.h>
#endif
#if CPU_TYPE == M64
#include <iom64v.h>
#endif
#if CPU_TYPE == M32
#include <iom32v.h>
#endif
#if CPU_TYPE == M16
//#include <iom16v.h>
#endif
#if CPU_TYPE == M8
#include <iom8v.h>
#endif

#ifndef TRUE
#define TRUE  1
#endif
#ifndef FALSE
#define FALSE 0
#endif
#ifndef NULL
#define NULL 0
#endif
#define MIN(a,b)            ((a<b)?(a):(b))
#define MAX(a,b)            ((a>b)?(a):(b))
#define ABS(x)                ((x>0)?(x):(-x))
typedef unsigned char  uint8;
typedef signed   char  int8;
typedef unsigned int   uint16;
typedef signed   int   int16;
typedef unsigned long  uint32;
typedef signed   long  int32;


#define ADDR_1 PORTC|=(1<<2)
#define ADDR_0 PORTC&=~(1<<2)

#define DIN_1 PORTC|=(1<<3)
#define DIN_0 PORTC&=~(1<<3)

#define MAX485_1 PORTC|=(1<<4)
#define MAX485_0 PORTC&=~(1<<4)

#define TLC2543_CS_1 PORTB|=(1<<3)
#define TLC2543_CS_0 PORTB&=~(1<<3)

#define TLC2543_EOC (PINB&(1<<PB2))

#define TLV5618_1 PORTB|=(1<<4)
#define TLV5618_0 PORTB&=~(1<<4)

#endif

unsigned char tr[60]={0};
unsigned char re[60]={0};
unsigned int com_usart=0;
unsigned char com_temp=0;
unsigned char com_flag=0;
unsigned int ADH[11]={2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000};
unsigned int ADL[11]={0,0,0,0,0,0,0,0,0,0,0};
unsigned int AD[11]={0,0,0,0,0,0,0,0,0,0,0,};
unsigned int DAH[2]={2459,2459};
unsigned int DAL[2]={0,0};
unsigned int DA[2]={0,0};
unsigned char EEPROM_DATA[59];

sbit PINA=P2^6;
sbit PORTC=P2^7;         
   
void delay_us(unsigned int i)
{
     while(i--)
     {
        //NOP();
        ;
    }        
}
void delay_ms(unsigned int i)
{
      unsigned int a=0,b;   
      for(b=i;b>0;b--)   
    {
        for(a=1331;a>0;a--)   
        {
            ;
        }
     }
}
unsigned short CRC16(unsigned char *puchMsg,unsigned char length)
{
    unsigned short reg_crc=0;
    unsigned short s_crcchk=0;
    s_crcchk=0x0000;
    reg_crc=0xFFFF;
    while(length--)
    {
        reg_crc^=*puchMsg++;
        for(s_crcchk=0;s_crcchk<8;s_crcchk++)
        {
             if(reg_crc&0x01)
            {
                 reg_crc=(reg_crc>>1)^0xA001;
            }               
            else
            {
                 reg_crc=reg_crc>>1;   
            }
        }
    }
    return reg_crc;
}
unsigned char addr_read(void)
{
    unsigned char add=0;
    ADDR_0;
    delay_us(1);
    add=PINA;
    ADDR_1;
    return add;         
}
unsigned char state_read(void)
{
     unsigned char i=0;
     DIN_0;
     delay_us(1);
     i=~PINA;
     DIN_1;
     return i;   
}
void TLV5618(unsigned int data,unsigned char channel)
{
    unsigned int mode=0;
    TLV5618_spi_init();
    if(channel==0)
        mode=0xC000;
    else if(channel==1)
        mode=0x4000;
    else if(channel==2)
        mode=0x2000;
    data|=mode;
    TLV5618_0;
    spi_send((unsigned char)(data/256));
    spi_send((unsigned char)(data&0xFF));
    TLV5618_1;
    TLC2543_spi_init();
}
unsigned int read_adc(unsigned char channel)
{
    unsigned int adc=0;
    unsigned char adch=0,adcl=0;
    unsigned char code,TLC2543_channel=0;
    unsigned char i=0;
    code=0x0C;
    switch(channel)
    {
         case 0: code|=0x00;
                      break;
          case 1: code|=0x10;
                      break;
          case 2: code|=0x20;
                      break;
          case 3: code|=0x30;
                      break;
        case 4 : code|=0x40;
                     break;
          case 5 : code|=0x50;
                     break;
        case 6 : code|=0x60;
                     break;
          case 7 : code|=0x70;
                     break;
        case 8 : code|=0x80;
                     break;
          case 9 : code|=0x90;
                     break;     
          case 10 : code|=0xA0;
                     break;
          default: code|=0x00;
                  break;
    }
    CLI();
    while(!TLC2543_EOC);
    delay_us(5);
    TLC2543_CS_0;
    delay_us(20);
    adch=spi_writeread(code);
    adcl=spi_writeread(code);
    delay_us(10);
    TLC2543_CS_1;
    delay_us(20);
    adc=adcl|((int)adch<<8);
    adc>>=4;
    SEI();
    return adc;
}
unsigned int adc_process(unsigned char channel)
{
     unsigned char i=0,j=0;
     unsigned int temp=0;
     unsigned int process[21];
     temp=read_adc(channel);
     temp=read_adc(channel);
     for(i=0;i<21;i++)
    {
         process[i]=read_adc(channel);        
    }
    for(i=0;i<21;i++)
    {
         for(j=20;j<i;j--)
        {
            if(process[i]<process[j])
            {
                 temp=process[i];
                process[i]=process[j];
                process[j]=temp;
            }
        }
    }
    temp=(process[8]+process[9]+process[10]+process[11]+process[12])/5;
    return temp;
}
void usart_transmit(void)
{
     WDR();   
}
void usart_receive(void)
{
     unsigned char i=0;
     unsigned int temp=0;
     unsigned char lenbyte;
     unsigned int n;
    unsigned int saddr;
    unsigned int crc;
    float ltemp=0;
    if(re[0]==addr_read())
    {
        if((unsigned int)(re[6]|(re[7]<<8))==CRC16(re,6))
        {
             tr[0]=re[0];
               tr[1]=re[1];
               switch(re[1])
               {
                 case 0x02:
                {
                     n=re[5]|((unsigned int)(re[4]<<8));
                         if(n<=8)
                        n=8-n;
                    else
                        lenbyte=1;
                         tr[2]=lenbyte;
                         tr[3]=state_read();
                        for(i=0;i<n;i++)
                         tr[3]<<=1;
                        for(i=0;i<n;i++)
                        tr[3]>>=1;
                         crc=CRC16(tr,3+lenbyte);
                         tr[3+lenbyte]=(unsigned char)crc;
                         tr[4+lenbyte]=(unsigned char)(crc>>8);
                        Com_putstring(tr,5+lenbyte,&RTbuf_UART0);
                         break;
                }
                case 0x03:
                {
                    n=re[5]|((unsigned int)(re[4]<<8));
                    lenbyte=2*n;
                    tr[2]=lenbyte;
                    saddr=re[3]|((unsigned int)(re[2]<<8));
                    saddr*=2;
                    for(i=0;i<n;i++)
                    {
                        if(n<28)
                        {
                            tr[3+2*i]=EEPROM_DATA[saddr+2];
                            tr[4+2*i]=EEPROM_DATA[saddr+1];
                        }
                        else
                        {
                             tr[3+2*i]=0;
                                   tr[4+2*i]=0;
                        }
                        saddr+=2;
                    }
                        crc=CRC16(tr,3+lenbyte);
                        tr[3+lenbyte]=(unsigned char)crc;
                        tr[4+lenbyte]=(unsigned char)(crc>>8);
                        Com_putstring(tr,5+lenbyte,&RTbuf_UART0);
                        break;
                }
                case 0x04:
                {
                    n=re[5]|((unsigned int)(re[4]<<8));
                    lenbyte=2*n;
                    tr[2]=lenbyte;
                    saddr=re[3]|((unsigned int)(re[2]<<8));
                    for(i=0;i<n;i++)
                    {
                        if((saddr>=0)&&(saddr<=10))
                        {
                            temp=adc_process(saddr);
                            if(temp<600)
                                temp=600;
                                 ltemp=temp;
                                 temp=(unsigned int)((ltemp-600)*(ADH[saddr]-ADL[saddr])/2400+ADL[saddr]);
                        }
                        else
                            tr[3+2*i]=(unsigned char)(temp>>8);
                             tr[4+2*i]=(unsigned char)temp;
                             saddr++;
                    }
                    crc=CRC16(tr,3+lenbyte);
                    tr[3+lenbyte]=(unsigned char)crc;
                    tr[4+lenbyte]=(unsigned char)(crc>>8);
                    Com_putstring(tr,5+lenbyte,&RTbuf_UART0);
                    break;
                }
                case 0x05:
                {
                    lenbyte=2;
                    saddr=re[3]|((unsigned int)(re[2]<<8));
                    if((re[4]==0xFF)&&(re[5]==0x00))
                    {
                        i=0x01;
                        i<<=7-saddr;
                        PORTD&=~i;
                    }
                    else if((re[4]==0x00)&&(re[5]=0x00))
                    {
                        i=0x01;
                        i<<=7-saddr;
                        PORTD|=i;
                    }
                    else
                        tr[2]=re[2];
                         tr[3]=re[3];
                        tr[4]=re[4];
                         tr[5]=re[5];
                        crc=CRC16(tr,6);
                         tr[6]=(unsigned char)crc;
                         tr[7]=(unsigned char)(crc>>8);
                         Com_putstring(tr,8,&RTbuf_UART0);
                         break;
                }
                case 0x06:
                {
                    n=re[5]|((unsigned int)(re[4]<<8));
                    saddr=re[3]|((unsigned int)(re[2]<<8));
                    if((saddr>=0)&&(saddr<=21))
                    {
                        if(saddr%2==0)
                        ADH[saddr/2]=n;
                        else
                        ADH[(saddr-1)/2]=n;
                    }
                    else if((saddr>=22)&&(saddr<=27) )
                    {
                        saddr=22;
                        if(saddr%3==1)
                            DAH[(saddr-1)/3]=n;
                        else if(saddr%3==2)
                            DAH[(saddr-2)/3];
                        else
                        {
                            DA[saddr/3]=n;
                            ltemp=n;
                            n=(unsigned int)(ltemp*(DAH[0]-DAL[0])/2000+DAL[0]);
                            TLV5618(n,saddr/3);
                        }
                    }
                    else
                        tr[2]=re[2];
                         tr[3]=re[3];
                         tr[4]=re[4];
                         tr[5]=re[5];
                         crc=CRC16(tr,6);
                         tr[6]=(unsigned char)crc;
                         tr[7]=(unsigned char)(crc>>8);
                         Com_putstring(tr,8,&RTbuf_UART0);
                         EEPROM_DATA[0]=0x85;
                         for(i=0;i<11;i++)
                         {
                            EEPROM_DATA[4*i+1]=(unsigned char)(ADH[i]>>8);
                            EEPROM_DATA[4*i+2]=(unsigned char)ADH[i];
                            EEPROM_DATA[4*i+3]=(unsigned char)(ADL[i]>>8);
                            EEPROM_DATA[4*i+4]=(unsigned char)ADL[i];
                        }
                        for(i=0;i<2;i++)
                        {
                            EEPROM_DATA[45+6*i]=(unsigned char)(DA[i]>>8);
                                  EEPROM_DATA[46+6*i]=(unsigned char)DA[i];
                                  EEPROM_DATA[47+6*i]=(unsigned char)(DAH[i]>>8);
                                  EEPROM_DATA[48+6*i]=(unsigned char)DAH[i];
                                  EEPROM_DATA[49+6*i]=(unsigned char)(DAL[i]>>8);
                                  EEPROM_DATA[50+6*i]=(unsigned char)DAL[i];
                        }
                        crc=CRC16(EEPROM_DATA,57);
                        EEPROM_DATA[57]=(unsigned char)crc;
                        EEPROM_DATA[58]=(unsigned char)(crc>>8);
                        EEPROM_WRITE(0x10,EEPROM_DATA);
                        EEPROM_WRITE(0xC0,EEPROM_DATA);
                        break;
                }
                default: break;
            }            
        }
        else
            ;
    }
    WDR();
}
void TLV5618_spi_init(void)
{
    SPCR=0x5B;
    SPSR=0x00;
}
void TLC2543_spi_init(void)
{
    SPCR=(1<<SPE)|(1<<MSTR);   
}
unsigned char spi_writeread(unsigned char cData)
{
    SPDR=cData;
    while(!(SPSR&0x80));
    cData=SPSR;
    return SPDR;
}
void spi_send(unsigned char cData)
{
    SPDR=cData;
    while(!(SPSR&0x80))
    {
        ;   
    }   
     WDR();   
}
unsigned char spi_receive(void)
{
    while(!(SPSR&(1<<SPIF)))   
    {
         ;   
    }
    return SPDR;
}
void time1_init(void)
{
    TCCR1B=0x00;
    TCNT1H=0xE1;
    TCNT1L=0x7C;
    OCR1AH=0x1E;
    OCR1AL=0x84;
    OCR1BH=0x1E;
    OCR1BL=0x84;
    ICR1H=0x1E;
    ICR1L=0x84;
    TCCR1A=0x00;
    TCCR1B=0X05;
}
#pragma interrupt_handler time1_ovf_isr:9
void time1_ovf_isr(void)
{
    unsigned int temp=0;
    unsigned char i=0;
    TCNT1H=0xE1;
    TCNT1L=0x7C;
    for(i=0;i<11;i++)
    {
        temp=adc_process(i);
        tr[2*i]=(unsigned char)temp;
        tr[2*i+1]=(unsigned char)(temp>>8);
        temp=0;
    }
    Com_putstring(tr,22,&RTbuf_UART0);
}
unsigned char data_init(void)
{
    unsigned char temp=0;
    unsigned char sign=0;
    unsigned char sign1=0,sign2=0;
    unsigned char i=0;
    unsigned int crc=0;
    if(EEPROMread(0x10)==0x85)
    {
        EEPROM_READ(0x10,EEPROM_DATA);
        if((unsigned int)(EEPROM_DATA[57]|(EEPROM_DATA[58]<<8))==CRC16(EEPROM_DATA,57))
        {
            sign1=0xFF;
            sign=0xFF;
            for(i=0;i<11;i++)
            {
                ADH[i]=EEPROM_DATA[4*i+2]|(EEPROM_DATA[4*i+1]<<8);
                ADL[i]=EEPROM_DATA[4*i+4]|(EEPROM_DATA[4*i+3]<<8);
            }
            for(i=0;i<2;i++)
            {
                DA[i]=EEPROM_DATA[6*i+46]|(EEPROM_DATA[6*i+45]<<8);
                DAH[i]=EEPROM_DATA[6*i+48]|(EEPROM_DATA[6*i+47]<<8);
                DAL[i]=EEPROM_DATA[6*i+50]|(EEPROM_DATA[6*i+49]<<8);
            }
        }
        else
            sign1=0x00;
    }
    else
        sign1=0x00;
    if(EEPROMread(0xC0)==0x85)
    {
        EEPROM_READ(0xC0,EEPROM_DATA);
        if((unsigned int)(EEPROM_DATA[57]|(EEPROM_DATA[58]<<8))==CRC16(EEPROM_DATA,57))
        {
             sign2=0xFF;
               if(sign1!=0xFF)
               {
                sign=0xFF;
                for(i=0;i<11;i++)
                {
                    ADH[i]=EEPROM_DATA[4*i+2]|(EEPROM_DATA[4*i+1]<<8);
                    ADL[i]=EEPROM_DATA[4*i+4]|(EEPROM_DATA[4*i+3]<<8);
                }
                for(i=0;i<2;i++)
                {
                    DA[i]=EEPROM_DATA[6*i+46]|(EEPROM_DATA[6*i+45]<<8);
                    DAH[i]=EEPROM_DATA[6*i+48]|(EEPROM_DATA[6*i+47]<<8);
                    DAL[i]=EEPROM_DATA[6*i+50]|(EEPROM_DATA[6*i+49]<<8);
                }
            }               
        }
        else
            sign2=0x00;
    }
    else
        sign2=0x00;
        if((sign1==0xFF)&&(sign2==0x00))
            EEPROM_WRITE(0xC0,EEPROM_DATA);
        if((sign1==0x00)&&(sign2==0xFF))
            EEPROM_WRITE(0x10,EEPROM_DATA);
        if((sign1==0x00)&&(sign2==0x00))
        {
            sign=0x00;
            EEPROM_DATA[0]=0x85;
            for(i=0;i<11;i++)
            {
                EEPROM_DATA[4*i+1]=(unsigned char)(ADH[i]>>8);
                EEPROM_DATA[4*i+2]=(unsigned char)ADH[i];
                EEPROM_DATA[4*i+3]=(unsigned char)(ADL[i]>>8);
                EEPROM_DATA[4*i+4]=(unsigned char)ADL[i];
            }
            for(i=0;i<2;i++)
            {
                EEPROM_DATA[45+6*i]=(unsigned char)(DA[i]>>8);
                EEPROM_DATA[46+6*i]=(unsigned char)DA[i];
                EEPROM_DATA[47+6*i]=(unsigned char)(DAH[i]>>8);
                EEPROM_DATA[48+6*i]=(unsigned char)DAH[i];
                EEPROM_DATA[49+6*i]=(unsigned char)(DAL[i]>>8);
                EEPROM_DATA[50+6*i]=(unsigned char)DAL[i];
            }
            crc=CRC16(EEPROM_DATA,57);
            EEPROM_DATA[57]=(unsigned char)crc;
            EEPROM_DATA[58]=(unsigned char)(crc>>8);
            EEPROM_WRITE(0x10,EEPROM_DATA);
            EEPROM_WRITE(0xC0,EEPROM_DATA);
        }
        return (sign);
}
void port_init(void)
{
    PORTA=0xFF;
    DDRA=0x00;
    PORTB=0xFC;
    DDRB=0xBC;
    PORTC=0x0C;
    DDRC=0x1C;
    PORTD=0xFC;
    DDRD=0xFC;
}
void watchdog_init(void)
{
     WDR();
     WDTCR=0x1F;
     WDTCR=0x0F;   
}
void init_devices(void)
{
    CLI();
    watchdog_init();
    WDR();
    port_init();
    WDR();
    Com_init();
    WDR();
    TLC2543_spi_init();
    WDR();
    data_init();
    WDR();
    MCUCR=0x00;
    GICR=0x00;
    ACSR=0x80;
    SEI();
}
void main(void)
{
    init_devices();
    while(1)
    {
            if(Com_R_count(&RTbuf_UART0)!=0)
        {
            Com_getstring(&com_temp,1,&RTbuf_UART0);
            Com_Rbuf_Clear(&RTbuf_UART0);
            com_usart++;
            if((com_temp==addr_read())&&(com_flag!=0xFF))
            {
                re[0]=com_temp;
                com_temp=0;
                com_usart=0;
                com_flag=0xFF;
            }
            if((com_flag==0xFF)&&(com_usart==1))
            {
                if((com_temp==0x02)||(com_temp==0x03)||(com_temp==0x04)||(com_temp==0x05)||(com_temp==0x06))
                {
                    re[1]=com_temp;
                    com_temp=0;
                }
                else
                {
                    com_temp=0;
                    com_flag=0x00;
                }
            }
            if((com_flag==0xFF)&&(com_usart==2))
            {
                re[2]=com_temp;
                com_temp=0;
            }
            if((com_flag==0xFF)&&(com_usart==3))
            {
                re[3]=com_temp;
                   com_temp=0;               
            }
            if((com_flag==0xFF)&&(com_usart==4))
            {
                re[4]=com_temp;
                com_temp=0;
            }
            if((com_flag==0xFF)&&(com_usart==5))
            {
                re[5]=com_temp;
                com_temp=0;
            }
            if((com_flag==0xFF)&&(com_usart==6))
            {
                re[6]=com_temp;
                com_temp=0;
            }
            if((com_flag==0xFF)&&(com_usart==7))
            {
                re[7]=com_temp;
                com_temp=0;
                usart_receive();
                com_flag=0x00;
                com_usart=0;
            }
        }
        WDR();
    }
}
搜索更多相关主题的帖子: modbus 数据 代码 采集 
2010-09-19 13:58
vandychan
Rank: 15Rank: 15Rank: 15Rank: 15Rank: 15
等 级:贵宾
威 望:18
帖 子:2296
专家分:6418
注 册:2010-8-20
收藏
得分:14 
太长了

到底是“出来混迟早要还”还是“杀人放火金腰带”?
2010-09-19 14:28
xuexibianc
Rank: 2
等 级:论坛游民
帖 子:35
专家分:34
注 册:2010-9-8
收藏
得分:0 
主要是定义很长啊
2010-09-19 16:10
xuexibianc
Rank: 2
等 级:论坛游民
帖 子:35
专家分:34
注 册:2010-9-8
收藏
得分:0 
大家帮帮忙吧!!
2010-09-21 13:40
快速回复:高手帮帮忙?利用modbus采集数据。帮我看看代码哪边出问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.043576 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved