当前位置:网站首页>Motor monitoring system based on MCGS and stm32

Motor monitoring system based on MCGS and stm32

2022-06-26 00:50:00 To, violet

0、 Project brief introduction

     This project is a monitoring system developed by our school and department with the help of the factory , Based on resource sharing 、 The author summarizes the project with this article . The current demand of the project is relatively simple , Main detection 5 Road temperature information and reflect its information to MCGS On the configuration screen , Two of them use 18B20 Digital temperature sensor , Three way use PT100 Thermistor . Next, I will start from MCGS、MODBUS Bus protocol and PT100 Drive circuit and other aspects are explained in detail .

One 、MCGS brief introduction

    MCGS It is developed by Beijing Kunlun Tongtai Automation Software Technology Co., Ltd. based on windows Configuration system of the platform . This project mainly uses MCGS Embedded version of , Is commonly known as MCGS Configure the touch screen ( Figure 1 ). The screen has a 9 Needle interface , Support RS232 and RS485 Communication protocol .( Figure 2 , User interface ) This project mainly uses MCGS adopt RS485 The bus uses MODBUS-RTU Protocol for networking communication .

Control page

Two 、MODBUS Protocol is introduced

MODBUS The communication protocol is composed of Modicon stay 1979 Invented in , It is also the most commonly used industrial bus protocol nowadays .

1、MODBUS There are four main types of registers in : Coil register 、 Discrete input registers 、 Input register and holding register .

2、 Common function codes

Function code describe position / Word operation Number of operations
01H Read coil register Bit operation Single 、 Multiple
02H Read the discrete access register Bit operation Single 、 Multiple
03H Read holding register Word operation Single 、 Multiple
04H Read input register Word operation Single 、 Multiple
05H Write coil register Bit operation Single
06H Write hold register Word operation Single
............

     All used in this project are 4 Area readable and writable holding registers are used only by function codes 03H( read )、06H( Write ) Two

3、MODBUS message

    MCGS When the machine is started, it will continuously send messages to read the required registers .MCGS The format of read message is as follows

    

 

Slave address Function code Register start address high bit Register start address low order The number of registers is high The number of registers is low CRC High position CRC Low position
010300640001  

     It is not difficult to see that the message is read 1 Slave No 100H A message that addresses data from a register , The message response is as follows :

 

Slave address Function code Number of bytes data-high The data is low CRC High position CRC Low position
0103010064  

     The read / write and response of other registers are similar . Please refer to MODBUS Function code , ad locum MODBUS Not much .

3、 ... and 、STM32 simulation MODBUS agreement

    The main control chip used in this project is Cortex-M3 kernel ST The company's STM32F103 Series of microcontrollers .

1、RS485 turn TTL

    Because the embedded chip can receive TTL level , So it is necessary to RS485 The level of is converted to TTL level . Here we use MAX485 chip , The driving circuit is shown in the following figure , Considering that the stability of hardware automatic flow direction control is not high , Therefore, the circuit design does not use automatic flow control .

MAX485 Drive circuit

2、MAX485

    MAX485 The pin diagram and truth table are shown in the figure below

MAX485 Pin diagram and internal structure

3、MAX485 Pin function

 

Pin name function
1RO Receiver output
2RE Receiver output enable , by 0 Allow output
3DE Drive receive enable , by 1 Allow the drive to work
4DI Drive input
5GND The earth
6A485+
7B485-
8VCC Drive power supply ,5V

4、STM32 Receive code

    Because both sending and receiving are in the form of message , In addition, it is necessary to ensure that there is strong discrimination ability in case of communication errors, so TIM2 The timer is used to judge the receiving timeout . In the serial port interrupt program, two static variables are defined to count and cache data .

void USART1_IRQHandler()
{
	static unsigned char zs_cTime;// Amount of data saved 
	static unsigned char zs_cData[20];// Save the received data 
	unsigned char i;
	OSIntEnter();// The notification system has entered an interrupt 
	USART_ClearFlag(USART1, USART_FLAG_RXNE);// eliminate USARTx Pending flag bit 
	if(g_Tset>=1)// Whether to receive data timeout 
	{
		g_Tset = 0;// Clear flag 
		zs_cTime = 0;// Clear the counter 
		for(i=0;i<20;i++)
		{
			zs_cData[i] = 0;// Empty array 
		}
	}
	TIM_Cmd(TIM2, DISABLE);// off timer 
	zs_cData[zs_cTime] = USART_ReceiveData(USART1);// Receive recent data 
	zs_cTime++;
	g_cUartData = zs_cData;// Reading data 
	TIM_SetCounter(TIM2,0);// Reload Time2 Value 
	TIM_Cmd(TIM2, ENABLE);// Start timer 
	OSIntExit();// Notify the system of an interrupt completion 
}

    TIM2 initialization

