Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize temperature Sensor and OLED

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article focuses on "how to achieve temperature sensor and OLED", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to realize temperature sensor and OLED.

1. OLED driver

two。 AHT20 driver of temperature and humidity sensor

3. Drawing dial

Since the dial is half-round and generally starts at 9 o'clock, it rotates along the pointer, corresponding to the actual temperature value, corresponding to the minimum value at 9 o'clock and the maximum value at 3 o'clock.

Therefore, when we draw the dial, it is best to draw the dial from the angle of 0 at 9 o'clock, 90 degrees at 12 o'clock and 180 degrees at 3 o'clock.

To draw a dial is to set the center of the circle (x _ (~) y) and the radius r, then calculate the two points on the circle, and connect the two adjacent points by calling the drawing straight line function.

/ * DrawArc. Draw a semicircle * start_angle in degree * sweep in degree * XMague y is the position of the center * radius is the radius * start_angle starting angle, 9 o'clock as 0 angle * sweep scanning angle * rotate clockwise * / void ssd1306_DrawArc_from9 (uint8_t x, uint8_t y, uint8_t radius, uint16_t start_angle, uint16_t sweep, SSD1306_COLOR color) {float approx_degree Uint32_t approx_segments; uint8_t xp1,xp2; uint8_t yp1,yp2; uint32_t count = 0; uint32_t loc_sweep = 0; float rad; loc_sweep = ssd1306_NormalizeTo0_360 (sweep); count = (ssd1306_NormalizeTo0_360 (start_angle)); approx_segments = loc_sweep; approx_degree = 1; while (count < approx_segments) {rad = ssd1306_DegToRad (count*approx_degree) Xp1 = x-(int8_t) (cos (rad) * radius); yp1 = y-(int8_t) (sin (rad) * radius); count++; if (count! = approx_segments) {rad = ssd1306_DegToRad (count*approx_degree);} else {rad = ssd1306_DegToRad (loc_sweep);} xp2 = x-(int8_t) (cos (rad) * radius); yp2 = y-(int8_t) (sin (rad) * radius) Ssd1306_DrawLine (xp1,yp1,xp2,yp2,color);} return;}

4. Draw a pointer

Drawing the pointer is relatively simple, that is, find a point on the circumference, connect the point with the center of the circle, and become a pointer. By changing the size of the radius, the length of the pointer can be realized.

The difficulty is how to correspond the angle of the pointer to the actual temperature value. Assuming that the temperature ranges from-15 degrees Celsius to 45 degrees Celsius, with a total of 60 degrees Celsius, the corresponding temperature value for each angle is 60 degrees Celsius 180.

If the current temperature is 20 degrees, the corresponding angle is 20 degrees 60 / 180 degrees.

/ * draw a straight line from the center to the circumference of the circle to realize the pointer * / void ssd1306_draw_line_of_arc (uint8_t x, uint8_t y, uint8_t radius, uint16_t angle,SSD1306_COLOR color) {float approx_degree; uint8_t xp1; uint8_t yp1; uint32_t count = 0; float rad; count = (ssd1306_NormalizeTo0_360 (angle)); approx_degree = 1; rad = ssd1306_DegToRad (count*approx_degree) Xp1 = x-(int8_t) (cos (rad) * radius); yp1 = y-(int8_t) (sin (rad) * radius); ssd1306_DrawLine (x, y, xp1, yp1, color);}

5. Draw scale

For the method of drawing the scale, you can refer to the method of drawing the pointer above. The straight line drawn from the same angle and the intersection of two circles with the same center and different radius are connected to the scale.

/ * draw a tick mark * / void ssd1306_draw_line_of_kedu (uint8_t x, uint8_t y, uint8_t radius, uint16_t angle,SSD1306_COLOR color) {float approx_degree; uint8_t xp1,xp2; uint8_t yp1,yp2; uint32_t count = 0; float rad; count = (ssd1306_NormalizeTo0_360 (angle)); approx_degree = 1; rad = ssd1306_DegToRad (count*approx_degree) Xp1 = x-(int8_t) (cos (rad) * radius); yp1 = y-(int8_t) (sin (rad) * radius); xp2 = x-(int8_t) (cos (rad) * (radius-4)); / / the length of tick marks is 4 yp2 = y-(int8_t) (sin (rad) * (radius-4)); ssd1306_DrawLine (xp1, yp1, xp2, yp2, color);}

