Design of PIC microcontroller for host computer data acquisition

Publisher:茶叶侠Latest update time:2020-01-15 Source: elecfans Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

basic skills

In this design, data processing can use powerful software such as MATLAB on PC, but this type of existing data processing software cannot directly control the lower computer acquisition module of the specific data acquisition system. Therefore, it is necessary to write corresponding upper computer software for the specific data acquisition system. The upper computer software is designed and written for the above purpose and is the control front end and data storage and processing center of the entire acquisition system. The control function mainly includes controlling the start and end of the lower computer acquisition, the frequency of acquisition, etc. The data processing function mainly includes drawing waveform graphs, displaying data in lists, and storing data in files. Storing data in files will facilitate the use of existing data processing software to perform some numerical algorithm processing on the data to achieve scientific research, conclusion verification, etc.


Development Environment

The C++ programming language can well implement object-oriented programming ideas. When using C++ to write the host computer program, each functional module can be encapsulated into a class. Modifying the implementation of a class and adding class functions will not affect the framework of the entire program, which makes it easy to maintain and expand functions. In addition, the software functions we want to implement require calling a large number of Windows API function libraries, so VC++6.0 is used as the development environment for the host computer.


Program function module division

The overall functional module mainly includes three modules, namely, HID device reading and writing module, data acquisition module, and data processing module.

Design of PIC microcontroller for host computer data acquisition

Searching and reading and writing HID devices

(1) Enumeration

After the USB host detects that a USB device is plugged in, it will enumerate the device. Enumeration is to read some information from the device to know what kind of device it is and how to communicate, so that the host can load the appropriate driver based on this information.

(2)HID

Human Interface Device (HID) refers to devices that interact directly with people, such as mice and keyboards. In Windows, devices with similar properties and providing similar services are classified as one device type, and one type of device can use a universal device driver. On a PC running Windows 98 or higher, applications can use the HID class driver built into the operating system to communicate with the HID. This makes it easy to develop and run USB devices that comply with the HID class.

(3) Searching for HID devices

There are many HID-related API functions built into the Windows operating system. By calling these functions, you can start searching for the specified HID device. The ultimate goal of searching for the HID device is to obtain the path name of the device, the device's access capacity and other information, so as to prepare for future reading and writing of the device.

Design of PIC microcontroller for host computer data acquisition

(4) Reading and writing HID devices

After obtaining the path of the HID device, you can start reading and writing the HID device. The reading and writing of the device is also achieved by calling the corresponding functions.


Control the lower computer to collect data

The upper computer sends commands to the lower computer, controls the lower computer to collect data, and obtains data from the lower computer. In this process, the synchronization of the two threads must be handled properly, that is, the data collection thread and the data processing thread can work in coordination to ensure that the system can work correctly and stably. The specific solution is to implement atomic operations on certain data accesses, that is, when one thread accesses public data, another thread cannot interrupt until the operation thread completes the operation and gives up the right to use the data, and then the other thread can access the data.


After the lower computer obtains the relevant parameters for collection, it can start collecting data, collecting data at a certain interval. When the number of collected data reaches the limit value, the collection is completed. At this time, the lower computer starts to send the collected data to the upper computer.

Design of PIC microcontroller for host computer data acquisition

Processing of collected data by the host computer

After the upper computer sends the data acquisition command to the lower computer, all it has to do is wait for the lower computer to complete the acquisition and receive the data. Therefore, the upper computer will cyclically query the working status of the lower computer. Once it detects the sign that the lower computer has completed the acquisition, the upper computer will start processing the data.

There are three types of data processing:

(1) Draw a waveform graph

There are two requirements for drawing a waveform graph: first, it should not flicker frequently, which would affect observation; second, the waveform graph is dynamic, because the drawing area is limited, while the collected data is constantly increasing, so the waveform graph is required to be able to be updated dynamically.

(2) Add to list display

All data collected so far can be viewed intuitively.

(3) Save to file

Use powerful data processing software to process the data more deeply.

Interface display

Collecting unipolar sine wave working interface

Design of PIC microcontroller for host computer data acquisition

Code:

1 HID device communication module implementation code /*hid.h header file*/

2 #ifndef HID_H

3 #define HID_H

4 #include

5 #include

6 #include

7 #include “commonuse.h”

8 using std::string;

9 #pragma comment( lib, “setupapi.lib” )

10 external “C” {

11 #include “hidsdi.h”

12 }

13 #pragma comment( lib, “hid.lib” )

14

15

16 class Hid