void _init_Time2(void)
{
	TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;	//0.0025s Break once 
	TIM_TimeBaseStructure.TIM_Prescaler = 35999;			
	TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
	TIM_TimeBaseStructure.TIM_Period = 5;//
	TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
	TIM_TimeBaseInit(TIM2,&TIM_TimeBaseStructure);
	TIM_PrescalerConfig(TIM2, 35999, TIM_PSCReloadMode_Immediate);
	TIM_Cmd(TIM2, DISABLE);
	TIM_ClearFlag(TIM2, TIM_FLAG_Update);// Clears the interrupt flag bit 
	TIM_ITConfig(TIM2, TIM_IT_Update,ENABLE);// To interrupt 
}

    TIM2 interrupt

void TIM2_IRQHandler()
{
	OSIntEnter();// The notification system has entered an interrupt 
	TIM_ClearFlag(TIM2, TIM_FLAG_Update);// Clears the interrupt flag bit 
	TIM_Cmd(TIM2, DISABLE);// off timer 
	g_Tset = 1;// Interrupt flag bit 
	OSMboxPost(Uart1Box,&g_Tset);// Send flag bit 
	OSIntExit();// Notify the system of an interrupt completion 
}

4、CRC Computing unit

unsigned char auchCRCHi[]={
0x00,0xC1,0x81,0x40,0x01,0xC0,0x80,0x41,0x01,0xC0,
0x80,0x41,0x00,0xC1,0x81,0x40,0x01,0xC0,0x80,0x41,
0x00,0xC1,0x81,0x40,0x00,0xC1,0x81,0x40,0x01,0xC0,
0x80,0x41,0x01,0xC0,0x80,0x41,0x00,0xC1,0x81,0x40,
0x00,0xC1,0x81,0x40,0x01,0xC0,0x80,0x41,0x00,0xC1,
0x81,0x40,0x01,0xC0,0x80,0x41,0x01,0xC0,0x80,0x41,
0x00,0xC1,0x81,0x40,0x01,0xC0,0x80,0x41,0x00,0xC1,
0x81,0x40,0x00,0xC1,0x81,0x40,0x01,0xC0,0x80,0x41,
0x00,0xC1,0x81,0x40,0x01,0xC0,0x80,0x41,0x01,0xC0,
0x80,0x41,0x00,0xC1,0x81,0x40,0x00,0xC1,0x81,0x40,
0x01,0xC0,0x80,0x41,0x01,0xC0,0x80,0x41,0x00,0xC1,
0x81,0x40,0x01,0xC0,0x80,0x41,0x00,0xC1,0x81,0x40,
0x00,0xC1,0x81,0x40,0x01,0xC0,0x80,0x41,0x01,0xC0,
0x80,0x41,0x00,0xC1,0x81,0x40,0x00,0xC1,0x81,0x40,
0x01,0xC0,0x80,0x41,0x00,0xC1,0x81,0x40,0x01,0xC0,
0x80,0x41,0x01,0xC0,0x80,0x41,0x00,0xC1,0x81,0x40,
0x00,0xC1,0x81,0x40,0x01,0xC0,0x80,0x41,0x01,0xC0,
0x80,0x41,0x00,0xC1,0x81,0x40,0x01,0xC0,0x80,0x41,
0x00,0xC1,0x81,0x40,0x00,0xC1,0x81,0x40,0x01,0xC0,
0x80,0x41,0x00,0xC1,0x81,0x40,0x01,0xC0,0x80,0x41,
0x01,0xC0,0x80,0x41,0x00,0xC1,0x81,0x40,0x01,0xC0,
0x80,0x41,0x00,0xC1,0x81,0x40,0x00,0xC1,0x81,0x40,
0x01,0xC0,0x80,0x41,0x01,0xC0,0x80,0x41,0x00,0xC1,
0x81,0x40,0x00,0xC1,0x81,0x40,0x01,0xC0,0x80,0x41,
0x00,0xC1,0x81,0x40,0x01,0xC0,0x80,0x41,0x01,0xC0,
0x80,0x41,0x00,0xC1,0x81,0x40
};
unsigned char auchCRCLo[]={
0x00,0xC0,0xC1,0x01,0xC3,0x03,0x02,0xC2,0xC6,0x06,
0x07,0xC7,0x05,0xC5,0xC4,0x04,0xCC,0x0C,0x0D,0xCD,
0x0F,0xCF,0xCE,0x0E,0x0A,0xCA,0xCB,0x0B,0xC9,0x09,
0x08,0xC8,0xD8,0x18,0x19,0xD9,0x1B,0xDB,0xDA,0x1A,
0x1E,0xDE,0xDF,0x1F,0xDD,0x1D,0x1C,0xDC,0x14,0xD4,
0xD5,0x15,0xD7,0x17,0x16,0xD6,0xD2,0x12,0x13,0xD3,
0x11,0xD1,0xD0,0x10,0xF0,0x30,0x31,0xF1,0x33,0xF3,
0xF2,0x32,0x36,0xF6,0xF7,0x37,0xF5,0x35,0x34,0xF4,
0x3C,0xFC,0xFD,0x3D,0xFF,0x3F,0x3E,0xFE,0xFA,0x3A,
0x3B,0xFB,0x39,0xF9,0xF8,0x38,0x28,0xE8,0xE9,0x29,
0xEB,0x2B,0x2A,0xEA,0xEE,0x2E,0x2F,0xEF,0x2D,0xED,
0xEC,0x2C,0xE4,0x24,0x25,0xE5,0x27,0xE7,0xE6,0x26,
0x22,0xE2,0xE3,0x23,0xE1,0x21,0x20,0xE0,0xA0,0x60,
0x61,0xA1,0x63,0xA3,0xA2,0x62,0x66,0xA6,0xA7,0x67,
0xA5,0x65,0x64,0xA4,0x6C,0xAC,0xAD,0x6D,0xAF,0x6F,
0x6E,0xAE,0xAA,0x6A,0x6B,0xAB,0x69,0xA9,0xA8,0x68,
0x78,0xB8,0xB9,0x79,0xBB,0x7B,0x7A,0xBA,0xBE,0x7E,
0x7F,0xBF,0x7D,0xBD,0xBC,0x7C,0xB4,0x74,0x75,0xB5,
0x77,0xB7,0xB6,0x76,0x72,0xB2,0xB3,0x73,0xB1,0x71,
0x70,0xB0,0x50,0x90,0x91,0x51,0x93,0x53,0x52,0x92,
0x96,0x56,0x57,0x97,0x55,0x95,0x94,0x54,0x9C,0x5C,
0x5D,0x9D,0x5F,0x9F,0x9E,0x5E,0x5A,0x9A,0x9B,0x5B,
0x99,0x59,0x58,0x98,0x88,0x48,0x49,0x89,0x4B,0x8B,
0x8A,0x4A,0x4E,0x8E,0x8F,0x4F,0x8D,0x4D,0x4C,0x8C,
0x44,0x84,0x85,0x45,0x87,0x47,0x46,0x86,0x82,0x42,
0x43,0x83,0x41,0x81,0x80,0x40
};
// data   length   return CRC
unsigned int crc16(unsigned char *puchMsg,unsigned int usDataLen)
{
	unsigned char uchCRCHi=0xFF;
	unsigned char uchCRCLo=0xFF;
	unsigned long uIndex;
	while(usDataLen--)
	{
		uIndex=uchCRCHi^*puchMsg++;
		uchCRCHi=uchCRCLo^auchCRCHi[uIndex];
		uchCRCLo=auchCRCLo[uIndex];
	}
	return(uchCRCHi<<8|uchCRCLo);
}