6. Use the above functions to draw the thermometer we designed

/ / Update temperature and humidity void ssd1306_update_temper (float temp, float humi) {int angle = 0; char buf [100] = {0}; ssd1306_Fill (Black); uint8_t XLY Font_6x8 r; / / temperature dial, semicircle XLY 30; YYZ 28; rche 28; ssd1306_DrawCircle (XMAT 2m White); ssd1306_SetCursor (XLV r, y); ssd1306_DrawString ("- 15", Font_6x8, White) Ssd1306_SetCursor (x+r-6, y); ssd1306_DrawString ("45", Font_6x8, White); ssd1306_SetCursor (Xmur6, y-r+6); ssd1306_DrawString ("15", Font_6x8, White); ssd1306_DrawArc_from9 (xmemy r, 0180, White); / / ssd1306_DrawArc_from9 (xmemy r-1,0180, White); ssd1306_draw_line_of_kedu Ssd1306_draw_line_of_kedu; ssd1306_draw_line_of_kedu; angle = (int) ((temp + 15) * 180x60); ssd1306_draw_line_of_arc (xrecoery r-2lance angle, White); ssd1306_SetCursor (x+r+6, y-r/2); sprintf (buf, "% .1fC", temp); ssd1306_DrawString (buf, Font_7x10, White) / / humidity dial, semicircular XLV 30; YLV 55; RH 22; ssd1306_DrawCircle (Xrecedence 2 White); ssd1306_SetCursor (x-r-4, y); ssd1306_DrawString ("20", Font_6x8, White); ssd1306_SetCursor (x+r-6, y); ssd1306_DrawString ("100s", Font_6x8, White); ssd1306_SetCursor (XLV 6, y-r+6); ssd1306_DrawString ("60", Font_6x8, White) Ssd1306_DrawArc_from9 (xmemyrer, 0180, White); / / ssd1306_DrawArc_from9 (xrecedicyrect r-1,0180, White); ssd1306_draw_line_of_kedu (xrecedicyrecy rreco 45pr); ssd1306_draw_line_of_kedu (xrecedicyrecy rphi90 White); ssd1306_draw_line_of_kedu (xmemyparr135pr White); angle = (int) ((humi-20) * 180ppt 80) Ssd1306_draw_line_of_arc (x, y, rmuri 2angle, White); ssd1306_SetCursor (x+r+6, y-r/2); sprintf (buf, "% .1f", humi); ssd1306_DrawString (buf, Font_7x10, White); / / ssd1306_TestArc (); ssd1306_UpdateScreen ();}

7. Initialization of I2C pin and task initialization

Void Ssd1306TestTask (void* arg) {(void) arg; uint32_t retval = 0; GpioInit (); IoSetFunc (WIFI_IOT_IO_NAME_GPIO_13, WIFI_IOT_IO_FUNC_GPIO_13_I2C0_SDA); IoSetFunc (WIFI_IOT_IO_NAME_GPIO_14, WIFI_IOT_IO_FUNC_GPIO_14_I2C0_SCL); I2cInit (WIFI_IOT_I2C_IDX_0, OLED_I2C_BAUDRATE); WatchDogDisable (); usleep (20 / 1000); ssd1306_Init () Ssd1306_Fill (Black); ssd1306_SetCursor (0,0); ssd1306_DrawString ("Hello HarmonyOS!", Font_7x10, White); uint32_t start = HAL_GetTick (); ssd1306_UpdateScreen (); uint32_t end = HAL_GetTick (); printf ("ssd1306_UpdateScreen time cost:% d ms.\ r\ n", end-start); / / retval = AHT20_Calibrate (); printf ("AHT20_Calibrate:% d\ r\ n", retval); TestGetTick () While (1) {float temp = 0, humi = 0; / / retval = AHT20_StartMeasure (); / / printf ("AHT20_StartMeasure:% d\ r\ n", retval); retval = AHT20_GetTemperHumi (& temp, & humi); printf ("AHT20_GetMeasureResult:% d, temp =% .2f, humi =% .2f\ n", retval, temp, humi); ssd1306_update_temper (temp, humi); sleep (1); / / ssd1306_TestAll () }} at this point, I believe you have a deeper understanding of "how to achieve temperature sensor and OLED". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report