# 接线图
# 树莓派管脚图
# 代码
| #include <stdio.h> |
| #include <wiringPi.h> |
| #include <pcf8591.h> |
| #include <math.h> |
| |
| #define PCF 120 |
| #define DOpin 0 |
| |
| void Print(int x) |
| { |
| switch(x) |
| { |
| case 0: |
| printf("\n************\n" ); |
| printf( "* Too Hot! *\n" ); |
| printf( "************\n\n"); |
| break; |
| case 1: |
| printf("\n***********\n" ); |
| printf( "* Better~ *\n" ); |
| printf( "***********\n\n"); |
| break; |
| default: |
| printf("\n**********************\n" ); |
| printf( "* Print value error. *\n" ); |
| printf( "**********************\n\n"); |
| break; |
| } |
| } |
| |
| int main() |
| { |
| unsigned char analogVal; |
| double Vr, Rt, temp; |
| int tmp, status; |
| |
| if(wiringPiSetup() == -1){ |
| printf("setup wiringPi failed !"); |
| return 1; |
| } |
| |
| pcf8591Setup(PCF, 0x48); |
| |
| pinMode(DOpin, INPUT); |
| |
| status = 0; |
| while(1) |
| { |
| printf("loop"); |
| analogVal = analogRead(PCF + 0); |
| Vr = 5 * (double)(analogVal) / 255; |
| Rt = 10000 * (double)(Vr) / (5 - (double)(Vr)); |
| temp = 1 / (((log(Rt/10000)) / 3950)+(1 / (273.15 + 25))); |
| temp = temp - 273.15; |
| printf("Current temperature : %lf\n", temp); |
| |
| |
| |
| |
| |
| tmp = digitalRead(DOpin); |
| |
| |
| |
| |
| |
| |
| if (tmp != status) |
| { |
| Print(tmp); |
| status = tmp; |
| } |
| |
| delay (200); |
| } |
| return 0; |
| } |
编译命令: gcc thermistor.c -o thermistor -lwiringPi -lm
# Python
| |
| import PCF8591 as ADC |
| import RPi.GPIO as GPIO |
| import time |
| import math |
| |
| DO = 17 |
| GPIO.setmode(GPIO.BCM) |
| |
| def setup(): |
| ADC.setup(0x48) |
| GPIO.setup(DO, GPIO.IN) |
| |
| def Print(x): |
| if x == 1: |
| print '' |
| print '***********' |
| print '* Better~ *' |
| print '***********' |
| print '' |
| if x == 0: |
| print '' |
| print '************' |
| print '* Too Hot! *' |
| print '************' |
| print '' |
| |
| def loop(): |
| status = 1 |
| tmp = 1 |
| while True: |
| analogVal = ADC.read(0) |
| Vr = 5 * float(analogVal) / 255 |
| Rt = 10000 * Vr / (5 - Vr) |
| temp = 1/(((math.log(Rt / 10000)) / 3950) + (1 / (273.15+25))) |
| temp = temp - 273.15 |
| print 'temperature = ', temp, 'C' |
| |
| |
| |
| |
| |
| |
| |
| |
| if temp > 33: |
| tmp = 0; |
| elif temp < 31: |
| tmp = 1; |
| |
| |
| if tmp != status: |
| Print(tmp) |
| status = tmp |
| |
| time.sleep(0.2) |
| |
| if __name__ == '__main__': |
| try: |
| setup() |
| loop() |
| except KeyboardInterrupt: |
| pass |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| import smbus |
| import time |
| |
| |
| bus = smbus.SMBus(1) |
| |
| |
| def setup(Addr): |
| global address |
| address = Addr |
| |
| def read(chn): |
| if chn == 0: |
| bus.write_byte(address,0x40) |
| if chn == 1: |
| bus.write_byte(address,0x41) |
| if chn == 2: |
| bus.write_byte(address,0x42) |
| if chn == 3: |
| bus.write_byte(address,0x43) |
| bus.read_byte(address) |
| return bus.read_byte(address) |
| |
| def write(val): |
| temp = val |
| temp = int(temp) |
| |
| bus.write_byte_data(address, 0x40, temp) |
| |
| if __name__ == "__main__": |
| setup(0x48) |
| while True: |
| print 'AIN0 = ', read(0) |
| print 'AIN1 = ', read(1) |
| tmp = read(0) |
| tmp = tmp*(255-125)/255+125 |
| write(tmp) |
| |