Single-Chip Microcomputer: Thermistor Temperature Measurement and display to digital tube (Thermometer)
As shown in, PCF8591 has 9 and 10 feet, one is the data cable SDA, and the other is the clock line SCL. Connect to the MCU P2.0 and P2.1 respectively. Why choose these two pins? Because 51 Single-Chip Microcomputer does not have an IIC bus interface, it needs to use a common IO simulation, so it selects two I/O interfaces. What is VREF? The reference voltage is also the maximum voltage it can measure. How to control it? Today, I will not talk about the IIC bus, but the control process. You can see the device manual in four steps: 1. Select the device as the sending Address byte. 2. Send control byte and select the corresponding channel. 3. resend the address byte and select the device. 4. receive data from the target channel. The procedure is: AD sampling, serial sending, and cyclic execution.
See the code for specific implementation:
# Include
# Define uchar unsigned char # define uint unsigned int # define PCF8591 0x90sbit SCL = P2 ^ 0; sbit SDA = P2 ^ 1; sbit spk = P1 ^ 4; sbit WEI = P2 ^ 7; // weisbit DUAN = P2 ^ 6; // duanunsigned char code dofly_DuanMa [] = {0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f, 0x77, 0x7c, 0x39, 0x5e, 0x79,0x71,0x40}; // The field code value is 0 ~ 9 unsigned char code dofly_WeiMa [] = {0xfe, 0xfd, 0xfb, 0xf7, 0xef, 0xdf, 0xbf, 0x7f}; // the corresponding Digital tubes are lit, that is, the location code void delay (int z) {while (z --);} void Screen (int I, int n) {P0 = 0; DUAN = 1; DUAN = 0; p0 = dofly_WeiMa [I]; WEI = 1; WEI = 0; P0 = dofly_DuanMa [n]; DUAN = 1; DUAN = 0; delay( 250 );} void Get_out_print (uchar temperatureNum) {if (temperatureNum/100> 0) {Screen (3, temperatureNum/100); temperatureNum % = 100;} if (temperatureNum/10> 0) {Scr Een (4, temperatureNum/10);} Screen (5, temperatureNum % 10); Screen (7, 12); // display c} void delay () // latency 4-5 microseconds {;}void delay_1ms (uint z) {uint x, y; for (x = z; x> 0; x --) for (y = 110; y> 0; y --) ;}void start () {SDA = 1; delay (); SCL = 1; delay (); SDA = 0; delay ();} void stop () {SDA = 0; delay (); SCL = 1; delay (); SDA = 1; delay ();} void respons () {uchar I; SCL = 1; delay (); while (SDA = 1) & (I <250) I ++; SCL = 0; delay ();} void init (){ SDA = 1; delay (); SCL = 1; delay ();} uchar read_byte () {uchar I, k; SCL = 0; delay (); SDA = 1; delay (); for (I = 0; I <8; I ++) {SCL = 1; delay (); k = (k <1) | SDA; SCL = 0; delay ();} return k;} void write_byte (uchar date) {uchar I, temp; temp = date; for (I = 0; I <8; I ++) {temp = temp <1; SCL = 0; delay (); SDA = CY; delay (); SCL = 1; delay ();} SCL = 0; delay (); SDA = 1; delay ();} void write_add (uchar control, uchar date) {start (); write_ B Yte (PCF8591); respons (); write_byte (control); respons (); write_byte (date); respons (); stop ();} uchar read_add (uchar control) {uchar date; start (); write_byte (PCF8591); respons (); write_byte (control); respons (); start (); write_byte (PCF8591 + 1 ); respons (); date = read_byte (); stop (); return date;} uchar temperatureNum = 0; void main () {TMOD = 0x01; EA = 1; ET0 = 1; TR0 = 1; init (); while (1) {temperatureNum = read_a Dd (0x41); // Get_out_print (151-temperatureNum) ;}} void Timer0_isr (void) interrupt 1 {TH0 = 0xfe; TL0 = 0; if (temperatureNum <118) spk =! Spk ;}
Note: The temperature may not be very accurate, because my voltage-temperature correspondence is not carefully measured .. Roughly y =-x + 151