Temperature Sensor + I2C + Serial Port + PC (pyserial) Example

Source: Internet
Author: User

Temperature Sensor + I2C + Serial Port + PC (pyserial) Example


The example is a bit messy, mainly for learning:

Temperature sensor DS18B20, E2PROM, I2C, serial port read/write, PC (pyserial) Data Display


SerialPort. py PC serial host program, you can set the maximum alarm temperature

You need to install python3 or above and the pyserial library. The example is written in python3.4.

After the upper computer sends an alarm temperature to the MCU, the MCU stores the temperature to E2PROM,

After the single-chip microcomputer is powered off and restarted, it reads the alarm value from E2PROM.


I2C. h I2C bus read/write and settings

Read and Write settings for lcd1602.h lcd1602

Read, write, and set temp_ds18b20


# Define at24c02 0xa0 // address of the device whose data is written by at24c02

# Define max_temp_e2prom_flag 0xff // memory data mark with the upper limit of the alarm temperature.

Sbit power_port = P2 ^ 3; // Alarm port


Int max_temp = 340; // The maximum alarm temperature. The default value is 340.

Int max_temp_buf = 0; // alarm upper limit temperature cache for serial port

Int sp_read_count = 0; // serial port data, read Byte Count

Bit sp_read_flag = 0; // serial port reading flag

Uint8 buf_byte; // serial data cache



Download the example of serial communication and E2PROM and I2C bus:

Http://pan.baidu.com/s/1pJ9aKaf

#! /Usr/bin/ENV Python # encoding = UTF-8 "====================== ========================================================== ============= single-chip microcomputer example, PC copyright (c) junyang studio 2014-07-26 document junyang Studio ([email protected]) Updated logo: 14:00:00 [email protected] ==================================== ========================================================== = "Import serialimport structport = 2 # Serial Port Number baudrate = 4800 # baud rate max_temp = 335 # alarm temperature SER = serial. serial () def writetemp (temp): H = int (temp/256) L = TEMP % 256 DATA = struct. pack ("B"), h) Ser. write (data) Data = struct. pack ("B"), L) Ser. write (data) def readtemp (): Print (SER) while (1): H = Ser. read () L = Ser. read () M = int. from_bytes (H + L, byteorder = 'Big ', signed = false) S = STR (m) S = s [0] + s [1] + ". "+ s [2] +" ℃ "Print (s) def main (): Ser. setport (port) # Set the serial port number Ser. setbaudrate (baudrate) # Set the baud rate Ser. open () writetemp (max_temp) readtemp () Ser. close () If _ name _ = "_ main _": Main ()



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.