Abstract: Water wastage in agriculture is a serious problem in China, with low irrigation water utilization efficiency. Therefore, improving the utilization rate of agricultural irrigation water resources is crucial. To conserve and better utilize agricultural irrigation water resources, a real-time monitoring system for agricultural irrigation flow based on CAN bus control was designed. This system features sensitive measurement and display functions. It uses an AT89C51 microcontroller as the platform for flow display, an RG-1 flow meter for flow measurement, and an independent CAN communication controller (SJA1000) and CAN bus standard to design a water flow monitoring system. This system boasts high measurement accuracy, simple structure, low cost, and good stability and repeatability. It can replace traditional flow monitoring systems, achieving accurate monitoring and control of agricultural irrigation water flow.
Keywords: CAN bus; microcontroller; control system; flow meter; SJA1000
AgriculturalirrigationbasedonCANbuscontrolmonitoringsystemdesign
WENLong-Liu1, ZHENGChanJiang1
(College of Automation and Electronic Engineering, Qingdao University of Science and Technology, Qingdao266042, China)
Abstract: Theshortageofwaterresourceinourcountry,resourceutilizationislow,howtoimprovetheutilizationofagriculturalirrigationwaterisextremelyimportant.Basedonfieldbuscommunicationandcontroltechnologyiscurrentlyoneofthemaintechno logyinthefieldofindustrialautomaticcontrol,buildingcontrolsystembysingle-chipmicrocomputerandwithSJA1000andTJA1050constructionofCANbuscontrollerforagriculturalirrigationwatermetermonitoring,achievethegoalofsavingwater.
Keywords: CANbus;Single-chipmicrocomputer;Flowgauge;TJA1050;SJA1000;Controlsystem;
0 Introduction
In recent years, with the development of the economy and society, the demand for water in various places has gradually increased, and the contradiction between water supply and demand has become increasingly prominent. my country's water resource utilization efficiency is very low, the construction of agricultural irrigation water conservancy facilities is backward, and water waste is serious. Therefore, it is extremely important to improve the utilization rate of agricultural irrigation water resources. By measuring the real-time information of irrigation flow, water resources can be effectively saved and utilized, which requires an automatic control system. The key to ensuring system stability and reducing system cost is the communication method used in the automatic control system. Fieldbus-based communication and control technology is one of the main technologies in the field of industrial automatic control. It has the advantages of information digitization and control decentralization, and its application in the field of automatic control is becoming increasingly widespread. The international advanced level of fieldbus technology has reached the stage of promoting the control of 10km distance with two-core carrier cable, but in the application of field agricultural irrigation control systems in my country, the control technology based on the bus method is still quite weak [1]. Therefore, by drawing on advanced design ideas, an irrigation control system based on fieldbus with independent intellectual property rights can be developed, which meets the needs of agricultural modernization for automation technology.
1 System Composition
A field network is formed, and a data conversion system based on the AT89C51 processor is used. The CAN bus module of this system uses the AT89C51 as the microprocessor. For the CAN bus communication interface, Philips' SJA1000 and TJA1050 chips are used. The SJA1000 is an independent CAN communication controller, and the TJA1050 is a high-performance CAN bus transceiver. The circuit mainly consists of four parts: the AT89C51 microcontroller, the independent CAN communication controller SJA1000, the CAN bus transceiver TJA1050, and the flow meter. The AT89C51 microprocessor is responsible for initializing the SJA1000 and controlling the SJA1000 to perform communication tasks such as data reception and transmission. The system structure schematic diagram is shown in Figure 1-1.
Figure 1-1 System Structure Schematic Diagram
1. Irrigation System Measurement and Data Processing
The water flow sensor mainly consists of a valve body, a water flow rotor assembly, and a Hall sensor. It is installed at the inlet to detect the inlet water flow. When water passes through the water flow rotor assembly, the magnetic rotor rotates, and its speed changes with the flow rate. The Hall sensor outputs a corresponding pulse signal, which is fed back to the controller. The controller then determines the water flow rate and adjusts accordingly. The wiring ports include a positive terminal, a signal output line, and a negative terminal. The microcontroller calculates the number of output pulses and, combined with the parameters of the water flow meter, calculates the flowing water flow rate. The microcontroller's I/O interface connects to the memory, allowing it to process and store the acquired data. The microcontroller's I/O interface connects to the LCD and the host computer, enabling real-time display of the processed data and uploading it to the host computer for storage. The wiring method for the flow meter is shown in Figure 1-2.
Figure 1-2 Water flow sensor wiring port
1.2 Composition of the Communication Section of the Irrigation System
There are two main types of CAN bus devices: one is a microcontroller with on-chip CAN and the other is a standalone CAN controller. This design uses the PHILIPS SJA1000 CAN controller and TJA1050 bus transceiver. The TJA1050 can support 110 CAN nodes, and the SJA1000 supports the CAN2.0A/B protocol. The SJA1000 is used in controller area networks in automotive and general industrial environments. It is a replacement for the PHILIPS Semiconductor PCA82C200 CAN controller (BasicCAN). Moreover, it adds a working mode that supports the CAN2.0B protocol with many new features [2]. The chip contains circuits such as information buffer, bit stream processing, bit timing logic, receive filtering, and error management logic, and is equipped with rich function registers. It can complete data framing, bus filling, error detection, bus arbitration, and error definition processing. The SJA1000's data lines AD0-AD7 are connected to the microcontroller's P0 port, and /CS is connected to P2.0. When P2.0 is low, the external memory address of the CPU can be selected as SJA1000. The CPU can read/write operations on SJA1000 through these addresses. The ALE, /WR, and /RD pins of SJA1000 are connected to the corresponding pins of the CPU. /INT is connected to the INT0 of the CPU. The CPU accesses SJA1000 through interrupt. In the experiment, the anti-interference capability of the CAN bus node should be strengthened. This requires that the RX0 and TX0 of SJA1000 are not directly connected to the RXD and TXD of TJA1050, but are connected to TJA1000 through the optocoupler 6N137. In this way, the electrical isolation between the nodes on the bus can be better achieved. However, it should be noted that the two power supplies used in the optocoupler part must be completely electrically isolated. Otherwise, the use of optocouplers will lose its meaning. Power isolation can be achieved by using a switching power supply with multiple 5V isolated outputs [3]. The connection principle diagram between the microcontroller and SJ1000 is shown in Figure 1-3:
Figure 1-3 Schematic diagram of SJA1000 interface with microcontroller
TJA1050 is the interface between the Controller Area Network (CAN) protocol controller and the physical bus. TJA1050 can provide differential receiving performance for the CAN controller and differential transmitting performance for the bus. It can achieve optimal matching of the output signals CANH and CANL, and reduce electromagnetic radiation. The CAN bus interface of TJA1050 has some anti-interference and safety measures. The two pins of TJA1000, CANL and CANH, are connected to the CAN bus with a 5.1Ω resistor, which can play a current limiting protection role, so that TJA1000 is not damaged by overcurrent[4]. Two 30pF capacitors are connected in parallel between ground and CANL and CANH, which have the ability to prevent radiation and filter high-frequency interference on the bus. In addition, two surge protectors can be connected between CANH, CANL and ground. When transient interference occurs between ground and the two input terminals, the discharge of the surge protectors can play a certain protective role[5]. The schematic diagram of TJA1050 is shown in Figure 1-4:
Figure 1-4 Design Schematic Diagram of TJA1050
2. System Software Design
The software design of the CAN bus mainly includes three parts: CAN initialization program, message sending program, and message receiving program [6]. CAN initialization mainly sets the communication parameters of CAN. The CAN control registers that need to be initialized are: mode register, time division register, receive code register, mask register, bus timing register, output control register, etc. It is worth noting that the above registers can only be written to and accessed when the CAN controller is in the reset state [7]. The data sending program takes out the data to be sent from the data storage area, forms an information frame, fills the host ID address into the frame header, and sends the information frame to the CAN controller's send buffer. After receiving the host's send request, the sending program starts the send command. The information is sent from the CAN controller to the bus automatically by the CAN controller. The information is also sent from the CAN bus to the CAN controller's receive buffer automatically by the CAN controller. The receiving program only needs to read the information from the receive buffer and store it in the data storage area.
2.1 CAN bus node initialization program
Node initialization primarily refers to the initialization of the 89C51 microcontroller and the SJA1000 CAN controller after system power-on, ensuring the operating frequency, output characteristics, and baud rate. 89C51 initialization can be performed in conjunction with their monitoring tasks, mainly involving enabling and disabling interrupts, and using and setting timers. This section focuses on SJA1000 initialization. The SJA1000 lacks an internal microprocessor; its initialization relies on programming the 89C51. SJA1000 initialization can only be performed in reset mode. Therefore, the first step in the SJA1000 initialization program is to switch the operating mode to reset mode, followed by setting the acceptance filtering method, etc. In the CAN protocol physical layer, the communication baud rate and synchronization jump width are determined by the programs in the timer registers BTR0 and BTR1. It is crucial to emphasize that the contents of these two registers must be identical for all nodes in the system; otherwise, communication will be impossible. After the initialization settings are completed, set the reset request position to '0', and the SJA1000 will be able to enter the working state to complete normal communication tasks [8]. The initialization procedure is as follows:
#include<80c196kd.h> // Includes controller register definitions
#include_SFR_H_
#include_FUNCS_H_
#define BASE_CAN0xa000 // Define the base address of the CAN controller
typedef struct {
unsigned int id; /* Message identifier */
unsignedcharrtr; /* Remote frame bit */
unsignedchardlen; /* Data length */
unsignedchardata[8]; /* data */
}MSG_STRUCT; /* Represents CAN protocol frames using C language structures */
void init_can(){
*(unsignedchar*)(BASE_CAN+0)=0x01;
/*SJA1000 enters reset state*/
*(unsignedchar*)(BASE_CAN+4)=0x00;
/* Initialize the Receive Code Register (ACR) */
*(unsignedchar*)(BASE_CAN+5)=0xff;
/* Initialize the Receive Mask Register (AMR) */
*(unsignedchar*)(BASE_CAN+6)=0x00;
/* Initialize bus timing register BTR0 */
*(unsignedchar*)(BASE_CAN+7)=0x14;
/* Initialize bus timing register BTR1 */
*(unsignedchar*)(BASE_CAN+8)=0xfa;
/* Initialize the output control register OCR */
}
2.2 Message Sending Program
The sending program is responsible for sending node messages. When sending, the user only needs to combine the data to be sent into a frame according to a specific format, send it to the SJA1000 send buffer, and set the send request flag (TR) bit in the SJA1000 command register. The SJA1000 will automatically start the sending process. However, before sending a message to the SJA1000 send buffer, it is necessary to check whether the send buffer is released. Only when the send buffer flag (TBS) is "1" is the send buffer released, and new messages can be written to the send buffer. Otherwise, when the send buffer is locked, new messages cannot be written to the send buffer. The sending program is divided into two types: data frames and remote frames. Remote frames have no data field. The sending program is generally written as a subroutine. The message sending program is as follows:
unsignedcharcan_send(MSG_STRUCTsmsg)
{unsignedcharv;
inti;
v=*(unsignedchar*)(BASE_CAN+2);
if(v&0x08) /* Determine if data can be sent */
{v=smsg.id>>3;/*The identifier is sent to the identification code register*/
*(unsignedchar*)(BASE_CAN+10)=v;
v=*(unsignedchar*)(BASE_CAN+10);
v=smsg.id&7; /* Identifier code 0-2 digits, RTR, DLC */
v<<=5;
v += smsg.dlen;
*(unsignedchar*)(BASE_CAN+11)=v;
for(i=0;ii
{
*(unsignedchar*)(BASE_CAN+12+i)=smsg.data[i];
}
*(unsignedchar*)(BASE_CAN+1)=0x01;
return(1);
}
else
return(0);
}
2.3 Message Receiving Procedure
The message reception of SJA1000 is completed independently. After the received message is filtered and accepted, it is temporarily placed in the receive buffer FIFO. After the message enters the receive buffer, the RBS of the status register will be set to '1'. At the same time, if the RIE of the interrupt enable register is set to '1', the RI bit of the interrupt register will also be set to '1'. Then SJA1000 sends an interrupt request to the CPU. Message reception can be performed using either polling or interrupt reception. If the real-time requirements of communication are not so strong, polling can be used [9]. The message reception procedure is as follows:
unsignedcharcan_receive()
{
MSG_STRUCTrmsg;
inti;
unsigned char buf1, buf2;
while((*(unsignedchar*)(BASE_CAN+2))&0x01)
/* Determine if there is any receivable information */
{
buf1=*(unsignedchar*)(BASE_CAN+20);
/* Extract a frame of information */
buf2=*(unsignedchar*)(BASE_CAN+21);
rmsg.dlen=buf2&0x0f; /* Data length */
for(i=0;i
rmsg.data[i]=*(unsignedchar*)(BASE_CAN+22+i);
}
*(unsignedchar*)(BASE_CAN+1)=0x04;
/*Release the receive buffer*/
rmsg.rtr=(buf2>>4)&0x01;/*Remote frame*/
rmsg.id=buf1; /* Retrieve the message identifier */
rmsg.id <<= 3;
rmsg.id|=(buf2>>5)&0x06;
switch(rmsg.id) /* Switch to the appropriate data processing program based on the identifier */
case
......
break
}
3. Summary of the necessity of system application
China is a country with severe water scarcity, making the promotion of water-saving irrigation imperative. Implementing a cubic metering-based pricing system is an inevitable trend for farmland irrigation water use, ultimately aiming to achieve integrated microcomputer-based automatic monitoring, metering, and billing of farmland irrigation water. The CAN bus is a hot topic in the field of automation control, hailed as the local area network of automation, and is now widely used in various fields of industrial control. This design effectively integrates the controller with the CAN bus, fully leveraging its advantages to play a greater role in farmland irrigation monitoring.
References
[1] Rao Yuntao, Zou Jijun, Zheng Yongyun. Fieldbus CAN Principles and Application Technology [M]. Beijing: Beijing University of Aeronautics and Astronautics Press, 2003.
[2] Bi Xueqin. Proceedings of the Symposium on Fieldbus and Development Trends: Current Status and Development of Process Detection and Control Instruments and Systems, 1996(12): 252-257
[3] Wu Weijie. Design and research of water-saving irrigation automatic control system based on CAN bus [J] Water Saving Irrigation 2006(1)13-5
[4] Wu Kuanming. CAN Bus Principles and Application System Design. Beijing: Beijing University of Aeronautics and Astronautics Press, 1996.
[5] Shi Jiugen, Zhang Peiren, Chen Zhenyong. CAN Fieldbus System Design Technology. Beijing: National Defense Industry Press, 2004.
[6] Chen Yang, Liu Shusheng, Long Zhiqiang. Research on data communication system based on CAN bus [J]. Measurement and Control Technology, 2000, 19(10): 53-55
[7] Shi Jiugen et al. CAN Fieldbus Design Technology [M]. Beijing: National Defense Industry Press, 2004.
[8] Cai Huafeng, Liao Dongchu, Pan Jian, et al. Application of CAN controller in C8051F040 [J]. Microcontroller & Embedded System Applications, 2005(1): 55-58.
[9] Zhang Lei, Li Yao, Liu Guanghui. Field monitoring system based on CAN bus network [J]. Instrumentation Technology and Sensors, 2007(12):39-41,49