17 {

18

19 public:

20 Hid(const string &DeviceIdStr = MY_DEVICE_ID);

21 //Hid(DWORD Vid, DWORD Pid) {}

22 ~Hid() ;

23 BOOL Connect() ;

24 //BOOL ChangeDevice() {}

25 BOOL WriteHid(const BYTE * WriteBuff);

26 BOOL ReadHid(BYTE * ReadBuff);

27 BOOL IsWriteValid() const { return m_WriteValid ; }

28 BOOL IsReadValid() const { return m_ReadValid ; }

29 BOOL IsConnected() const { return m_IsConnected; }

30 const string & GetDeviceIDDesc() const { return m_DeviceIdStr ;}

31 private:

32 BOOL GetWRHandle() ;

33 private:

34 HANDLE m_WriteHandle;

35 HANDLE m_ReadHandle ;

36 string m_DeviceIdStr; //Device description string

37 DWORD m_PID;

38 DWORD m_VID;

39 BOOL m_IsConnected ; //Is it connected?

40 BOOL m_ReadValid; //Whether the read operation is possible

41 BOOL m_WriteValid; //Whether the write operation is possible

42 BYTE m_RWBuff[USB_BUFF_SIZE+1] ; // read and write buffer

43

44

45 } ;

46

47

48

49 #endif

50

51

52 /*hic.cpp source file*/

53

54 #include “Hid.h”

55

56 Hid::Hid(const string &DeviceIdStr):

57 m_DeviceIdStr(DeviceIdStr)

58 {

59

60 m_WriteHandle = INVALID_HANDLE_VALUE ;

61 m_ReadHandle = INVALID_HANDLE_VALUE ;

62 m_PID = 0;

63 m_WID = 0;

64 m_IsConnected = FALSE ;

65 m_ReadValid = FALSE ;

66 m_WriteValid = FALSE;

67 strcpy((char *)m_RWBuff,“”) ;

68 }

69

70 BOOL Hid::GetWRHandle()

71 {

72 GUID InterfaceClassGuid =

73 {0x4d1e55b2, 0xf16f, 0x11cf, 0x88, 0xcb, 0x00, 0x11, 0x11, 0x00, 0x00, 0x30};

74 HDEVINFO DeviceInfoTable = INVALID_HANDLE_VALUE;

75 PSP_DEVICE_INTERFACE_DATA InterfaceDataStructure = new SP_DEVICE_INTERFACE_DATA;

76 PSP_DEVICE_INTERFACE_DETAIL_DATA DetailedInterfaceDataStructure = new SP_DEVICE_INTERFACE_DETAIL_DATA;

77 SP_DEVINFO_DATA DevInfoData;

78

79 DWORD InterfaceIndex = 0;

80 DWORD StatusLastError = 0;

81 DWORD dwRegType;

82 DWORD dwRegSize;

83 DWORD StructureSize = 0;

84 PBYTE PropertyValueBuffer;

85 bool MatchFound = false;

86 DWORD ErrorStatus;

87 DeviceInfoTable = SetupDiGetClassDevs(&InterfaceClassGuid, NULL, NULL, DIGCF_PRESENT “ DIGCF_DEVICEINTERFACE);

88 while(true)

89 {

90 InterfaceDataStructure-》cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);

91 if(SetupDiEnumDeviceInterfaces(DeviceInfoTable, NULL, &InterfaceClassGuid, InterfaceIndex, InterfaceDataStructure))

92 {

93 ErrorStatus = GetLastError();

94 if(ERROR_NO_MORE_ITEMS == ErrorStatus)

95 {

96 SetupDiDestroyDeviceInfoList(DeviceInfoTable);

97 return FALSE;

98 }

99 }

100 else

101 {

102

103 ErrorStatus = GetLastError();

104 SetupDiDestroyDeviceInfoList(DeviceInfoTable);

105 return FALSE;

106 }

107

108 DevInfoData.cbSize = sizeof(SP_DEVINFO_DATA);

109 SetupDiEnumDeviceInfo(DeviceInfoTable, InterfaceIndex, &DevInfoData);

110

111 SetupDiGetDeviceRegistryProperty(DeviceInfoTable, &DevInfoData, SPDRP_HARDWAREID, &dwRegType, NULL, 0, &dwRegSize);

112 Pr

Reference address:Design of PIC microcontroller for host computer data acquisition

Previous article:How to use PIC microcontroller to realize the function of timing stopwatch
Next article:The configuration word of the PIC microcontroller is defined using the CONFIG command

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号