msp430f149 modbus rtu 通信的问题

2019-03-24 12:02发布

新手求教msp430 modbus rtu通信的问题测试环境:
1)485芯片用的是max485,单片机用是TI的msp430F149.

问题:
请前辈帮看下代码,哪里有错误没?望前辈不吝赐教!谢谢
以下是代码:

#include  <msp430x14x.h>
#include  "cry1602.h"
#include "crc.h"
#define uint unsigned int
#define uchar unsigned char
#define DRE_out     P3DIR |= BIT3     //连接485芯片的DE,RE端口的IO设置为输出状态
#define DE          P3OUT |= BIT3     //设置485芯片处于发送状态
#define RE          P3OUT &= ~BIT3    //设置485芯片处于接收状态
void Trans_val(uint m);
void Set_UART1(void);
uchar tishi[] = {"error:"};
uchar shuzi[] = {"0123456789."};
uchar sendbuf[]={0x01,0x04,0x00,0x00,0x00,0x01,0x00,0x01};
uchar tem[5],tem1[8];
uchar resvbuf[9];
int i,j;
uchar resvend,temp2;
uint temp1,crc1,temp;
/****************主函数****************/
void main(void)
{
    WDTCTL = WDTPW + WDTHOLD;                 //关闭看门狗
    LcdReset();  
    DRE_out;
    DE;
    Set_UART1();
    _EINT();
    i=0;
   while(1)
    {
      i=0;
   while(i<8)
  {
    TXBUF1=sendbuf;
   while(!(IFG2 & UTXIFG1));
    IFG2 &= ~UTXIFG1;
   i++;
  }
  i=0;
  RE;
    }
}
/****************液晶显示*************/
void displayout()
{
   crc1=crccheck(resvbuf,7);
   temp=resvbuf[8];
   temp=temp << 8 | resvbuf[7];
   if(temp==crc1)
   {   Disp1Char(13,1,0x43);  
       unsigned long m;
       temp1 |=resvbuf[3];
       temp1= temp1<<8 | resvbuf[4];
       m=(temp1/16)*16+temp1%16;
       Trans_val(m);
    }
  else
   {
     DispNChar( 0,0,10,tishi);
   }
}   

/*******************************************
函数名称:Set_UART1
功    能:设置MCU的UART1对应的寄存器
参    数:无
返回值  :无
********************************************/
void Set_UART1(void)
{
    P3SEL |= 0xC0;                            //将P3.6,P3.7选做UART1的通信端口
    ME2 |= UTXE1 + URXE1;                     //使能USART1的TXD和RXD
    UCTL1 |= CHAR;                            //选择8-bit字符
    UTCTL1 |= SSEL0;                          //驱动时钟选择ACLK
    UBR01 = 0x03;                             //波特率9600
    UBR11 = 0x00;
    UMCTL1 = 0x6B;                            //调整
    UCTL1 &= ~SWRST;                          //初始化UART状态机
    IE2 |= URXIE1;
}
/*******************************************
函数名称:接收中断函数
********************************************/
#pragma vector=UART1RX_VECTOR
__interrupt void UART1_RX (void)
{
  if (URXIFG1)  
{
    resvbuf[j++]=RXBUF1;  
       IFG2 &=~ URXIFG1 ;
   if(j==8)
   {
     j=0;
  resvend=1;
   }   
}
  
}
/***液晶显示子函数****/
void Trans_val(uint Hex_Val)
{
   uchar ptr[6];
    uint a;
    a =Hex_Val;
     ptr[0]=a/1000;    //百位
    ptr[1]=a/1000%10;    //百位
   
    ptr[2]=a/100%10;   //个位
     ptr[3]=10;  //十位
    ptr[4]=a/10%10;    //小数点后第一位
    ptr[5]=a%10;       //小数点后第二位
    for(i = 0;i <6;i++)
      Disp1Char((6 + i),1,shuzi[ptr]);
}

下面是crc校验的子程序
#include "msp430f149.h"
#include "crc.h"
uint crccheck(uchar *str, uint usDataLen)
{      
        uint i,j,c,crc;
        crc=0xff;
        for(i=0;i<usDataLen;i++)
        {
          c=str&0xffff;
          crc^=c;
          for(j=0;j<8;j++)
          {
            if(crc&0x0001)
            {
              crc>>=1;
              crc^=0xa001;
            }
            else
              crc>>=1;
          }
}
        return (crc);
}




[ 本帖最后由 Domosday 于 2013-11-27 01:49 编辑 ] 此帖出自小平头技术问答
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
6条回答
Domosday
2019-03-24 21:52
< :TI_MSP430_内容页_SA7 --> 谢谢您的回复,学习了!!!自己的学识尚浅啊~

一周热门 更多>

相关问题

    相关文章