Four 、 Hengli source and PT100 Drive circuit

    PT100 It is a commonly used temperature sensor in industry , The main feature of the sensor is that it is in 0 When PT100 The resistance value of the thermistor is 100Ω And the linearity is relatively good .

PT100 temperature - Resistance change curve

    PT100 Drive circuit scheme I : Single arm bridge R1、R2、R3 Is the bridge resistance ,RL Is thermistor , When the ambient temperature is 0 When the degree of ,PT100 The resistance shown is 100Ω, here V1、V2 The potential difference between is 0.. When the ambient temperature changes ,PT100 The resistance changes accordingly . and V1、V2 Along with PT100 Change and show a connection with PT100 The potential difference of a linear change in resistance .

Single arm bridge circuit

    This circuit has the advantage of simple design , But the shortcomings are also very obvious R1、R2、R3 The resistance accuracy of is relatively high . Therefore, the second scheme is adopted for the project .

 

    PT100 Drive circuit scheme II : Constant current source is adopted , The following circuit diagram . In circuit V0 The left end of is a first-order integrating circuit , The single chip microcomputer gives an accurate frequency of 1KHz Of PWM Modulation waveform , stay V0 The point forms a reference voltage .V0 Equivalent to constant voltage source , so V1=(V2-V0)/2. From deficiency to shortness V1=V3. because R3=R2 therefore V4=2V3=2V1=(V2-V0). From deficiency to shortness V2=V5. so Ir6=(V5-V4)/R6.IR6=[V2-(V2-V0)]/R6 so IR6=V0/R6.

