Summary-Debug STM32F103ZET6 and peripheral sensors _mh-z14a carbon dioxide sensor

Source: Internet
Author: User

1. Overview

The module is mh-z14a carbon dioxide sensor, UART output, using USART2. 2. Wiring

The positive (pin 1) is connected to the P5 3 feet 5V, the negative (PIN 3) is connected to the P5 5 feet Gnd,rxd (pin 11) P5 6 feet Txd,txd (pin 19) connected P5 7 feet RXD. 3. Debug Key issues (1) Baud rate 9600, data bit 8 bit, stop bit 1 bit, check bit none. (2) Communication mode: STM32 sends the instruction of reading CO2 concentration through US2_TX, the sensor returns the concentration data by US2_RX. (3) Read gas concentration value command

0xFF 0x01 0x86 0x00 0x00 0x00 0x00 0x00 0x79

return value

0xFF 0x86 High Low----checksum value

Gas concentration value = high * + Low

(4) Calibration and calculation

<1> from Byte1 to byte7:0x01 + 0x86 + 0x00 + 0x00 + 0x00 + 0x00 + 0x00 = 0x87

<2> Reverse: 0xff-0x87 = 0x78

<3> Inverse plus 1:0x78 + 0x01 = 0x79

Char getchecksum (char *packet)
{
char I, checksum;
for (i = 1; i < 8; i++)
{
checksum + = Packet[i]
;
} checksum = 0xff–checksum;
Checksum + = 1;
return checksum;
}

(5) Start by using the Usart_senddata function to send command data to the serial port:

    for (i = 0; i < 9; i++)//Send read CO2 gas concentration instruction, after sending a byte must delay a period of time sensor to correctly receive the instruction
    {
	usart_senddata (usart2,co2txbuffer[ I]);
	Delay_ms (+);
    }

The discovery sends the reading CO2 gas concentration instruction, after sending a byte must delay a period of time the sensor to receive the instruction correctly, altogether sends 9 bytes.

It was later discovered that there was a dedicated send data function usart_senddata_length (usart2,co2txbuffer,9):

void Usart_senddata_length (usart_typedef* usartx, u8* data,u16 Length)
{/
  * Check The parameters */

	U16 i;< C3/>for (i=0;i<length;i++)
	{
		usart_senddata (Usartx, data[i]);//Send data to the serial while
		(Usart_getflagstatus ( USARTX,USART_FLAG_TC)!=set);//wait for Send end
	}
    
(6) Serial 2 interrupt processing function, update CO2 gas concentration data co2data:

void Usart2_irqhandler (void)                //Serial 2 Interrupt Service program
{
	char i, checksum = 0;
	if (Usart_getitstatus (USART2, usart_it_rxne)! = RESET)//Receive interrupt
		{
			usart_rx_buf_2[usart2count++]  = Usart_ Receivedata (USART2);//Read 9 bytes to usart_rx_buf_2
			if (usart2count = = 9)
				Usart2count = 0;//when the 9th character is received, the usart_rx_ from the array Buf_1 Start write//
			
			calculate checksum
			//1, from Byte1 to byte7:0x01 + 0x86 + 0x00 + 0x00 + 0x00 + 0x00 + 0x00 = 0x87
			//2, Reverse: 0xFF- 0x87 = 0x78
			//3, Inverse plus 1:0x78 + 0x01 = 0x79 for
			(i = 1; i < 8; i++)
			{
			checksum + = usart_rx_buf_2[i];< c16/>}
			checksum = 0xff-checksum;
			Checksum + = 1;
			}	
			if (checksum = = Usart_rx_buf_2[8])//prove that the received data is correct
				co2data = usart_rx_buf_2[2] * + usart_rx_buf_2[3];
	
}			
(7) Code address
4.mh-z14a Carbon dioxide sensor data











Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.