📜 ⬆️ ⬇️

"Patch" mirror in the car

A colleague at work bought a new car Chery Tiggo FL, where the Chinese stuffed many different functions for the low cost of the car. One of the most unusual things in the car turned out to be a “magic” mirror, which is endowed with the functions of a barometer, altimeter and compass.

The main disadvantage of all the stuffing of this car is the lack of temperature sensors behind the car and inside it. It was decided to change this disadvantage with a colleague by modifying the standard rear-view mirror with non-standard methods.


')
So, let's start creating a “patch” on the mirror.

Formulation of the problem. It is necessary in the car to receive information about the ambient temperature and the temperature inside the cabin. As a restriction, we assume that we will not install additional “collective farm” devices. As an admission, we take the opportunity to reject the altimeter and barometer readings. The rear-view mirror shown in the figure is the source data.

Analysis of the source data. Any business begins with the analysis of the source data, so we start with the dismantling of the mirror and its analysis. Judging by the appearance in the mirror, there are two seven-segment LED indicators of 4 characters each, which means you can either get to them or invent something of your own.

In general, it is worth noting that the implementation of the functions of the altimeter, barometer and compass is quite disgusting in the mirror, because the readings float strongly, the values ​​are multiples of 100, and we do not calibrate the compass using known methods. Therefore, these functions can be thrown out on occasion. Moreover, it is known that the simultaneous measurement of altitude and pressure by a single barometric sensor with an acceptable accuracy is impossible. If the height changes, then the sensor should measure only the height, and if we stand still, then we measure only the atmospheric pressure. Judging by the instrument readings, it seems that in the mirror there is one compass and one barometric sensor.

Inside the mirror is such a board:



So the autopsy showed that we have a board inside the mirror, on which TM1638 drivers are located to control the LED indicators, and the logic is sewn into the MPC82E54AS2 controller. MEMSIC C2122M is used as a sensor. While there is no desire to rewrite the current controller and modify the board, we decided to make our own board for the mirror, which will implement our logic and output the information we need from the sensors.

The solution of the problem. So, we decided to make our own board, which repeats the dimensions of the standard board and indicators, but it will not use the compass indicator, we do not need it. The logic of the device is as follows:
1) There are three buttons: "+", "PWR", "-": add brightness, turn the backlight on or off, reduce brightness;
2) When you press the buttons, the backlight turns on and the information is displayed for 20 seconds;
3) If the screen is on, PWR turns it off;
4) Temperature data is updated every 1 second.

Accordingly, the functional diagram of the device has the form:



Thus, in accordance with the scheme we have a microcontroller for SPI interacts with two LED drivers, two temperature sensors. Everything is powered by a repetitive circuit to use part of the radio elements from the board.

We will use the STM8S003 as the cheapest of the STM8S line as a microcontroller, it has enough peripherals, and in particular, we only need GPIO, RCC, SPI. As temperature sensors used DS18B20, which do not make up the deficit and is on hand. MAX7221 is used to control LED indicators.

MK STM8S003 has the following characteristics:



The choice of this MC is due to its simplicity, cheapness in a crisis, as well as the availability of simple development tools (STM8S-Discovery). For those who do not want to breed a signet, you can do the same thing on the breadboard using the following simple debugging handkerchief:



The price on ebay is only 120 rubles.

Proceeding from the functional diagram, the following basic circuit diagram is obtained:



A double-sided board was made using photoresist, they also made a purple mask layer, which resulted in the following product, a kind of "standard" board and a "patch" of the board with its own logic:



The software implementation is quite simple, first worked on the Arduino base, then the functionality is ported to the STM8S combat product, ready for installation on the vehicle. Bench tests on air condition and on time between failures, the board was successful, so the board is now being assembled in a vehicle When a colleague does the installation, then lay out a comprehensive report on all changes to his car.

Inside the source code are three simple libraries in the C language:


From the programming point of view, the key point is the fact that the STM8 has clocks, so the delays for DS18b20 are implemented on the base timer number 4, for example, the 15 µs delay function for the controller at 16 MHz is:

void _delay_15us(u16 n_15us) { /* Init TIMER 4 */ CLK_PeripheralClockConfig(CLK_PERIPHERAL_TIMER4, ENABLE); /* : / (2^3) = 8 /3 */ TIM4->PSCR = 3; /* SYS_CLK_HSI_DIV1 Auto-Reload: 16M / 8 = 2M,  15  30  */ TIM4->ARR = 29; // 30 - 1 /*      */ TIM4->CNTR = 29; /*    */ TIM4->SR1 &= ~TIM4_SR1_UIF; /*   */ TIM4->CR1 |= TIM4_CR1_CEN; while(n_15us--) { while((TIM4->SR1 & TIM4_SR1_UIF) == 0) ; TIM4->SR1 &= ~TIM4_SR1_UIF; } /*   */ TIM4->CR1 &= ~TIM4_CR1_CEN; CLK_PeripheralClockConfig(CLK_PERIPHERAL_TIMER4, DISABLE); } 


An example of an exchange with the DS18b20 sensor by means of UART1 was suggested by artko :
 void ds1820_uartinit(uint32_t baud) { USART_DeInit(USART1); USART_Init(USART1, baud, USART_WordLength_8b, USART_StopBits_1, USART_Parity_No, USART_Mode_Rx | USART_Mode_Tx); } void ds1820_write(uint8_t byte) { UART1->DR = byte; while (!(UART1->SR & (uint8_t)UART1_FLAG_TC)); //    } uint8_t ds1820_read() { return ((uint8_t)UART1->DR); } void ds_write(uint8_t byte) { int i; for(i = 0; i < 8; i ++) { if(byte & 1) { ds1820_write(0xff); } else { ds1820_write(0x00); } ds1820_read(); byte >>= 1; } } uint8_t ds_read() { int i; uint8_t res = 0; for(i = 0; i < 8; i ++) { ds1820_write(0xff); uint8_t c = ds1820_read(); if(c == 0xff) { res |= (1 << i); } } return res; } void ds1820_startconversion() { ds1820_uartinit(9600); ds1820_write(0xf0); ds1820_uartinit(115200); ds_write(0xcc); ds_write(0x44); } int16_t ds1820_readtemp() { ds1820_uartinit(9600); ds1820_write(0xf0); ds1820_uartinit(115200); ds_write(0xcc); ds_write(0xBE); uint8_t temp1 = ds_read(); uint8_t temp2 = ds_read(); int16_t temp3 = (uint16) temp2 * (uint16)0x0100L + (uint16) temp1; return temp3; }     ds1820_uartinit(9600); ds1820_write(0xf0); 


The brightness level of the indicators is stored in non-volatile memory on the principle of "memory card" described using the structure. For each structure field, there is a function to take a value and a function to record a new value.

Findings. So, a device has been developed for a car that solves the problem of measuring the temperature inside and outside the cabin. It seemed a simple function, but for some reason it is not in Chinese cars, or it is very hidden from the average man.

Demonstration of the functionality of the new board:



Datasheets for the components of the "standard fee":
www.moly-tech.com/uppdf/MEMSIC/MMC2122MG%20RevC.pdf
kazus.ru/datasheets/pdf-data/3675111/MEGAWIN/MPC82E54AS2.html
cholla.mmto.org/computers/avr/cool_parts/tm1638.pdf

Board source code:
Source Code for IAR

UPD:
As promised in the mirror assembly process:


Tests for negative temperatures:


Mirror Assembly!

Source: https://habr.com/ru/post/253145/


All Articles