⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 usbuart0.c

📁 基于MODBUS协议的串口通信下位机软件的设计
💻 C
字号:

 



//使用F330,串口为P0.4,P0.5;操作方法:插上短路环J11,拔掉J12,连上USB线。
//-----------------------------------------------------------------------------
// UART.c
//-----------------------------------------------------------------------------


//-----------------------------------------------------------------------------
// 所包含的头文件
//------------------------------------------------------------------------

#include <c8051f330.h>                 
#include <stdio.h>
#include <lcddriver.h>
#define ADDR 0X01
//-----------------------------------------------------------------------------
// 常量定义
//-----------------------------------------------------------------------------

#define SYSCLK          24500000             // 系统工作频率
#define BAUDRATE        9600               // 串口工作波特率
#define TIMER2_RATE     1000                 // 定时器2溢出标志位频率
#define uchar unsigned char
#define uint unsigned int
 
//-----------------------------------------------------------------------------
// F33x’定时器2的16位SFR
//-----------------------------------------------------------------------------

sfr16 TMR2RL   = 0xca;                 
sfr16 TMR2     = 0xcc;                 

//-----------------------------------------------------------------------------

// 功能函数
//-----------------------------------------------------------------------------

void SYSCLK_Init (void);
void PORT_Init (void);
void UART0_Init (void);
void Timer2_Init (int);
bit ReceiveBit;
bit recieve_flag=0;
unsigned char temp;
unsigned char xdata  table[18] ={"welcome 231"}; 
uchar i,point,aba;
uint ctr1,crc;
uchar data sbufda,tx_count,sent,kcc1,kcc2,aaa,bbb;
uchar data rbuf[8];
uchar data tbuf[18];
/*void delay(unsigned int m)
{
unsigned int n;
n=0;
while(n<m)
{n++;}
return;
}*/
uint crc16(uchar *str,uint num);
//-----------------------------------------------------------------------------
// 主程序
//-----------------------------------------------------------------------------

 void main (void) {

   char i=0;
   PCA0MD &= ~0x40;                    // 禁止看门狗
                                       
   PORT_Init();                        // 初始化端口I/O
   
   SYSCLK_Init ();                     //  初始化系统时钟
   
   Timer2_Init(SYSCLK/TIMER2_RATE);    //  初始化 Timer 2 
   
   UART0_Init();                       //  串口初始化
   Initialize_LCD();
  EA=1;
  ES0=1;
  while(1)	
  {

  if(recieve_flag)
  {
    WriteCommand(0x80,1);   //写地址
   for( i=0;i<16 ; i++)
   {
   	  
	 
  		    WriteData(tbuf[i]);


   }
	 WriteCommand(0xc0,1);   //写地址
	 WriteData(tbuf[16]);
	 WriteData(tbuf[17]);
	recieve_flag=0;
   }	  
  }		 	
  
  /*
  while(ReceiveBit==1)
  {
  ES0=0;
  EA=0;
  ReceiveBit=0;
  //delay(60000);
 //delay(60000);
 //delay(60000);
 SBUF0=temp;
 while(TI0==0);
 TI0=0;
//delay(60000);
// delay(60000);
 //delay(60000);
 ES0=1;
 EA=1;
  }
  }
  //printf (" KEN :) Happy new year!  \n ");                //  输出字符     			 */
 
}

  

//-----------------------------------------------------------------------------
// 端口初始化
//-----------------------------------------------------------------------------
//
// Configure the Crossbar and GPIO ports.
//
// P0.4 - UART TX
// P0.5 - UART RX
// P3.3 - LED

void PORT_Init (void)
{
   //P0SKIP  |= 0x01;                    
  // P0MDIN  |= 0x01;                    // 配置P0.0为模拟出入
   P0MDOUT |= 0x10;                    // 使能 UTX 为推挽输出
 //  P1MDOUT |= 0x08;                    // 使能 LED 为推挽输出
   XBR0    = 0x01;                     // 使能 UART on P0.4(TX) and P0.5
                     
   XBR1    = 0x40;                  
}
//-----------------------------------------------------------------------------
// 系统时钟初始化
//-----------------------------------------------------------------------------


void SYSCLK_Init (void)
{
   OSCICN |= 0x03;                     // 配置内部振荡器的的最大频率
                                        
   RSTSRC  = 0x04;                     // 使能始终丢失检测寄存器

}

//-----------------------------------------------------------------------------
// 串口初始化
//-----------------------------------------------------------------------------