Constant current source circuit

Through the above constant current source circuit , Known to flow through PT100 Current IR6, measurement V5 Point voltage can be obtained PT100 The resistance of .

5、 ... and 、STM32 Inside ADC And DMA

Because it is necessary to ensure the real-time performance of the whole system , Therefore, the query method cannot be used to read ADC The data of , Therefore, internal DMA. The code is as follows ( omitted RCC On ).

DAM Initializer

void init_DMA()
{
	DMA_InitTypeDef DMA_InitStructure;
	DMA_DeInit(DMA1_Channel1); // take DMA The passage of 1 Register reset to default 
	DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)&ADC1->DR; //DMA peripherals ADC Base address 
	DMA_InitStructure.DMA_MemoryBaseAddr = (u32)&AD_Value; //DMA Memory base address 
	DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; // Memory as the destination of data transmission 
	DMA_InitStructure.DMA_BufferSize = 3; //DMA The tunnel DMA Cache size 
	DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; // The peripheral address register remains unchanged 
	DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; // The memory address register is incremented 
	DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; // Data width is 16 position 
	DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; // Data width is 16 position 
	DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; // Working in circular cache mode 
	DMA_InitStructure.DMA_Priority = DMA_Priority_High; //DMA passageway  x Have high priority 
	DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; //DMA passageway x Not set to memory to memory transfer 
	DMA_Init(DMA1_Channel1, &DMA_InitStructure); // according to DMA_InitStruct The parameter specified in DMA The passage of 
	DMA_Cmd(DMA1_Channel1, ENABLE);// open  DMA
}

ADC Initializer

void init_ADC()
{
	ADC_InitTypeDef ADC_InitStructure;
	 ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
	 ADC_InitStructure.ADC_ScanConvMode = ENABLE;
	 ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;	// Set to continuous 
	 ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
	 ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
	 ADC_InitStructure.ADC_NbrOfChannel = 3;		// Pay attention to the numbers 3, To collect ADC1 The three channels of 
	 ADC_Init(ADC1, &ADC_InitStructure);
	 
	 ADC_TempSensorVrefintCmd(ENABLE);	// Turn on the... For internal temperature detection ADC passageway 
	 ADC_RegularChannelConfig(ADC1,ADC_Channel_1,1,ADC_SampleTime_239Cycles5);		// Notice the scanning sequence here RANK Parameters ( namely 1、2、3)
	 ADC_RegularChannelConfig(ADC1,ADC_Channel_TempSensor,2,ADC_SampleTime_239Cycles5);	// Pay attention to the setting of scanning cycle 
	 ADC_RegularChannelConfig(ADC1,ADC_Channel_Vrefint,3,ADC_SampleTime_239Cycles5);
	 
	 ADC_DMACmd(ADC1, ENABLE);	// Can make DMA
	 ADC_ExternalTrigConvCmd(ADC1, DISABLE);// Disable external trigger 
	
	 ADC_Cmd(ADC1, ENABLE);// Can make ADC

	 ADC_ResetCalibration(ADC1);// Reset the calibration register 

	 while(ADC_GetResetCalibrationStatus(ADC1));// Determine whether the reset is complete 

	 ADC_StartCalibration(ADC1);// Start calibration 

	 while(ADC_GetCalibrationStatus(ADC1));// Is the calibration complete 
	 
	 ADC_SoftwareStartConvCmd(ADC1, ENABLE);//
}

6、 ... and 、 Motor fault judgment

    Fault judgment is divided into two aspects , The first aspect is to give an alarm when the temperature exceeds a certain set value , But such an alarm system will lack rapidity , That is to say, the alarm will be given only after the excessive temperature caused by the measured object that has failed for a period of time is detected . Obviously, such a judgment method is not desirable , Or it can only be used as a standard for auxiliary judgment .

    In order to improve the advance of system judgment, we need to obtain the first-order differential and second-order differential of temperature change over a period of time .

    Suppose the following figure shows the temperature change curve of the motor .

Temperature curve

     Calculate the second derivative of the data according to the change curve

The second derivative of the temperature curve

    It is not difficult to see the bending reversal of the temperature change curve through the second derivative , It is not difficult to see that the motor is locked at this time .

7、 ... and 、 summary

    The project mainly involves MODBUS Communication protocol , as well as STM32 Of ADC coordination DMA Realize multi-channel data acquisition . The author's ability is limited , Give us more advice .

原网站

版权声明
本文为[To, violet]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202180601243914.html