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

📄 modbus_rtu.c

📁 STC12C5412AD做的温度检测控制的源程序.有AD检测,可控硅过零触发,PID算法.
💻 C
字号:

//#include ".\STC89c5x.H"
#include".\Globle.h"
#include".\Init.h"
#include".\STC_RST.h"
#include".\Function.h"
#include".\SEEPROM.h"
#define	UART_LEN	50
UBYTE gvc_tmp;
UBYTE gvc_timer_flg;
UBYTE gvc_num_tmp;
UBYTE gvc_satue;
extern unsigned int idata  i;
unsigned char cy_receive_buf[UART_LEN];
unsigned char  cy_uartnumber=0;
unsigned char gvc_data_num=0;
unsigned char gvc_data_buf[40];
UBYTE    cy_uartcomplete=0;//串口接受完一组数据
UBYTE * gvp_data_point;
UBYTE 	sy_10ms;
WordType pca_tmp;
UBYTE pca_js=0;
UBYTE PwmFlg=0;	
UBYTE Adc_js=0;
UBYTE AdcFlg;
UBYTE PWMValue;
UWORD	second_js;					// 2 second
UBYTE PidFlg;						// pid caculate flag;
UWORD Micro_js=0;					//
UBYTE	M10_js;						
UBYTE	Feng_js;
//-----------------------------------------------------------------------------

union crc  //check crc 2 byte;
	{
		unsigned int word;
		struct 
			{
				unsigned char	hi;
				unsigned char	lo;
				
			}bytes;
	};
union crc   crc_chec;

void crc_chec_fuc(unsigned char datain)
	{

		unsigned char i;
		crc_chec.bytes.lo=crc_chec.bytes.lo^datain;
		for(i=0;i<8;i++)
			{
				if((crc_chec.word & 0x0001)==1)
					{
						crc_chec.word=crc_chec.word>>1;
						crc_chec.word=crc_chec.word^0xa001;
					}
				else
					crc_chec.word=crc_chec.word>>1;
			}
	}

void start_crc_chec(void)
	{ 
		unsigned char i;
		crc_chec.word=0xffff;
		for(i=0;i<(gvc_num_tmp-2);i++)
			crc_chec_fuc(cy_receive_buf[i]);
		
	}
void delay()
{
	UBYTE i=255;
	while(--i);
}

void generic_crc(unsigned char cyz_num)
	{
		unsigned char	i;
		
		crc_chec.word=0xffff;
		for(i=0;i<cyz_num;i++)
			crc_chec_fuc(cy_receive_buf[i]);
		
		cy_receive_buf[cyz_num+1]=crc_chec.bytes.hi;
		cy_receive_buf[cyz_num]=crc_chec.bytes.lo;
	}

//发送一个字节
void uart_send(unsigned char message)
 	{
		SBUF=message;
		while(!TI);
		TI=0;
	}

void SendChar(char message)
{
		SBUF=message;
		while(!TI);
		TI=0;


}
//发送一帧数据
void frame_send(unsigned char t)//发送数据桢
	{
		unsigned char i;
		
		for(i=0;i<t;i++)
			//uart_send(gvc_send_buf[i]);
			uart_send(cy_receive_buf[i]);

	}

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

void uart_run(void)
	{ 
		//unsigned char x,y,x_num,y_num;
		unsigned int cy_q,cy_s;
		WordType lvi_add_tmp;
		UWORD tmp;

		if(cy_uartcomplete)

			{
		

			cy_uartcomplete=0;
			//uart_send(cy_receive_buf[0]);
				start_crc_chec();
			//	uart_send(crc_chec.bytes.hi);
			//	uart_send(crc_chec.bytes.lo);
				
				if((crc_chec.bytes.hi==cy_receive_buf[gvc_num_tmp-1])\
					&&(crc_chec.bytes.lo==cy_receive_buf[gvc_num_tmp-2]))	    //crc
						    {		
						    		//uart_send(0x77);
								
						    		switch(cy_receive_buf[1])
								    {
										
										case 3:													//读寄存器
											lvi_add_tmp.bytes.bh=cy_receive_buf[2];
											lvi_add_tmp.bytes.bl=cy_receive_buf[3];
											lvi_add_tmp.wd*=2;
											if((lvi_add_tmp.wd<40)&&(cy_receive_buf[5]<40)&&((lvi_add_tmp.wd+cy_receive_buf[5])<40))
												{

													cy_receive_buf[2]=cy_receive_buf[5]*2;			//data number
													gvc_data_num=3;
													cy_q=lvi_add_tmp.wd+cy_receive_buf[5]*2;		//the end addr


													
													for(cy_s=lvi_add_tmp.wd;cy_s<cy_q;cy_s++)
														{
															cy_receive_buf[gvc_data_num]=gvc_data_buf[cy_s];
															gvc_data_num++;
															
														}
													generic_crc(gvc_data_num);
													gvc_data_num=gvc_data_num+2;
													frame_send(gvc_data_num);//发送数据
													
												}
											
										break;

										case 6:												//写单个寄存器											
											lvi_add_tmp.bytes.bh=cy_receive_buf[2];
											lvi_add_tmp.bytes.bl=cy_receive_buf[3];
											lvi_add_tmp.wd*=2;
											if(lvi_add_tmp.wd<40)
												{

													gvc_data_buf[lvi_add_tmp.wd]=cy_receive_buf[4];
													gvc_data_buf[lvi_add_tmp.wd+1]=cy_receive_buf[5];
													gvc_data_num=6;
													generic_crc(gvc_data_num);
													gvc_data_num=gvc_data_num+2;
													frame_send(gvc_data_num);//发送数据
													ParaFresh(lvi_add_tmp.wd/2);
													tmp=cy_receive_buf[4];
													tmp<<=8;
													tmp+=cy_receive_buf[5];
													write_int(lvi_add_tmp.wd, tmp);
													//for(i=0;i<10;i++)
													//	ParaFresh(i);
												}


										break;

										case 16:
											lvi_add_tmp.bytes.bh=cy_receive_buf[2];
											lvi_add_tmp.bytes.bl=cy_receive_buf[3];
											lvi_add_tmp.wd*=2;
											if((lvi_add_tmp.wd<40)&&(cy_receive_buf[5]<40)&&((lvi_add_tmp.wd+cy_receive_buf[5])<40))
												{
													cy_q=cy_receive_buf[6];						//byte counter
													for(cy_s=0;cy_s<cy_q;cy_s++)
														{
															gvc_data_buf[lvi_add_tmp.wd+cy_s]=cy_receive_buf[7+cy_s];
														}
													
													gvc_data_num=6;
													generic_crc(gvc_data_num);
													gvc_data_num=gvc_data_num+2;
													frame_send(gvc_data_num);//发送数据
																									
												}
											for(i=0;i<10;i++)
												ParaFresh(i);


										break;
										
										default:
										break;
								     }								
							
								
						    }
				//
					
				cy_uartnumber=0;
				delay();
				delay();
			//	}

			}
	}			   


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

void Uart_int() interrupt 4 
{
	if(RI)
		{
			
			gvc_tmp=SBUF;

			cy_receive_buf[cy_uartnumber]=gvc_tmp;
			cy_uartnumber++;
					
			//if((cy_uartnumber>=40)||((cy_uartnumber==1)&&(cy_receive_buf[0]!=1)))
			if(cy_uartnumber>=40)
				cy_uartnumber=0;
			TL0=TL0_VALUE;
			TH0=TH0_VALUE;
			TR0=1;
			RI=0;
			//-------------------------------------------
			
			uart_rst_receive();

		}
			
}



void Timer0_interrupt() interrupt 1
{
	TH0=TH0_VALUE;
	TL0=TL0_VALUE;
	TR0=0;
	gvc_num_tmp=cy_uartnumber;
	cy_uartnumber=0;
	cy_uartcomplete=1;

}

void PCA_interrupt() interrupt 6
{
	
	//LED1=!LED1;
	pca_tmp.wd+=0xAF;
	CCAP0L=pca_tmp.bytes.bl;
	CCAP0H=pca_tmp.bytes.bh;
	CCF0=0;
	if(PwmFlg)
		{
			pca_js++;
			
			if(pca_js<(100-PWMValue))
				OUT2=1;
			else
				OUT2=0;

			if(pca_js<(100-SetFlow))
				OUT1=1;
			else
				OUT1=0;
			
			if(pca_js>=100)
				{
					pca_js=0;
					OUT1=1;
					OUT2=1;
					PwmFlg=0;
					//LED1=!LED1;

				}
			
		}
	Adc_js++;
	if(Adc_js>=200)
		{
			Adc_js=0;
			AdcFlg=1;
		}
	second_js++;
	
	if(second_js>=1053)				//100ms
		{

			second_js=0;
			Micro_js++;
			if(Micro_js>=AdjTime)
				{
					Micro_js=0;
					PidFlg=1;
				}
		}
	#if 0
	M10_js++;
	if(M10_js>=105)			//10ms
		{
			M10_js=0;
			Feng_js++;
			if(Feng_js<(100-SetFlow))
				OUT1=1;
			else
				OUT1=0;
			if(Feng_js>=100)
				{
					Feng_js=0;
					OUT1=1;
				}
				

		}
	#endif

}

void EX0_interrupt() interrupt  0 using 1
{
	
	//LED2=!LED2;
	PwmFlg=1;



}

⌨️ 快捷键说明

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