void UART0_Init (void)
{
   SCON0 = 0x10;                       
   if (SYSCLK/BAUDRATE/2/256 < 1) {

      TH1 = -(SYSCLK/BAUDRATE/2);
      CKCON &= ~0x0B;                  
      CKCON |=  0x08;
   } else if (SYSCLK/BAUDRATE/2/256 < 4) {
      TH1 = -(SYSCLK/BAUDRATE/2/4);
      CKCON &= ~0x0B;                                    
      CKCON |=  0x09;
   } else if (SYSCLK/BAUDRATE/2/256 < 12) {
      TH1 = -(SYSCLK/BAUDRATE/2/12);
      CKCON &= ~0x0B;                  
   } else {
      TH1 = -(SYSCLK/BAUDRATE/2/48);
      CKCON &= ~0x0B;                   
      CKCON |=  0x02;
   }

   TL1 = TH1;                          
   TMOD &= ~0xf0;                      
   TMOD |=  0x20;                       
   TR1 = 1;                            
  // TI0 = 1;                            
}
//-----------------------------------------------------------------------------
// 时钟2中断
//-----------------------------------------------------------------------------

void Timer2_Init (int counts)
{
   TMR2CN = 0x00;                      // 停止时钟2;清除 TF2H 和TF2L;
                                       
   CKCON |= 0x10;                      
   TMR2RL  = -counts;                  
   TMR2    = TMR2RL;                   
   ET2 = 0;                            
   TR2 = 1;                            
}
void UATR0_ISR(void)  interrupt 4
{
if(TI0)
{
  
TI0=0;
 sbufda++;
 if(sbufda<=tx_count)
 {
  ctr1=0;	  //发送
  SBUF0=tbuf[sbufda];
  }
  else
  {
   ctr1=1;		//接收
   sbufda=0;
   recieve_flag=1;
   }
}
//temp=SBUF0;
//ReceiveBit=1;


else
{ 
RI0=0;
rbuf[0]=rbuf[1];
rbuf[1]=rbuf[2];
rbuf[2]=rbuf[3];
rbuf[3]=rbuf[4];
rbuf[4]=rbuf[5];
rbuf[5]=rbuf[6];
rbuf[6]=rbuf[7];
rbuf[7]=SBUF0;
if(rbuf[0]==ADDR)
{
 crc=crc16(rbuf,8);
 if(crc==0)
 {
  switch(rbuf[1])
  {
  case 0x03	  :
        tbuf[0]=rbuf[0];
		tbuf[1]=rbuf[1];
		tbuf[2]=(rbuf[5])<<1;
		point=tbuf[2];
		sent=3;
		aba=rbuf[3];
		for(i=0;i<point;i++)
		{
		 tbuf[sent]=table[aba];
		 aba++;
		 sent++;
		 }
		 crc=crc16(tbuf,sent);
		 sent++;
		 tbuf[sent]=crc/256;
		 sent--;
		 tbuf[sent]=crc%256;
		 ctr1=0;
		 SBUF0=tbuf[0];
		 ES0=1;
		 sent++;
		 tx_count=sent;
		 sbufda=0;
		
		 break;
   case 0x04 :
        tbuf[0]=rbuf[0];
		tbuf[1]=rbuf[1];
		tbuf[2]=2;
		tbuf[3]=kcc1;
		tbuf[4]=kcc2;
		sent=5;
		crc=crc16(tbuf,sent);
		sent++;
		tbuf[sent]=crc/256;
		sent--;
		tbuf[sent]=crc%256;
		ctr1=0;
		SBUF0=tbuf[0];
		ES0=1;
		sent++;
		tx_count=sent;
		sbufda=0;
		break;
    case 0x06 :
        tbuf[0]=rbuf[0];
		tbuf[1]=rbuf[1];
		tbuf[2]=rbuf[2];
		tbuf[3]=rbuf[3];
		tbuf[4]=rbuf[4];
		tbuf[5]=rbuf[5];
		tbuf[6]=rbuf[6];
		tbuf[7]=rbuf[7];
		aaa=rbuf[4];
		bbb=rbuf[5];
		ctr1=0;
		SBUF0=tbuf[0];
		ES0=1;
		sent++;
		tx_count=sent;
		sbufda=0;
		break;
	default:
	    break;
		}
		      }
		}
		}
}


uint crc16(uchar *str,uint num)
{ 
 uint i1,j1,c1,crc1;
 crc1=0xffff;
 for(i1=0;i1<num;i1++)
 {
  c1=str[i1]&0x00ff;
  crc1^=c1;
  for(j1=0;j1<8;j1++)
  {
   if(crc1&0x0001)
   {
    crc1>>=1;
	crc1^=0xa001;
    }
	else
	crc1>>=1;
   }
  }
  return(crc1);
  }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -